diff --git a/model/MailboxHandler.php b/model/MailboxHandler.php index 54f2a7a5..d80c86c1 100644 --- a/model/MailboxHandler.php +++ b/model/MailboxHandler.php @@ -4,15 +4,121 @@ /** * Simple class to represent a user. */ -class MailboxHandler { +class MailboxHandler extends PFAHandler { + + protected $domain_field = 'domain'; + + # init $this->struct, $this->db_table and $this->id_field + protected function initStruct() { + $this->db_table = 'mailbox'; + $this->id_field = 'username'; + + $this->struct=array( + # field name allow display in... type $PALANG label $PALANG description default / options / ... + # editing? form list + 'username' => pacol( $this->new, 1, 1, 'text', '' , '' , '' ), + 'local_part' => pacol( 0, 0, 0, 'text', '' , '' , '' ), + 'domain' => pacol( 0, 0, 0, 'enum', '' , '' , '' ), + 'maildir' => pacol( 0, 0, 0, 'text', '' , '' , '' ), + 'password' => pacol( 1, 1, 0, 'pass', '' , '' , '' ), + 'password2' => pacol( 1, 1, 0, 'pass', '' , '' , '', + /*options*/ '', + /*not_in_db*/ 0, + /*dont_write_to_db*/ 1, + /*select*/ 'password as password2' + ), + 'name' => pacol( 1, 1, 1, 'text', '' , '' , '' ), + 'quota' => pacol( 1, 1, 1, 'int' , '' , '' , '' ), + 'active' => pacol( 1, 1, 1, 'bool', '' , '' , 1 ), + 'created' => pacol( 0, 0, 1, 'ts', '' , '' ), + 'modified' => pacol( 0, 0, 1, 'ts', '' , '' ), + # TODO: add virtual 'notified' column and allow to display who received a vacation response? + ); + } + + # messages used in various functions. + # always list the key to hand over to Lang::read + # the only exception is 'logname' which uses the key for db_log + protected function initMsg() { + $this->msg['error_already_exists'] = 'pCreate_mailbox_username_text_error2'; + $this->msg['error_does_not_exist'] = 'pCreate_mailbox_username_text_error1'; + if ($this->new) { + $this->msg['logname'] = 'create_mailbox'; + $this->msg['store_error'] = 'pCreate_mailbox_result_error'; + } else { + $this->msg['logname'] = 'edit_mailbox'; + $this->msg['store_error'] = 'pCreate_mailbox_result_error'; # TODO: better error message + } + } + + /* + * Configuration for the web interface + */ + public function webformConfig() { + return array( + # $PALANG labels + 'formtitle_create' => 'pCreate_mailbox_welcome', + 'formtitle_edit' => 'pEdit_mailbox_welcome', + 'create_button' => 'pCreate_mailbox_button', + 'successmessage' => 'pCreate_mailbox_result_success', + + # various settings + 'required_role' => 'admin', + 'listview' => 'list-virtual.php', + 'early_init' => 1, # 0 for create-domain + ); + } + + + protected function validate_new_id() { + if ($this->id == '') { + $this->errormsg[] = Lang::read('pCreate_mailbox_username_text_error1'); + return false; + } + + list($local_part,$domain) = explode ('@', $this->id); + + if(!$this->create_allowed($domain)) { + $this->errormsg[] = Lang::read('pCreate_mailbox_username_text_error3'); + return false; + } + + # check if an alias with this name already exists - if yes, don't allow to create the mailbox + $handler = new AliasHandler(1); + if (!$handler->init($this->id)) { + $this->errormsg[] = Lang::read('pCreate_mailbox_username_text_error2'); + return false; + } + + return check_email($this->id); # TODO: check_email should return error message instead of using flash_error itsself + } + + /** + * check number of existing mailboxes for this domain - is one more allowed? + */ + private function create_allowed($domain) { + $limit = get_domain_properties ($domain); - protected $username = null; + if ($limit['mailboxes'] == 0) return true; # unlimited + if ($limit['mailboxes'] < 0) return false; # disabled + if ($limit['mailbox_count'] >= $limit['mailboxes']) return false; + return true; + } - public $errormsg = array(); - public function __construct($username) { - $this->username = strtolower($username); +/* function already exists (see old code below + public function delete() { + $this->errormsg[] = '*** deletion not implemented yet ***'; + return false; # XXX function aborts here! XXX } +*/ + + + +/******************************************************************************************************************** + old functions - we'll see what happens to them + (at least they should use the *Handler functions instead of doing SQL) +/********************************************************************************************************************/ public function change_pass($old_password, $new_password) { error_log('MailboxHandler->change_pass is deprecated. Please use MailboxHandler->change_pw!'); @@ -29,36 +135,29 @@ class MailboxHandler { * as per the configuration in config.inc.php */ public function change_pw($new_password, $old_password, $match = true) { - list(/*NULL*/,$domain) = explode('@', $username); - - $E_username = escape_string($this->username); - $table_mailbox = table_by_key('mailbox'); + list(/*NULL*/,$domain) = explode('@', $this->id); if ($match == true) { - $active = db_get_boolean(True); - $result = db_query("SELECT password FROM $table_mailbox WHERE username='$E_username' AND active='$active'"); - $result = db_assoc($result['result']); - - if (pacrypt($old_password, $result['password']) != $result['password']) { - db_log ($domain, 'edit_password', "MATCH FAILURE: " . $this->username); + if (!$this->login($this->id, $old_password)) { + db_log ($domain, 'edit_password', "MATCH FAILURE: " . $this->id); $this->errormsg[] = 'Passwords do not match'; # TODO: make translatable return false; - } + } } $set = array( 'password' => pacrypt($new_password) , ); - $result = db_update('mailbox', 'username', $this->username, $set ); + $result = db_update('mailbox', 'username', $this->id, $set ); if ($result != 1) { - db_log ($domain, 'edit_password', "FAILURE: " . $this->username); + db_log ($domain, 'edit_password', "FAILURE: " . $this->id); $this->errormsg[] = Lang::read('pEdit_mailbox_result_error'); return false; } - db_log ($domain, 'edit_password', $this->username); + db_log ($domain, 'edit_password', $this->id); return true; } @@ -103,7 +202,7 @@ class MailboxHandler { # TODO: copy/move all checks and validations from create-mailbox.php here - $username = $this->username; + $username = $this->id; list($local_part,$domain) = explode ('@', $username); @@ -191,7 +290,7 @@ class MailboxHandler { # TODO: move "send the mail" to a function $fTo = $username; $fFrom = smtp_get_admin_email(); - if(empty($fFrom) || $fFrom == 'CLI') $fFrom = $this->username; + if(empty($fFrom) || $fFrom == 'CLI') $fFrom = $this->id; $fSubject = Lang::read('pSendmail_subject_text'); $fBody = Config::read('welcome_text'); @@ -207,26 +306,8 @@ class MailboxHandler { } - - - public function view() { - - $username = $this->username; - $table_mailbox = table_by_key('mailbox'); - -# TODO: check if DATE_FORMAT works in MySQL and PostgreSQL -# TODO: maybe a more fine-grained date format would be better for non-CLI usage - $result = db_query("SELECT username, name, maildir, quota, local_part, domain, DATE_FORMAT(created, '%d.%m.%y') AS created, DATE_FORMAT(modified, '%d.%m.%y') AS modified, active FROM $table_mailbox WHERE username='$username'"); - if ($result['rows'] != 0) { - $this->return = db_array($result['result']); - return true; - } - $this->errormsg = $result['error']; - return false; - } - public function delete() { - $username = $this->username; + $username = $this->id; list(/*$local_part*/,$domain) = explode ('@', $username); $E_username = escape_string($username); diff --git a/scripts/shells/mailbox.php b/scripts/shells/mailbox.php index 918683de..b224b7cb 100644 --- a/scripts/shells/mailbox.php +++ b/scripts/shells/mailbox.php @@ -466,7 +466,10 @@ class ViewTask extends Shell { function __handle($address) { - $handler = new MailboxHandler($address); + $handler = new MailboxHandler(); + if ( ! $handler->init($address)) { + $this->error("Not found!", "The mailbox you have searched could not be found."); + } if ( ! $handler->view() ) { $this->error("Not Found!", "The mailbox you have searched could not be found."); } @@ -478,6 +481,7 @@ class ViewTask extends Shell { $this->out(sprintf("+%'-25s+%'-15s+%'-10s+%'-20s+%'-8s+%'-8s+%'-6s+",'','','','','','','')); $this->out(sprintf('|%25s|%15s|%10s|%20s|%8s|%8s|%6s|', 'Address', 'Name', 'Quota', 'Dir', 'Created', 'Modified', 'Active')); $this->out(sprintf("+%'-25s+%'-15s+%'-10s+%'-20s+%'-8s+%'-8s+%'-6s+",'','','','','','','')); + $result['maildir'] = '--- skipped ---'; # TODO: include in view() result - or don't (try to) display it $this->out(sprintf('|%25s|%15s|%10s|%20s|%8s|%8s|%6s|', $result['username'], $result['name'], $result['quota'], $result['maildir'], $result['created'], $result['modified'], $result['active'])); $this->out(sprintf("+%'-25s+%'-15s+%'-10s+%'-20s+%'-8s+%'-8s+%'-6s+",'','','','','','','')); diff --git a/users/password.php b/users/password.php index b0945252..7b5b0531 100644 --- a/users/password.php +++ b/users/password.php @@ -66,8 +66,9 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") if ($error == 0) { - $uh = new MailboxHandler($username); - if($uh->change_pw($fPassword, $fPassword_current) ) { + $mh = new MailboxHandler(); + $mh->init($username); # TODO: error handling + if($mh->change_pw($fPassword, $fPassword_current) ) { flash_info($PALANG['pPassword_result_success']); header("Location: main.php"); exit(0); diff --git a/xmlrpc.php b/xmlrpc.php index 66653f43..5ebac43a 100644 --- a/xmlrpc.php +++ b/xmlrpc.php @@ -73,7 +73,8 @@ class UserProxy { * @return boolean true on success */ public function changePassword($old_password, $new_password) { - $uh = new MailboxHandler($_SESSION['username']); + $uh = new MailboxHandler(); + if (!$uh->init($_SESSION['username'])) return false; return $uh->change_pw($new_password, $old_password); } @@ -83,7 +84,7 @@ class UserProxy { * @return boolean true if successful. */ public function login($username, $password) { - $uh = new MailboxHandler($_SESSION['username']); + $uh = new MailboxHandler(); # $_SESSION['username']); return $uh->login($username, $password); } }