modules/systemuser/include/useraccounts.php
2903b74d
 <?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.
 */
2903b74d
 
 require_once("inc/debug.php");
 
 
 
 function customer_may_have_useraccounts()
 {
2626dd47
     $customerno = (int) $_SESSION['customerinfo']['customerno'];
     $result = db_query("SELECT COUNT(*) FROM system.useraccounts WHERE kunde=?", array($customerno));
     return ($result->rowCount() > 0);
2903b74d
 }
 
2626dd47
 function customer_useraccount($uid)
 {
     $args = array(":uid" => $uid, ":customerno" => $_SESSION['customerinfo']['customerno']);
     $result = db_query("SELECT 1 FROM system.useraccounts WHERE kunde=:customerno AND uid=:uid AND kundenaccount=1", $args);
     return $result->rowCount() > 0;
51a82357
 }
2903b74d
 
8c86a8ce
 function primary_useraccount()
 {
2626dd47
     if (! ($_SESSION['role'] & ROLE_SYSTEMUSER)) {
         return null;
     }
     $customerno = (int) $_SESSION['customerinfo']['customerno'];
     $result = db_query("SELECT MIN(uid) AS uid FROM system.useraccounts WHERE kunde=?", array($customerno));
     $uid = $result->fetch(PDO::FETCH_OBJ)->uid;
     DEBUG("primary useraccount: {$uid}");
     return $uid;
8c86a8ce
 }
 
 
92f133ee
 function available_shells()
 {
2626dd47
     $result = db_query("SELECT path, name FROM system.shells WHERE usable=?", array(1));
     $ret = array();
     while ($s = $result->fetch()) {
         $ret[$s['path']] = $s['name'];
     }
     DEBUG($ret);
     return $ret;
92f133ee
 }
 
2903b74d
 
 function list_useraccounts()
 {
2626dd47
     $customerno = (int) $_SESSION['customerinfo']['customerno'];
     $result = db_query("SELECT uid,username,name,erstellungsdatum,quota,shell FROM system.useraccounts WHERE kunde=?", array($customerno));
     $ret = array();
     while ($item = $result->fetch()) {
         array_push($ret, $item);
     }
     #DEBUG($ret);
     return $ret;
2903b74d
 }
 
 
09edb607
 function get_account_details($uid, $customerno=0)
2903b74d
 {
2626dd47
     $uid = (int) $uid;
     $customerno = (int) $customerno;
     if ($customerno == 0) {
         $customerno = $_SESSION['customerinfo']['customerno'];
     }
     $args = array(":uid" => $uid, ":customerno" => $customerno);
6f376cd9
     $result = db_query("SELECT uid,username,name,shell,server,quota,erstellungsdatum,passwordlogin FROM system.useraccounts WHERE kunde=:customerno AND uid=:uid", $args);
2626dd47
     if ($result->rowCount() == 0) {
         system_failure("Cannot find the requestes useraccount (for this customer).");
     }
     return $result->fetch();
2903b74d
 }
 
92f133ee
 function get_used_quota($uid)
 {
2626dd47
     $uid = (int) $uid;
     $result = db_query("SELECT s.hostname AS server, systemquota, systemquota_used, mailquota, mailquota_used FROM system.v_quota AS q LEFT JOIN system.servers AS s ON (s.id=q.server) WHERE uid=?", array($uid));
     $ret = array();
     while ($line = $result->fetch()) {
         $ret[] = $line;
     }
     #DEBUG($ret);
     return $ret;
92f133ee
 }
2903b74d
 
 
92f133ee
 function set_account_details($account)
2903b74d
 {
2626dd47
     $customerno = null;
     if ($_SESSION['role'] & ROLE_CUSTOMER) {
         $customerno = (int) $_SESSION['customerinfo']['customerno'];
     } else {
         $customerno = (int) $_SESSION['userinfo']['customerno'];
     }
 
     if ($account['name'] == '') {
         $account['name'] = null;
     }
c42127d3
     $args = array(":fullname" => filter_input_oneline($account['name']),
                 ":shell" => filter_input_oneline($account['shell']),
f4a55204
                 ":quota" => $account['quota'],
                 ":uid" => $account['uid'],
6f376cd9
                 ":customerno" => $customerno,
                 ":passwordlogin" => $account['passwordlogin']);
2903b74d
 
6f376cd9
     db_query("UPDATE system.useraccounts SET name=:fullname, quota=:quota, shell=:shell, passwordlogin=:passwordlogin WHERE kunde=:customerno AND uid=:uid", $args);
2626dd47
     logger(LOG_INFO, "modules/systemuser/include/useraccounts", "systemuser", "updated details for uid {$args[":uid"]}");
2903b74d
 }
 
92f133ee
 function get_customer_quota()
 {
2626dd47
     $cid = (int) $_SESSION['customerinfo']['customerno'];
     $result = db_query("SELECT SUM(u.quota) AS assigned, cq.quota AS max FROM system.customerquota AS cq INNER JOIN system.useraccounts AS u ON (u.kunde=cq.cid) WHERE cq.cid=?", array($cid));
     $ret = $result->fetch();
     DEBUG($ret);
     return $ret;
92f133ee
 }