Portal details updated when customer record is updated. See #32
[PHPCredLocker.git] / lib / db / authdb.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
<?php
/** Authentication related DB functions
*
* Copyright (C) 2012 B Tasker
* Released under GNU GPL V2
* See LICENSE
*
*/
defined('_CREDLOCK') or die;
 
class AuthDB extends BTDB{
 
 
/** Check whether the current IP has been banned
*
* @arg ip
*
* @return boolean - true if ip banned
*/
function checkForBan($ip){
$expire = date('Y-m-d H:i:s');
$ip = $this->stringEscape($ip);
$sql = "SELECT * FROM #__bannedIPs WHERE `IP`='$ip' AND `Expiry` > '$expire'";
$this->setQuery($sql);
return $this->loadResult();
 
}
 
 
 
 
/** Add a customer record to the portal authentication table
*
* @arg id - Customers Id
* @arg email - Customer's login email address
* @arg pass - A pre-salted pass phrase
* @arg active - tinyint(1)
*
* @return mysql object
*/
function addCusttoPortal($id,$email,$pass,$active = 0){
 
$crypt = new Crypto;
 
$id = $this->stringEscape($id);
$email = $this->stringEscape($crypt->encrypt($email,'auth'));
$pass = $this->stringEscape($crypt->encrypt($pass,'auth'));
$active = $this->stringEscape($active);
 
 
 
$sql = "INSERT INTO #__CustPortal VALUES('$id','$email','$pass','$active')";
$this->setQuery($sql);
return $this->runQuery();
 
}
 
 
/** Edit the Portal login details for the specified customer - Passwords done seperately
*
* @arg id - Customer ID
* @arg email - Customers login email address
*
*/
function editPortalCustDetails($id,$email){
$crypt = new Crypto;
$id = $this->stringEscape($id);
$email = $this->stringEscape($crypt->encrypt($email,'auth'));
 
$sql = "UPDATE #__CustPortal SET `email`='$email' WHERE `id`='$id'";
$this->setQuery($sql);
return $this->runQuery();
}
 
 
 
/** If an IP has crossed the ban threshold, ban them
*
* @arg threshold - How many attempts are they allowed?
* @arg thresholddate - Whens the earliest date that should be considered?
* @arg ip - The IP to check (and possibly ban)
*
*/
function implementBan($threshold,$thresholddate,$bantime,$ip){
$ip = $this->stringEscape($ip);
 
$sql = "SELECT SUM(FailedAttempts) as failcount FROM #__FailedLogins WHERE FailedIP='$ip' AND LastAttempt > '$thresholddate'";
$this->setQuery($sql);
 
$tries =$this->loadResult();
 
    if ($tries->failcount > $threshold){
     $sql = "INSERT INTO #__bannedIPs (`IP`,`Expiry`) VALUES ('$ip','$bantime') ON DUPLICATE KEY UPDATE `Expiry`='$bantime'";
      $this->setQuery($sql);
    $this->runQuery();
 
    }
 
 
}
 
/** Log Failed Attempt
*
* @arg username string
* @arg username ip
*
*/
function LogFailedAttempt($username,$ip){
 
$username = $this->stringEscape($username);
$ip = $this->stringEscape($ip);
$date = date("Y-m-d H:i:s");
 
$sql = "INSERT INTO #__FailedLogins (`username`,`FailedAttempts`,`LastAttempt`,`FailedIP`) ".
"VALUES('$username','1','$date','$ip') ON DUPLICATE KEY UPDATE `FailedAttempts`=`FailedAttempts`+1, `LastAttempt`='$date'";
$this->setQuery($sql);
 
$this->runQuery();
 
 
 
}
 
 
 
/** Delete the specified User
*
* @arg username string
*
* @return boolean
*/
function DelUser($username){
 
$username = $this->stringEscape($username);
$sql = "DELETE FROM #__Users WHERE `username`='$username'";
$this->setQuery($sql);
 
return $this->runQuery();
}
 
 
 
 
 
/** Create a new user in the database
*
* @arg user - Object containing all User details to be inserted
* 
* @return boolean
*/
function addUser($user){
 
 
$crypt = new Crypto;
$user->username = $this->stringEscape($user->username);
$user->groups = $this->stringEscape(implode(",",$user->groups));
$user->pass = $this->stringEscape($crypt->encrypt($user->pass.":".$user->salt,'auth'));
$user->RealName = $this->stringEscape($user->RealName);
 
$sql = "INSERT INTO #__Users (`username`,`Name`,`pass`,`membergroup`) ".
"VALUES('{$user->username}','{$user->RealName}','{$user->pass}','{$user->groups}')";
 
$this->setQuery($sql);
 
$result = $this->runQuery();
 
    
    if ($result){
    // Insert was successful, we need to add to the audit log
    $log = new Logging;
    $log->logEntry($user->username,0);
    return true;
 
    }else{
    // Insert failed, return false.
 
    return false;
 
    }
 
}
 
 
 
 
/** Add a user to a group
*
*/
function userAppendGroup($user,$group){
 
$user = $this->stringEscape($user);
$group = $this->stringEscape($group);
 
$sql = "UPDATE #__Users SET membergroup = CONCAT(membergroup,',$group') WHERE username='$user'";
$this->setQuery($sql);
 
return $this->runQuery();
}
 
 
/** Edit user
*
* @arg user - Object containing all User details to be inserted
* 
* @return boolean
*/
function editUser($user){
 
 
 
$user->username = $this->stringEscape($user->username);
$user->groups = $this->stringEscape(implode(",",$user->groups));
$user->RealName = $this->stringEscape($user->RealName);
 
 
$sql = "Update #__Users SET `username`='{$user->username}',`Name`='{$user->RealName}',`membergroup`='{$user->groups}'";
 
 
if ($user->pass){
 
$crypt = new Crypto;
 
$user->pass = $this->stringEscape($crypt->encrypt($user->pass.":".$user->salt,'auth'));
$sql .= ", `pass`= '{$user->pass}'";
}
 
$sql .= " WHERE `username`='{$user->username}'";
 
 
 
 
 
$this->setQuery($sql);
 
$result = $this->runQuery();
 
    
    if ($result){
    // Insert was successful, we need to add to the audit log
    $log = new Logging;
    $log->logEntry($user->username,1);
    return true;
 
    }else{
    // Insert failed, return false.
 
    return false;
 
    }
 
}
 
 
 
 
 
/** Called when a user attempts to log in
* Retrieves everything needed to verify authentication and create the session
*
* @arg username - string
*
* @return object or boolean
*/
function retrieveUserCreds($username){
 
 
$username = $this->stringEscape($username);
 
$sql = "SELECT pass,membergroup,Name FROM #__Users WHERE username='$username'";
$this->setQuery($sql);
 
if ($res = $this->runQuery()){
 
return mysql_fetch_object($res);
}
 
return false;
 
 
}
 
 
 
 
/** Get a user's details
*
* @arg username string
*
* @return object
*/
function getUserDets($username){
 
$username = $this->stringEscape($username);
$sql = "SELECT Name, membergroup FROM #__Users WHERE username='$username'";
$this->setQuery($sql);
 
return $this->loadResult();
 
 
 
}
 
 
/** Add a User Group
*
* @arg group string
*
*/
function addGroup($name){
 
$crypt = new Crypto;
$name = $crypt->encrypt($name,'Groups');
 
$name = $this->stringEscape($name);
$sql = "INSERT INTO #__Groups (`Name`) VALUES('$name')";
$this->setQuery($sql);
 
$id = $this->insertID();
 
if ($id){
 $log = new Logging;
    $log->logEntry($id,13);
return $id;
}else{
return false;
}
}
 
 
/** Edit a User Group
*
* @arg id - INT 
* @arg group string
*
*/
function editGroup($id,$name){
 
$crypt = new Crypto;
$name = $crypt->encrypt($name,'Groups');
$id = $this->stringEscape($id);
$name = $this->stringEscape($name);
$sql = "UPDATE #__Groups SET `Name`='$name' WHERE `id`='$id'";
$this->setQuery($sql);
 
 
 
  if ($this->runQuery()){
    $log = new Logging;
    $log->logEntry($id,18);
    return true;
    }else{
    return false;
    }
 
}
 
 
/** Establish a database session for the current user
*
* @arg SessionID - string
* @arg expires - datetime
* @arg sesskey - string
*
* @return boolean
*/
function EstablishSession($sessionID,$expires,$sesskey){
$sessionID = $this->stringEscape($sessionID);
$expires = $this->stringEscape($expires);
$sesskey = $this->stringEscape($sesskey);
 
$user = BTMain::getUser()->name;
$created = date('Y-m-d H:i:s');
 
 
// IP is provided by the client, so we're not going to just trust it!
$ip = $this->stringEscape(BTMain::getip());
 
$sql = "INSERT INTO #__Sessions (`SessionID`,`Created`,`User`,`Expires`,`ClientIP`,`SessKey`) ".
"VALUES ('$sessionID','$created','$user','$expires','$ip','$sesskey')";
 
$this->setQuery($sql);
return $this->runQuery();
 
 
 
}
 
 
/** Grab user details based on the provided sessionID
*
* @arg sess - String 
*
* @return object
*/
function getUserSession($sess){
 
$sess = $this->stringEscape($sess);
$date = date('Y-m-d H:i:s');
$ip = BTMain::getip();
 
$sql = "SELECT a.ClientIP, a.SessKey, a.`Expires`, b.* FROM #__Sessions as a LEFT JOIN #__Users as b ON a.User = b.username WHERE a.SessionID = '$sess' AND a.Expires > '$date' AND a.`ClientIP` = '$ip'";
$this->setQuery($sql);
 
return $this->loadResult();
 
 
}
 
 
 
/** Remove a session from the database
*
* @arg SessionID - string
*
* @return boolean
*/
function KillSession($sessID){
 
$sessID = $this->stringEscape($sessID);
 
$sql = "SELECT `Expires` FROM #__Sessions WHERE `SessionID`='$sessID'";
$this->setQuery($sql);
$exp = $this->loadResult();
 
$sql = "DELETE FROM #__Sessions WHERE `SessionID`='$sessID'";
$this->setQuery($sql);
$res = $this->runQuery();
 
// Log the event
$log = new Logging;
$log->logEntry('',12);
 
return $exp->Expires;
}
 
 
/** Retrieve group data (Names will need decrypting)
*
*/
function retrieveGroupNames(){
 
  if(BTMain::checkisSuperAdmin()){
    $where = "1=1";
  }else{
 
    $grps = implode(",",BTMain::getUser()->groups);
 
    $where = "id IN ($grps)";
  }
 
 
$sql = "SELECT * FROM #__Groups WHERE $where";
$this->setQuery($sql);
return $this->loadResults();
 
}
 
 
 
/** Retrieve the name of a Group based on ID
*
* @arg id INT
*
* @return object
*/
function retrieveGrpById($id){
 
$id = $this->stringEscape($id);
$sql = "SELECT Name FROM #__Groups WHERE id='$id'";
$this->setQuery($sql);
return $this->loadResult();
 
}
 
 
 
 
/** Delete the specified group
*
* Any credentials still recorded against the group will be set to SuperAdmin only
*
* @arg id -INT
*
* @return boolean
*/
function delGroup($id){
 
$id = $this->stringEscape($id);
$sql = "DELETE FROM #__Cred WHERE `Group`='$id'";
$this->setQuery($sql);
$this->runQuery();
 
$sql = "DELETE FROM #__Groups WHERE `id`='$id'";
$this->setQuery($sql);
 
if($this->runQuery()){
 
 
$log = new Logging;
$log->logEntry($id,14);
return true;
}else{
return false;
}
 
}
 
 
 
 
/** Retrieve a User list
*
* @return object
*/
function listUsers(){
 
$sql = "SELECT username, Name FROM #__Users ORDER BY username ASC";
$this->setQuery($sql);
 
return $this->loadResults();
 
 
 
}
 
 
 
 
 
}
 
?>