AliasHandler.php:

- add initStruct() (not the final version, but works for now)
- add initMsg()
- replace $this->username with $this->id everywhere
- drop __construct() - default __construct will be used now

users/edit-alias.php, xmlrpc.php, VacationHandler.php, scripts/shells/alias.php:
- use default init sequence for AliasHandler (new, then ->init())


git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1310 a1433add-5e2c-0410-b055-b7f2511e0802
pull/2/head
Christian Boltz 13 years ago
parent b8812686aa
commit da9a69dd0d

@ -16,20 +16,43 @@ class AliasHandler extends PFAHandler {
*/ */
public $return = null; public $return = null;
/** protected function initStruct() {
* @param string $username $this->db_table = 'alias';
*/ $this->id_field = 'address';
public function __construct($username) {
$this->username = strtolower($username); $this->struct=array(
# field name allow display in... type $PALANG label $PALANG description default / options / ...
# editing? form list
'address' => pacol( $this->new, 1, 1, 'mail', 'pCreate_alias_domain_alias' , 'pCreate_alias_domain_alias_text' ),
'goto' => pacol( 1, 1, 1, 'mail', 'pCreate_alias_domain_target' , 'pCreate_alias_domain_target_text' ),
'domain' => pacol( $this->new, 0, 0, 'text', '' , '' ),
'active' => pacol( 1, 1, 1, 'bool', 'pAdminEdit_domain_active' , '' , 1 ),
'created' => pacol( 0, 0, 1, 'ts', 'created' , '' ),
'modified' => pacol( 0, 0, 1, 'ts', 'pAdminList_domain_modified' , '' ),
);
}
protected function initMsg() {
$this->msg['error_already_exists'] = 'pCreate_alias_address_text_error2';
$this->msg['error_does_not_exist'] = 'pCreate_alias_address_text_error1'; # TODO: better error message
if ($this->new) {
$this->msg['logname'] = 'create_alias';
$this->msg['store_error'] = 'pCreate_alias_result_error';
} else {
$this->msg['logname'] = 'edit_alias';
$this->msg['store_error'] = 'pEdit_alias_result_error';
}
} }
/** /**
* @return bool true if succeed * @return bool true if succeed
* (may be an empty list, especially if $CONF['alias_control'] is turned off...) * (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. * @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($all=false) { public function get($all=false) {
$E_username = escape_string($this->username); $E_username = escape_string($this->id);
$table_alias = table_by_key('alias'); $table_alias = table_by_key('alias');
$sql = "SELECT * FROM $table_alias WHERE address='$E_username'"; $sql = "SELECT * FROM $table_alias WHERE address='$E_username'";
@ -71,7 +94,7 @@ class AliasHandler extends PFAHandler {
public function is_mailbox_alias($address) { public function is_mailbox_alias($address) {
global $CONF; global $CONF;
if($address != $this->username) { # avoid false positives if $address is a mailbox if($address != $this->id) { # avoid false positives if $address is a mailbox
return false; return false;
} }
@ -120,7 +143,7 @@ class AliasHandler extends PFAHandler {
} }
$addresses = array_unique($addresses); $addresses = array_unique($addresses);
list (/*NULL*/, $domain) = explode('@', $this->username); list (/*NULL*/, $domain) = explode('@', $this->id);
if ( ! $this->get(true) ) die("Alias not existing?"); # TODO: better error behaviour if ( ! $this->get(true) ) die("Alias not existing?"); # TODO: better error behaviour
@ -142,7 +165,7 @@ class AliasHandler extends PFAHandler {
if($flags == 'remote_only') { if($flags == 'remote_only') {
foreach($addresses as $address) { # TODO: write a remove_from_array function, see http://tech.petegraham.co.uk/2007/03/22/php-remove-values-from-array/ foreach($addresses as $address) { # TODO: write a remove_from_array function, see http://tech.petegraham.co.uk/2007/03/22/php-remove-values-from-array/
// strip out our username... if it's in the list given. // strip out our username... if it's in the list given.
if($address != $this->username) { if($address != $this->id) {
$new_list[] = $address; $new_list[] = $address;
} }
} }
@ -150,8 +173,8 @@ class AliasHandler extends PFAHandler {
} }
if($flags == 'forward_and_store') { if($flags == 'forward_and_store') {
if(!in_array($this->username, $addresses)) { if(!in_array($this->id, $addresses)) {
$addresses[] = $this->username; $addresses[] = $this->id;
} }
} }
$new_list = array(); $new_list = array();
@ -161,15 +184,15 @@ class AliasHandler extends PFAHandler {
} }
} }
$addresses = array_unique($new_list); $addresses = array_unique($new_list);
$E_username = escape_string($this->username); $E_username = escape_string($this->id);
$goto = implode(',', $addresses); $goto = implode(',', $addresses);
if(sizeof($addresses) == 0) { if(sizeof($addresses) == 0) {
# $result = db_delete('alias', 'address', $this->username); # '"DELETE FROM $table_alias WHERE address = '$username'"; # TODO: should never happen and causes broken behaviour # $result = db_delete('alias', 'address', $this->id); # '"DELETE FROM $table_alias WHERE address = '$username'"; # TODO: should never happen and causes broken behaviour
error_log("Alias set to empty / Attemp to delete: " . $this->username); # TODO: more/better error handling - maybe just return false? error_log("Alias set to empty / Attemp to delete: " . $this->id); # TODO: more/better error handling - maybe just return false?
} }
if($this->hasAliasRecord() == false) { # TODO should never happen in update() - see also the comments on handling DELETE above if($this->hasAliasRecord() == false) { # TODO should never happen in update() - see also the comments on handling DELETE above
$alias_data = array( $alias_data = array(
'address' => $this->username, 'address' => $this->id,
'goto' => $goto, 'goto' => $goto,
'domain' => $domain, 'domain' => $domain,
'active' => db_get_boolean(True), 'active' => db_get_boolean(True),
@ -179,7 +202,7 @@ class AliasHandler extends PFAHandler {
$alias_data = array( $alias_data = array(
'goto' => $goto, 'goto' => $goto,
); );
$result = db_update('alias', 'address', $this->username, $alias_data); $result = db_update('alias', 'address', $this->id, $alias_data);
} }
if($result != 1) { if($result != 1) {
return false; return false;
@ -195,7 +218,7 @@ class AliasHandler extends PFAHandler {
*/ */
public function hasStoreAndForward() { public function hasStoreAndForward() {
$result = $this->get(true); # TODO: error checking? $result = $this->get(true); # TODO: error checking?
if(in_array($this->username, $this->return)) { if(in_array($this->id, $this->return)) {
return true; return true;
} }
return false; return false;
@ -205,7 +228,7 @@ class AliasHandler extends PFAHandler {
* @return boolean true if the user has an alias record (i.e row in alias table); else false. * @return boolean true if the user has an alias record (i.e row in alias table); else false.
*/ */
public function hasAliasRecord() { public function hasAliasRecord() {
$username = escape_string($this->username); $username = escape_string($this->id);
$table_alias = table_by_key('alias'); $table_alias = table_by_key('alias');
$sql = "SELECT * FROM $table_alias WHERE address = '$username'"; $sql = "SELECT * FROM $table_alias WHERE address = '$username'";
$result = db_query($sql); $result = db_query($sql);
@ -224,15 +247,15 @@ class AliasHandler extends PFAHandler {
return false; return false;
} }
if ($this->is_mailbox_alias($this->username) ) { if ($this->is_mailbox_alias($this->id) ) {
$this->errormsg[] = 'This alias belongs to a mailbox and can\'t be deleted.'; # TODO: make translatable $this->errormsg[] = 'This alias belongs to a mailbox and can\'t be deleted.'; # TODO: make translatable
return false; return false;
} }
$result = db_delete('alias', 'address', $this->username); $result = db_delete('alias', 'address', $this->id);
if( $result == 1 ) { if( $result == 1 ) {
list(/*NULL*/,$domain) = explode('@', $this->username); list(/*NULL*/,$domain) = explode('@', $this->id);
db_log ($domain, 'delete_alias', $this->username); db_log ($domain, 'delete_alias', $this->id);
return true; return true;
} }
} }

@ -13,7 +13,8 @@ class VacationHandler {
* @return boolean true on success. * @return boolean true on success.
*/ */
function remove() { function remove() {
$ah = new AliasHandler($this->username); $ah = new AliasHandler();
$ah->init($this->username);
$result = $ah->get(true); $result = $ah->get(true);
if($result === true) { // fetch all # TODO check $result, error handling if($result === true) { // fetch all # TODO check $result, error handling
$aliases = $ah->return; $aliases = $ah->return;
@ -53,7 +54,8 @@ class VacationHandler {
* Why do we bother storing true/false in the vacation table if the alias dictates it anyway? * Why do we bother storing true/false in the vacation table if the alias dictates it anyway?
*/ */
function check_vacation() { function check_vacation() {
$ah = new AliasHandler($this->username); $ah = new AliasHandler();
$ah->init($this->username);
$success = $ah->get(true); # fetch all. $success = $ah->get(true); # fetch all.
if (!$success) { if (!$success) {
return false; # TODO: error handling? return false; # TODO: error handling?
@ -127,7 +129,8 @@ class VacationHandler {
} }
# TODO error check # TODO error check
# TODO wrap whole function in db_begin / db_commit (or rollback)? # TODO wrap whole function in db_begin / db_commit (or rollback)?
$ah = new AliasHandler($this->username); $ah = new AliasHandler();
$ah->init($this->username);
$alias = $ah->get(true); $alias = $ah->get(true);
$aliases = $ah->return; $aliases = $ah->return;
$vacation_address = $this->getVacationAlias(); $vacation_address = $this->getVacationAlias();

@ -103,7 +103,8 @@ class AddTask extends Shell {
*/ */
function __handle($address, $goto) { function __handle($address, $goto) {
$handler = new AliasHandler($address); $handler = new AliasHandler(1);
$handler->init($address);
$return = $handler->add($goto); $return = $handler->add($goto);
if($return == 1) { if($return == 1) {
@ -227,7 +228,8 @@ class DeleteTask extends Shell {
### (and will probably cause some error messages that I added today ;-) ### (and will probably cause some error messages that I added today ;-)
### Implemented check it please! ### Implemented check it please!
$handler = new AliasHandler($address); $handler = new AliasHandler();
$handler->init($address);
$status = $handler->delete(); $status = $handler->delete();
if ($status == true) { if ($status == true) {
$this->out("Mailbox of '$address' was deleted."); $this->out("Mailbox of '$address' was deleted.");
@ -296,7 +298,8 @@ class ViewTask extends Shell {
function __handle($address) { function __handle($address) {
$handler = new AliasHandler($address); $handler = new AliasHandler();
$handler->init($address);
$status = $handler->get(); # TODO: set the "all" flag? $status = $handler->get(); # TODO: set the "all" flag?
if ( ! $status) { if ( ! $status) {
$this->error("Error: Not Found", "The requested alias was not found!"); $this->error("Error: Not Found", "The requested alias was not found!");
@ -343,3 +346,5 @@ class ViewTask extends Shell {
} }
} }
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */

@ -37,7 +37,9 @@ $USERID_USERNAME = authentication_get_username();
$tmp = preg_split ('/@/', $USERID_USERNAME); $tmp = preg_split ('/@/', $USERID_USERNAME);
$USERID_DOMAIN = $tmp[1]; $USERID_DOMAIN = $tmp[1];
$ah = new AliasHandler($USERID_USERNAME); $ah = new AliasHandler();
$ah->init($USERID_USERNAME);
$smarty->assign ('USERID_USERNAME', $USERID_USERNAME); $smarty->assign ('USERID_USERNAME', $USERID_USERNAME);

@ -139,7 +139,8 @@ class AliasProxy {
* @return array - array of aliases this user has. Array may be empty. * @return array - array of aliases this user has. Array may be empty.
*/ */
public function get() { public function get() {
$ah = new AliasHandler($_SESSION['username']); $ah = new AliasHandler();
$ah->init($_SESSION['username']);
/* I see no point in returning special addresses to the user. */ /* I see no point in returning special addresses to the user. */
$ah->get(false); $ah->get(false);
return $ah->result; return $ah->result;
@ -151,7 +152,8 @@ class AliasProxy {
* @return boolean true * @return boolean true
*/ */
public function update($addresses, $flags) { public function update($addresses, $flags) {
$ah = new AliasHandler($_SESSION['username']); $ah = new AliasHandler();
$ah->init($_SESSION['username']);
/** /**
* if the user is on vacation, they should use VacationProxy stuff to remove it * if the user is on vacation, they should use VacationProxy stuff to remove it
* and we'll never return the vacation address from here anyway * and we'll never return the vacation address from here anyway
@ -164,7 +166,8 @@ class AliasProxy {
* (i.e. their email address is also in the alias table). IF it returns false, then it's 'remote_only' * (i.e. their email address is also in the alias table). IF it returns false, then it's 'remote_only'
*/ */
public function hasStoreAndForward() { public function hasStoreAndForward() {
$ah = new AliasHandler($_SESSION['username']); $ah = new AliasHandler();
$ah->init($_SESSION['username']);
return $ah->hasStoreAndForward(); return $ah->hasStoreAndForward();
} }
} }

Loading…
Cancel
Save