Till Brehm
2014-08-13 19b5e01da02aa2115ee802cf2e43a7f3e58f4eac
commit | author | age
532ae5 1 <?php
L 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 class auth {
32     var $client_limits = null;
33
34     public function get_user_id()
35     {
5308a8 36         global $app;
604c0c 37         return $app->functions->intval($_SESSION['s']['user']['userid']);
532ae5 38     }
7fe908 39
532ae5 40     public function is_admin() {
L 41         if($_SESSION['s']['user']['typ'] == 'admin') {
42             return true;
43         } else {
44             return false;
45         }
7fe908 46     }
19b5e0 47     
TB 48     public function is_superadmin() {
49         if($_SESSION['s']['user']['typ'] == 'admin' && $_SESSION['s']['user']['userid'] === 1) {
50             return true;
51         } else {
52             return false;
53         }
54     }
7fe908 55
532ae5 56     public function has_clients($userid) {
L 57         global $app, $conf;
7fe908 58
65ea2e 59         $userid = $app->functions->intval($userid);
532ae5 60         $client = $app->db->queryOneRecord("SELECT client.limit_client FROM sys_user, client WHERE sys_user.userid = $userid AND sys_user.client_id = client.client_id");
71c5c2 61         if($client['limit_client'] != 0) {
532ae5 62             return true;
L 63         } else {
64             return false;
65         }
66     }
7fe908 67
532ae5 68     //** This function adds a given group id to a given user.
7fe908 69     public function add_group_to_user($userid, $groupid) {
532ae5 70         global $app;
7fe908 71
65ea2e 72         $userid = $app->functions->intval($userid);
M 73         $groupid = $app->functions->intval($groupid);
7fe908 74
532ae5 75         if($userid > 0 && $groupid > 0) {
L 76             $user = $app->db->queryOneRecord("SELECT * FROM sys_user WHERE userid = $userid");
7fe908 77             $groups = explode(',', $user['groups']);
MC 78             if(!in_array($groupid, $groups)) $groups[] = $groupid;
79             $groups_string = implode(',', $groups);
532ae5 80             $sql = "UPDATE sys_user SET groups = '$groups_string' WHERE userid = $userid";
L 81             $app->db->query($sql);
82             return true;
83         } else {
84             return false;
85         }
86     }
87
88     //** This function returns given client limit as integer, -1 means no limit
89     public function get_client_limit($userid, $limitname)
90     {
91         global $app;
604c0c 92         
TB 93         $userid = $app->functions->intval($userid);
19b5e0 94         if(!preg_match('/^[a-zA-Z0-9\-\_]{1,64}$/',$limitname)) $app->error('Invalid limit name '.$limitname);
604c0c 95         
532ae5 96         // simple query cache
7fe908 97         if($this->client_limits===null)
532ae5 98             $this->client_limits = $app->db->queryOneRecord("SELECT client.* FROM sys_user, client WHERE sys_user.userid = $userid AND sys_user.client_id = client.client_id");
7fe908 99
532ae5 100         // isn't client -> no limit
L 101         if(!$this->client_limits)
102             return -1;
7fe908 103
532ae5 104         if(isset($this->client_limits['limit_'.$limitname])) {
L 105             return $this->client_limits['limit_'.$limitname];
7fe908 106         }
MC 107     }
108
532ae5 109     //** This function removes a given group id from a given user.
7fe908 110     public function remove_group_from_user($userid, $groupid) {
532ae5 111         global $app;
7fe908 112
65ea2e 113         $userid = $app->functions->intval($userid);
M 114         $groupid = $app->functions->intval($groupid);
7fe908 115
532ae5 116         if($userid > 0 && $groupid > 0) {
L 117             $user = $app->db->queryOneRecord("SELECT * FROM sys_user WHERE userid = $userid");
7fe908 118             $groups = explode(',', $user['groups']);
MC 119             $key = array_search($groupid, $groups);
532ae5 120             unset($groups[$key]);
7fe908 121             $groups_string = implode(',', $groups);
532ae5 122             $sql = "UPDATE sys_user SET groups = '$groups_string' WHERE userid = $userid";
L 123             $app->db->query($sql);
124             return true;
125         } else {
126             return false;
127         }
128     }
7fe908 129
532ae5 130     public function check_module_permissions($module) {
L 131         // Check if the current user has the permissions to access this module
19b5e0 132         $user_modules = explode(',',$_SESSION["s"]["user"]["modules"]);
TB 133         if(!in_array($module,$user_modules)) {
532ae5 134             // echo "LOGIN_REDIRECT:/index.php";
L 135             header("Location: /index.php");
136             exit;
137         }
138     }
7fe908 139
f9c7f3 140     public function get_random_password($length = 8) {
T 141         $base64_alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
142         $password = '';
143         for ($n=0;$n<$length;$n++) {
7fe908 144             $password.=$base64_alphabet[mt_rand(0, 63)];
f9c7f3 145         }
T 146         return $password;
147     }
7fe908 148
c614f1 149     public function crypt_password($cleartext_password) {
T 150         $salt="$1$";
151         $base64_alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
152         for ($n=0;$n<8;$n++) {
7fe908 153             $salt.=$base64_alphabet[mt_rand(0, 63)];
c614f1 154         }
T 155         $salt.="$";
7fe908 156         return crypt($cleartext_password, $salt);
c614f1 157     }
7fe908 158
532ae5 159 }
L 160
7fe908 161 ?>