SF.net SVN: postfixadmin:[978] trunk
Brought to you by:
christian_boltz,
gingerdog
From: <chr...@us...> - 2011-03-02 22:37:28
|
Revision: 978 http://postfixadmin.svn.sourceforge.net/postfixadmin/?rev=978&view=rev Author: christian_boltz Date: 2011-03-02 22:37:22 +0000 (Wed, 02 Mar 2011) Log Message: ----------- DomainHander: on the way to a common interface for all classes and easy-to-edit field lists. With lots of inspiration from fetchmail.php and a base class I started 2.5 years ago. model/DomainHandler.php - base on PFAHandler (see below) - new protected variables - $id_field (column that contains $username) - $struct (field list) - $defaults (default values, option lists) - $new (switch between new and edit mode) - change $username to protected (preparation for move to PFAHandler) - add optional $new parameter to __construct. Targets (not yet implemented): - early validation of $username (domain name in this case) - if $new == 1, check that item does NOT exist and is a valid domain - else: check if item exists. error out if not. - new function initStruct to fill $id_field, $struct, $defaults - add(): - use an array as parameter instead of single parameters Advantage: this makes it easy to add another field - use _inp_* base validation - create default aliases only in create mode, not in edit mode - view(): fix error message - added various TODO notes. Some affect design questions - feedback welcome ;-) scripts/shells/domain.php: - change $handler->add call to array usage - add some TODO notes - some whitespace fixes in execute() model/PFAHandler.php: - new base class for *Handler classes - contains only some generic input validation for now - more code will be moved from DomainHandler to PFAHandler later Modified Paths: -------------- trunk/model/DomainHandler.php trunk/scripts/shells/domain.php Added Paths: ----------- trunk/model/PFAHandler.php Modified: trunk/model/DomainHandler.php =================================================================== --- trunk/model/DomainHandler.php 2011-03-02 21:36:41 UTC (rev 977) +++ trunk/model/DomainHandler.php 2011-03-02 22:37:22 UTC (rev 978) @@ -4,48 +4,111 @@ /** * Handlers User level alias actions - e.g. add alias, get aliases, update etc. */ -class DomainHandler { +class DomainHandler extends PFAHandler { - private $username = null; # actually it's the domain - variable name kept for consistence with the other classes + protected $username = null; # actually it's the domain - variable name kept for consistence with the other classes + protected $id_field = null; + protected $struct = array(); + protected $defaults = array(); + protected $new = 0; # 1 on create, otherwise 0 - public $errormsg = array(); /** * @param string $username */ - public function __construct($username) { + public function __construct($username, $new = 0) { $this->username = $username; + if ($new) $this->new = 1; + # TODO: if $new == 1, check that item does NOT exist and is a valid (in this case) domain + # TODO: else: check if item exists. error out if not. + # TODO: target: if construct succeeds, $this->username is valid + $this->initStruct(); } + private function initStruct() { + $this->id_field = 'domain'; + + # TODO: merge $struct and $defaults to one array? + # TODO: use a helper function to fill $struct with named keys instead of [0], [1], ... + # TODO: find a way to handle field labels - not sure if the fetchmail way (construct $LANG keys from field name) is perfect + + $this->struct=array( // list($editible,$view,$type) + # field name allow display field? type + # editing? form list + "domain" => array( $this->new, 1, 1, 'text' ), + "description" => array( 1, 1, 1, 'text' ), + "aliases" => array( 1, 1, 1, 'num' ), + "mailboxes" => array( 1, 1, 1, 'num' ), + "maxquota" => array( 1, 1, 1, 'num' ), + "quota" => array( 0, 0, 0, 'num' ), # currently unused, reserved for domain total quota + "transport" => array( 1, 1, 1, 'enum' ), + "backupmx" => array( 1, 1, 1, 'bool' ), + "active" => array( 1, 1, 1, 'bool' ), + "created" => array( 0, 0, 1, 'text' ), + "modified" => array( 0, 0, 1, 'text' ), + ); + # labels and descriptions are taken from $PALANG['pFetchmail_field_xxx'] and $PALANG['pFetchmail_desc_xxx'] + + $this->defaults=array( + 'aliases' => Config::read('aliases'), + 'mailboxes' => Config::read('mailboxes'), + 'maxquota' => Config::read('maxquota'), + 'quota' => 0, # TODO: Config::read(''), - config option does not exist yet + 'transport' => $this->getTransports(), + 'backupmx' => 0, + 'active' => 1, + ); + } + public function getTransports() { return Config::read('transport_options'); } - + + # TODO: specific for CLI? If yes, move to CLI code public function getTransport($id) { $transports = Config::read('transport_options'); return $transports[$id-1]; } - public function add($desc, $a, $m, $t, $q, $default, $backup) { + public function add($values) { +# ($desc, $a, $m, $t, $q, $default, $backup) + + # TODO: make this a generic function for add and edit + # TODO: move DB writes etc. to separate save() function + + ($values['backupmx'] == true) ? $values['backupmx'] = db_get_boolean(true) : $values['backupmx'] = db_get_boolean(false); - ($backup == true) ? $backup = db_get_boolean(true) : $backup = db_get_boolean(false); - - $arr = array( - 'domain' => $this->username, - 'description' => $desc, - 'aliases' => $a, - 'mailboxes' => $m, - 'maxquota' => $q, - 'transport' => $this->getTransport($t), - 'backupmx' => $backup, - ); - - $result = db_insert('domain', $arr); + $values['domain'] = $this->username; + + # base validation + $checked = array(); + foreach($this->struct as $key=>$row) { + list($editable, $displayform, $displaylist, $type) = $row; + if ($editable != 0){ + $func="_inp_".$type; + $val=safepost($key); + if ($type!="password" || strlen($values[$key]) > 0 || $this->new == 1) { # skip on empty (aka unchanged) password on edit + if (method_exists($this, $func) ) { + $checked[$key] = $this->{$func}($values[$key]); + } else { + # TODO: warning if no validation function exists? + $checked[$key] = $values[$key]; + } + } + } + } + + # TODO: more validation + + $domain = $this->username; # TODO fix variable names below + + $checked['domain'] = $this->username; + $result = db_insert('domain', $checked); if ($result != 1) { $this->errormsg[] = Lang::read('pAdminCreate_domain_result_error') . "\n($domain)\n"; return false; } else { - if ($default) { + if ($this->new && $values['default_aliases']) { foreach (Config::read('default_aliases') as $address=>$goto) { $address = $address . "@" . $domain; # TODO: use AliasHandler->add instead of writing directly to the alias table @@ -75,7 +138,8 @@ $this->return = db_array($result['result']); return true; } - $this->errormsg = $result['error']; + $this->errormsg[] = "Domain " . $this->username . " does not exist."; +# $this->errormsg[] = $result['error']; return false; } /** Added: trunk/model/PFAHandler.php =================================================================== --- trunk/model/PFAHandler.php (rev 0) +++ trunk/model/PFAHandler.php 2011-03-02 22:37:22 UTC (rev 978) @@ -0,0 +1,16 @@ +<?php +class PFAHandler { + function _inp_num($val) { + return (int)($val); + } + + function _inp_bool($val) { + return $val ? db_get_boolean(true): db_get_boolean(false); + } + + function _inp_password($val){ + # TODO: fetchmail specific. Not suited for mailbox/admin passwords. + return base64_encode($val); + } +} +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ Property changes on: trunk/model/PFAHandler.php ___________________________________________________________________ Added: svn:keywords + Id Added: svn:eol-style + native Modified: trunk/scripts/shells/domain.php =================================================================== --- trunk/scripts/shells/domain.php 2011-03-02 21:36:41 UTC (rev 977) +++ trunk/scripts/shells/domain.php 2011-03-02 22:37:22 UTC (rev 978) @@ -136,8 +136,20 @@ function __handle($domain, $desc, $a, $m, $t, $q, $default, $backup) { - $handler = new DomainHandler($domain); - $return = $handler->add($desc, $a, $m, $t, $q, $default, $backup); + $handler = new DomainHandler($domain, 1); + $values = array( + 'domain' => $domain, + 'description' => $desc, + 'aliases' => $a, + 'mailboxes' => $m, + 'maxquota' => $q, + # 'quota' => + 'transport' => $handler->getTransport($t), + 'backupmx' => $backup, + 'active' => $a, + 'default_aliases' => $default, + ); + $return = $handler->add($values); if(!$return) { $this->error("Error:", join("\n", $handler->errormsg)); @@ -154,6 +166,7 @@ * @access public */ function help() { +# TODO: this is the DOMAIN shell... $this->hr(); $this->out("Usage: postfixadmin-cli user add <address> [<password>] <name> <quota> [-g]"); $this->hr(); @@ -216,12 +229,12 @@ function execute() { if (empty($this->args)) { - $this->__interactive(); + $this->__interactive(); } if (!empty($this->args[0])) { - $output = $this->__handle($this->args[0]); - $this->out($output); + $output = $this->__handle($this->args[0]); + $this->out($output); } } /** @@ -342,6 +355,7 @@ * @access public */ function help() { +# TODO: this is the DOMAIN shell... $this->out(""); $this->hr(); $this->out("Usage: postfixadmin-cli user view <address>"); @@ -354,3 +368,4 @@ } } +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |