forked from liamjack/Auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.class.php
1180 lines (936 loc) · 35.5 KB
/
auth.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
class auth
{
public $mysqli;
public $errormsg;
public $successmsg;
function __construct()
{
include("config.php");
$this->mysqli = new mysqli($db['host'], $db['user'], $db['pass'], $db['name']);
unset($db['pass']); // $mysqli is public, remove password for security
}
/*
* Log user in via MySQL Database
* @param string $username
* @param string $password
* @return boolean
*/
function login($username, $password)
{
include("config.php");
include("lang.php");
if(!isset($_COOKIE["auth_session"]))
{
$attcount = $this->getattempt($_SERVER['REMOTE_ADDR']);
if($attcount >= $auth_conf['max_attempts'])
{
$this->errormsg[] = $lang[$loc]['auth']['login_lockedout'];
$this->errormsg[] = $lang[$loc]['auth']['login_wait30'];
return false;
}
else
{
// Input verification :
if(strlen($username) == 0) { $this->errormsg[] = $lang[$loc]['auth']['login_username_empty']; return false; }
elseif(strlen($username) > 30) { $this->errormsg[] = $lang[$loc]['auth']['login_username_long']; return false; }
elseif(strlen($username) < 3) { $this->errormsg[] = $lang[$loc]['auth']['login_username_short']; return false; }
elseif(strlen($password) == 0) { $this->errormsg[] = $lang[$loc]['auth']['login_password_empty']; return false; }
elseif(strlen($password) > 30) { $this->errormsg[] = $lang[$loc]['auth']['login_password_short']; return false; }
elseif(strlen($password) < 5) { $this->errormsg[] = $lang[$loc]['auth']['login_password_long']; return false; }
else
{
// Input is valid
$password = $this->hashpass($password);
$query = $this->mysqli->prepare("SELECT isactive FROM users WHERE username = ? AND password = ?");
$query->bind_param("ss", $username, $password);
$query->bind_result($isactive);
$query->execute();
$query->store_result();
$count = $query->num_rows;
$query->fetch();
$query->close();
if($count == 0)
{
// Username and / or password are incorrect
$this->errormsg[] = $lang[$loc]['auth']['login_incorrect'];
$this->addattempt($_SERVER['REMOTE_ADDR']);
$attcount = $attcount + 1;
$remaincount = $auth_conf['max_attempts'] - $attcount;
$this->LogActivity("UNKNOWN", "AUTH_LOGIN_FAIL", "Username / Password incorrect - {$username} / {$password}");
$this->errormsg[] = sprintf($lang[$loc]['auth']['login_attempts_remaining'], $remaincount);
return false;
}
else
{
// Username and password are correct
if($isactive == "0")
{
// Account is not activated
$this->LogActivity($username, "AUTH_LOGIN_FAIL", "Account inactive");
$this->errormsg[] = $lang[$loc]['auth']['login_account_inactive'];
return false;
}
else
{
// Account is activated
$this->newsession($username);
$this->LogActivity($username, "AUTH_LOGIN_SUCCESS", "User logged in");
$this->successmsg[] = $lang[$loc]['auth']['login_success'];
return true;
}
}
}
}
}
else
{
// User is already logged in
$this->errormsg[] = $lang[$loc]['auth']['login_already'];
return false;
}
}
/*
* Register a new user into the database
* @param string $username
* @param string $password
* @param string $verifypassword
* @param string $email
* @return boolean
*/
function register($username, $password, $verifypassword, $email)
{
include("config.php");
include("lang.php");
if(!isset($_COOKIE["auth_session"]))
{
// Input Verification :
if(strlen($username) == 0) { $this->errormsg[] = $lang[$loc]['auth']['register_username_empty']; }
elseif(strlen($username) > 30) { $this->errormsg[] = $lang[$loc]['auth']['register_username_long']; }
elseif(strlen($username) < 3) { $this->errormsg[] = $lang[$loc]['auth']['register_username_short']; }
if(strlen($password) == 0) { $this->errormsg[] = $lang[$loc]['auth']['register_password_empty']; }
elseif(strlen($password) > 30) { $this->errormsg[] = $lang[$loc]['auth']['register_password_long']; }
elseif(strlen($password) < 5) { $this->errormsg[] = $lang[$loc]['auth']['register_password_short']; }
elseif($password !== $verifypassword) { $this->errormsg[] = $lang[$loc]['auth']['register_password_nomatch']; }
elseif(strstr($password, $username)) { $this->errormsg[] = $lang[$loc]['auth']['register_password_username']; }
if(strlen($email) == 0) { $this->errormsg[] = $lang[$loc]['auth']['register_email_empty']; }
elseif(strlen($email) > 100) { $this->errormsg[] = $lang[$loc]['auth']['register_email_long']; }
elseif(strlen($email) < 5) { $this->errormsg[] = $lang[$loc]['auth']['register_email_short']; }
elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) { $this->errormsg[] = $lang[$loc]['auth']['register_email_invalid']; }
if(count($this->errormsg) == 0)
{
// Input is valid
$query = $this->mysqli->prepare("SELECT * FROM users WHERE username=?");
$query->bind_param("s", $username);
$query->execute();
$query->store_result();
$count = $query->num_rows;
$query->close();
if($count != 0)
{
// Username already exists
$this->LogActivity("UNKNOWN", "AUTH_REGISTER_FAIL", "Username ({$username}) already exists");
$this->errormsg[] = $lang[$loc]['auth']['register_username_exist'];
return false;
}
else
{
// Username is not taken
$query = $this->mysqli->prepare("SELECT * FROM users WHERE email=?");
$query->bind_param("s", $email);
$query->execute();
$query->store_result();
$count = $query->num_rows;
$query->close();
if($count != 0)
{
// Email address is already used
$this->LogActivity("UNKNOWN", "AUTH_REGISTER_FAIL", "Email ({$email}) already exists");
$this->errormsg[] = $lang[$loc]['auth']['register_email_exist'];
return false;
}
else
{
// Email address isn't already used
$password = $this->hashpass($password);
$activekey = $this->randomkey(15);
$query = $this->mysqli->prepare("INSERT INTO users (username, password, email, activekey) VALUES (?, ?, ?, ?)");
$query->bind_param("ssss", $username, $password, $email, $activekey);
$query->execute();
$query->close();
$message_from = $auth_conf['email_from'];
$message_subj = $auth_conf['site_name'] . " - Account activation required !";
$message_cont = "Hello {$username}<br/><br/>";
$message_cont .= "You recently registered a new account on " . $auth_conf['site_name'] . "<br/>";
$message_cont .= "To activate your account please click the following link<br/><br/>";
$message_cont .= "<b><a href=\"" . $auth_conf['base_url'] . "?page=activate&username={$username}&key={$activekey}\">Activate my account</a></b>";
$message_head = "From: {$message_from}" . "\r\n";
$message_head .= "MIME-Version: 1.0" . "\r\n";
$message_head .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
mail($email, $message_subj, $message_cont, $message_head);
$this->LogActivity($username, "AUTH_REGISTER_SUCCESS", "Account created and activation email sent");
$this->successmsg[] = $lang[$loc]['auth']['register_success'];
return true;
}
}
}
else
{
return false;
}
}
else
{
// User is logged in
$this->errormsg[] = $lang[$loc]['auth']['register_email_loggedin'];
return false;
}
}
/*
* Creates a new session for the provided username and sets cookie
* @param string $username
*/
function newsession($username)
{
include("config.php");
$hash = md5(microtime());
// Fetch User ID :
$query = $this->mysqli->prepare("SELECT id FROM users WHERE username=?");
$query->bind_param("s", $username);
$query->bind_result($uid);
$query->execute();
$query->fetch();
$query->close();
// Delete all previous sessions :
$query = $this->mysqli->prepare("DELETE FROM sessions WHERE username=?");
$query->bind_param("s", $username);
$query->execute();
$query->close();
$ip = $_SERVER['REMOTE_ADDR'];
$expiredate = date("Y-m-d H:i:s", strtotime($auth_conf['session_duration']));
$expiretime = strtotime($expiredate);
$query = $this->mysqli->prepare("INSERT INTO sessions (uid, username, hash, expiredate, ip) VALUES (?, ?, ?, ?, ?)");
$query->bind_param("issss", $uid, $username, $hash, $expiredate, $ip);
$query->execute();
$query->close();
setcookie("auth_session", $hash, $expiretime);
}
/*
* Deletes the user's session based on hash
* @param string $hash
*/
function deletesession($hash)
{
include("config.php");
include("lang.php");
$query = $this->mysqli->prepare("SELECT username FROM sessions WHERE hash=?");
$query->bind_param("s", $hash);
$query->bind_result($username);
$query->execute();
$query->store_result();
$count = $query->num_rows;
$query->fetch();
$query->close();
if($count == 0)
{
// Hash doesn't exist
$this->LogActivity("UNKNOWN", "AUTH_LOGOUT", "User session cookie deleted - Database session not deleted - Hash ({$hash}) didn't exist");
$this->errormsg[] = $lang[$loc]['auth']['deletesession_invalid'];
setcookie("auth_session", $hash, time() - 3600);
}
else
{
// Hash exists, Delete all sessions for that username :
$query = $this->mysqli->prepare("DELETE FROM sessions WHERE username=?");
$query->bind_param("s", $username);
$query->execute();
$query->close();
$this->LogActivity($username, "AUTH_LOGOUT", "User session cookie deleted - Database session deleted - Hash ({$hash})");
setcookie("auth_session", $hash, time() - 3600);
}
}
/*
* Provides an associative array of user info based on session hash
* @param string $hash
* @return array $session
*/
function sessioninfo($hash)
{
include("config.php");
include("lang.php");
$query = $this->mysqli->prepare("SELECT uid, username, expiredate, ip FROM sessions WHERE hash=?");
$query->bind_param("s", $hash);
$query->bind_result($session['uid'], $session['username'], $session['expiredate'], $session['ip']);
$query->execute();
$query->store_result();
$count = $query->num_rows;
$query->fetch();
$query->close();
if($count == 0)
{
// Hash doesn't exist
$this->errormsg[] = $lang[$loc]['auth']['sessioninfo_invalid'];
setcookie("auth_session", $hash, time() - 3600);
return false;
}
else
{
// Hash exists
return $session;
}
}
/*
* Checks if session is valid (Current IP = Stored IP + Current date < expire date)
* @param string $hash
* @return bool
*/
function checksession($hash)
{
$query = $this->mysqli->prepare("SELECT username, expiredate, ip FROM sessions WHERE hash=?");
$query->bind_param("s", $hash);
$query->bind_result($username, $db_expiredate, $db_ip);
$query->execute();
$query->store_result();
$count = $query->num_rows;
$query->fetch();
$query->close();
if($count == 0)
{
// Hash doesn't exist
setcookie("auth_session", $hash, time() - 3600);
$this->LogActivity($username, "AUTH_CHECKSESSION", "User session cookie deleted - Hash ({$hash}) didn't exist");
return false;
}
else
{
if($_SERVER['REMOTE_ADDR'] != $db_ip)
{
// Hash exists, but IP has changed
$query = $this->mysqli->prepare("DELETE FROM sessions WHERE username=?");
$query->bind_param("s", $username);
$query->execute();
$query->close();
setcookie("auth_session", $hash, time() - 3600);
$this->LogActivity($username, "AUTH_CHECKSESSION", "User session cookie deleted - IP Different ( DB : {$db_ip} / Current : " . $_SERVER['REMOTE_ADDR'] . " )");
return false;
}
else
{
$expiredate = strtotime($db_expiredate);
$currentdate = strtotime(date("Y-m-d H:i:s"));
if($currentdate > $expiredate)
{
// Hash exists, IP is the same, but session has expired
$query = $this->mysqli->prepare("DELETE FROM sessions WHERE username=?");
$query->bind_param("s", $username);
$query->execute();
$query->close();
setcookie("auth_session", $hash, time() - 3600);
$this->LogActivity($username, "AUTH_CHECKSESSION", "User session cookie deleted - Session expired ( Expire date : {$db_expiredate} )");
return false;
}
else
{
// Hash exists, IP is the same, date < expiry date
return true;
}
}
}
}
/*
* Returns a random string, length can be modified
* @param int $length
* @return string $key
*/
function randomkey($length = 10)
{
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
$key = "";
for($i = 0; $i < $length; $i++)
{
$key .= $chars{rand(0, strlen($chars) - 1)};
}
return $key;
}
/*
* Activate a user's account
* @param string $username
* @param string $key
* @return boolean
*/
function activate($username, $key)
{
include("config.php");
include("lang.php");
// Input verification
if(strlen($username) == 0) { $this->errormsg[] = $lang[$loc]['auth']['activate_username_empty']; return false; }
elseif(strlen($username) > 30) { $this->errormsg[] = $lang[$loc]['auth']['activate_username_long']; return false; }
elseif(strlen($username) < 3) { $this->errormsg[] = $lang[$loc]['auth']['activate_username_short']; return false; }
elseif(strlen($key) == 0) { $this->errormsg[] = $lang[$loc]['auth']['activate_key_empty']; return false; }
elseif(strlen($key) > 15) { $this->errormsg[] = $lang[$loc]['auth']['activate_key_long']; return false; }
elseif(strlen($key) < 15) { $this->errormsg[] = $lang[$loc]['auth']['activate_key_short']; return false; }
else
{
// Input is valid
$query = $this->mysqli->prepare("SELECT isactive, activekey FROM users WHERE username=?");
$query->bind_param("s", $username);
$query->bind_result($isactive, $activekey);
$query->execute();
$query->store_result();
$count = $query->num_rows;
$query->fetch();
$query->close();
if($count == 0)
{
// User doesn't exist
$this->LogActivity("UNKNOWN", "AUTH_ACTIVATE_FAIL", "Username Incorrect : {$username}");
$this->errormsg[] = $lang[$loc]['auth']['activate_username_incorrect'];
return false;
}
else
{
// User exists
if($isactive == 1)
{
// Account is already activated
$this->LogActivity($username, "AUTH_ACTIVATE_FAIL", "Account already activated");
$this->errormsg[] = $lang[$loc]['auth']['activate_account_activated'];
return true;
}
else
{
// Account isn't activated
if($key == $activekey)
{
// Activation keys match
$new_isactive = 1;
$new_activekey = "0";
$query = $this->mysqli->prepare("UPDATE users SET isactive=?, activekey=? WHERE username=?");
$query->bind_param("iss", $new_isactive, $new_activekey, $username);
$query->execute();
$query->close();
$this->LogActivity($username, "AUTH_ACTIVATE_SUCCESS", "Activation successful. Key Entry deleted.");
$this->successmsg[] = $lang[$loc]['auth']['activate_success'];
return true;
}
else
{
// Activation Keys don't match
$this->LogActivity($username, "AUTH_ACTIVATE_FAIL", "Activation keys don't match ( DB : {$activekey} / Given : {$key} )");
$this->errormsg[] = $lang[$loc]['auth']['activate_key_incorrect'];
return false;
}
}
}
}
}
/*
* Changes a user's password, providing the current password is known
* @param string $username
* @param string $currpass
* @param string $newpass
* @param string $verifynewpass
* @return boolean
*/
function changepass($username, $currpass, $newpass, $verifynewpass)
{
include("config.php");
include("lang.php");
if(strlen($username) == 0) { $this->errormsg[] = $lang[$loc]['auth']['changepass_username_empty']; }
elseif(strlen($username) > 30) { $this->errormsg[] = $lang[$loc]['auth']['changepass_username_long']; }
elseif(strlen($username) < 3) { $this->errormsg[] = $lang[$loc]['auth']['changepass_username_short']; }
if(strlen($currpass) == 0) { $this->errormsg[] = $lang[$loc]['auth']['changepass_currpass_empty']; }
elseif(strlen($currpass) < 5) { $this->errormsg[] = $lang[$loc]['auth']['changepass_currpass_short']; }
elseif(strlen($currpass) > 30) { $this->errormsg[] = $lang[$loc]['auth']['changepass_currpass_long']; }
if(strlen($newpass) == 0) { $this->errormsg[] = $lang[$loc]['auth']['changepass_newpass_empty']; }
elseif(strlen($newpass) < 5) { $this->errormsg[] = $lang[$loc]['auth']['changepass_newpass_short']; }
elseif(strlen($newpass) > 30) { $this->errormsg[] = $lang[$loc]['auth']['changepass_newpass_long']; }
elseif(strstr($newpass, $username)) { $this->errormsg[] = $lang[$loc]['auth']['changepass_password_username']; }
elseif($newpass !== $verifynewpass) { $this->errormsg[] = $lang[$loc]['auth']['changepass_password_nomatch']; }
if(count($this->errormsg) == 0)
{
$currpass = $this->hashpass($currpass);
$newpass = $this->hashpass($newpass);
$query = $this->mysqli->prepare("SELECT password FROM users WHERE username=?");
$query->bind_param("s", $username);
$query->bind_result($db_currpass);
$query->execute();
$query->store_result();
$count = $query->num_rows;
$query->fetch();
$query->close();
if($count == 0)
{
$this->LogActivity("UNKNOWN", "AUTH_CHANGEPASS_FAIL", "Username Incorrect ({$username})");
$this->errormsg[] = $lang[$loc]['auth']['changepass_username_incorrect'];
return false;
}
else
{
if($currpass == $db_currpass)
{
$query = $this->mysqli->prepare("UPDATE users SET password=? WHERE username=?");
$query->bind_param("ss", $newpass, $username);
$query->execute();
$query->close();
$this->LogActivity($username, "AUTH_CHANGEPASS_SUCCESS", "Password changed");
$this->successmsg[] = $lang[$loc]['auth']['changepass_success'];
return true;
}
else
{
$this->LogActivity($username, "AUTH_CHANGEPASS_FAIL", "Current Password Incorrect ( DB : {$db_currpass} / Given : {$currpass} )");
$this->errormsg[] = $lang[$loc]['auth']['changepass_currpass_incorrect'];
return false;
}
}
}
else
{
return false;
}
}
/*
* Changes the stored email address based on username
* @param string $username
* @param string $email
* @return boolean
*/
function changeemail($username, $email)
{
include("config.php");
include("lang.php");
if(strlen($username) == 0) { $this->errormsg[] = $lang[$loc]['auth']['changeemail_username_empty']; }
elseif(strlen($username) > 30) { $this->errormsg[] = $lang[$loc]['auth']['changeemail_username_long']; }
elseif(strlen($username) < 3) { $this->errormsg[] = $lang[$loc]['auth']['changeemail_username_short']; }
if(strlen($email) == 0) { $this->errormsg[] = $lang[$loc]['auth']['changeemail_email_empty']; }
elseif(strlen($email) > 100) { $this->errormsg[] = $lang[$loc]['auth']['changeemail_email_long']; }
elseif(strlen($email) < 5) { $this->errormsg[] = $lang[$loc]['auth']['changeemail_email_short']; }
elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) { $this->errormsg[] = $lang[$loc]['auth']['changeemail_email_invalid']; }
if(count($this->errormsg) == 0)
{
$query = $this->mysqli->prepare("SELECT email FROM users WHERE username=?");
$query->bind_param("s", $username);
$query->bind_result($db_email);
$query->execute();
$query->store_result();
$count = $query->num_rows;
$query->fetch();
$query->close();
if($count == 0)
{
$this->LogActivity("UNKNOWN", "AUTH_CHANGEEMAIL_FAIL", "Username Incorrect ({$username})");
$this->errormsg[] = $lang[$loc]['auth']['changeemail_username_incorrect'];
return false;
}
else
{
if($email == $db_email)
{
$this->LogActivity($username, "AUTH_CHANGEEMAIL_FAIL", "Old and new email matched ({$email})");
$this->errormsg[] = $lang[$loc]['auth']['changeemail_email_match'];
return false;
}
else
{
$query = $this->mysqli->prepare("UPDATE users SET email=? WHERE username=?");
$query->bind_param("ss", $email, $username);
$query->execute();
$query->close();
$this->LogActivity($username, "AUTH_CHANGEEMAIL_SUCCESS", "Email changed from {$db_email} to {$email}");
$this->successmsg[] = $lang[$loc]['auth']['changeemail_success'];
return true;
}
}
}
else
{
return false;
}
}
/*
* Give the user the ability to change their password if the current password is forgotten
* by sending email to the email address associated to that user
* @param string $username
* @param string $email
* @param string $key
* @param string $newpass
* @param string $verifynewpass
* @return boolean
*/
function resetpass($username = '0', $email ='0', $key = '0', $newpass = '0', $verifynewpass = '0')
{
include("config.php");
include("lang.php");
$attcount = $this->getattempt($_SERVER['REMOTE_ADDR']);
if($attcount >= $auth_conf['max_attempts'])
{
$this->errormsg[] = $lang[$loc]['auth']['resetpass_lockedout'];
$this->errormsg[] = $lang[$loc]['auth']['resetpass_wait30'];
return false;
}
else
{
if($username == '0' && $key == '0')
{
if(strlen($email) == 0) { $this->errormsg[] = $lang[$loc]['auth']['resetpass_email_empty']; }
elseif(strlen($email) > 100) { $this->errormsg[] = $lang[$loc]['auth']['resetpass_email_long']; }
elseif(strlen($email) < 5) { $this->errormsg[] = $lang[$loc]['auth']['resetpass_email_short']; }
elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) { $this->errormsg[] = $lang[$loc]['auth']['resetpass_email_invalid']; }
$resetkey = $this->randomkey(15);
$query = $this->mysqli->prepare("SELECT username FROM users WHERE email=?");
$query->bind_param("s", $email);
$query->bind_result($username);
$query->execute();
$query->store_result();
$count = $query->num_rows;
$query->fetch();
$query->close();
if($count == 0)
{
$this->errormsg[] = $lang[$loc]['auth']['resetpass_email_incorrect'];
$attcount = $attcount + 1;
$remaincount = $auth_conf['max_attempts'] - $attcount;
$this->LogActivity("UNKNOWN", "AUTH_RESETPASS_FAIL", "Email incorrect ({$email})");
$this->errormsg[] = sprintf($lang[$loc]['auth']['resetpass_attempts_remaining'], $remaincount);
$this->addattempt($_SERVER['REMOTE_ADDR']);
return false;
}
else
{
$query = $this->mysqli->prepare("UPDATE users SET resetkey=? WHERE username=?");
$query->bind_param("ss", $resetkey, $username);
$query->execute();
$query->close();
$message_from = $auth_conf['email_from'];
$message_subj = $auth_conf['site_name'] . " - Password reset request !";
$message_cont = "Hello {$username}<br/><br/>";
$message_cont .= "You recently requested a password reset on " . $auth_conf['site_name'] . "<br/>";
$message_cont .= "To proceed with the password reset, please click the following link :<br/><br/>";
$message_cont .= "<b><a href=\"" . $auth_conf['base_url'] . "?page=forgot&username={$username}&key={$resetkey}\">Reset My Password</a></b>";
$message_head = "From: {$message_from}" . "\r\n";
$message_head .= "MIME-Version: 1.0" . "\r\n";
$message_head .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
mail($email, $message_subj, $message_cont, $message_head);
$this->LogActivity($username, "AUTH_RESETPASS_SUCCESS", "Reset pass request sent to {$email} ( Key : {$resetkey} )");
$this->successmsg[] = $lang[$loc]['auth']['resetpass_email_sent'];
return true;
}
}
else
{
// Reset Password
if(strlen($key) == 0) { $this->errormsg[] = $lang[$loc]['auth']['resetpass_key_empty']; }
elseif(strlen($key) < 15) { $this->errormsg[] = $lang[$loc]['auth']['resetpass_key_short']; }
elseif(strlen($key) > 15) { $this->errormsg[] = $lang[$loc]['auth']['resetpass_key_long']; }
if(strlen($newpass) == 0) { $this->errormsg[] = $lang[$loc]['auth']['resetpass_newpass_empty']; }
elseif(strlen($newpass) > 30) { $this->errormsg[] = $lang[$loc]['auth']['resetpass_newpass_long']; }
elseif(strlen($newpass) < 5) { $this->errormsg[] = $lang[$loc]['auth']['resetpass_newpass_short']; }
elseif(strstr($newpass, $username)) { $this->errormsg[] = $lang[$loc]['auth']['resetpass_newpass_username']; }
elseif($newpass !== $verifynewpass) { $this->errormsg[] = $lang[$loc]['auth']['resetpass_newpass_nomatch']; }
if(count($this->errormsg) == 0)
{
$query = $this->mysqli->prepare("SELECT resetkey FROM users WHERE username=?");
$query->bind_param("s", $username);
$query->bind_result($db_key);
$query->execute();
$query->store_result();
$count = $query->num_rows;
$query->fetch();
$query->close();
if($count == 0)
{
$this->errormsg[] = $lang[$loc]['auth']['resetpass_username_incorrect'];
$attcount = $attcount + 1;
$remaincount = $auth_conf['max_attempts'] - $attcount;
$this->LogActivity("UNKNOWN", "AUTH_RESETPASS_FAIL", "Username incorrect ({$username})");
$this->errormsg[] = sprintf($lang[$loc]['auth']['resetpass_attempts_remaining'], $remaincount);
$this->addattempt($_SERVER['REMOTE_ADDR']);
return false;
}
else
{
if($key == $db_key)
{
$newpass = $this->hashpass($newpass);
$resetkey = '0';
$query = $this->mysqli->prepare("UPDATE users SET password=?, resetkey=? WHERE username=?");
$query->bind_param("sss", $newpass, $resetkey, $username);
$query->execute();
$query->close();
$this->LogActivity($username, "AUTH_RESETPASS_SUCCESS", "Password reset - Key reset");
$this->successmsg[] = $lang[$loc]['auth']['resetpass_success'];
return true;
}
else
{
$this->errormsg[] = $lang[$loc]['auth']['resetpass_key_incorrect'];
$attcount = $attcount + 1;
$remaincount = 5 - $attcount;
$this->LogActivity($username, "AUTH_RESETPASS_FAIL", "Key Incorrect ( DB : {$db_key} / Given : {$key} )");
$this->errormsg[] = sprintf($lang[$loc]['auth']['resetpass_attempts_remaining'], $remaincount);
$this->addattempt($_SERVER['REMOTE_ADDR']);
return false;
}
}
}
else
{
return false;
}
}
}
}
/*
* Checks if the reset key is correct for provided username
* @param string $username
* @param string $key
* @return boolean
*/
function checkresetkey($username, $key)
{
include("config.php");
include("lang.php");
$attcount = $this->getattempt($_SERVER['REMOTE_ADDR']);
if($attcount >= $auth_conf['max_attempts'])
{
$this->errormsg[] = $lang[$loc]['auth']['resetpass_lockedout'];
$this->errormsg[] = $lang[$loc]['auth']['resetpass_wait30'];
return false;
}
else
{
if(strlen($username) == 0) { return false; }
elseif(strlen($username) > 30) { return false; }
elseif(strlen($username) < 3) { return false; }
elseif(strlen($key) == 0) { return false; }
elseif(strlen($key) < 15) { return false; }
elseif(strlen($key) > 15) { return false; }
else
{
$query = $this->mysqli->prepare("SELECT resetkey FROM users WHERE username=?");
$query->bind_param("s", $username);
$query->bind_result($db_key);
$query->execute();
$query->store_result();
$count = $query->num_rows;
$query->fetch();
$query->close();
if($count == 0)
{
$this->LogActivity("UNKNOWN", "AUTH_CHECKRESETKEY_FAIL", "Username doesn't exist ({$username})");
$this->addattempt($_SERVER['REMOTE_ADDR']);
$this->errormsg[] = $lang[$loc]['auth']['checkresetkey_username_incorrect'];
$attcount = $attcount + 1;
$remaincount = $auth_conf['max_attempts'] - $attcount;
$this->errormsg[] = sprintf($lang[$loc]['auth']['checkresetkey_attempts_remaining'], $remaincount);
return false;
}
else
{
if($key == $db_key)
{
return true;
}
else
{
$this->LogActivity($username, "AUTH_CHECKRESETKEY_FAIL", "Key provided is different to DB key ( DB : {$db_key} / Given : {$key} )");
$this->addattempt($_SERVER['REMOTE_ADDR']);
$this->errormsg[] = $lang[$loc]['auth']['checkresetkey_key_incorrect'];
$attcount = $attcount + 1;
$remaincount = $auth_conf['max_attempts'] - $attcount;
$this->errormsg[] = sprintf($lang[$loc]['auth']['checkresetkey_attempts_remaining'], $remaincount);
return false;
}
}
}
}
}
/*
* Deletes a user's account. Requires user's password
* @param string $username
* @param string $password
* @return boolean
*/
function deleteaccount($username, $password)
{
include("config.php");
include("lang.php");
if(strlen($username) == 0) { $this->errormsg[] = $lang[$loc]['auth']['deleteaccount_username_empty']; }
elseif(strlen($username) > 30) { $this->errormsg[] = $lang[$loc]['auth']['deleteaccount_username_long']; }
elseif(strlen($username) < 3) { $this->errormsg[] = $lang[$loc]['auth']['deleteaccount_username_short']; }
if(strlen($password) == 0) { $this->errormsg[] = $lang[$loc]['auth']['deleteaccount_password_empty']; }
elseif(strlen($password) > 30) { $this->errormsg[] = $lang[$loc]['auth']['deleteaccount_password_long']; }
elseif(strlen($password) < 5) { $this->errormsg[] = $lang[$loc]['auth']['deleteaccount_password_short']; }
if(count($this->errormsg) == 0)
{
$password = $this->hashpass($password);
$query = $this->mysqli->prepare("SELECT password FROM users WHERE username=?");
$query->bind_param("s", $username);
$query->bind_result($db_password);
$query->execute();
$query->store_result();
$count = $query->num_rows;
$query->fetch();
$query->close();
if($count == 0)
{
$this->LogActivity("UNKNOWN", "AUTH_DELETEACCOUNT_FAIL", "Username Incorrect ({$username})");
$this->errormsg[] = $lang[$loc]['auth']['deleteaccount_username_incorrect'];
return false;
}
else
{
if($password == $db_password)
{
$query = $this->mysqli->prepare("DELETE FROM users WHERE username=?");
$query->bind_param("s", $username);
$query->execute();
$query->close();
$query = $this->mysqli->prepare("DELETE FROM sessions WHERE username=?");