Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
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() {
9edea9 49         if($_SESSION['s']['user']['typ'] == 'admin' && $_SESSION['s']['user']['userid'] == 1) {
19b5e0 50             return true;
TB 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);
cc7a82 60         $client = $app->db->queryOneRecord("SELECT client.limit_client FROM sys_user, client WHERE sys_user.userid = ? AND sys_user.client_id = client.client_id", $userid);
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) {
cc7a82 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);
cc7a82 80             $sql = "UPDATE sys_user SET groups = ? WHERE userid = ?";
MC 81             $app->db->query($sql, $groups_string, $userid);
532ae5 82             return true;
L 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)
cc7a82 98             $this->client_limits = $app->db->queryOneRecord("SELECT client.* FROM sys_user, client WHERE sys_user.userid = ? AND sys_user.client_id = client.client_id", $userid);
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) {
9d9833 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);
cc7a82 122             $sql = "UPDATE sys_user SET groups = ? WHERE userid = ?";
MC 123             $app->db->query($sql, $groups_string, $userid);
532ae5 124             return true;
L 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
a563d5 132         $module = trim(preg_replace('@\s+@', '', $module));
19b5e0 133         $user_modules = explode(',',$_SESSION["s"]["user"]["modules"]);
a563d5 134         if(strpos($module, ',') !== false){
MC 135             $can_use_module = false;
136             $tmp_modules = explode(',', $module);
137             if(is_array($tmp_modules) && !empty($tmp_modules)){
138                 foreach($tmp_modules as $tmp_module){
139                     if($tmp_module != ''){
140                         if(in_array($tmp_module,$user_modules)) {
141                             $can_use_module = true;
142                             break;
143                         }
144                     }
145                 }
146             }
147             if(!$can_use_module){
148                 // echo "LOGIN_REDIRECT:/index.php";
149                 header("Location: /index.php");
150                 exit;
151             }
152         } else {
153             if(!in_array($module,$user_modules)) {
154                 // echo "LOGIN_REDIRECT:/index.php";
155                 header("Location: /index.php");
156                 exit;
157             }
532ae5 158         }
L 159     }
9edea9 160     
TB 161     public function check_security_permissions($permission) {
162         
163         global $app;
164         
165         $app->uses('getconf');
166         $security_config = $app->getconf->get_security_config('permissions');
167
168         $security_check = false;
169         if($security_config[$permission] == 'yes') $security_check = true;
170         if($security_config[$permission] == 'superadmin' && $app->auth->is_superadmin()) $security_check = true;
171         if($security_check !== true) {
172             $app->error($app->lng('security_check1_txt').' '.$permission.' '.$app->lng('security_check2_txt'));
173         }
174         
175     }
7fe908 176
ffb04d 177     public function get_random_password($minLength = 8, $special = false) {
MC 178         if($minLength < 8) $minLength = 8;
179         $maxLength = $minLength + 5;
180         $length = mt_rand($minLength, $maxLength);
181         
182         $alphachars = "abcdefghijklmnopqrstuvwxyz";
183         $upperchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
184         $numchars = "1234567890";
185         $specialchars = "!@#_";
186         
187         $num_special = 0;
188         if($special == true) {
189             $num_special = intval(mt_rand(0, round($length / 4))) + 1;
f9c7f3 190         }
ffb04d 191         $numericlen = mt_rand(1, 2);
MC 192         $alphalen = $length - $num_special - $numericlen;
193         $upperlen = intval($alphalen / 2);
194         $alphalen = $alphalen - $upperlen;
195         $password = '';
196         
197         for($i = 0; $i < $alphalen; $i++) {
198             $password .= substr($alphachars, mt_rand(0, strlen($alphachars) - 1), 1);
199         }
200         
201         for($i = 0; $i < $upperlen; $i++) {
202             $password .= substr($upperchars, mt_rand(0, strlen($upperchars) - 1), 1);
203         }
204         
205         for($i = 0; $i < $num_special; $i++) {
206             $password .= substr($specialchars, mt_rand(0, strlen($specialchars) - 1), 1);
207         }
208         
209         for($i = 0; $i < $numericlen; $i++) {
210             $password .= substr($numchars, mt_rand(0, strlen($numchars) - 1), 1);
211         }
212         
213         return str_shuffle($password);
f9c7f3 214     }
7fe908 215
c614f1 216     public function crypt_password($cleartext_password) {
T 217         $salt="$1$";
218         $base64_alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
219         for ($n=0;$n<8;$n++) {
7fe908 220             $salt.=$base64_alphabet[mt_rand(0, 63)];
c614f1 221         }
T 222         $salt.="$";
7fe908 223         return crypt($cleartext_password, $salt);
c614f1 224     }
5af0cf 225     
TB 226     public function csrf_token_get($form_name) {
227         /* CSRF PROTECTION */
228         // generate csrf protection id and key
229         $_csrf_id = uniqid($form_name . '_'); // form id
230         $_csrf_key = sha1(uniqid(microtime(true), true)); // the key
231         if(!isset($_SESSION['_csrf'])) $_SESSION['_csrf'] = array();
232         if(!isset($_SESSION['_csrf_timeout'])) $_SESSION['_csrf_timeout'] = array();
233         $_SESSION['_csrf'][$_csrf_id] = $_csrf_key;
234         $_SESSION['_csrf_timeout'][$_csrf_id] = time() + 3600; // timeout hash in 1 hour
235         
236         return array('csrf_id' => $_csrf_id,'csrf_key' => $_csrf_key);
237     }
238     
239     public function csrf_token_check() {
240         global $app;
241         
242         if(isset($_POST) && is_array($_POST)) {
243             $_csrf_valid = false;
244             if(isset($_POST['_csrf_id']) && isset($_POST['_csrf_key'])) {
245                 $_csrf_id = trim($_POST['_csrf_id']);
246                 $_csrf_key = trim($_POST['_csrf_key']);
247                 if(isset($_SESSION['_csrf']) && isset($_SESSION['_csrf'][$_csrf_id]) && isset($_SESSION['_csrf_timeout']) && isset($_SESSION['_csrf_timeout'][$_csrf_id])) {
248                     if($_SESSION['_csrf'][$_csrf_id] === $_csrf_key && $_SESSION['_csrf_timeout'] >= time()) $_csrf_valid = true;
249                 }
250             }
251             if($_csrf_valid !== true) {
252                 $app->log('CSRF attempt blocked. Referer: ' . (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'unknown'), LOGLEVEL_WARN);
253                 $app->error($app->lng('err_csrf_attempt_blocked'));
254             }
255             $_SESSION['_csrf'][$_csrf_id] = null;
256             $_SESSION['_csrf_timeout'][$_csrf_id] = null;
257             unset($_SESSION['_csrf'][$_csrf_id]);
258             unset($_SESSION['_csrf_timeout'][$_csrf_id]);
259             
260             if(isset($_SESSION['_csrf_timeout']) && is_array($_SESSION['_csrf_timeout'])) {
261                 $to_unset = array();
262                 foreach($_SESSION['_csrf_timeout'] as $_csrf_id => $timeout) {
263                     if($timeout < time()) $to_unset[] = $_csrf_id;
264                 }
265                 foreach($to_unset as $_csrf_id) {
266                     $_SESSION['_csrf'][$_csrf_id] = null;
267                     $_SESSION['_csrf_timeout'][$_csrf_id] = null;
268                     unset($_SESSION['_csrf'][$_csrf_id]);
269                     unset($_SESSION['_csrf_timeout'][$_csrf_id]);
270                 }
271                 unset($to_unset);
272             }
273         }
274     }
7fe908 275
532ae5 276 }
L 277
7fe908 278 ?>