modules/email/include/mailaccounts.php
defbfa55
 <?php
c208bd90
 /*
 This file belongs to the Webinterface of schokokeks.org Hosting
 
cf54502a
 Written 2008-2018 by schokokeks.org Hosting, namely
c208bd90
   Bernd Wurst <bernd@schokokeks.org>
   Hanno Böck <hanno@schokokeks.org>
 
 To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
 
2626dd47
 You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see
c208bd90
 http://creativecommons.org/publicdomain/zero/1.0/
 
 Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
 */
defbfa55
 
 require_once('inc/debug.php');
fb92f399
 require_once('inc/base.php');
af8c8976
 require_once('inc/security.php');
defbfa55
 
c0d71b32
 require_once('class/domain.php');
 
3000accf
 require_once('common.php');
 
defbfa55
 function mailaccounts($uid)
 {
2626dd47
     $uid = (int) $uid;
     $result = db_query("SELECT m.id,concat_ws('@',`m`.`local`,if(isnull(`m`.`domain`),:masterdomain,`d`.`domainname`)) AS `account`, `m`.`password` AS `cryptpass`,`m`.`maildir` AS `maildir`,aktiv from (`mail`.`mailaccounts` `m` left join `mail`.`v_domains` `d` on((`d`.`id` = `m`.`domain`))) WHERE m.uid=:uid ORDER BY if(isnull(`m`.`domain`),:masterdomain,`d`.`domainname`), local", array(":masterdomain" => config("masterdomain"), ":uid" => $uid));
     DEBUG("Found ".@$result->rowCount()." rows!");
     $accounts = array();
     if (@$result->rowCount() > 0) {
         while ($acc = @$result->fetch(PDO::FETCH_OBJ)) {
             array_push($accounts, array('id'=> $acc->id, 'account' => $acc->account, 'mailbox' => $acc->maildir, 'cryptpass' => $acc->cryptpass, 'enabled' => ($acc->aktiv == 1)));
         }
     }
     return $accounts;
defbfa55
 }
 
 function get_mailaccount($id)
 {
2626dd47
     $id = (int) $id;
     $uid = (int) $_SESSION['userinfo']['uid'];
     $result = db_query("SELECT concat_ws('@',`m`.`local`,if(isnull(`m`.`domain`),:masterdomain,`d`.`domainname`)) AS `account`, `m`.`password` AS `cryptpass`,`m`.`maildir` AS `maildir`,aktiv from (`mail`.`mailaccounts` `m` left join `mail`.`v_domains` `d` on((`d`.`id` = `m`.`domain`))) WHERE m.id=:mid AND m.uid=:uid", array(":masterdomain" => config("masterdomain"), ":uid" => $uid, ":mid" => $id));
     DEBUG("Found ".$result->rowCount()." rows!");
     if ($result->rowCount() != 1) {
         system_failure('Dieser Mailaccount existiert nicht oder gehört Ihnen nicht');
     }
     $acc = $result->fetch(PDO::FETCH_OBJ);
     $ret = array('account' => $acc->account, 'mailbox' => $acc->maildir,  'enabled' => ($acc->aktiv == 1));
     DEBUG(print_r($ret, true));
     return $ret;
defbfa55
 }
 
 function change_mailaccount($id, $arr)
 {
2626dd47
     $id = (int) $id;
     $uid = (int) $_SESSION['userinfo']['uid'];
     $conditions = array();
     $values = array(":id" => $id, ":uid" => $uid);
 
     if (isset($arr['account'])) {
         list($local, $domain) = explode('@', $arr['account'], 2);
         if ($domain == config('masterdomain')) {
             $values[':domain'] = null;
         } else {
             $domain = new Domain((string) $domain);
             if ($domain->id == null) {
                 $values[":domain"] = null;
             } else {
                 $domain->ensure_userdomain();
                 $values[":domain"] = $domain->id;
             }
         }
         $values[":local"] = $local;
         $conditions[] = "local=:local";
         $conditions[] = "domain=:domain";
     }
     if (isset($arr['mailbox'])) {
         array_push($conditions, "`maildir`=:maildir");
         if ($arr['mailbox'] == '') {
             $values[":maildir"] = null;
         } else {
             $values[":maildir"] = $arr['mailbox'];
         }
     }
 
     if (isset($arr['password'])) {
         $encpw = encrypt_mail_password($arr['password']);
         array_push($conditions, "`password`=:password");
         $values[":password"] = $encpw;
     }
 
     if (isset($arr['enabled'])) {
         array_push($conditions, "`aktiv`=:aktiv");
         $values[":aktiv"] = ($arr['enabled'] == 'Y' ? 1 : 0);
     }
defbfa55
 
 
2626dd47
     db_query("UPDATE mail.mailaccounts SET ".implode(",", $conditions)." WHERE id=:id AND uid=:uid", $values);
     logger(LOG_INFO, "modules/imap/include/mailaccounts", "imap", "updated account »{$id}«");
defbfa55
 }
 
 function create_mailaccount($arr)
 {
2626dd47
     $values = array();
defbfa55
 
2626dd47
     if (($arr['account']) == '') {
         system_failure('empty account name!');
     }
defbfa55
 
2626dd47
     $values[':uid'] = (int) $_SESSION['userinfo']['uid'];
defbfa55
 
2626dd47
     list($local, $domain) = explode('@', $arr['account'], 2);
     if ($domain == config('masterdomain')) {
         $values[':domain'] = null;
     } else {
         $domain = new Domain((string) $domain);
         if ($domain->id == null) {
             $values[':domain'] = null;
         } else {
             $domain->ensure_userdomain();
             $values[':domain'] = $domain->id;
         }
9075c60d
     }
c0d71b32
 
2626dd47
     $values[':local'] = $local;
defbfa55
 
2626dd47
     if (isset($arr['mailbox'])) {
         if ($arr['mailbox'] == '') {
             $values[':maildir'] = null;
         } else {
             $values[':maildir'] = $arr['mailbox'];
         }
     }
defbfa55
 
 
2626dd47
     if (isset($arr['password'])) {
         $values[':password'] = encrypt_mail_password($arr['password']);
     }
defbfa55
 
2626dd47
     if (isset($arr['enabled'])) {
         $values[':aktiv'] = ($arr['enabled'] == 'Y' ? 1 : 0);
     }
defbfa55
 
 
2626dd47
     $fields = array_map(function ($k) {
         return substr($k, 1);
     }, array_keys($values));
     db_query("INSERT INTO mail.mailaccounts (".implode(',', $fields).") VALUES (".implode(",", array_keys($values)).")", $values);
     logger(LOG_INFO, "modules/imap/include/mailaccounts", "imap", "created account »{$arr['account']}«");
defbfa55
 }
 
9086c9ad
 
ce0caaf4
 function get_mailaccount_id($accountname)
 {
2626dd47
     list($local, $domain) = explode('@', $accountname, 2);
ce0caaf4
 
2626dd47
     $args = array(":local" => $local,
c797217e
                 ":domain" => $domain);
ce0caaf4
 
2626dd47
     $result = db_query("SELECT acc.id FROM mail.mailaccounts AS acc LEFT JOIN mail.v_domains AS dom ON (dom.id=acc.domain) WHERE local=:local AND dom.domainname=:domain", $args);
     if (($result->rowCount() == 0) && ($domain == config('masterdomain'))) {
         unset($args[':domain']);
         $result = db_query("SELECT acc.id FROM mail.mailaccounts AS acc WHERE local=:local AND acc.domain IS NULL", $args);
     }
     if ($result->rowCount() != 1) {
         system_failure('account nicht eindeutig');
     }
     $acc = $result->fetch();
     return $acc['id'];
ce0caaf4
 }
9086c9ad
 
defbfa55
 
 function delete_mailaccount($id)
 {
2626dd47
     $id = (int) $id;
     db_query("DELETE FROM mail.mailaccounts WHERE id=?", array($id));
     logger(LOG_INFO, "modules/imap/include/mailaccounts", "imap", "deleted account »{$id}«");
defbfa55
 }
 
 
 function check_valid($acc)
 {
2626dd47
     $user = $_SESSION['userinfo'];
     DEBUG("Account-data: ".print_r($acc, true));
     DEBUG("User-data: ".print_r($user, true));
     if ($acc['mailbox'] != '') {
         if (substr($acc['mailbox'], 0, strlen($user['homedir'])+1) != $user['homedir'].'/') {
             return "Die Mailbox muss innerhalb des Home-Verzeichnisses liegen. Sie haben »".$acc['mailbox']."« als Mailbox angegeben, Ihr Home-Verzeichnis ist »".$user['homedir']."/«.";
         }
         if (! check_path($acc['mailbox'])) {
             return "Sie verwenden ungültige Zeichen in Ihrem Mailbox-Pfad.";
         }
     }
 
     if ($acc['account'] == '' || strpos($acc['account'], '@') == 0) {
         return "Es wurde kein Benutzername angegeben!";
     }
     if (strpos($acc['account'], '@') === false) {
         return "Es wurde kein Domain-Teil im Account-Name angegeben. Account-Namen müssen einen Domain-Teil enthalten. Im Zweifel versuchen Sie »@".config('masterdomain')."«.";
     }
 
     list($local, $domain) = explode('@', $acc['account'], 2);
     verify_input_username($local);
     $tmpdomains = get_domain_list($user['customerno'], $user['uid']);
     $domains = array();
     foreach ($tmpdomains as $dom) {
         $domains[] = $dom->fqdn;
     }
 
     if (array_search($domain, $domains) === false) {
         if ($domain == config('masterdomain')) {
             if (substr($local, 0, strlen($user['username'])) != $user['username'] || ($acc['account'][strlen($user['username'])] != '-' && $acc['account'][strlen($user['username'])] != '@')) {
                 return "Sie haben »@".config('masterdomain')."« als Domain-Teil angegeben, aber der Benutzer-Teil beginnt nicht mit Ihrem Benutzername!";
             }
         } else {
             return "Der angegebene Domain-Teil (»".htmlentities($domain, ENT_QUOTES, "UTF-8")."«) ist nicht für Ihren Account eingetragen. Sollte dies ein Fehler sein, wenden sie sich bitte an einen Administrator!";
         }
     }
 
     return '';
defbfa55
 }
 
 
bff5f442
 function imap_on_vmail_domain()
 {
2626dd47
     $uid = (int) $_SESSION['userinfo']['uid'];
     $result = db_query("SELECT m.id FROM mail.mailaccounts AS m INNER JOIN mail.virtual_mail_domains AS vd USING (domain) WHERE vd.hostname IS NULL AND m.uid=?", array($uid));
     if ($result->rowCount() > 0) {
         return true;
     }
     return false;
bff5f442
 }
 
 function user_has_only_vmail_domains()
 {
2626dd47
     $uid = (int) $_SESSION['userinfo']['uid'];
     $result = db_query("SELECT id FROM mail.v_vmail_domains WHERE useraccount=?", array($uid));
     // User hat keine VMail-Domains
     if ($result->rowCount() == 0) {
         return false;
     }
     $result = db_query("SELECT d.id FROM mail.v_domains AS d LEFT JOIN mail.v_vmail_domains AS vd USING (domainname) WHERE vd.id IS NULL AND d.user=?", array($uid));
     // User hat keine Domains die nicht vmail-Domains sind
     if ($result->rowCount() == 0) {
         return true;
     }
d77e71db
     return false;
bff5f442
 }