2006-04-26 12:28:53 +00:00
< ? php
/*
----------------------------------------------------------------------
LICENSE
This program is free software ; you can redistribute it and / or
modify it under the terms of the GNU General Public License ( GPL )
as published by the Free Software Foundation ; either version 2
of the License , or ( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
To read the license please visit http :// www . gnu . org / copyleft / gpl . html
----------------------------------------------------------------------
Purpose of file : Manage Email accounts and aliases .
----------------------------------------------------------------------
*/
2009-09-08 05:29:38 +00:00
2006-04-26 12:28:53 +00:00
/**
2009-09-08 05:29:38 +00:00
* This class handle emails ( pop and / or aliases and even wrapper for internal
* classes ) of hosted users .
2006-04-26 12:28:53 +00:00
*
2012-08-23 18:46:47 +00:00
* @ copyright AlternC - Team 2012 - 09 - 01 http :// alternc . com /
* This class is directly using the following alternc MySQL tables :
* address = any used email address will be defined here , mailbox = pop / imap mailboxes , recipient = redirection from an email to another
* and indirectly the domain class , to know domain names from their id in the DB .
* This class is also defining a few hooks , search -> invoke in the code .
2006-04-26 12:28:53 +00:00
*/
class m_mail {
2012-08-23 18:46:47 +00:00
/* ----------------------------------------------------------------- */
/** domain list for this account
2006-04-26 12:28:53 +00:00
* @ access private
*/
var $domains ;
/* ----------------------------------------------------------------- */
2012-08-23 18:46:47 +00:00
/** Number of results for a pager display
* @ access public
2006-04-26 12:28:53 +00:00
*/
2012-08-23 18:46:47 +00:00
var $total ;
2006-04-26 12:28:53 +00:00
2009-09-08 05:29:38 +00:00
2006-04-26 12:28:53 +00:00
/* ----------------------------------------------------------------- */
2012-08-23 18:46:47 +00:00
/** Quota list ( hook for quota class )
2006-04-26 12:28:53 +00:00
*/
function alternc_quota_names () {
return " mail " ;
}
2012-08-23 18:46:47 +00:00
/* ----------------------------------------------------------------- */
/** get_quota ( hook for quota class ), returns the number of used
* service for a quota - bound service
* @ param $name string the named quota we want
* @ return the number of used service for the specified quota ,
* or false if I ' m not the one for the named quota
*/
2012-04-06 10:10:36 +00:00
function alternc_get_quota ( $name ) {
global $db , $err , $cuid ;
if ( $name == " mail " ) {
$err -> log ( " mail " , " getquota " );
2012-08-23 18:46:47 +00:00
$db -> query ( " SELECT COUNT(*) AS cnt FROM address WHERE domain_id in (select id from domaines where compte= $cuid ); " );
2012-04-06 10:10:36 +00:00
$db -> next_record ();
return $db -> f ( " cnt " );
}
2012-08-23 18:46:47 +00:00
return false ;
2012-04-06 10:10:36 +00:00
}
2009-09-08 05:29:38 +00:00
2012-08-23 18:46:47 +00:00
/* ----------------------------------------------------------------- */
/** Password policy kind used in this class ( hook for admin class )
* @ return array an array of policykey => " policy name (for humans) "
2009-11-30 05:02:53 +00:00
*/
function alternc_password_policy () {
2012-08-23 18:46:47 +00:00
return array ( " pop " => _ ( " Email account password " ));
2009-11-30 05:02:53 +00:00
}
2012-04-06 10:10:36 +00:00
2006-04-26 12:28:53 +00:00
/* ----------------------------------------------------------------- */
2012-08-23 18:46:47 +00:00
/** Returns the list of mail - hosting domains for a user
2009-09-08 05:29:38 +00:00
* @ return array indexed array of hosted domains
2006-04-26 12:28:53 +00:00
*/
function enum_domains () {
2012-04-06 10:10:36 +00:00
global $db , $err , $cuid ;
$err -> log ( " mail " , " enum_domains " );
$db -> query ( " select d.id, d.domaine, count(a.id) as nb_mail FROM domaines d left join address a on a.domain_id=d.id where d.compte = $cuid group by d.id order by d.domaine asc; " );
2012-08-23 18:46:47 +00:00
$this -> enum_domains = array ();
2012-04-06 10:10:36 +00:00
while ( $db -> next_record ()){
$this -> enum_domains [] = $db -> Record ;
2006-04-26 12:28:53 +00:00
}
2012-04-06 10:10:36 +00:00
return $this -> enum_domains ;
}
2009-09-08 05:29:38 +00:00
2012-08-23 18:46:47 +00:00
/* ----------------------------------------------------------------- */
2012-04-06 10:10:36 +00:00
/* function used to list every mail address hosted on a domain .
2012-08-23 18:46:47 +00:00
* @ param $dom_id integer the domain id .
* @ param $search string search that string in recipients or address .
* @ param $offset integer skip THAT much emails in the result .
* @ param $count integer return no more than THAT much emails .
* @ result an array of each mail hosted under the domain .
2006-04-26 12:28:53 +00:00
*/
2012-08-23 18:46:47 +00:00
function enum_domain_mails ( $dom_id = null , $search = " " , $offset = 0 , $count = 30 ){
global $db , $err , $cuid , $hooks ;
2012-04-06 10:10:36 +00:00
$err -> log ( " mail " , " enum_domains_mail " );
2012-08-23 18:46:47 +00:00
$search = trim ( $search );
$where = " a.domain_id= $dom_id " ;
if ( $search ) $where .= " AND (a.address LIKE '% " . addslashes ( $search ) . " %' OR r.recipients LIKE '% " . addslashes ( $search ) . " %') " ;
$db -> query ( " SELECT count(a.id) AS total FROM address a LEFT JOIN recipient r ON r.address_id=a.id WHERE $where ; " );
$db -> next_record ();
$this -> total = $db -> f ( " total " );
$db -> query ( " SELECT a.id, a.address, a.password, a.enabled, d.domaine AS domain, m.quota*1024*1024 AS quota, m.bytes AS used, NOT ISNULL(m.id) AS islocal, a.type, r.recipients, m.lastlogin
FROM ( address a LEFT JOIN mailbox m ON m . address_id = a . id ) LEFT JOIN recipient r ON r . address_id = a . id , domaines d
WHERE $where AND d . id = a . domain_id
LIMIT $offset , $count ; " );
if ( ! $db -> next_record ()) {
$err -> raise ( " mail " , _ ( " No mail found for this query " ));
2012-05-16 15:33:04 +00:00
return false ;
}
2006-04-26 12:28:53 +00:00
$res = array ();
2012-08-23 18:46:47 +00:00
do {
$details = $db -> Record ;
// if necessary, fill the typedata with data from hooks ...
if ( $details [ " type " ]) {
$result = $hooks -> invoke ( " mail_get_details " , array ( $details [ " id " ])); // Will fill typedata if necessary
$details [ " typedata " ] = implode ( " <br /> " , $result );
}
$res [] = $details ;
} while ( $db -> next_record ());
2006-04-26 12:28:53 +00:00
return $res ;
}
2012-08-23 18:46:47 +00:00
/* ----------------------------------------------------------------- */
/** Function used to insert a new mail into de the db
* should be used by the web interface , not by third - party programs .
*
* This function calls the hook " hooks_mail_cancreate "
* which must return FALSE if the user can ' t create this email , and raise and error accordingly
*
* @ param $dom_id integer A domain_id ( owned by the user )
* ( will be the part at the right of the @ in the email )
* @ param $mail string the left part of the email to create ( something @ dom_id )
* @ return an hashtable containing the database id of the newly created mail ,
* or false if an error occured ( $err is filled accordingly )
2012-04-06 10:10:36 +00:00
*/
2012-08-23 18:46:47 +00:00
function create ( $dom_id , $mail ){
global $err , $db , $cuid , $quota , $dom ;
2012-04-06 10:10:36 +00:00
$err -> log ( " mail " , " create " );
2012-08-23 18:46:47 +00:00
// Validate the domain id
if ( ! ( $domain = $dom -> get_domain_byid ( $dom_id ))) {
return false ;
2012-08-19 20:50:47 +00:00
}
2012-08-23 18:46:47 +00:00
// Validate the email syntax:
$m = $mail . " @ " . $domain ;
if ( ! filter_var ( $m , FILTER_VALIDATE_EMAIL )){
$err -> raise ( " mail " , _ ( " The email you entered is syntaxically incorrect " ));
return false ;
2012-04-06 10:10:36 +00:00
}
2012-08-23 18:46:47 +00:00
// Call other classes to check we can create it:
$cancreate = $hooks -> invoke ( 'hooks_mail_cancreate' , array ( $dom_id , $domain , $mail ));
if ( in_array ( false , $cancreate , true )) {
return false ;
2006-04-26 12:28:53 +00:00
}
2012-08-19 20:50:47 +00:00
2012-08-23 18:46:47 +00:00
// Check the quota:
2012-08-19 20:50:47 +00:00
if ( ! $quota -> cancreate ( " mail " )) {
2012-08-23 18:46:47 +00:00
$err -> raise ( " mail " , _ ( " You cannot create email addresses: your quota is over. " ));
2012-08-19 20:50:47 +00:00
return false ;
}
2012-08-23 18:46:47 +00:00
// Already exists?
$db -> query ( " SELECT * FROM address WHERE domain_id= " . $dom_id . " AND address=' " . addslashes ( $mail ) . " '; " );
if ( $db -> next_record ()) {
$err -> raise ( " mail " , _ ( " This email address already exists " ));
return false ;
}
// Create it now
$db -> query ( " INSERT INTO address (domain_id, address) VALUES ( $dom_id , ' " . addslashes ( $mail ) . " '); " );
if ( ! ( $id = $db -> lastid ())) {
$err -> raise ( " mail " , _ ( " An unexpected error occured when creating the email " ));
return false ;
}
return $id ;
}
2012-08-19 20:50:47 +00:00
2012-08-23 18:46:47 +00:00
/* ----------------------------------------------------------------- */
/** function used to get every information we can on a mail
* @ param $mail_id integer
* @ return array a hashtable with all the informations for that email
*/
function get_details ( $mail_id ) {
global $db , $err , $cuid ;
$err -> log ( " mail " , " get_details " );
$mail_id = intval ( $mail_id );
// We fetch all the informations for that email: these will fill the hastable :
$db -> query ( " SELECT a.address, a.password, a.enabled, d.domaine AS domain, m.quota, m.bytes/1024/1024 AS used, NOT ISNULL(m.id) AS islocal, a.type
FROM address a LEFT JOIN mailbox m ON m . address_id = a . id , domaines d WHERE a . id = $mail_id AND d . id = a . domain_id ; " );
if ( ! $db -> next_record ()) return false ;
$details = $db -> Record ;
// if necessary, fill the typedata with data from hooks ...
if ( $details [ " type " ]) {
$result = $hooks -> invoke ( " mail_get_details " , array ( $mail_id )); // Will fill typedata if necessary
$details [ " typedata " ] = implode ( " <br /> " , $result );
}
return $details ;
2006-04-26 12:28:53 +00:00
}
2012-08-23 18:46:47 +00:00
/* ############################################################ */
/* ############################################################ */
/* After that line, to be deleted / checked & co. */
/* ############################################################ */
/* ############################################################ */
/* ----------------------------------------------------------------- */
/** function used to list the first letter used in the list of the emails hosted in a specific domain
* @ param $domain_id integer the domain id .
* @ result an array of each letter used in mail hosted under the domain .
*/
function enum_doms_mails_letters ( $domain_id ) {
global $err , $cuid , $db ;
$err -> log ( " mail " , " enum_doms_mails_letters " );
$domain_id = intval ( $domain_id );
$db -> query ( " select distinct left(ad.address,1) as letter from address ad,where ad.domain_id = $domain_id ; " );
$res = array ();
while ( $db -> next_record ()) {
$res [] = $db -> f ( " letter " );
2006-04-26 12:28:53 +00:00
}
2012-08-23 18:46:47 +00:00
return $res ;
2006-04-26 12:28:53 +00:00
}
2012-08-23 18:46:47 +00:00
/* FIXME: check who is using that function and delete it when unused */
function cancreate ( $dom_id , $email ){
return true ;
}
/* FIXME: check who is using that function and delete it when unused */
2012-04-06 10:10:36 +00:00
function form ( $mail_id ) {
2006-04-26 12:28:53 +00:00
}
2009-09-08 05:29:38 +00:00
2012-08-23 18:46:47 +00:00
/* FIXME: check who is using that function and delete it when unused */
function hooks_mail_cancreate ( $dom_id , $domain , $mail_arg ) {
2012-04-06 10:10:36 +00:00
global $db , $err ;
2012-08-23 18:46:47 +00:00
return true ;
2006-04-26 12:28:53 +00:00
}
2012-04-06 10:10:36 +00:00
/**
* @ param : mail_id
* fonction used to invoque the " hooks " corresponding to each mail relative classes
* the two foreach are used to format the array the way we want .
*/
function list_properties ( $mail_id ) {
global $err , $hooks ;
$err -> log ( " mail " , " list_properties " );
$prop = $hooks -> invoke ( " hooks_mail_properties_list " , array ( $mail_id ));
$final = Array ();
/* Ici on :
- trie / fait du ménage
- prend en premier les properties non avancées
- prend en second les properties avancées ( donc en bas )
- on pense a avoir un trie par label , histoire d 'avoir une cohérence d' affichage
*/
$f_simple = Array ();
$f_adv = Array ();
foreach ( $prop as $k => $v ) {
if ( empty ( $v ) ) continue ; // on continue si le tableau était vide
if ( isset ( $v [ 'label' ] ) ) { // si c'est directement le tableau qu'on souhaite
if ( isset ( $v [ 'advanced' ]) && $v [ 'advanced' ]) {
$f_adv [] = $v ;
} else { // option simple
$f_simple [] = $v ;
}
} else {
foreach ( $v as $k2 => $v2 ) { // sinon on joue avec les sous-tableau
if ( isset ( $v2 [ 'advanced' ]) && $v2 [ 'advanced' ]) {
$f_adv [] = $v2 ;
} else { // option simple
$f_simple [] = $v2 ;
}
}
2006-04-26 12:28:53 +00:00
}
}
2012-04-06 10:10:36 +00:00
$v_simple = usort ( $f_simple , 'list_properties_order' );
$v_adv = usort ( $f_adv , 'list_properties_order' );
$final = array_merge ( $f_simple , $f_adv );
return $final ;
2006-04-26 12:28:53 +00:00
}
2012-04-06 10:10:36 +00:00
2006-04-26 12:28:53 +00:00
2012-04-06 10:10:36 +00:00
/**
* activate a mail address .
* @ param integer mail_id : unique mail identifier
*/
function enable ( $mail_id ){
global $db , $err ;
$err -> log ( " mail " , " enable " );
if ( ! $db -> query ( " UPDATE address SET enabled=1 where id= $mail_id ; " )) return false ;
2006-04-26 12:28:53 +00:00
}
2012-08-23 18:46:47 +00:00
2012-04-06 10:10:36 +00:00
/**
* disable a mail address .
* @ param integer mail_id : unique mail identifier
*/
function disable ( $mail_id ){
global $db , $err ;
$err -> log ( " mail " , " enable " );
if ( ! $db -> query ( " UPDATE address SET enabled=0 where id= $mail_id ; " )) return false ;
2006-04-26 12:28:53 +00:00
}
2012-08-23 18:46:47 +00:00
2012-04-06 10:10:36 +00:00
/**
* setpasswd a mail address .
* @ param integer mail_id : unique mail identifier
*/
function setpasswd ( $mail_id , $pass , $passwd_type ){
global $db , $err , $admin ;
$err -> log ( " mail " , " setpasswd " );
if ( ! $admin -> checkPolicy ( " pop " , $mail_full , $pass )) return false ;
2012-08-23 18:46:47 +00:00
if ( ! $db -> query ( " UPDATE address SET password=' " . _md5cr ( $pass ) . " ' where id= $mail_id ; " )) return false ;
2006-04-26 12:28:53 +00:00
}
2009-09-08 05:29:38 +00:00
2012-04-06 10:10:36 +00:00
/**
* mail_delete a mail address .
* @ param integer mail_id : unique mail identifier
TODO : mail del
*/
function mail_delete ( $mail_id ){
global $db , $err , $admin ;
$err -> log ( " mail " , " mail_delete " );
// $db->query("
/* supprimer de la table address
supprimer la mailbox si il yen a une .
supprimer alias et redirection .
supprimer les alias associé si il ne sont relié a aucunes autre addresses .
*/
2009-09-08 05:29:38 +00:00
2010-03-04 16:16:13 +00:00
}
2012-05-21 15:58:20 +00:00
/**
* Export the mail information of an account
* @ return : str , chaine de caractere containing every usefull mail informations .
*
*/
function alternc_export_conf () {
global $db , $err , $mail_localbox ;
$err -> log ( " mail " , " export " );
$domain = $this -> enum_domains ();
$str = " <mail> \n " ;
$onepop = false ;
foreach ( $domain as $d ) {
$str .= " <domain> \n <name> " . xml_entities ( $d [ " domaine " ]) . " </name> \n " ;
$s = $this -> enum_domain_mails ( $d [ " id " ]);
if ( count ( $s )) {
while ( list ( $key , $val ) = each ( $s )){
$test = $this -> mail_get_details ( $val [ 'id' ]);
$str .= " <address> \n " ;
$str .= " <name> " . xml_entities ( $val [ " address " ]) . " </name> \n " ;
$str .= " <enabled> " . xml_entities ( $val [ " enabled " ]) . " </enabled> \n " ;
if ( is_array ( $test [ " is_local " ])){
$str .= " <islocal>oui</islocal> \n " ;
$str .= " <path> " . $test [ " is_local " ][ " path " ] . " </path> \n " ;
$str .= " <quota> " . $test [ " is_local " ][ " quota " ] . " </quota> \n " ;
} else {
$str .= " <islocal>non</islocal> \n " ;
}
if ( ! empty ( $test [ " recipients " ])){
foreach ( $test [ " recipients " ] as $recip ){
$str .= " <recipients> " . $recip . " <recipients> \n " ;
}
}
if ( ! empty ( $test [ " alias " ])){
foreach ( $test [ " alias " ] as $alias ){
$str .= " <alias> " . $alias . " <alias> \n " ;
}
}
$str .= " </address> \n " ;
}
}
$str .= " </domain> \n " ;
}
$str .= " </mail> \n " ;
return $str ;
}
2006-04-26 12:28:53 +00:00
} /* Class m_mail */
2012-04-06 10:10:36 +00:00
2006-04-26 12:28:53 +00:00
?>