2009-03-13 21:48:24 +01:00
< ? php
/**
* Handlers User level alias actions - e . g . add alias , get aliases , update etc .
*/
class AliasHandler {
private $username = null ;
2010-12-26 20:48:10 +01:00
# TODO: implement a "delete" method. Pseudocode:
# - check if alias exists
# - check if mailbox exists - if yes, error out (never delete the alias of a mailbox!)
# - (if still here) delete alias
2009-03-13 22:48:38 +01:00
/**
* @ param string $username
*/
2009-03-13 21:48:24 +01:00
public function __construct ( $username ) {
2010-12-26 20:48:10 +01:00
$this -> username = strtolower ( $username );
2009-03-13 21:48:24 +01:00
}
2009-03-13 22:48:38 +01:00
2009-03-13 21:48:24 +01:00
/**
2009-03-13 22:48:38 +01:00
* @ return array - list of email addresses the user ' s mail is forwarded to .
2010-12-26 20:48:10 +01:00
* ( may be an empty list , especially if $CONF [ 'alias_control' ] is turned off ... )
2009-03-13 21:48:24 +01:00
* @ 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 ) {
$username = escape_string ( $this -> username );
$table_alias = table_by_key ( 'alias' );
$sql = " SELECT * FROM $table_alias WHERE address=' $username ' " ;
$result = db_query ( $sql );
2010-12-26 20:48:10 +01:00
if ( $result [ 'rows' ] != 1 ) {
return false ;
}
# TODO: whitespace fix: move ~20 lines below one step left
2009-03-13 21:48:24 +01:00
$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 ) {
2010-12-26 20:48:10 +01:00
$this -> return = $list ;
return true ;
2009-03-13 21:48:24 +01:00
}
2010-12-26 20:48:10 +01:00
$filtered_list = array ();
2009-03-13 21:48:24 +01:00
/* if !$all, remove vacation & mailbox aliases */
foreach ( $list as $address ) {
if ( $address != '' ) {
if ( $this -> is_vacation_address ( $address ) || $this -> is_mailbox_alias ( $address )) {
2010-12-26 20:48:10 +01:00
# TODO: store "vacation_active" and "mailbox" status - should be readable public
2009-03-13 21:48:24 +01:00
}
else {
2010-12-26 20:48:10 +01:00
$filtered_list [] = $address ;
2009-03-13 21:48:24 +01:00
}
}
}
2010-12-26 20:48:10 +01:00
$this -> return = $filtered_list ;
2010-12-26 01:20:12 +01:00
return true ;
2009-03-13 21:48:24 +01:00
}
/**
* @ 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 .
2010-12-26 20:48:10 +01:00
* TODO : comment for @ return : does alias_control really matter here ?
2009-03-13 21:48:24 +01:00
*/
public function is_mailbox_alias ( $address ) {
global $CONF ;
$username = $this -> username ;
if ( $address == $username ) {
2010-12-26 20:48:10 +01:00
# TODO: check (via SQL query) if there is really a mailbox with this address
2009-03-13 21:48:24 +01:00
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' ) {
2010-12-26 20:48:10 +01:00
if ( stripos ( $address , '@' . $CONF [ 'vacation_domain' ])) { # TODO: check full vacation address user#domain.com@vacation_domain
2009-03-13 21:48:24 +01:00
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
2010-12-26 20:48:10 +01:00
* Set the user 's aliases to those provided. If $addresses ends up being empty the alias record is removed. # TODO: deleting that' s buggy behaviour , error out instead
2009-03-13 21:48:24 +01:00
*/
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.
2010-12-26 20:48:10 +01:00
# TODO: move all validation from edit-alias/create-alias and users/edit-alias here
2009-03-13 21:48:24 +01:00
$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 );
2010-12-26 20:48:10 +01:00
list ( /*NULL*/ , $domain ) = explode ( '@' , $this -> username );
if ( ! $this -> get ( true ) ) die ( " Alias not existing? " ); # TODO: better error behaviour
2009-03-13 21:48:24 +01:00
2010-12-26 20:48:10 +01:00
foreach ( $this -> return as $address ) {
2009-03-13 21:48:24 +01:00
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' ) {
2010-12-26 20:48:10 +01:00
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/
2009-03-13 21:48:24 +01:00
// 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 != '' ) {
2010-12-26 20:48:10 +01:00
$new_list [] = $address ; # TODO use remove_from_array, see above
2009-03-13 21:48:24 +01:00
}
}
$addresses = array_unique ( $new_list );
2010-12-26 20:48:10 +01:00
$E_username = escape_string ( $this -> username );
$goto = implode ( ',' , $addresses );
# $table_alias = table_by_key('alias');
2009-03-13 21:48:24 +01:00
if ( sizeof ( $addresses ) == 0 ) {
2010-12-26 20:48:10 +01:00
# $result = db_delete('alias', 'address', $this->username); # '"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?
2009-03-13 21:48:24 +01:00
}
2010-12-26 20:48:10 +01:00
if ( $this -> hasAliasRecord () == false ) { # TODO should never happen in update() - see also the comments on handling DELETE above
2009-03-13 21:48:24 +01:00
$true = db_get_boolean ( True );
2010-12-26 20:48:10 +01:00
$alias_data = array (
'address' => $this -> username ,
'goto' => $goto ,
'domain' => $domain ,
'active' => db_get_boolean ( True ),
);
$result = db_insert ( 'alias' , $alias_data );
# $sql = "INSERT INTO $table_alias (address, goto, domain, created, modified, active) VALUES ('$username', '$goto', '$domain', NOW(), NOW(), '$true')";
# $result = db_query($sql);
2009-03-13 21:48:24 +01:00
}
else {
2010-12-26 20:48:10 +01:00
$alias_data = array (
'goto' => $goto ,
);
$result = db_update ( 'alias' , " address = ' $E_username ' " , $alias_data );
# $sql = "UPDATE $table_alias SET goto = '$goto', modified = NOW() WHERE address = '$username'";
2009-03-13 21:48:24 +01:00
}
2010-12-26 20:48:10 +01:00
if ( $result != 1 ) {
2009-03-13 21:48:24 +01:00
return false ;
}
2010-12-26 20:48:10 +01:00
db_log ( $this -> username , $domain , 'edit_alias' , " $E_username -> $goto " );
2009-03-13 21:48:24 +01:00
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 () {
2010-12-26 20:48:10 +01:00
$result = $this -> get ( true ); # TODO: error checking?
if ( in_array ( $this -> username , $this -> return )) {
2009-03-13 21:48:24 +01:00
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 ;
}
2010-12-26 01:20:12 +01:00
/**
* @ return return value of previously called method
*/
public function result () {
return $this -> return ;
}
}
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */