2010-09-27 01:14:42 +02:00
< ? php
2011-10-16 21:33:31 +02:00
# $Id$
2010-09-27 01:14:42 +02:00
2011-10-16 21:33:31 +02:00
/**
2010-09-27 01:14:42 +02:00
* Handlers User level alias actions - e . g . add alias , get aliases , update etc .
*/
2011-03-02 23:37:22 +01:00
class DomainHandler extends PFAHandler {
2010-09-27 01:14:42 +02:00
2011-03-02 23:37:22 +01:00
protected $username = null ; # actually it's the domain - variable name kept for consistence with the other classes
2011-10-16 20:50:33 +02:00
protected $db_table = null ;
2011-03-02 23:37:22 +01:00
protected $id_field = null ;
protected $struct = array ();
protected $new = 0 ; # 1 on create, otherwise 0
2010-09-27 01:14:42 +02:00
public $errormsg = array ();
/**
* @ param string $username
*/
2011-03-02 23:37:22 +01:00
public function __construct ( $username , $new = 0 ) {
2011-04-17 18:31:09 +02:00
$this -> username = strtolower ( $username ); # TODO: find a better place for strtolower() to avoid a special constructor in DomainHandler (or agree that $username should be lowercase in all *Handler classes ;-)
2011-03-02 23:37:22 +01:00
if ( $new ) $this -> new = 1 ;
# TODO: if $new == 1, check that item does NOT exist and is a valid (in this case) domain
# TODO: else: check if item exists. error out if not.
# TODO: target: if construct succeeds, $this->username is valid
$this -> initStruct ();
}
private function initStruct () {
2011-10-16 20:50:33 +02:00
$this -> db_table = 'domain' ;
2011-03-02 23:37:22 +01:00
$this -> id_field = 'domain' ;
2011-07-30 00:06:52 +02:00
# TODO: shorter PALANG labels ;-)
# TODO: hardcode 'default' to Config::read in pacol()?
$transp = boolconf ( 'transport' ) ? 1 : 0 ; # TOOD: use a function or write a Config::intbool function
2011-10-16 20:50:33 +02:00
$quota = boolconf ( 'quota' ) ? 1 : 0 ; # TOOD: use a function or write a Config::intbool function
2011-07-30 00:06:52 +02:00
$dom_q = boolconf ( 'domain_quota' ) ? 1 : 0 ; # TOOD: use a function or write a Config::intbool function
2011-03-02 23:37:22 +01:00
2011-04-20 00:08:20 +02:00
$this -> struct = array (
2011-07-30 00:06:52 +02:00
# field name allow display in... type $PALANG label $PALANG description default / options / not in database
2011-03-02 23:37:22 +01:00
# editing? form list
2011-10-16 21:33:31 +02:00
'domain' => pacol ( $this -> new , 1 , 1 , 'text' , 'pAdminEdit_domain_domain' , '' ),
'description' => pacol ( 1 , 1 , 1 , 'text' , 'pAdminEdit_domain_description' , '' ),
'aliases' => pacol ( 1 , 1 , 1 , 'num' , 'pAdminEdit_domain_aliases' , 'pAdminEdit_domain_aliases_text' , Config :: read ( 'aliases' ) ),
'mailboxes' => pacol ( 1 , 1 , 1 , 'num' , 'pAdminEdit_domain_mailboxes' , 'pAdminEdit_domain_mailboxes_text' , Config :: read ( 'mailboxes' ) ),
'maxquota' => pacol ( $quota , $quota , $quota , 'num' , 'pAdminEdit_domain_maxquota' , 'pAdminEdit_domain_maxquota_text' , Config :: read ( 'maxquota' ) ),
'quota' => pacol ( $dom_q , $dom_q , $dom_q , 'num' , 'pAdminEdit_domain_quota' , 'pAdminEdit_domain_maxquota_text' , Config :: read ( 'domain_quota_default' ) ),
'transport' => pacol ( $transp , $transp , $transp , 'enum' , 'pAdminEdit_domain_transport' , 'pAdminEdit_domain_transport_text' , Config :: read ( 'transport_default' ) ,
2011-07-30 00:06:52 +02:00
/*options*/ $this -> getTransports () ),
2011-10-16 21:33:31 +02:00
'backupmx' => pacol ( 1 , 1 , 1 , 'bool' , 'pAdminEdit_domain_backupmx' , '' ),
'active' => pacol ( 1 , 1 , 1 , 'bool' , 'pAdminEdit_domain_active' , '' ),
'default_aliases' => pacol ( $this -> new , 1 , 0 , 'bool' , 'pAdminCreate_domain_defaultaliases ' , '' , '' , '' , /*not in db*/ 1 ),
'created' => pacol ( 0 , 0 , 0 , 'ts' , '' /* TODO: "created" label */ , '' ),
'modified' => pacol ( 0 , 0 , 1 , 'ts' , 'pAdminList_domain_modified' , '' ),
2011-03-02 23:37:22 +01:00
);
2010-09-27 01:14:42 +02:00
}
public function getTransports () {
2011-02-14 00:01:44 +01:00
return Config :: read ( 'transport_options' );
2010-09-27 01:14:42 +02:00
}
2011-03-02 23:37:22 +01:00
# TODO: specific for CLI? If yes, move to CLI code
2010-09-27 01:14:42 +02:00
public function getTransport ( $id ) {
2011-02-14 00:01:44 +01:00
$transports = Config :: read ( 'transport_options' );
return $transports [ $id - 1 ];
2010-09-27 01:14:42 +02:00
}
2011-10-16 21:33:31 +02:00
2011-03-02 23:37:22 +01:00
public function add ( $values ) {
# TODO: make this a generic function for add and edit
2011-10-16 20:50:33 +02:00
# TODO: move DB writes etc. to separate save() function (to allow on-the-fly validation before saving to DB)
2011-03-02 23:37:22 +01:00
( $values [ 'backupmx' ] == true ) ? $values [ 'backupmx' ] = db_get_boolean ( true ) : $values [ 'backupmx' ] = db_get_boolean ( false );
2011-10-16 20:50:33 +02:00
if ( $this -> new == 1 ) {
$values [ $this -> id_field ] = $this -> username ;
}
2011-03-02 23:37:22 +01:00
# base validation
$checked = array ();
foreach ( $this -> struct as $key => $row ) {
2011-10-16 21:33:31 +02:00
if ( $row [ 'editable' ] == 0 ) { # not editable
2011-10-16 20:50:33 +02:00
if ( $this -> new == 1 ) {
$checked [ $key ] = $row [ 'default' ];
}
2011-10-16 21:33:31 +02:00
} else {
2011-07-30 00:06:52 +02:00
$func = " _inp_ " . $row [ 'type' ];
2011-10-16 20:50:33 +02:00
# TODO: error out if an editable field is not set in $values (on $this->new) -or- skip if in edit mode
$val = $values [ $key ];
2011-07-30 00:06:52 +02:00
if ( $row [ 'type' ] != " password " || strlen ( $values [ $key ]) > 0 || $this -> new == 1 ) { # skip on empty (aka unchanged) password on edit
2011-03-02 23:37:22 +01:00
if ( method_exists ( $this , $func ) ) {
$checked [ $key ] = $this -> { $func }( $values [ $key ]);
} else {
# TODO: warning if no validation function exists?
$checked [ $key ] = $values [ $key ];
}
}
}
}
# TODO: more validation
2011-10-16 20:50:33 +02:00
# $checked[$this->id_field] = $this->username; # should already be set (if $this->new) via values[$this->id_field] and the base check
$db_values = $checked ;
unset ( $db_values [ 'default_aliases' ]); # TODO: automate based on $this->struct
$result = db_insert ( $this -> db_table , $db_values );
2011-02-14 00:01:44 +01:00
if ( $result != 1 ) {
2011-04-20 00:08:20 +02:00
$this -> errormsg [] = Lang :: read ( 'pAdminCreate_domain_result_error' ) . " \n ( " . $this -> username . " ) \n " ;
2011-02-14 00:15:40 +01:00
return false ;
2011-02-14 00:01:44 +01:00
} else {
2011-03-02 23:37:22 +01:00
if ( $this -> new && $values [ 'default_aliases' ]) {
2011-02-14 00:01:44 +01:00
foreach ( Config :: read ( 'default_aliases' ) as $address => $goto ) {
2011-04-20 00:08:20 +02:00
$address = $address . " @ " . $this -> username ;
2011-02-14 00:01:44 +01:00
# TODO: use AliasHandler->add instead of writing directly to the alias table
2010-09-27 01:14:42 +02:00
$arr = array (
'address' => $address ,
'goto' => $goto ,
2011-04-20 00:08:20 +02:00
'domain' => $this -> username ,
2011-02-14 00:01:44 +01:00
);
$result = db_insert ( 'alias' , $arr );
2011-07-30 00:06:52 +02:00
# TODO: error checking
2010-09-27 01:14:42 +02:00
}
}
2011-07-30 00:06:52 +02:00
$tMessage = Lang :: read ( 'pAdminCreate_domain_result_success' ) . " <br />( " . $this -> username . " )</br /> " ; # TODO: remove <br> # TODO: tMessage is not used/returned anywhere
2010-09-27 01:14:42 +02:00
}
2011-04-20 00:08:20 +02:00
if ( ! domain_postcreation ( $this -> username )) {
2011-07-30 00:06:52 +02:00
$tMessage = Lang :: read ( 'pAdminCreate_domain_error' ); # TODO: tMessage is not used/returned anywhere
2010-09-27 01:14:42 +02:00
}
2011-04-20 00:08:20 +02:00
db_log ( $this -> username , 'create_domain' , " " );
2011-02-14 00:15:40 +01:00
return true ;
2010-09-27 01:14:42 +02:00
}
2011-10-16 21:33:31 +02:00
public function view () {
2011-10-16 20:50:33 +02:00
$table_domain = table_by_key ( $this -> db_table );
2011-10-16 21:33:31 +02:00
2011-02-14 00:01:44 +01:00
$E_domain = escape_string ( $this -> username );
$result = db_query ( " SELECT domain, description, aliases, mailboxes, maxquota, quota, transport, backupmx, DATE_FORMAT(created, '%d.%m.%y') AS created, DATE_FORMAT(modified, '%d.%m.%y') AS modified, active FROM $table_domain WHERE domain=' $E_domain ' " );
2010-09-27 01:14:42 +02:00
if ( $result [ 'rows' ] != 0 ) {
2011-02-14 00:01:44 +01:00
$this -> return = db_array ( $result [ 'result' ]);
2011-02-14 00:15:40 +01:00
return true ;
2010-09-27 01:14:42 +02:00
}
2011-03-02 23:37:22 +01:00
$this -> errormsg [] = " Domain " . $this -> username . " does not exist. " ;
# $this->errormsg[] = $result['error'];
2011-02-14 00:15:40 +01:00
return false ;
2010-09-27 01:14:42 +02:00
}
2011-02-14 00:35:49 +01:00
/**
* @ return true on success false on failure
*/
2011-10-16 21:33:31 +02:00
public function delete () {
if ( ! $this -> view () ) {
2011-02-14 00:35:49 +01:00
$this -> errormsg [] = 'A domain with that name does not exist.' ; # TODO: make translatable
return false ;
}
$this -> errormsg [] = '*** Domain deletion not implemented yet ***' ;
return false ; # XXX function aborts here until TODO below is implemented! XXX
# TODO: recursively delete mailboxes, aliases, alias_domains, fetchmail entries etc. before deleting the domain
# TODO: move the needed code from delete.php here
2011-10-16 20:50:33 +02:00
$result = db_delete ( $this -> db_table , $this -> id_field , $this -> username );
2011-10-16 21:33:31 +02:00
if ( $result == 1 ) {
2011-02-14 00:35:49 +01:00
list ( /*NULL*/ , $domain ) = explode ( '@' , $this -> username );
2011-02-15 23:20:27 +01:00
db_log ( $domain , 'delete_domain' , $this -> username ); # TODO delete_domain is not a valid db_log keyword yet because we don't yet log add/delete domain
2011-02-14 00:35:49 +01:00
return true ;
}
}
2010-09-27 01:14:42 +02:00
}
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */