From f8fb724dcf0fd45050df66a6f88d7c9999078d58 Mon Sep 17 00:00:00 2001 From: Christian Boltz Date: Tue, 18 Oct 2011 22:11:22 +0000 Subject: [PATCH] model/DomainHandler.php: - split __construct to - __construct($new=0) - init $struct etc. and - init($username) - validate $username This allows reading of $struct before doing any check on the username, and will also avoid problems when I implement a function to list all domains - init(): use direct return values instead of $this->return scripts/shells/domain.php, create-domain.php - update to use new DomainHandler->init() git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1219 a1433add-5e2c-0410-b055-b7f2511e0802 --- create-domain.php | 4 ++-- model/DomainHandler.php | 27 ++++++++++++++++++--------- scripts/shells/domain.php | 14 +++++++------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/create-domain.php b/create-domain.php index 034e2a85..cfd3aea9 100644 --- a/create-domain.php +++ b/create-domain.php @@ -58,8 +58,8 @@ foreach($form_fields as $key => $field) { if ($_SERVER['REQUEST_METHOD'] == "POST") { - $handler = new DomainHandler($values['domain'], 1); - if (!$handler->result()) { + $handler = new DomainHandler(1); + if (!$handler->init($values['domain'])) { $error = 1; $pAdminCreate_domain_domain_text_error = join("
", $handler->errormsg); } diff --git a/model/DomainHandler.php b/model/DomainHandler.php index 413dc452..25a606df 100644 --- a/model/DomainHandler.php +++ b/model/DomainHandler.php @@ -16,35 +16,44 @@ class DomainHandler extends PFAHandler { public $errormsg = array(); - # error messages used in __construct() and view() + # error messages used in init() and view() protected $error_already_exists = 'pAdminCreate_domain_domain_text_error'; protected $error_does_not_exist = 'domain_does_not_exist'; /** - * @param string $username + * Constructor: fill $struct etc. + * @param string $new */ - public function __construct($username, $new = 0) { - $this->username = strtolower($username); # TODO: find a better place for strtolower() to avoid a special constructor in DomainHandler (or agree that $username should be lowercase in all *Handler classes ;-) + public function __construct($new = 0) { if ($new) $this->new = 1; - $this->initStruct(); + } + + /** + * initialize with $username and check if it is valid + * @param string $username + */ + public function init($username) { + $this->username = strtolower($username); $exists = $this->view(false); - $this->return = false; # be pessimistic by default - if ($new) { + if ($this->new) { if ($exists) { $this->errormsg[] = Lang::read($this->error_already_exists); + return false; } elseif (!$this->validate_id() ) { # errormsg filled by validate_id() + return false; } else { - $this->return = true; + return true; } } else { # edit mode if (!$exists) { $this->errormsg[] = Lang::read($this->error_does_not_exist); + return false; } else { - $this->return = true; + return true; } } } diff --git a/scripts/shells/domain.php b/scripts/shells/domain.php index 130ff88e..0d7a5071 100644 --- a/scripts/shells/domain.php +++ b/scripts/shells/domain.php @@ -110,7 +110,7 @@ class AddTask extends Shell { $question = "Domain Quota (in MB):"; $d = $this->in($question); - $handler = new DomainHandler($domain); + $handler = new DomainHandler(); $transports = $handler->getTransports(); $qt[] = 'Choose transport option'; foreach ($transports AS $key => $val) { @@ -141,8 +141,8 @@ class AddTask extends Shell { function __handle($domain, $desc, $a, $m, $t, $q, $d, $default, $backup) { - $handler = new DomainHandler($domain, 1); - if (!$handler->result()) { + $handler = new DomainHandler(1); + if (!$handler->init($domain)) { $this->error("Error:",join("\n", $handler->errormsg)); return; } @@ -269,8 +269,8 @@ class DeleteTask extends Shell { * @access private */ function __handle($address) { - $handler = new DomainHandler($address); - if (!$handler->result()) { + $handler = new DomainHandler(); + if (!$handler->init($address)) { $this->error("Error:",join("\n", $handler->errormsg)); return; } @@ -345,8 +345,8 @@ class ViewTask extends Shell { function __handle($domain) { - $handler = new DomainHandler($domain); - if (!$handler->result()) { + $handler = new DomainHandler(); + if (!$handler->init($domain)) { $this->error("Error:",join("\n", $handler->errormsg)); return; }