59 var
$forbiddenchars=array(
'"',
"'",
'\\',
'/',
'?',
'!',
'*',
'$',
'|',
'#',
'+');
84 $this->srv_submission =
variable_get(
'mail_human_submission',
'%%FQDN%%',
'Human name for mail server (submission protocol)', array(array(
'desc'=>
'Name',
'type'=>
'string')));
85 $this->srv_smtp =
variable_get(
'mail_human_smtp',
'%%FQDN%%',
'Human name for mail server (SMTP protocol)', array(array(
'desc'=>
'Name',
'type'=>
'string')));
86 $this->srv_smtps =
variable_get(
'mail_human_smtps',
'%%FQDN%%',
'Human name for mail server (SMTPS protocol)', array(array(
'desc'=>
'Name',
'type'=>
'string')));
87 $this->srv_imap =
variable_get(
'mail_human_imap',
'%%FQDN%%',
'Human name for IMAP mail server', array(array(
'desc'=>
'Name',
'type'=>
'string')));
88 $this->srv_imaps =
variable_get(
'mail_human_imaps',
'%%FQDN%%',
'Human name for IMAPS mail server', array(array(
'desc'=>
'Name',
'type'=>
'string')));
89 $this->srv_pop3 =
variable_get(
'mail_human_pop3',
'%%FQDN%%',
'Human name for POP3 mail server', array(array(
'desc'=>
'Name',
'type'=>
'string')));
90 $this->srv_pop3s =
variable_get(
'mail_human_pop3s',
'%%FQDN%%',
'Human name for POP3s mail server', array(array(
'desc'=>
'Name',
'type'=>
'string')));
95 'title' => _(
"Email Addresses"),
96 'ico' =>
'images/mail.png',
105 'txt' => htmlentities($d[
"domaine"]).
' '.htmlentities(
"(".$d[
"nb_mail"].
")"),
106 'url' =>
"mail_list.php?domain_id=".urlencode($d[
'id']),
115 if (empty($this->cache_domain_mail_size)) {
116 $db->query(
"SELECT SUBSTRING_INDEX(user,'@', -1) as domain, SUM(quota_dovecot) AS sum FROM dovecot_view group by domain ;");
117 while ($db->next_record() ) {
118 $dd = $db->f(
'domain');
119 $this->cache_domain_mail_size[ $dd ] = $db->f(
'sum');
122 if (
isset( $this->cache_domain_mail_size[
$domain]) )
return $this->cache_domain_mail_size[
$domain];
131 'domain' =>$dom->get_domain_byid($domain_id),
136 $db->query(
"select r.recipients as dst, a.id mail_id from address a, recipient r where a.domain_id = $domain_id and r.address_id = a.id and a.address='';");
137 if ($db->next_record()) {
138 $rr[
'target'] = $db->f(
'dst');
139 $rr[
'mail_id'] = $db->f(
'mail_id');
143 if (empty($rr[
'target'])) {
145 } elseif (substr($rr[
'target'],0,1)==
'@') {
146 $rr[
'type']=
'domain';
156 if (empty(
$catch[
'mail_id']))
return false;
157 return $this->
delete(
$catch[
'mail_id']);
162 $target=rtrim($target);
163 if ( substr_count($target,
'@') == 0 ) {
164 $target =
'@'.$target;
167 if ( substr($target,0,1) ==
'@' ) {
170 if (!filter_var($target,FILTER_VALIDATE_EMAIL)) {
171 $err->raise(
"mail",_(
"The email you entered is syntaxically incorrect"));
176 return $this->
create_alias($domain_id,
'', $target,
"catchall",
true);
189 $err->log(
"mail",
"getquota");
190 $q=Array(
"name"=>
"mail",
"description"=>_(
"Email addresses"),
"used"=>0);
191 $db->query(
"SELECT COUNT(*) AS cnt FROM address a, domaines d WHERE a.domain_id=d.id AND d.compte=$cuid AND a.type='';");
192 if ($db->next_record()) {
193 $q[
'used']=$db->f(
"cnt");
204 return array(
"pop"=>_(
"Email account password"));
214 $err->log(
"mail",
"enum_domains");
220 IFNULL( COUNT(a.id), 0) as nb_mail
222 domaines d LEFT JOIN address a ON (d.id=a.domain_id AND a.type='')
233 while($db->next_record()){
236 return $this->enum_domains;
248 $err->log(
"mail",
"available");
249 list($login,
$domain)=explode(
"@",$mail,2);
251 if (!($dom_id=$dom->get_domain_byname(
$domain))) {
255 if (!filter_var($mail,FILTER_VALIDATE_EMAIL)) {
256 $err->raise(
"mail",_(
"The email you entered is syntaxically incorrect"));
260 $db->query(
"SELECT a.id FROM address a WHERE a.domain_id=".$dom_id.
" AND a.address='".addslashes($login).
"';");
261 if ($db->next_record()) {
277 function enum_domain_mails($dom_id = null, $search=
"", $offset=0, $count=30, $show_systemmails=
false){
279 $err->log(
"mail",
"enum_domains_mail");
281 $search=trim($search);
283 $where=
"a.domain_id=$dom_id";
284 if ($search) $where.=
" AND (a.address LIKE '%".addslashes($search).
"%' OR r.recipients LIKE '%".addslashes($search).
"%')";
285 if (!$show_systemmails) $where.=
" AND type='' ";
286 $db->query(
"SELECT count(a.id) AS total FROM address a LEFT JOIN recipient r ON r.address_id=a.id WHERE $where;");
288 $this->total=$db->f(
"total");
289 if ($count!=-1) $limit=
"LIMIT $offset,$count";
else $limit=
"";
290 $db->query(
"SELECT a.id, a.address, a.password, a.`enabled`, a.mail_action, d.domaine AS domain, m.quota, m.quota*1024*1024 AS quotabytes, m.bytes AS used, NOT ISNULL(m.id) AS islocal, a.type, r.recipients, m.lastlogin, a.domain_id
291 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
292 WHERE $where AND d.id=a.domain_id $limit ;");
293 if (! $db->next_record()) {
294 $err->raise(
"mail",_(
"No email found for this query"));
299 $details=$db->Record;
301 if ($details[
"type"]) {
302 $result=$hooks->invoke(
"hook_mail_get_details",array($details));
303 $details[
"typedata"]=implode(
"<br />",$result);
306 }
while ($db->next_record());
312 if ($detail[
'type']==
'catchall')
return _(sprintf(
"Special mail address for catch-all. <a href='mail_manage_catchall.php?domain_id=%s'>Click here to manage it.</a>",$detail[
'domain_id']));
329 function create($dom_id, $mail,$type=
"",$dontcheck=
false){
331 $err->log(
"mail",
"create",$mail);
334 if (!(
$domain=$dom->get_domain_byid($dom_id))) {
340 if (!filter_var($m,FILTER_VALIDATE_EMAIL) && !$dontcheck) {
341 $err->raise(
"mail",_(
"The email you entered is syntaxically incorrect"));
346 $cancreate=$hooks->invoke(
"hook_mail_cancreate",array($dom_id,$mail));
347 if (in_array(
false,$cancreate,
true)) {
352 if (!$quota->cancreate(
"mail")) {
353 $err->raise(
"mail",_(
"You cannot create email addresses: your quota is over"));
357 $db->query(
"SELECT * FROM address WHERE domain_id=".$dom_id.
" AND address='".addslashes($mail).
"';");
358 if ($db->next_record()) {
359 $err->raise(
"mail",_(
"This email address already exists"));
363 $db->query(
"INSERT INTO address (domain_id, address,type) VALUES ($dom_id, '".addslashes($mail).
"','$type');");
364 if (!($id=$db->lastid())) {
365 $err->raise(
"mail",_(
"An unexpected error occured when creating the email"));
379 $err->log(
"mail",
"get_details");
388 $db->query(
"SELECT a.id, a.address, a.password, a.enabled, d.domaine AS domain, m.path, m.quota, m.quota*1024*1024 AS quotabytes, m.bytes AS used, NOT ISNULL(m.id) AS islocal, a.type, r.recipients, m.lastlogin, a.mail_action, m.mail_action AS mailbox_action 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 a.id=".
$mail_id.
" AND d.id=a.domain_id;");
389 if (! $db->next_record())
return false;
390 $details=$db->Record;
392 if ($details[
"type"]) {
393 $result=$hooks->invoke(
"hook_mail_get_details",array(
$mail_id));
394 $details[
"typedata"]=implode(
"<br />",$result);
415 $db->query(
"SELECT concat(a.address,'@',d.domaine) AS email FROM address a, domaines d WHERE d.id=a.domain_id AND a.id=$mail_id AND d.compte=$cuid;");
416 if ($db->next_record()) {
417 return $this->isitmy_cache[
$mail_id]=$db->f(
"email");
419 $err->raise(
"mail",_(
"This email is not yours, you can't change anything on it"));
420 return $this->isitmy_cache[
$mail_id]=
false;
434 if (is_array(
$list)) {
435 foreach(
$list as $one) {
436 $this->
delete($one[
"id"]);
445 $db->query(
"select compte as uid from domaines d, address a where a.domain_id = d.id and a.id = $mail_id");
446 if ( !$db->next_record()) {
449 return $db->f(
'uid');
463 $err->log(
"mail",
"delete");
468 $err->raise(
"mail",_(
"The email you entered is syntaxically incorrect"));
480 $db->query(
"SELECT a.id, a.type, a.mail_action, m.mail_action AS mailbox_action, NOT ISNULL(m.id) AS islocal FROM address a LEFT JOIN mailbox m ON m.address_id=a.id WHERE a.id='$mail_id';");
481 if (!$db->next_record()) {
482 $err->raise(
"mail",_(
"The email %s does not exist, it can't be deleted"),$mail);
485 if ($db->f(
"mail_action")!=
"OK" || ($db->f(
"islocal") && $db->f(
"mailbox_action")!=
"OK")) {
486 $err->raise(
"mail",_(
"The email %s is already marked for deletion, it can't be deleted"),$mail);
491 if ($db->f(
"islocal")) {
493 $db->query(
"UPDATE address SET mail_action='DELETE', enabled=0 WHERE id='$mail_id';");
494 $db->query(
"UPDATE mailbox SET mail_action='DELETE' WHERE address_id='$mail_id';");
495 $err->raise(
"mail",_(
"The email %s has been marked for deletion"),$mail);
498 $db->query(
"DELETE FROM address WHERE id='$mail_id';");
499 $db->query(
"DELETE FROM mailbox WHERE address_id='$mail_id';");
500 $db->query(
"DELETE FROM recipient WHERE address_id='$mail_id';");
501 $err->raise(
"mail",_(
"The email %s has been successfully deleted"),$mail);
517 $err->log(
"mail",
"undelete");
522 $err->raise(
"mail",_(
"The email you entered is syntaxically incorrect"));
531 $db->query(
"SELECT a.id, a.type, a.mail_action, m.mail_action AS mailbox_action, NOT ISNULL(m.id) AS islocal FROM address a LEFT JOIN mailbox m ON m.address_id=a.id WHERE a.id='$mail_id';");
532 if (!$db->next_record()) {
533 $err->raise(
"mail",_(
"The email %s does not exist, it can't be undeleted"),$mail);
536 if ($db->f(
"type")!=
"") {
537 $err->raise(
"mail",_(
"The email %s is special, it can't be undeleted"),$mail);
540 if ($db->f(
"mailbox_action")!=
"DELETE" || $db->f(
"mail_action")!=
"DELETE") {
541 $err->raise(
"mail",_(
"Sorry, deletion of email %s is already in progress, or not marked for deletion, it can't be undeleted"),$mail);
546 if ($db->f(
"islocal")) {
548 $db->query(
"UPDATE address SET mail_action='OK', `enabled`=1 WHERE id='$mail_id';");
549 $db->query(
"UPDATE mailbox SET mail_action='OK' WHERE address_id='$mail_id';");
550 $err->raise(
"mail",_(
"The email %s has been undeleted"),$mail);
553 $err->raise(
"mail",_(
"-- Program Error -- The email %s can't be undeleted"),$mail);
567 $err->log(
"mail",
"setpasswd");
570 if (!$admin->checkPolicy(
"pop",$email,$pass))
return false;
571 if (!$db->query(
"UPDATE address SET password='"._md5cr($pass).
"' where id=$mail_id;"))
return false;
583 $err->log(
"mail",
"enable");
585 if (!$db->query(
"UPDATE address SET `enabled`=1 where id=$mail_id;"))
return false;
597 $err->log(
"mail",
"disable");
599 if (!$db->query(
"UPDATE address SET `enabled`=0 where id=$mail_id;"))
return false;
617 $delivery=mysql_real_escape_string($delivery);
618 $err->log(
"mail",
"set_details");
622 if ($me[
"islocal"] && !$islocal) {
624 $db->query(
"UPDATE mailbox SET mail_action='DELETE' WHERE address_id=".
$mail_id.
";");
626 if (!$me[
"islocal"] && $islocal) {
629 if($delivery==
"dovecot"){
630 $path=
ALTERNC_MAIL.
"/".substr($me[
"address"].
"_",0,1).
"/".$me[
"address"].
"_".$me[
"domain"];
632 foreach($this->forbiddenchars as $str) {
633 if (strpos($me[
"address"],$str)!==
false) {
634 $err->raise(
"mail",_(
"There is forbidden characters in your email address. You can't make it a POP/IMAP account, you can only use it as redirection to other emails"));
639 foreach($this->specialchars as $str) {
640 if (strpos($me[
"address"],$str)!==
false) {
645 $db->query(
"INSERT INTO mailbox SET address_id=$mail_id, delivery='$delivery', path='".addslashes($path).
"';");
647 if ($me[
"islocal"] && $islocal && $me[
"mailbox_action"]==
"DELETE") {
648 $db->query(
"UPDATE mailbox SET mail_action='OK' WHERE mail_action='DELETE' AND address_id=".
$mail_id.
";");
652 if ($quotamb!=0 && $quotamb<(intval($me[
"used"]/1024/1024)+1)) {
653 $quotamb=intval($me[
"used"]/1024/1024)+1;
654 $err->raise(
"mail",_(
"You set a quota smaller than the current mailbox size. Since it's not allowed, we set the quota to the current mailbox size"));
656 $db->query(
"UPDATE mailbox SET quota=".intval($quotamb).
" WHERE address_id=".
$mail_id.
";");
659 $r=explode(
"\n",$recipients);
663 if ($m && ( filter_var($m,FILTER_VALIDATE_EMAIL) || $dontcheck)
664 && $m!=($me[
"address"].
"@".$me[
"domain"])) {
668 $db->query(
"DELETE FROM recipient WHERE address_id=".
$mail_id.
";");
670 $db->query(
"INSERT INTO recipient SET address_id=".
$mail_id.
", recipients='".addslashes($red).
"';");
684 $err->log(
"mail",
"add_wrapper",
"creating $delivery $m address");
686 $mail_id=$mail->create($dom_id,$m,$delivery);
700 $err->log(
"mail",
"create_alias",
"creating $m alias for $alias type $type");
702 $mail_id=$mail->create($dom_id,$m,$type,$dontcheck);
716 $err->log(
"mail",
"del_wrapper");
726 global
$db,
$err,$mail_localbox;
727 $err->log(
"mail",
"export");
732 $str.=
" <domain>\n <name>".xml_entities($d[
"domain"]).
"</name>\n";
737 $str.=
" <address>\n";
738 $str.=
" <name>".xml_entities(
$val[
"address"]).
"</name>\n";
739 $str.=
" <enabled>".xml_entities(
$val[
"enabled"]).
"</enabled>\n";
740 if(is_array(
$val[
"islocal"])){
741 $str.=
" <islocal>1</islocal>\n";
742 $str.=
" <quota>".$val[
"quota"].
"</quota>\n";
743 $str.=
" <path>".$val[
"path"].
"</path>\n";
745 $str.=
" <islocal>0</islocal>\n";
747 if(!empty(
$val[
"recipients"])){
748 $r=explode(
"\n",
$val[
"recipients"]);
749 foreach(
$r as $recip){
750 $str.=
" <recipients>".$recip.
"<recipients>\n";
753 $str.=
" </address>\n";
756 $str.=
" </domain>\n";
770 $db->query(
"SELECT login,pass FROM mxaccount;");
772 while ($db->next_record()) {
775 if (!count(
$res))
return false;
788 $login=mysql_real_escape_string($login);
789 $pass=mysql_real_escape_string($pass);
790 $db->query(
"SELECT * FROM mxaccount WHERE login='$login' AND pass='$pass';");
791 if ($db->next_record()) {
803 $db->query(
"SELECT domaine FROM domaines WHERE gesmx=1 ORDER BY domaine");
806 while ($db->next_record()) {
807 $lst[]=$db->f(
"domaine");
808 $tt.=$db->f(
"domaine");
811 # Generate an integrity check
812 $obj=array(
'integrity'=>md5($tt),
'items'=>$lst);
816 return json_encode($obj);
819 foreach ($lst as $l) { echo $l.
"\n"; }
835 $login=mysql_real_escape_string($login);
836 $pass=mysql_real_escape_string($pass);
837 $db->query(
"SELECT * FROM mxaccount WHERE login='$login'");
838 if ($db->next_record()) {
839 $err->raise(
"mail",_(
"The slave MX account was not found"));
842 $db->query(
"INSERT INTO mxaccount (login,pass) VALUES ('$login','$pass')");
854 $login=mysql_real_escape_string($login);
855 $db->query(
"DELETE FROM mxaccount WHERE login='$login'");
869 $err->log(
"mail",
"hook_dom_add_slave_domain",$domain_id);
884 $err->log(
"mail",
"hook_dom_add_mx_domain",$domain_id);
886 $db->query(
"SELECT value FROM variable where name='mailname_bounce';");
887 if (!$db->next_record()) {
888 $err->raise(
"mail",_(
"The email %s does not exist, it can't be deleted"),$mail);
891 $mailname=$db->f(
"value");
893 return $this->
create_alias($domain_id,
'postmaster', $mem->user[
'login'].
'@'.$mailname );
906 "imap" => array(
"port" => 143,
"protocol" =>
"tcp",
"mandatory" => 1),
907 "imaps" => array(
"port" => 993,
"protocol" =>
"tcp",
"mandatory" => 1),
908 "pop" => array(
"port" => 110,
"protocol" =>
"tcp",
"mandatory" => 1),
909 "pops" => array(
"port" => 995,
"protocol" =>
"tcp",
"mandatory" => 1),
910 "smtp" => array(
"port" => 25,
"protocol" =>
"tcp",
"mandatory" => 1),
911 "sieve" => array(
"port" => 2000,
"protocol" =>
"tcp",
"mandatory" => 1),
912 "submission" => array(
"port" => 587,
"protocol" =>
"tcp",
"mandatory" => 0),