SF.net SVN: postfixadmin:[1019] trunk
Brought to you by:
christian_boltz,
gingerdog
From: <chr...@us...> - 2011-03-28 23:15:18
|
Revision: 1019 http://postfixadmin.svn.sourceforge.net/postfixadmin/?rev=1019&view=rev Author: christian_boltz Date: 2011-03-28 23:15:11 +0000 (Mon, 28 Mar 2011) Log Message: ----------- Renamed UserHandler to MailboxHandler to make clear it's about mailboxes (as discussed with GingerDog on IRC yesterday). Also renamed user to mailbox in the CLI. - renamed model/UserHandler.php to MailboxHandler.php - renamed scripts/shells/user.php to mailbox.php - replaced UserHandler / user with MailboxHandler / mailbox in various files - unrelated cleanup: deleted obsolete scripts/models-ext directory Modified Paths: -------------- trunk/scripts/postfixadmin-cli.php trunk/scripts/shells/alias.php trunk/users/login.php trunk/users/password.php trunk/xmlrpc.php Added Paths: ----------- trunk/model/MailboxHandler.php trunk/scripts/shells/mailbox.php Removed Paths: ------------- trunk/model/UserHandler.php trunk/scripts/models-ext/ trunk/scripts/shells/user.php Copied: trunk/model/MailboxHandler.php (from rev 1018, trunk/model/UserHandler.php) =================================================================== --- trunk/model/MailboxHandler.php (rev 0) +++ trunk/model/MailboxHandler.php 2011-03-28 23:15:11 UTC (rev 1019) @@ -0,0 +1,315 @@ +<?php +# $Id$ + +/** + * Simple class to represent a user. + */ +class MailboxHandler { + + protected $username = null; + + public $errormsg = array(); + + public function __construct($username) { + $this->username = strtolower($username); + } + + public function change_pass($old_password, $new_password) { + error_log('MailboxHandler->change_pass is deprecated. Please use MailboxHandler->change_pw!'); + return $this->change_pw($new_password, $old_password); + } + + /** + * @return boolean true on success; false on failure + * @param string $old_password + * @param string $new_passwords + * @param bool $match = true + * + * All passwords need to be plain text; they'll be hashed appropriately + * 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'); + + 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); + $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 ); + + if ($result != 1) { + db_log ($domain, 'edit_password', "FAILURE: " . $this->username); + $this->errormsg[] = Lang::read('pEdit_mailbox_result_error'); + return false; + } + + db_log ($domain, 'edit_password', $this->username); + return true; + } + + /** + * Attempt to log a user in. + * @param string $username + * @param string $password + * @return boolean true on successful login (i.e. password matches etc) + */ + public static function login($username, $password) { + $username = escape_string($username); + + $table_mailbox = table_by_key('mailbox'); + $active = db_get_boolean(True); + $query = "SELECT password FROM $table_mailbox WHERE username='$username' AND active='$active'"; + + $result = db_query ($query); + if ($result['rows'] == 1) + { + $row = db_array ($result['result']); + $crypt_password = pacrypt ($password, $row['password']); + + if($row['password'] == $crypt_password) { + return true; + } + } + return false; + } +/** + * Add mailbox + * @param password string password of account + * @param gen boolean + * @param name string + * + */ + public function add($password, $name = '', $quota = -999, $active = true, $mail = true ) { +# FIXME: default value of $quota (-999) is intentionally invalid. Add fallback to default quota. +# Solution: Invent an sub config class with additional informations about domain based configs like default qouta. +# FIXME: Should the parameters be optional at all? +# TODO: check if parameters are valid/allowed (quota?). +# TODO: most code should live in a separate function that can be used by add and edit. +# TODO: On the longer term, the web interface should also use this class. + +# TODO: copy/move all checks and validations from create-mailbox.php here + + $username = $this->username; + list($local_part,$domain) = explode ('@', $username); + + +#TODO: more self explaining language strings! + if(!check_mailbox ($domain)) { + $this->errormsg[] = Lang::read('pCreate_mailbox_username_text_error3'); + return false; + } + + # check if an alias with this name already exists + $result = db_query ("SELECT * FROM " . table_by_key('alias') . " WHERE address='" . escape_string($username) . "'"); + if ($result['rows'] == 1){ + $this->errormsg[] = Lang::read('pCreate_mailbox_username_text_error2'); + return false; + } + + $plain = $password; + $password = pacrypt ($password); + +# TODO: if we want to have the encryption method in the encrypted password string, it should be done in pacrypt(). No special handling here! +# if ( preg_match("/^dovecot:/", Config::read('encrypt')) ) { +# $split_method = preg_split ('/:/', Config::read('encrypt')); +# $method = strtoupper($split_method[1]); +# $password = '{' . $method . '}' . $password; +# } + +#TODO: 2nd clause should be the first for self explaining code. +#TODO: When calling config::Read with parameter we sould be right that read return false if the parameter isn't in our config file. + if(Config::read('maildir_name_hook') != 'NO' && function_exists(Config::read('maildir_name_hook')) ) { + $hook_func = $CONF['maildir_name_hook']; + $maildir = $hook_func ($fDomain, $fUsername); + } + elseif (Config::read('domain_path') == "YES") + { + if (Config::read('domain_in_mailbox') == "YES") + { + $maildir = $domain . "/" . $username . "/"; + } + else + { + $maildir = $domain . "/" . $local_part . "/"; + } + } + else + { + $maildir = $username . "/"; + } + + db_begin(); + + $active = db_get_boolean($active); + $quota = multiply_quota ($quota); + + $alias_data = array( + 'address' => $username, + 'goto' => $username, + 'domain' => $domain, + 'active' => $active, + ); + + $result = db_insert('alias', $alias_data); +#MARK: db_insert returns true/false?? + if ($result != 1) + { + $this->errormsg[] = Lang::read('pAlias_result_error') . "\n($username -> $username)\n"; + return false; + } + + $mailbox_data = array( + 'username' => $username, + 'password' => $password, + 'name' => $name, + 'maildir' => $maildir, + 'local_part' => $local_part, + 'quota' => $quota, + 'domain' => $domain, + 'active' => $active, + ); + $result = db_insert('mailbox', $mailbox_data); +#MARK: Same here! + if ($result != 1 || !mailbox_postcreation($username,$domain,$maildir, $quota)) { + $this->errormsg[] = Lang::read('pCreate_mailbox_result_error') . "\n($username)\n"; + db_rollback(); + return false; + } else { + db_commit(); + db_log ($domain, 'create_mailbox', $username); + + + if ($mail == true) + { + # TODO: move "send the mail" to a function + $fTo = $username; + $fFrom = Config::read('admin_email'); + $fSubject = Lang::read('pSendmail_subject_text'); + $fBody = Config::read('welcome_text'); + + if (!smtp_mail ($fTo, $fFrom, $fSubject, $fBody)) + { + $this->errormsg[] = Lang::read('pSendmail_result_error'); + return false; + } + } + + create_mailbox_subfolders($username,$plain); + + } + return true; + } + + + + + 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; + list(/*$local_part*/,$domain) = explode ('@', $username); + + $E_username = escape_string($username); + $E_domain = escape_string($domain); + +#TODO: At this level of table by key calls we should think about a solution in our query function and drupal like {mailbox} {alias}. +# Pseudocode for db_query etc. +# if {} in query then +# table_by_key( content between { and } ) +# else error + + $table_mailbox = table_by_key('mailbox'); + $table_alias = table_by_key('alias'); + $table_vacation = table_by_key('vacation'); + $table_vacation_notification = table_by_key('vacation_notification'); + + db_begin(); + +#TODO: ture/false replacement! + $error = 0; + + $result = db_query("SELECT * FROM $table_alias WHERE address = '$E_username' AND domain = '$domain'"); + if($result['rows'] == 1) { + $result = db_delete('alias', 'address', $username); + db_log ($domain, 'delete_alias', $username); + } else { + $this->errormsg[] = "no alias $username"; # todo: better message, make translatable + $error = 1; + } + + /* is there a mailbox? if do delete it from orbit; it's the only way to be sure */ + $result = db_query ("SELECT * FROM $table_mailbox WHERE username='$E_username' AND domain='$domain'"); + if ($result['rows'] == 1) + { + $result = db_delete('mailbox', 'username', $username); + $postdel_res=mailbox_postdeletion($username,$domain); + if ($result != 1 || !$postdel_res) + { + + $tMessage = Lang::read('pDelete_delete_error') . "$username ("; + if ($result['rows']!=1) # TODO: invalid test, $result is from db_delete and only contains the number of deleted rows + { + $tMessage.='mailbox'; + if (!$postdel_res) $tMessage.=', '; + $this->errormsg[] = "no mailbox $username"; # todo: better message, make translatable + $error = 1; + } + if (!$postdel_res) + { + $tMessage.='post-deletion'; + $this->errormsg[] = "post-deletion script failed"; # todo: better message, make translatable + $error = 1; + } + $this->errormsg[] = $tMessage.')'; + # TODO: does db_rollback(); make sense? Not sure because mailbox_postdeletion was already called (move the call checking the db_delete result?) + # TODO: maybe mailbox_postdeletion should be run after all queries, just before commit/rollback + $error = 1; +# return false; # TODO: does this make sense? Or should we still cleanup vacation and vacation_notification? + } + db_log ($domain, 'delete_mailbox', $username); + } else { + $this->errormsg[] = "no mailbox $username"; # TODO: better message, make translatable + $error = 1; + } + $result = db_query("SELECT * FROM $table_vacation WHERE email = '$E_username' AND domain = '$domain'"); + if($result['rows'] == 1) { + db_delete('vacation', 'email', $username); + db_delete('vacation_notification', 'on_vacation', $username); # TODO: delete vacation_notification independent of vacation? (in case of "forgotten" vacation_notification entries) + } + db_commit(); + if ($error != 0) return false; + return true; + } + +} + +/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ Deleted: trunk/model/UserHandler.php =================================================================== --- trunk/model/UserHandler.php 2011-03-24 13:00:23 UTC (rev 1018) +++ trunk/model/UserHandler.php 2011-03-28 23:15:11 UTC (rev 1019) @@ -1,315 +0,0 @@ -<?php -# $Id$ - -/** - * Simple class to represent a user. - */ -class UserHandler { - - protected $username = null; - - public $errormsg = array(); - - public function __construct($username) { - $this->username = strtolower($username); - } - - public function change_pass($old_password, $new_password) { - error_log('UserHandler->change_pass is deprecated. Please use UserHandler->change_pw!'); - return $this->change_pw($new_password, $old_password); - } - - /** - * @return boolean true on success; false on failure - * @param string $old_password - * @param string $new_passwords - * @param bool $match = true - * - * All passwords need to be plain text; they'll be hashed appropriately - * 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'); - - 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); - $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 ); - - if ($result != 1) { - db_log ($domain, 'edit_password', "FAILURE: " . $this->username); - $this->errormsg[] = Lang::read('pEdit_mailbox_result_error'); - return false; - } - - db_log ($domain, 'edit_password', $this->username); - return true; - } - - /** - * Attempt to log a user in. - * @param string $username - * @param string $password - * @return boolean true on successful login (i.e. password matches etc) - */ - public static function login($username, $password) { - $username = escape_string($username); - - $table_mailbox = table_by_key('mailbox'); - $active = db_get_boolean(True); - $query = "SELECT password FROM $table_mailbox WHERE username='$username' AND active='$active'"; - - $result = db_query ($query); - if ($result['rows'] == 1) - { - $row = db_array ($result['result']); - $crypt_password = pacrypt ($password, $row['password']); - - if($row['password'] == $crypt_password) { - return true; - } - } - return false; - } -/** - * Add mailbox - * @param password string password of account - * @param gen boolean - * @param name string - * - */ - public function add($password, $name = '', $quota = -999, $active = true, $mail = true ) { -# FIXME: default value of $quota (-999) is intentionally invalid. Add fallback to default quota. -# Solution: Invent an sub config class with additional informations about domain based configs like default qouta. -# FIXME: Should the parameters be optional at all? -# TODO: check if parameters are valid/allowed (quota?). -# TODO: most code should live in a separate function that can be used by add and edit. -# TODO: On the longer term, the web interface should also use this class. - -# TODO: copy/move all checks and validations from create-mailbox.php here - - $username = $this->username; - list($local_part,$domain) = explode ('@', $username); - - -#TODO: more self explaining language strings! - if(!check_mailbox ($domain)) { - $this->errormsg[] = Lang::read('pCreate_mailbox_username_text_error3'); - return false; - } - - # check if an alias with this name already exists - $result = db_query ("SELECT * FROM " . table_by_key('alias') . " WHERE address='" . escape_string($username) . "'"); - if ($result['rows'] == 1){ - $this->errormsg[] = Lang::read('pCreate_mailbox_username_text_error2'); - return false; - } - - $plain = $password; - $password = pacrypt ($password); - -# TODO: if we want to have the encryption method in the encrypted password string, it should be done in pacrypt(). No special handling here! -# if ( preg_match("/^dovecot:/", Config::read('encrypt')) ) { -# $split_method = preg_split ('/:/', Config::read('encrypt')); -# $method = strtoupper($split_method[1]); -# $password = '{' . $method . '}' . $password; -# } - -#TODO: 2nd clause should be the first for self explaining code. -#TODO: When calling config::Read with parameter we sould be right that read return false if the parameter isn't in our config file. - if(Config::read('maildir_name_hook') != 'NO' && function_exists(Config::read('maildir_name_hook')) ) { - $hook_func = $CONF['maildir_name_hook']; - $maildir = $hook_func ($fDomain, $fUsername); - } - elseif (Config::read('domain_path') == "YES") - { - if (Config::read('domain_in_mailbox') == "YES") - { - $maildir = $domain . "/" . $username . "/"; - } - else - { - $maildir = $domain . "/" . $local_part . "/"; - } - } - else - { - $maildir = $username . "/"; - } - - db_begin(); - - $active = db_get_boolean($active); - $quota = multiply_quota ($quota); - - $alias_data = array( - 'address' => $username, - 'goto' => $username, - 'domain' => $domain, - 'active' => $active, - ); - - $result = db_insert('alias', $alias_data); -#MARK: db_insert returns true/false?? - if ($result != 1) - { - $this->errormsg[] = Lang::read('pAlias_result_error') . "\n($username -> $username)\n"; - return false; - } - - $mailbox_data = array( - 'username' => $username, - 'password' => $password, - 'name' => $name, - 'maildir' => $maildir, - 'local_part' => $local_part, - 'quota' => $quota, - 'domain' => $domain, - 'active' => $active, - ); - $result = db_insert('mailbox', $mailbox_data); -#MARK: Same here! - if ($result != 1 || !mailbox_postcreation($username,$domain,$maildir, $quota)) { - $this->errormsg[] = Lang::read('pCreate_mailbox_result_error') . "\n($username)\n"; - db_rollback(); - return false; - } else { - db_commit(); - db_log ($domain, 'create_mailbox', $username); - - - if ($mail == true) - { - # TODO: move "send the mail" to a function - $fTo = $username; - $fFrom = Config::read('admin_email'); - $fSubject = Lang::read('pSendmail_subject_text'); - $fBody = Config::read('welcome_text'); - - if (!smtp_mail ($fTo, $fFrom, $fSubject, $fBody)) - { - $this->errormsg[] = Lang::read('pSendmail_result_error'); - return false; - } - } - - create_mailbox_subfolders($username,$plain); - - } - return true; - } - - - - - 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; - list(/*$local_part*/,$domain) = explode ('@', $username); - - $E_username = escape_string($username); - $E_domain = escape_string($domain); - -#TODO: At this level of table by key calls we should think about a solution in our query function and drupal like {mailbox} {alias}. -# Pseudocode for db_query etc. -# if {} in query then -# table_by_key( content between { and } ) -# else error - - $table_mailbox = table_by_key('mailbox'); - $table_alias = table_by_key('alias'); - $table_vacation = table_by_key('vacation'); - $table_vacation_notification = table_by_key('vacation_notification'); - - db_begin(); - -#TODO: ture/false replacement! - $error = 0; - - $result = db_query("SELECT * FROM $table_alias WHERE address = '$E_username' AND domain = '$domain'"); - if($result['rows'] == 1) { - $result = db_delete('alias', 'address', $username); - db_log ($domain, 'delete_alias', $username); - } else { - $this->errormsg[] = "no alias $username"; # todo: better message, make translatable - $error = 1; - } - - /* is there a mailbox? if do delete it from orbit; it's the only way to be sure */ - $result = db_query ("SELECT * FROM $table_mailbox WHERE username='$E_username' AND domain='$domain'"); - if ($result['rows'] == 1) - { - $result = db_delete('mailbox', 'username', $username); - $postdel_res=mailbox_postdeletion($username,$domain); - if ($result != 1 || !$postdel_res) - { - - $tMessage = Lang::read('pDelete_delete_error') . "$username ("; - if ($result['rows']!=1) # TODO: invalid test, $result is from db_delete and only contains the number of deleted rows - { - $tMessage.='mailbox'; - if (!$postdel_res) $tMessage.=', '; - $this->errormsg[] = "no mailbox $username"; # todo: better message, make translatable - $error = 1; - } - if (!$postdel_res) - { - $tMessage.='post-deletion'; - $this->errormsg[] = "post-deletion script failed"; # todo: better message, make translatable - $error = 1; - } - $this->errormsg[] = $tMessage.')'; - # TODO: does db_rollback(); make sense? Not sure because mailbox_postdeletion was already called (move the call checking the db_delete result?) - # TODO: maybe mailbox_postdeletion should be run after all queries, just before commit/rollback - $error = 1; -# return false; # TODO: does this make sense? Or should we still cleanup vacation and vacation_notification? - } - db_log ($domain, 'delete_mailbox', $username); - } else { - $this->errormsg[] = "no mailbox $username"; # TODO: better message, make translatable - $error = 1; - } - $result = db_query("SELECT * FROM $table_vacation WHERE email = '$E_username' AND domain = '$domain'"); - if($result['rows'] == 1) { - db_delete('vacation', 'email', $username); - db_delete('vacation_notification', 'on_vacation', $username); # TODO: delete vacation_notification independent of vacation? (in case of "forgotten" vacation_notification entries) - } - db_commit(); - if ($error != 0) return false; - return true; - } - -} - -/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ Modified: trunk/scripts/postfixadmin-cli.php =================================================================== --- trunk/scripts/postfixadmin-cli.php 2011-03-24 13:00:23 UTC (rev 1018) +++ trunk/scripts/postfixadmin-cli.php 2011-03-28 23:15:11 UTC (rev 1019) @@ -533,11 +533,11 @@ return array( - 'user' => array( - 'add'=> 'Adds a new user with mailbox.', - 'update'=> 'Updates a user.', - 'delete' => 'Deletes a user.', - 'pw' => 'Changes the PW for a user.', + 'mailbox' => array( + 'add'=> 'Adds a new mailbox.', + 'update'=> 'Updates a mailbox.', + 'delete' => 'Deletes a mailbox.', + 'pw' => 'Changes the PW for a mailbox.', ), 'alias' => array( 'add' => 'Adds a new alias.', Modified: trunk/scripts/shells/alias.php =================================================================== --- trunk/scripts/shells/alias.php 2011-03-24 13:00:23 UTC (rev 1018) +++ trunk/scripts/shells/alias.php 2011-03-28 23:15:11 UTC (rev 1019) @@ -222,8 +222,8 @@ */ function __handle($address) { -### TODO: don't use UserHandler, instead add delete function to AliasHandler (if not already there) -### using UserHandler for deleting aliases is like taking a sledgehammer to crack a nut +### TODO: don't use MailboxHandler, instead add delete function to AliasHandler (if not already there) +### using MailboxHandler for deleting aliases is like taking a sledgehammer to crack a nut ### (and will probably cause some error messages that I added today ;-) ### Implemented check it please! Copied: trunk/scripts/shells/mailbox.php (from rev 1018, trunk/scripts/shells/user.php) =================================================================== --- trunk/scripts/shells/mailbox.php (rev 0) +++ trunk/scripts/shells/mailbox.php 2011-03-28 23:15:11 UTC (rev 1019) @@ -0,0 +1,504 @@ +<?php + + +class PostfixAdminMailbox extends Shell { + +/** + * Contains tasks to load and instantiate + * + * @var array + * @access public + */ + var $tasks = array('Add', 'Update', 'Delete', 'Password', 'View'); + + + + +/** + * Show help for this shell. + * + * @access public + */ + function help() { + $head = "Usage: postfixadmin-cli mailbox <task> [<address>] [] [-m <method>]\n"; + $head .= "-----------------------------------------------\n"; + $head .= "Parameters:\n\n"; + + $commands = array( + 'task' => "\t<task>\n" . + "\t\tAvailable values:\n\n". + "\t\t".sprintf("%-20s %s", "view: ", "View an existing mailbox.")."\n". + "\t\t".sprintf("%-20s %s", "add: ", "Adds a new mailbox.")."\n". + "\t\t".sprintf("%-20s %s", "update: ", "Updates a mailbox.")."\n". + "\t\t".sprintf("%-20s %s", "delete: ", "Deletes a mailbox")."\n". + "\t\t".sprintf("%-20s %s", "password: ", "Changes the PW for a mailbox.")."\n", + 'address' => "\t[<address>]\n" . + "\t\tA CakePHP core class name (e.g: Component, HtmlHelper).\n", + ); + + $this->out($head); + if (!isset($this->args[1])) { + foreach ($commands as $cmd) { + $this->out("{$cmd}\n\n"); + } + } elseif (isset($commands[low($this->args[1])])) { + $this->out($commands[low($this->args[1])] . "\n\n"); + } else { + $this->out("Command '" . $this->args[1] . "' not found"); + } + } + + +} + +class AddTask extends Shell { +/** + * Execution method always used for tasks + * + * @access public + */ + + # Find one function that matches all executes in shell childclasses. + # Eventually getopts like call in __handle?? + + + function execute() { + if (empty($this->args)) { + $this->__interactive(); + } + + if (!empty($this->args[0])) { + if (!empty($this->params['g'])) { + $this->__handle($this->args[0], NULL, true, $this->args[1], $this->args[2]); + } else { + $this->__handle($this->args[0], $this->args[1], false, $this->args[2], $this->args[3]); + } + } + } +/** + * Interactive + * + * @access private + */ + function __interactive() { + while(0==0) { + $question = "Enter address:"; + $address = $this->in($question); + + if(preg_match("/^((?:(?:(?:[a-zA-Z0-9][\.\-\+_]?)*)[a-zA-Z0-9])+)\@((?:(?:(?:[a-zA-Z0-9][\.\-_]?){0,62})[a-zA-Z0-9])+)\.([a-zA-Z0-9]{2,6})$/", $address) == 1) + break; + + $this->err("Invalid emailaddress"); + + } + $question = "Do you want to generate a random password?"; + $random = $this->in($question, array('y','n')); + + $random == 'y' ? $random = true : $random = false; + + + $password = NULL; + if ($random == false) { + $question = "Enter the password:"; + $password = $this->in($question); + } + + $question = "Enter name:"; + $name = $this->in($question); + + $question = "Enter quota (MB):"; + $quota = $this->in($question); + + $question1[] = "Do you reallywant to add mailbox with this options?"; + $question1[] = "Address: \t$address"; + if($random) + $question1[] = "Random Password."; + else + $question1[] = "Password: \t$password"; + $question1[] = "Name: \t$name"; + $question1[] = "Quota: \t$quota MB"; + $create = $this->in(join("\n", $question1), array('y','n')); + + $create == 'y' ? $random = true : $random = false; + + if ($create) + $this->__handle($address, $password, $random, $name, $quota); + } + +/** + * Interactive + * + * @access private + */ + function __handle($address, $password, $gen = false, $name = '', $quota = 0) { + $pw = NULL; + if ($gen) { + $pw = generate_password(); + } elseif ($password != NULL) { + $pw = $password; + } + + $handler = new MailboxHandler($address); + $return = $handler->add($pw, $name, $quota, true, true ); +#CHECK! +if ( !empty($this->params['q']) ) { + + if( $return == false ) { + $this->_stop(1); + } +} else { + if( $return == false ) { +### When $this->error is used, $this->_stop is useless. +### Changed $this->error to stop with level 1. +### Eventually param q check in $this->error is better!! !Important! + $this->error("Error:", $handler->errormsg); + } else { + $this->out(""); + if ($name != '') + $this->out("Mailbox for $name generated."); + else + $this->out("Mailbox generated."); + $this->hr(); + $this->out(sprintf('Mailaddress: %-20s', $address)); + $this->out(sprintf('Password: %-20s',$pw)); + $this->out(sprintf('Quota: %-20sMB',$quota)); + $this->hr(); + } +#CHECK! +} + return; + } +/** + * Displays help contents + * + * @access public + */ + function help() { + $this->hr(); + $this->out("Usage: postfixadmin-cli mailbox add <address> [<password>] <name> <quota> [-g]"); + $this->hr(); + $this->out('Commands:'); + $this->out("\n\tadd\n\t\tAdds mailbox in interactive mode."); + $this->out("\n\tadd <address> [<password>] [-g] <name> <quota>\n\t\tAdds mailbox for <address> with password <password> of if -g with rand pw. <quota> in MB."); + $this->out(""); + $this->_stop(); + } + +} +class UpdateTask extends Shell { +/** + * Execution method always used for tasks + * + * @access public + */ + function execute() { + if (empty($this->args)) { + $this->help(); + //$this->__interactive(); + } + + if (!empty($this->args[0])) { + $this->help(); + } + } +/** + * Interactive + * + * @access private + */ + function __interactive() { + + } +/** + * Displays help contents + * + * @access public + */ + function help() { + $this->hr(); + $this->out("Not Implemented yet! If you want to change a password use the password command."); + /*$this->out("Usage: postfixadmin-cli mailbox update <args>"); + $this->hr(); + $this->out('Commands:'); + $this->out("\n\tmodel\n\t\tbakes model in interactive mode."); + $this->out("\n\tmodel <name>\n\t\tbakes model file with no associations or validation"); + $this->out("");*/ + $this->_stop(); + } + +} +class DeleteTask extends Shell { +/** + * Execution method always used for tasks + * + * @access public + */ + function execute() { + + if (empty($this->args)) { + $this->__interactive(); + } + + if (!empty($this->args[0])) { + $output = $this->__handle($this->args[0]); + $this->out($output); + + } + } +/** + * Interactive + * + * @access private + */ + function __interactive() { + $question[] = "Which Address do you want to delete?"; + + $address = $this->in(join("\n", $question)); + + + $question = "Do you really want to delete mailbox of '$address'?"; + + $create = $this->in($question, array('y','n')); + + $create == 'y' ? $random = true : $random = false; + + if ($create) + $this->__handle($address); + + } + /** + * Interactive + * + * @access private + */ + function __handle($address) { + + + $handler = new MailboxHandler($address); + $status = $handler->delete(); + if ( ! $status ) { + $this->error("Error:", join("\n", $handler->errormsg)); + + } else { + $this->out("Mailbox of '$address' was deleted."); + } + return; + + } +/** + * Displays help contents + * + * @access public + */ + function help() { + $this->hr(); + $this->out("Usage: postfixadmin-cli mailbox model <arg1>"); + $this->hr(); + $this->out('Commands:'); + $this->out("\n\tdelete\n\t\tdeletes mailbox in interactive mode."); + $this->out("\n\tdelete <address>\n\t\tdeletes mailbox with address <address>"); + $this->out(""); + $this->_stop(); + } + +} +class PasswordTask extends Shell { +/** + * Execution method always used for tasks + * + * @access public + */ + function execute() { + $random = false; + if (empty($this->args)) { + $this->__interactive(); + } + + if (!empty($this->args[0])) { + + $address = $this->args[0]; + + if (isset($this->params['g']) && $this->params['g'] == true ) { + $random = true; + $password = NULL; + } elseif (isset($this->args[1]) && strlen($this->args[1]) > 8) { # TODO use $CONF['min_password_length'] + $password = $this->args[1]; + } else { + + $this->Dispatch->stderr('Missing <newpw> or -g. Falling back to interactive mode.'); + $this->__interactive(); + } + $this->__handle($address, $password, $random); + + + } + } +/** + * Interactive + * + * @access private + */ + function __interactive() { + + while(0==0) { + $question = "Which address' password do you want to change?"; + $address = $this->in($question); + + if(preg_match("/^((?:(?:(?:[a-zA-Z0-9][\.\-\+_]?)*)[a-zA-Z0-9])+)\@((?:(?:(?:[a-zA-Z0-9][\.\-_]?){0,62})[a-zA-Z0-9])+)\.([a-zA-Z0-9]{2,6})$/", $address) == 1) + break; + + $this->err("Invalid emailaddress"); + + } + + + $question2[] = "Do you want to change the password?"; + $question2[] = "Are you really sure?"; + $sure = $this->in(join("\n", $question2), array('y','n')); + + + if ($sure == 'n' ) { + $this->out('You\'re not sure.'); + $this->_stop(); + } + + $question = "Do you want to generate a random password?"; + $random = $this->in($question, array('y','n')); + + $random == 'y' ? $random = true : $random = false; + + + $password = NULL; + if ($random == false) { + $question = "Pleas enter the new password?"; + $password = $this->in($question); + } + + $this->__handle($address, $password, $random); + + + + + } + /** + * Interactive + * + * @access private + */ + function __handle($address, $password = NULL, $random = false) { + + if ($random == true) { + $password = generate_password(); + } + if ($password != NULL) { + $handler = new MailboxHandler($address); + + if ( ! $handler->change_pw($password, NULL, false) ){ + $this->error("Change Password",join("\n", $handler->errormsg)); + } + } + + $this->out(""); + $this->out("Password updated."); + $this->hr(); + $this->out(sprintf('The Mail address is %20s', $address)); + $this->out(sprintf('The new password is %20s',$password)); + $this->hr(); + + return ; + } +/** + * Displays help contents + * + * @access public + */ + function help() { + $this->out(""); + $this->hr(); + $this->out("Usage: postfixadmin-cli mailbox password <address> [<newpw>] [-g]"); + $this->hr(); + $this->out('Commands:'); + $this->out("\n\tpassword\n\t\tchanges the password in interactive mode."); + $this->out("\n\tpassword <address> [<newpw>] [-g]\n\t\tchanges the password to <newpw> or if -g genereate a new pw for <address>"); + $this->out(""); + $this->_stop(); + } + +} +class ViewTask extends Shell { +/** + * Execution method always used for tasks + * + * @access public + */ + function execute() { + + if (empty($this->args)) { + $this->__interactive(); + } + + if (!empty($this->args[0])) { + $output = $this->__handle($this->args[0]); + $this->out($output); + + } + } +/** + * Interactive + * + * @access private + */ + function __interactive() { + $question[] = "Which Address do you want to view?"; + + $address = $this->in(join("\n", $question)); + + $this->__handle($address); + + + + } + /** + * Interactive + * + * @access private + */ + function __handle($address) { + + + $handler = new MailboxHandler($address); + if ( ! $handler->view() ) { + $this->error("Not Found!", "The mailbox you have searched could not be found."); + } +# TODO: offer alternative output formats (based on parameter) +# TODO: whitespace fix - 8 lines below + $result = $handler->return; + $this->out(sprintf("Entries for: %s\n", $address)); + $this->out(""); + $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+",'','','','','','','')); + $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+",'','','','','','','')); + + return; + + } +/** + * Displays help contents + * + * @access public + */ + function help() { + $this->out(""); + $this->hr(); + $this->out("Usage: postfixadmin-cli mailbox view <address>"); + $this->hr(); + $this->out('Commands:'); + $this->out("\n\tview\n\t\tView mailbox. Select address in interactive mode."); + $this->out("\n\tview <address>\n\t\tView mailbox with address <address>"); + $this->out(""); + $this->_stop(); + } + +} Deleted: trunk/scripts/shells/user.php =================================================================== --- trunk/scripts/shells/user.php 2011-03-24 13:00:23 UTC (rev 1018) +++ trunk/scripts/shells/user.php 2011-03-28 23:15:11 UTC (rev 1019) @@ -1,504 +0,0 @@ -<?php - - -class PostfixAdminUser extends Shell { - -/** - * Contains tasks to load and instantiate - * - * @var array - * @access public - */ - var $tasks = array('Add', 'Update', 'Delete', 'Password', 'View'); - - - - -/** - * Show help for this shell. - * - * @access public - */ - function help() { - $head = "Usage: postfixadmin-cli user <task> [<address>] [] [-m <method>]\n"; - $head .= "-----------------------------------------------\n"; - $head .= "Parameters:\n\n"; - - $commands = array( - 'task' => "\t<task>\n" . - "\t\tAvailable values:\n\n". - "\t\t".sprintf("%-20s %s", "view: ", "View an existing user.")."\n". - "\t\t".sprintf("%-20s %s", "add: ", "Adds a new user with mailbox.")."\n". - "\t\t".sprintf("%-20s %s", "update: ", "Updates a user.")."\n". - "\t\t".sprintf("%-20s %s", "delete: ", "Deletes a user")."\n". - "\t\t".sprintf("%-20s %s", "password: ", "Changes the PW for a user.")."\n", - 'address' => "\t[<address>]\n" . - "\t\tA CakePHP core class name (e.g: Component, HtmlHelper).\n", - ); - - $this->out($head); - if (!isset($this->args[1])) { - foreach ($commands as $cmd) { - $this->out("{$cmd}\n\n"); - } - } elseif (isset($commands[low($this->args[1])])) { - $this->out($commands[low($this->args[1])] . "\n\n"); - } else { - $this->out("Command '" . $this->args[1] . "' not found"); - } - } - - -} - -class AddTask extends Shell { -/** - * Execution method always used for tasks - * - * @access public - */ - - # Find one function that matches all executes in shell childclasses. - # Eventually getopts like call in __handle?? - - - function execute() { - if (empty($this->args)) { - $this->__interactive(); - } - - if (!empty($this->args[0])) { - if (!empty($this->params['g'])) { - $this->__handle($this->args[0], NULL, true, $this->args[1], $this->args[2]); - } else { - $this->__handle($this->args[0], $this->args[1], false, $this->args[2], $this->args[3]); - } - } - } -/** - * Interactive - * - * @access private - */ - function __interactive() { - while(0==0) { - $question = "Enter address:"; - $address = $this->in($question); - - if(preg_match("/^((?:(?:(?:[a-zA-Z0-9][\.\-\+_]?)*)[a-zA-Z0-9])+)\@((?:(?:(?:[a-zA-Z0-9][\.\-_]?){0,62})[a-zA-Z0-9])+)\.([a-zA-Z0-9]{2,6})$/", $address) == 1) - break; - - $this->err("Invalid emailaddress"); - - } - $question = "Do you want to generate a random password?"; - $random = $this->in($question, array('y','n')); - - $random == 'y' ? $random = true : $random = false; - - - $password = NULL; - if ($random == false) { - $question = "Enter the password:"; - $password = $this->in($question); - } - - $question = "Enter name:"; - $name = $this->in($question); - - $question = "Enter quota (MB):"; - $quota = $this->in($question); - - $question1[] = "Do you reallywant to add mailbox with this options?"; - $question1[] = "Address: \t$address"; - if($random) - $question1[] = "Random Password."; - else - $question1[] = "Password: \t$password"; - $question1[] = "Name: \t$name"; - $question1[] = "Quota: \t$quota MB"; - $create = $this->in(join("\n", $question1), array('y','n')); - - $create == 'y' ? $random = true : $random = false; - - if ($create) - $this->__handle($address, $password, $random, $name, $quota); - } - -/** - * Interactive - * - * @access private - */ - function __handle($address, $password, $gen = false, $name = '', $quota = 0) { - $pw = NULL; - if ($gen) { - $pw = generate_password(); - } elseif ($password != NULL) { - $pw = $password; - } - - $handler = new UserHandler($address); - $return = $handler->add($pw, $name, $quota, true, true ); -#CHECK! -if ( !empty($this->params['q']) ) { - - if( $return == false ) { - $this->_stop(1); - } -} else { - if( $return == false ) { -### When $this->error is used, $this->_stop is useless. -### Changed $this->error to stop with level 1. -### Eventually param q check in $this->error is better!! !Important! - $this->error("Error:", $handler->errormsg); - } else { - $this->out(""); - if ($name != '') - $this->out("Mailbox for $name generated."); - else - $this->out("Mailbox generated."); - $this->hr(); - $this->out(sprintf('Mailaddress: %-20s', $address)); - $this->out(sprintf('Password: %-20s',$pw)); - $this->out(sprintf('Quota: %-20sMB',$quota)); - $this->hr(); - } -#CHECK! -} - return; - } -/** - * Displays help contents - * - * @access public - */ - function help() { - $this->hr(); - $this->out("Usage: postfixadmin-cli user add <address> [<password>] <name> <quota> [-g]"); - $this->hr(); - $this->out('Commands:'); - $this->out("\n\tadd\n\t\tAdds mailbox in interactive mode."); - $this->out("\n\tadd <address> [<password>] [-g] <name> <quota>\n\t\tAdds mailbox for <address> with password <password> of if -g with rand pw. <quota> in MB."); - $this->out(""); - $this->_stop(); - } - -} -class UpdateTask extends Shell { -/** - * Execution method always used for tasks - * - * @access public - */ - function execute() { - if (empty($this->args)) { - $this->help(); - //$this->__interactive(); - } - - if (!empty($this->args[0])) { - $this->help(); - } - } -/** - * Interactive - * - * @access private - */ - function __interactive() { - - } -/** - * Displays help contents - * - * @access public - */ - function help() { - $this->hr(); - $this->out("Not Implemented yet! If you want to change a password use the password command."); - /*$this->out("Usage: postfixadmin-cli user update <args>"); - $this->hr(); - $this->out('Commands:'); - $this->out("\n\tmodel\n\t\tbakes model in interactive mode."); - $this->out("\n\tmodel <name>\n\t\tbakes model file with no associations or validation"); - $this->out("");*/ - $this->_stop(); - } - -} -class DeleteTask extends Shell { -/** - * Execution method always used for tasks - * - * @access public - */ - function execute() { - - if (empty($this->args)) { - $this->__interactive(); - } - - if (!empty($this->args[0])) { - $output = $this->__handle($this->args[0]); - $this->out($output); - - } - } -/** - * Interactive - * - * @access private - */ - function __interactive() { - $question[] = "Which Address do you want to delete?"; - - $address = $this->in(join("\n", $question)); - - - $question = "Do you really want to delete mailbox of '$address'?"; - - $create = $this->in($question, array('y','n')); - - $create == 'y' ? $random = true : $random = false; - - if ($create) - $this->__handle($address); - - } - /** - * Interactive - * - * @access private - */ - function __handle($address) { - - - $handler = new UserHandler($address); - $status = $handler->delete(); - if ( ! $status ) { - $this->error("Error:", join("\n", $handler->errormsg)); - - } else { - $this->out("Mailbox of '$address' was deleted."); - } - return; - - } -/** - * Displays help contents - * - * @access public - */ - function help() { - $this->hr(); - $this->out("Usage: postfixadmin-cli user model <arg1>"); - $this->hr(); - $this->out('Commands:'); - $this->out("\n\tdelete\n\t\tdeletes mailbox in interactive mode."); - $this->out("\n\tdelete <address>\n\t\tdeletes mailbox with address <address>"); - $this->out(""); - $this->_stop(); - } - -} -class PasswordTask extends Shell { -/** - * Execution method always used for tasks - * - * @access public - */ - function execute() { - $random = false; - if (empty($this->args)) { - $this->__interactive(); - } - - if (!empty($this->args[0])) { - - $address = $this->args[0]; - - if (isset($this->params['g']) && $this->params['g'] == true ) { - $random = true; - $password = NULL; - } elseif (isset($this->args[1]) && strlen($this->args[1]) > 8) { # TODO use $CONF['min_password_length'] - $password = $this->args[1]; - } else { - - $this->Dispatch->stderr('Missing <newpw> or -g. Falling back to interactive mode.'); - $this->__interactive(); - } - $this->__handle($address, $password, $random); - - - } - } -/** - * Interactive - * - * @access private - */ - function __interactive() { - - while(0==0) { - $question = "Which address' password do you want to change?"; - $address = $this->in($question); - - if(preg_match("/^((?:(?:(?:[a-zA-Z0-9][\.\-\+_]?)*)[a-zA-Z0-9])+)\@((?:(?:(?:[a-zA-Z0-9][\.\-_]?){0,62})[a-zA-Z0-9])+)\.([a-zA-Z0-9]{2,6})$/", $address) == 1) - break; - - $this->err("Invalid emailaddress"); - - } - - - $question2[] = "Do you want to change the password?"; - $question2[] = "Are you really sure?"; - $sure = $this->in(join("\n", $question2), array('y','n')); - - - if ($sure == 'n' ) { - $this->out('You\'re not sure.'); - $this->_stop(); - } - - $question = "Do you want to generate a random password?"; - $random = $this->in($question, array('y','n')); - - $random == 'y' ? $random = true : $random = false; - - - $password = NULL; - if ($random == false) { - $question = "Pleas enter the new password?"; - $password = $this->in($question); - } - - $this->__handle($address, $password, $random); - - - - - } - /** - * Interactive - * - * @access private - */ - function __handle($address, $password = NULL, $random = false) { - - if ($random == true) { - $password = generate_password(); - } - if ($password != NULL) { - $handler = new UserHandler($address); - - if ( ! $handler->change_pw($password, NULL, false) ){ - $this->error("Change Password",join("\n", $handler->errormsg)); - } - } - - $this->out(""); - $this->out("Password updated."); - $this->hr(); - $this->out(sprintf('The Mail address is %20s', $address)); - $this->out(sprintf('The new password is %20s',$password)); - $this->hr(); - - return ; - } -/** - * Displays help contents - * - * @access public - */ - function help() { - $this->out(""); - $this->hr(); - $this->out("Usage: postfixadmin-cli user password <address> [<newpw>] [-g]"); - $this->hr(); - $this->out('Commands:'); - ... [truncated message content] |