modules/subusers/include/subuser.php
4c7e58dd
 <?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.
 
 You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see 
 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.
 */
 
cd6247f7
 require_role(ROLE_SYSTEMUSER);
4c7e58dd
 require_once("inc/base.php");
 require_once("inc/security.php");
 require_once("inc/debug.php");
 
 
 function list_subusers()
 {
   $uid = (int) $_SESSION['userinfo']['uid'];
a53636c5
   $result = db_query("SELECT id, username, modules FROM system.subusers WHERE uid=?", array($uid));
4c7e58dd
   $subusers = array();
f1f231f5
   while ($item = $result->fetch())
4c7e58dd
   {
     $item['modules'] = explode(',', $item['modules']);
     $subusers[] = $item;
   }
   DEBUG($subusers);
   return $subusers;
 }
 
 
 function load_subuser($id) {
a53636c5
   $args = array(":id" => $id, ":uid" => $_SESSION['userinfo']['uid']);
4c7e58dd
   
a53636c5
   $result = db_query("SELECT id, username, modules FROM system.subusers WHERE uid=:uid AND id=:id", $args);
f1f231f5
   $item = $result->fetch();
4c7e58dd
   $item['modules'] = explode(',', $item['modules']);
   return $item;
 }
 
 
 function available_modules()
 {
   $modules = array();
e7798d59
   $allmodules = get_modules_info();
 
   // Das su-Modul ist hierfuer unwichtig
   unset($allmodules['su']);
 
   foreach ($allmodules as $modname => $modinfo)
4c7e58dd
   {
e7798d59
     if (isset($modinfo['permission']))
       $modules[$modname] = $modinfo['permission'];
4c7e58dd
   }
   return $modules;
 }
 
 function delete_subuser($id) {
a53636c5
   $args = array(":id" => $id, ":uid" => $_SESSION['userinfo']['uid']);
4c7e58dd
   
a53636c5
   db_query("DELETE FROM system.subusers WHERE id=:id AND uid=:uid", $args);
4c7e58dd
 }
 
 function empty_subuser()
 {
a53636c5
   $subuser = array("id" => NULL, 
                    "username" => $_SESSION['userinfo']['username'].'_', 
                    "modules" => array('index'));
4c7e58dd
   return $subuser;
 }
 
 function new_subuser($username, $requested_modules, $password) 
 {
a53636c5
   $username = filter_input_username($username);
4c7e58dd
   if (strpos($username, $_SESSION['userinfo']['username']) !== 0) {
     // Username nicht enthalten (FALSE) oder nicht am Anfang (>0)
     system_failure("Ungültiger Benutzername!");
   }
 
   if (!is_array($requested_modules)) {
     system_failure("Module nicht als array erhalten!");
   }
   DEBUG($requested_modules);
   $allmods = available_modules();
   $modules = array();
   foreach ($requested_modules as $mod) {
     if (isset($allmods[$mod])) {
       $modules[] = $mod;
     }
   }
   DEBUG($modules);
   if (count($modules) == 0) {
     system_failure("Es sind (nach der Filterung) keine Module mehr übrig!");
   }
   
   $result = strong_password($password);
   if ($result !== true) {
632f5398
     system_failure("Unsicheres Passwort: ".$result);
4c7e58dd
   }
 
a53636c5
   $args = array(":uid" => $_SESSION['userinfo']['uid'],
                 ":username" => $username,
                 ":password" => hash("sha256", $password),
                 ":modules" => implode(',', $modules));
 
   db_query("INSERT INTO system.subusers (uid, username, password, modules) VALUES (:uid, :username, :password, :modules)", $args);
4c7e58dd
 }
 
 
 function edit_subuser($id, $username, $requested_modules, $password) 
 {
   $uid = (int) $_SESSION['userinfo']['uid'];
 
   $id = (int) $id;
   $my_subusers = list_subusers();
   $valid = false;
   foreach ($my_subusers as $x) {
     if ($x['id'] == $id) {
       $valid = true;
     }
   }
   if (!$valid) {
     system_failure("Kann diesen Account nicht finden!");
   }
 
a53636c5
   $username = filter_input_username($username);
4c7e58dd
   if (strpos($username, $_SESSION['userinfo']['username']) !== 0) {
     // Username nicht enthalten (FALSE) oder nicht am Anfang (>0)
     system_failure("Ungültiger Benutzername!");
   }
 
 
   if (!is_array($requested_modules)) {
     system_failure("Module nicht als array erhalten!");
   }
   $allmods = available_modules();
   $modules = array();
   foreach ($requested_modules as $mod) {
     if (isset($allmods[$mod])) {
       $modules[] = $mod;
     }
   }
   if (count($modules) == 0) {
     system_failure("Es sind (nach der Filterung) keine Module mehr übrig!");
   }
   
a53636c5
   $args = array(":uid" => $_SESSION['userinfo']['uid'],
                 ":id" => $id,
                 ":username" => $username,
                 ":modules" => implode(',', $modules));
 
4c7e58dd
   $pwchange = '';
   if ($password) {
     $result = strong_password($password);
     if ($result !== true) {
632f5398
       system_failure("Unsicheres Passwort: ".$result);
4c7e58dd
     }
a53636c5
     $args[':password'] = hash("sha256", $password);
     $pwchange = ", password=:password";
4c7e58dd
   }
 
 
a53636c5
   db_query("UPDATE system.subusers SET username=:username, modules=:modules{$pwchange} WHERE id=:id AND uid=:uid", $args);
4c7e58dd
 }