From da3ef2e1c0f0081d0e2453fe6eb5a9fa93e52624 Mon Sep 17 00:00:00 2001 From: Christian Boltz Date: Sun, 26 Dec 2010 00:34:43 +0000 Subject: [PATCH] shells/alias.php: - changed AliasHandler->get call - it does not need the address as parameter - adopted to true/false return values - some minor changes and TODO notes models-ext/AliasHandler.php - deleted, replaced with a redirect to ../model/AliasHandler.php git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@913 a1433add-5e2c-0410-b055-b7f2511e0802 --- scripts/models-ext/AliasHandler.php | 190 +--------------------------- scripts/shells/alias.php | 20 ++- 2 files changed, 14 insertions(+), 196 deletions(-) diff --git a/scripts/models-ext/AliasHandler.php b/scripts/models-ext/AliasHandler.php index 2801e3b8..534623f5 100644 --- a/scripts/models-ext/AliasHandler.php +++ b/scripts/models-ext/AliasHandler.php @@ -1,190 +1,2 @@ username = $username; - } - - /** - * @return array - list of email addresses the user's mail is forwarded to. - * (may be an empty list, especially if $CONF['alias_control'] is turned off... - * @param boolean - by default we don't return special addresses (e.g. vacation and mailbox alias); pass in true here if you wish to. - */ - public function get($alias, $all=false) { - $alias = escape_string($alias); - $table_alias = table_by_key('alias'); - - $sql = "SELECT * FROM $table_alias WHERE address='$alias'"; - $result = db_query($sql); - if($result['rows'] == 1) { - $row = db_array ($result['result']); - // At the moment Postfixadmin stores aliases in it's database in a comma seperated list; this may change one day. - $list = explode(',', $row['goto']); - if($all) { - return $list; - } - - $new_list = array(); - /* if !$all, remove vacation & mailbox aliases */ - foreach($list as $address) { - if($address != '' ) { - if($this->is_vacation_address($address) || $this->is_mailbox_alias($address)) { - } - else { - $new_list[] = $address; - } - } - } - $list = $new_list; - $this->return = $list; - return 0; - } - return 1; - } - - /** - * @param string $address - * @param string $username - * @return boolean true if the username is an alias for the mailbox AND we have alias_control turned off. - */ - public function is_mailbox_alias($address) { - global $CONF; - $username = $this->username; - if($address == $username) { - return true; - } - return false; - } - - /** - * @param string $address - * @return boolean true if the address contains the vacation domain - */ - public function is_vacation_address($address) { - global $CONF; - if($CONF['vacation'] == 'YES') { - if(stripos($address, '@' . $CONF['vacation_domain'])) { - return true; - } - } - return false; - } - /** - * @return boolean true on success - * @param string $username - * @param array $addresses - list of aliases to set for the user. - * @param string flags - forward_and_store or remote_only or '' - * @param boolean $vacation_persist - set to false to stop the vacation address persisting across updates - * Set the user's aliases to those provided. If $addresses ends up being empty the alias record is removed. - */ - public function update($addresses, $flags = '', $vacation_persist=true) { - // find out if the user is on vacation or not; if they are, - // then the vacation alias needs adding to the db (as we strip it out in the get method) - // likewise with the alias_control address. - - $valid_flags = array('', 'forward_and_store', 'remote_only'); - if(!in_array($flags, $valid_flags)) { - die("Invalid flag passed into update()... : $flag - valid options are :" . implode(',', $valid_flags)); - } - $addresses = array_unique($addresses); - - $original = $this->get(true); - $tmp = preg_split('/@/', $this->username); - $domain = $tmp[1]; - - foreach($original as $address) { - if($vacation_persist) { - if($this->is_vacation_address($address)) { - $addresses[] = $address; - } - } - if($flags != 'remote_only') { - if($this->is_mailbox_alias($address)) { - $addresses[] = $address; - } - } - } - $addresses = array_unique($addresses); - - $new_list = array(); - if($flags == 'remote_only') { - foreach($addresses as $address) { - // strip out our username... if it's in the list given. - if($address != $this->username) { - $new_list[] = $address; - } - } - $addresses = $new_list; - } - - if($flags == 'forward_and_store') { - if(!in_array($this->username, $addresses)) { - $addresses[] = $this->username; - } - } - $new_list = array(); - foreach($addresses as $address) { - if($address != '') { - $new_list[] = $address; - } - } - $addresses = array_unique($new_list); - $username = escape_string($this->username); - $goto = escape_string(implode(',', $addresses)); - $table_alias = table_by_key('alias'); - if(sizeof($addresses) == 0) { - $sql = "DELETE FROM $table_alias WHERE address = '$username'"; - } - if($this->hasAliasRecord() == false) { - $true = db_get_boolean(True); - $sql = "INSERT INTO $table_alias (address, goto, domain, created, modified, active) VALUES ('$username', '$goto', '$domain', NOW(), NOW(), '$true')"; - } - else { - $sql = "UPDATE $table_alias SET goto = '$goto', modified = NOW() WHERE address = '$username'"; - } - $result = db_query($sql); - if($result['rows'] != 1) { - return false; - } - db_log($username, $domain, 'edit_alias', "$username -> $goto"); - return true; - } - - /** - * Determine whether a local delivery address is present. This is - * stores as an alias with the same name as the mailbox name (username) - * @return boolean true if local delivery is enabled - */ - public function hasStoreAndForward() { - $aliases = $this->get(true); - if(in_array($this->username, $aliases)) { - return true; - } - return false; - } - - /** - * @return boolean true if the user has an alias record (i.e row in alias table); else false. - */ - public function hasAliasRecord() { - $username = escape_string($this->username); - $table_alias = table_by_key('alias'); - $sql = "SELECT * FROM $table_alias WHERE address = '$username'"; - $result = db_query($sql); - if($result['rows'] == 1) { - return true; - } - return false; - } -} - -/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */ +require('../model/AliasHandler.php'); diff --git a/scripts/shells/alias.php b/scripts/shells/alias.php index 937aceb8..1c821f11 100644 --- a/scripts/shells/alias.php +++ b/scripts/shells/alias.php @@ -183,7 +183,7 @@ class UpdateTask extends Shell { */ function help() { $this->hr(); - $this->out("Not Implemented yet! If you want to change a password use the password command."); + $this->out("Not Implemented yet!"); /*$this->out("Usage: postfixadmin-cli user update "); //$this->hr(); //$this->out('Commands:'); @@ -244,10 +244,12 @@ class DeleteTask extends Shell { */ 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 +### (and will probably cause some error messages that I added today ;-) $handler = new UserHandler($address); $status = $handler->delete(); - if ($status == 0) { + if ($status == true) { $this->out("Mailbox of '$address' was deleted."); } else { @@ -357,7 +359,7 @@ class PasswordTask extends Shell { * @access private */ function __handle($address, $password = NULL, $random = false) { - +# TODO: Does PasswordTask really make sense for Aliases? Probably not... if ($random == true) { $password = generate_password(); } @@ -437,8 +439,10 @@ class ViewTask extends Shell { $handler = new AliasHandler($address); - $status = $handler->get($address); - if ($status == 0) { + $status = $handler->get(); # TODO: set the "all" flag? + if ( ! $status) { + # TODO: error message + } else { $result = $handler->return; $this->out(sprintf("Entries for: %s\n", $address)); @@ -446,6 +450,8 @@ class ViewTask extends Shell { foreach($result AS $goto) { $this->out("\t -> ".$goto); } + # TODO: display "deliver to mailbox" + # TODO: display if vacation is on? } return; @@ -467,4 +473,4 @@ class ViewTask extends Shell { $this->_stop(); } -} \ No newline at end of file +}