forked from causefx/Organizr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.php
executable file
·850 lines (822 loc) · 37.6 KB
/
user.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
<?php
/**
* A framework for simple user authentication.
*
* Users are recorded using {username, password, token} triplets.
* Whenever a user logs in successfully, his or her database
* entry is assigned a new random token, which is used in
* salting subsequent password checks.
*/
// Include functions if not already included
require_once('functions.php');
// Define Version
define('INSTALLEDVERSION', '1.323');
// Autoload frameworks
require_once(__DIR__ . '/vendor/autoload.php');
// Lazyload settings
$databaseConfig = configLazy('config/config.php');
if(file_exists('custom.css')) : define('CUSTOMCSS', 'true'); else : define('CUSTOMCSS', 'false'); endif;
$notifyExplode = explode("-", NOTIFYEFFECT);
define('FAIL_LOG', 'loginLog.json');
date_default_timezone_set(TIMEZONE);
class User
{
// =======================================================================
// IMPORTANT VALUES THAT YOU *NEED* TO CHANGE FOR THIS TO BE SECURE
// =======================================================================
// Keeping this location on ./... means that it will be publically visible to all,
// and you need to use htaccess rules or some such to ensure no one
// grabs your user's data.
//const USER_HOME = "../users/";
// In order for users to be notified by email of certain things, set this to true.
// Note that the server you run this on should have sendmail in order for
// notification emails to work. Also note that password resetting doesn't work
// unless mail notification is turned on.
const use_mail = ENABLEMAIL;
// This value should point to a directory that is not available to web users.
// If your documents are in ./public_html, for instance., then put database
// in something like ./database - that way, you don't have to rely on
// htaccess rules or the likes, because it's simply impossible to get to the
// database from a public, or private, URL.
//
// By default it's set to the stupidly dangerous and publically accessible same
// base dir as your web page. So change it, because people are going to try
// to download your database file. And succeed.
//const DATABASE_LOCATION = "../";
// if this is set to "true", registration failure due to known usernames is reported,
// and login failures are explained as either the wrong username or the wrong password.
// You really want to set this to 'false', but it's on true by default because goddamnit
// I'm going to confront you with security issues right off the bat =)
const unsafe_reporting = false;
/**
Think about security for a moment. On the one hand, you want your website
to not reveal whether usernames are already taken, so when people log in
you will want to say "username or password incorrect". However, you also want
to be able to tell people that they can't register because the username they
picked is already taken.
Because these are mutually exclusive, you can't do both using this framework.
You can either use unsafe reporting, where the system will will tell you that
a username exists, both during registration and login, or you can use safe
reporting, and then the system will reject registrations based on username
similarity, not exact match. But then it also won't say which of the username
or password in a login attempt was incorrect.
**/
// =======================================================================
// You can modify the following values, but they're not security related
// =======================================================================
// rename this to whatever you like
const DATABASE_NAME = "users"; // Obsolete
// this is the session timeout. If someone hasn't performed any page requests
// in [timeout] seconds, they're considered logged out.
const time_out = 604800;
// You'll probably want to change this to something sensible. If your site is
// www.sockmonkey.com, then you want this to be "sockmonkey.com"
const DOMAIN_NAME = "Organizr";
// This is going to be the "from" address
const MAILER_NAME = "noreply@organizr";
// if you want people to be able to reply to a real address, override
// this variable to "[email protected]" here.
const MAILER_REPLYTO = "noreply@organizr";
// =======================================================================
// Don't modify any variables beyond this point =)
// =======================================================================
// this is the global error message. If anything goes wrong, this tells you why.
var $error = "";
// progress log
var $info_log = array();
// Information logging
function info($string) { $this->info_log[] = $string; }
// error log
var $error_log = array();
// Error logging
function error($string) { $this->error_log[] = $string; }
// all possible values for a hexadecimal number
var $hex = "0123456789abcdef";
// all possible values for an ascii password, skewed a bit so the number to letter ratio is closer to 1:1
var $ascii = "0a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6A7B8C9D0E1F2G3H4I5J6K7L8M9N0O1P2Q3R4S5T6U7V8W9X0Y1Z23456789";
// the regular expression for email matching (see http://www.regular-expressions.info/email.html)
const emailregexp = "/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/";
// the regular expression for SHA1 hash matching
const sha1regexp = "/[0123456789abcdef]{40,40}/";
// this will tell us whether the client that requested the page is authenticated or not.
var $authenticated = false;
// the guest user name
const GUEST_USER = "guest user";
// this will contain the user name for the user doing the page request
var $username = User::GUEST_USER;
// if this is a properly logged in user, this will contain the data directory location for this user
var $userdir = false;
// the user's email address, if logged in.
var $email = "";
// the user's role in the system
var $role = "";
var $group = "";
// global database handle
var $database = false;
//EMAIL SHIT
function startEmail($email, $username, $subject, $body){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = SMTPHOST;
$mail->SMTPAuth = SMTPHOSTAUTH;
$mail->Username = SMTPHOSTUSERNAME;
$mail->Password = SMTPHOSTPASSWORD;
$mail->SMTPSecure = 'tls';
$mail->Port = SMTPHOSTPORT;
$mail->setFrom(SMTPHOSTSENDEREMAIL, SMTPHOSTSENDERNAME);
$mail->addReplyTo(SMTPHOSTSENDEREMAIL, SMTPHOSTSENDERNAME);
$mail->isHTML(true);
$mail->addAddress($email, $username);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->send();
}
// class object constructor
function __construct($registration_callback=false)
{
// session management comes first. Warnings are repressed with @ because it will warn if something else already called session_start()
@session_start();
if(!isset($_COOKIE['Organizr'])) {
if (empty($_SESSION["username"]) || empty($_SESSION["token"])) $this->resetSession();
}else{
$_SESSION["username"] = $_COOKIE['OrganizrU'];
}
// file location for the user database
$dbfile = DATABASE_LOCATION.'users.db';
// do we need to build a new database?
$rebuild = false;
if(!file_exists($dbfile)) { $rebuild = true;}
// bind the database handler
$this->database = new PDO("sqlite:" . $dbfile);
// If we need to rebuild, the file will have been automatically made by the PDO call,
// but we'll still need to define the user table before we can use the database.
if($rebuild) { $this->rebuild_database($dbfile); }
// finally, process the page request.
$this->process($registration_callback);
}
// this function rebuilds the database if there is no database to work with yet
function rebuild_database($dbfile)
{
$this->info("creating/rebuilding database as ".$dbfile);
$this->database->beginTransaction();
$create = "CREATE TABLE users (username TEXT UNIQUE, password TEXT, email TEXT UNIQUE, token TEXT, role TEXT, active TEXT, last TEXT);";
$this->database->exec($create);
$this->database->commit();
}
// process a page request
function process(&$registration_callback=false)
{
$this->database->beginTransaction();
if(isset($_POST["op"]))
{
$operation = $_POST["op"];
// logging in or out, and dropping your registration, may change authentication status
if($operation == "login") { $this->authenticated = $this->login(); }
// logout and unregister will unset authentication if successful
elseif($operation == "logout") { $this->authenticated = !$this->logout(); }
elseif($operation == "unregister") { $this->authenticated = !$this->unregister(); }
// anything else won't change authentication status.
elseif($operation == "register") { $this->register($registration_callback); }
elseif($operation == "update") { $this->update(); }
// we only allow password resetting if we can send notification mails
elseif($operation == "reset" && User::use_mail) { $this->reset_password(); }
}
// if the previous operations didn't authorise the current user,
// see if they're already marked as authorised in the database.
if(!$this->authenticated) {
$username = $_SESSION["username"];
if($username != User::GUEST_USER) {
$this->authenticated = $this->authenticate_user($username,"");
if($this->authenticated) { $this->mark_user_active($username); }}}
// at this point we can make some globals available.
$this->username = $_SESSION["username"];
$this->userdir = ($this->username !=User::GUEST_USER? USER_HOME . $this->username : false);
$this->email = $this->get_user_email($this->username);
$this->role = $this->get_user_role($this->username);
//$this->group = $this->get_user_group($this->username);
// clear database
$this->database->commit();
$this->database = null;
}
// ---------------------
// validation passthroughs
// ---------------------
/**
* Called when the requested POST operation is "login"
*/
function login()
{
// get relevant values
$username = $_POST["username"];
$sha1 = $_POST["sha1"];
$password = $_POST["password"];
$rememberMe = $_POST["rememberMe"];
// step 1: someone could have bypassed the javascript validation, so validate again.
if(!$this->validate_user_name($username)) {
$this->info("<strong>log in error:</strong> user name did not pass validation");
return false; }
if(preg_match(User::sha1regexp, $sha1)==0) {
$this->info("<strong>log in error:</strong> password did not pass validation");
return false; }
// step 2: if validation passed, log the user in
return $this->login_user($username, $sha1, $rememberMe == "true", $password);
}
/**
* Called when the requested POST operation is "logout"
*/
function logout()
{
// get relevant value
$username = $_POST["username"];
// step 1: validate the user name.
if(!$this->validate_user_name($username)) {
$this->info("<strong>log in error:</strong> user name did not pass validation");
return false; }
// step 2: if validation passed, log the user out
return $this->logout_user($username);
}
/**
* Users should always have the option to unregister
*/
function unregister()
{
// get relevant value
$username = $_POST["username"];
// step 1: validate the user name.
if(!$this->validate_user_name($username)) {
$this->info("<strong>unregistration error:</strong> user name did not pass validation");
return false; }
// step 2: if validation passed, drop the user from the system
return $this->unregister_user($username);
}
/**
* Called when the requested POST operation is "register"
*/
function register(&$registration_callback=false)
{
// get relevant values
$username = $_POST["username"];
$email = $_POST["email"];
$sha1 = $_POST["sha1"];
$settings = $_POST["settings"];
// step 1: someone could have bypassed the javascript validation, so validate again.
if(!$this->validate_user_name($username)) {
$this->info("<strong>registration error:</strong> user name did not pass validation");
return false; }
if(preg_match(User::emailregexp, $email)==0) {
$this->info("<strong>registration error:</strong> email address did not pass validation");
return false; }
if(preg_match(User::sha1regexp, $sha1)==0) {
$this->info("<strong>registration error:</strong> password did not pass validation");
return false; }
// step 2: if validation passed, register user
$registered = $this->register_user($username, $email, $sha1, $registration_callback, $settings);
if($registered && User::use_mail)
{
// send email notification
$from = User::MAILER_NAME;
$replyto = User::MAILER_REPLYTO;
$domain_name = User::DOMAIN_NAME;
$subject = User::DOMAIN_NAME . " registration";
$body = <<<EOT
Hi,
this is an automated message to let you know that someone signed up at $domain_name with the user name "$username", using this email address as mailing address.
Because of the way our user registration works, we have no idea which password was used to register this account (it gets one-way hashed by the browser before it is sent to our user registration system, so that we don't know your password either), so if you registered this account, hopefully you wrote your password down somewhere.
However, if you ever forget your password, you can click the "I forgot my password" link in the log-in section for $domain_name and you will be sent an email containing a new, ridiculously long and complicated password that you can use to log in. You can change your password after logging in, but that's up to you. No one's going to guess it, or brute force it, but if other people can read your emails, it's generally a good idea to change passwords.
If you were not the one to register this account, you can either contact us the normal way or —much easier— you can ask the system to reset the password for the account, after which you can simply log in with the temporary password and delete the account. That'll teach whoever pretended to be you not to mess with you!
Of course, if you did register it yourself, welcome to $domain_name!
- the $domain_name team
EOT;
$headers = "From: $from\r\n";
$headers .= "Reply-To: $replyto\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
//mail($email, $subject, $body, $headers);
$this->startEmail($email, $username, $subject, $body);
}
return $registered;
}
/**
* Called when the requested POST operation is "update"
*/
function update()
{
// get relevant values
@$username = trim($_POST["username"]);
@$email = trim($_POST["email"]);
@$sha1 = trim($_POST["sha1"]);
@$role = trim($_POST["role"]);
// step 1: someone could have bypassed the javascript validation, so validate again.
if($email !="" && preg_match(User::emailregexp, $email)==0) {
$this->info("<strong>registration error:</strong> email address did not pass validation");
return false; }
if($sha1 !="" && preg_match(User::sha1regexp, $sha1)==0) {
$this->info("<strong>registration error:</strong> password did not pass validation");
return false; }
// step 2: if validation passed, update the user's information
return $this->update_user($username, $email, $sha1, $role);
}
/**
* Reset a user's password
*/
function reset_password()
{
// get the email for which we should reset
$email = $_POST["email"];
// step 1: someone could have bypassed the javascript validation, so validate again.
if(preg_match(User::emailregexp, $email)==0) {
$this->info("email address did not pass validation");
return false; }
// step 2: if validation passed, see if there is a matching user, and reset the password if there is
$newpassword = $this->random_ascii_string(64);
$sha1 = sha1($newpassword);
$query = "SELECT username, token FROM users WHERE email = '$email'";
$username = "";
$token = "";
foreach($this->database->query($query) as $data) { $username = $data["username"]; $token = $data["token"]; break; }
// step 2a: if there was no user to reset a password for, stop.
if($username == "" || $token == "") return false;
// step 2b: if there was a user to reset a password for, reset it.
$dbpassword = $this->token_hash_password($username, $sha1, $token);
$update = "UPDATE users SET password = '$dbpassword' WHERE email= '$email'";
$this->database->exec($update);
$this->info("Email has been sent with new password");
// step 3: notify the user of the new password
$from = User::MAILER_NAME;
$replyto = User::MAILER_REPLYTO;
$domain_name = User::DOMAIN_NAME;
$subject = User::DOMAIN_NAME . " password reset request";
$body = <<<EOT
Hi,
this is an automated message to let you know that someone requested a password reset for the $domain_name user account with user name "$username", which is linked to this email address.
We've reset the password to the following 64 character string, so make sure to copy/paste it without any leading or trailing spaces:
$newpassword
If you didn't even know this account existed, now is the time to log in and delete it. How dare people use your email address to register accounts! Of course, if you did register it yourself, but you didn't request the reset, some jerk is apparently reset-spamming. We hope he gets run over by a steam shovel driven by rabid ocelots or something.
Then again, it's far more likely that you did register this account, and you simply forgot the password so you asked for the reset yourself, in which case: here's your new password, and thank you for your patronage at $domain_name!
- the $domain_name team
EOT;
$headers = "From: $from\r\n";
$headers .= "Reply-To: $replyto\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
//mail($email, $subject, $body, $headers);
$this->startEmail($email, $username, $subject, $body);
}
// ------------------
// specific functions
// ------------------
// session management: set session values
function setSession($username, $token)
{
$_SESSION["username"]=$username;
$_SESSION["token"]=$token;
}
// session management: reset session values
function resetSession()
{
$_SESSION["username"] = User::GUEST_USER;
$_SESSION["token"] = -1;
unset($_COOKIE['Organizr']);
setcookie('Organizr', '', time() - 3600, '/', DOMAIN);
setcookie('Organizr', '', time() - 3600, '/');
unset($_COOKIE['OrganizrU']);
setcookie('OrganizrU', '', time() - 3600, '/', DOMAIN);
setcookie('OrganizrU', '', time() - 3600, '/');
unset($_COOKIE['cookiePassword']);
setcookie("cookiePassword", '', time() - 3600, '/', DOMAIN);
setcookie("cookiePassword", '', time() - 3600, '/');
}
/**
* Validate a username. Empty usernames or names
* that are modified by making them SQL safe are
* considered not validated.
*/
function validate_user_name($username)
{
$cleaned = $this->clean_SQLite_string($username);
$validated = ($cleaned != "" && $cleaned==$username);
if(!$validated) { $this->error = "user name did not pass validation."; $this->error("user name did not pass validation."); }
return $validated;
}
/**
* Clean strings for SQL insertion as string in SQLite (single quote enclosed).
* Note that if the cleaning changes the string, this system won't insert.
* The validate_user_name() function will flag this as a validation failure and
* the database operation is never carried out.
*/
function clean_SQLite_string($string)
{
$search = array("'", "\\", ";");
$replace = array('', '', '');
return trim(str_replace($search, $replace, $string));
}
/**
* Verify that the given username is allowed
* to perform the given operation.
*/
function authenticate_user($username, $operation)
{
// actually logged in?
if($this->is_user_active($username)===false) { return false; }
// logged in, but do the tokens match?
$token = $this->get_user_token($username);
if(MULTIPLELOGIN == "false"){
if(isset($_COOKIE["Organizr"])){
if($_COOKIE["Organizr"] == $token){
return true;
}else{
$this->error("cookie token mismatch for $username");
unset($_COOKIE['Organizr']);
setcookie('Organizr', '', time() - 3600, '/', DOMAIN);
setcookie('Organizr', '', time() - 3600, '/');
unset($_COOKIE['OrganizrU']);
setcookie('OrganizrU', '', time() - 3600, '/', DOMAIN);
setcookie('OrganizrU', '', time() - 3600, '/');
unset($_COOKIE['cookiePassword']);
setcookie("cookiePassword", '', time() - 3600, '/', DOMAIN);
setcookie("cookiePassword", '', time() - 3600, '/');
return false;
}
}else{
if($token != $_SESSION["token"]) {
$this->error("token mismatch for $username");
return false;
}
// active, using the correct token -> authenticated
setcookie("cookiePassword", COOKIEPASSWORD, time() + (86400 * 7), "/", DOMAIN);
return true;
}
}else{
setcookie("cookiePassword", COOKIEPASSWORD, time() + (86400 * 7), "/", DOMAIN);
return true;
}
}
/**
* Unicode friendly(ish) version of strtolower
* see: http://ca3.php.net/manual/en/function.strtolower.php#91805
*/
function strtolower_utf8($string)
{
$convert_to = array( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï",
"ð", "ñ", "ò", "ó", "ô", "õ", "ö", "ø", "ù", "ú", "û", "ü", "ý", "а", "б", "в", "г", "д", "е", "ё", "ж",
"з", "и", "й", "к", "л", "м", "н", "о", "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ы",
"ь", "э", "ю", "я" );
$convert_from = array( "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï",
"Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "А", "Б", "В", "Г", "Д", "Е", "Ё", "Ж",
"З", "И", "Й", "К", "Л", "М", "Н", "О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ъ",
"Ь", "Э", "Ю", "Я" );
return str_replace($convert_from, $convert_to, $string);
}
/**
* This functions flattens user name strings for similarity comparison purposes
*/
function homogenise_username($string)
{
// cut off trailing numbers
$string = preg_replace("/\d+$/", '', $string);
// and then replace non-terminal numbers with
// their usual letter counterparts.
$s = array("1","3","4","5","7","8","0");
$r = array("i","e","a","s","t","ate","o");
$string = str_replace($s, $r, $string);
// finally, collapse case
return $this->strtolower_utf8($string);
}
/**
* We don't require assloads of personal information.
* A username and a password are all we want. The rest
* is profile information that can be set, but in no way
* needs to be, in the user's profile section
*/
function register_user($username, $email, $sha1, &$registration_callback = false, $settings)
{
$dbpassword = $this->token_hash_password($username, $sha1, "");
if($dbpassword==$sha1) die("password hashing is not implemented.");
$newRole = "admin";
$queryAdmin = "SELECT username FROM users";
foreach($this->database->query($queryAdmin) as $data) {
$newRole = "user";
}
// Does user already exist? (see notes on safe reporting)
if(User::unsafe_reporting) {
$query = "SELECT username FROM users WHERE username LIKE '$username'";
foreach($this->database->query($query) as $data) {
$this->info("user account for $username not created.");
$this->error = "this user name is already being used by someone else.";
$this->error("this user name is already being used by someone else.");
return false; }
} else {
$query = "SELECT username FROM users";
$usernames = array();
foreach($this->database->query($query) as $data) { $usernames[] = $this->homogenise_username($data["username"]); }
if(in_array($this->homogenise_username($username), $usernames)) {
//$this->info("user account for $username not created.");
$this->error = "<strong>$username</strong> is not allowed, because it is too similar to other user names.";
$this->error("<strong>$username</strong> is not allowed, because it is too similar to other user names.");
return false; }
}
// Is email address already in use? (see notes on safe reporting)
if (isset($email) && $email) {
$query = "SELECT * FROM users WHERE email = '$email'";
foreach($this->database->query($query) as $data) {
$this->info("user account for $username not created.");
$this->error = "this email address is already in use by someone else.";
$this->error("this email address is already in use by someone else.");
return false;
}
} else {
$email = $this->random_ascii_string(32).'@placeholder.eml';
}
// This user can be registered
$insert = "INSERT INTO users (username, email, password, token, role, active, last) ";
$insert .= "VALUES ('$username', '$email', '$dbpassword', '', '$newRole', 'false', '') ";
$this->database->exec($insert);
$query = "SELECT * FROM users WHERE username = '$username'";
foreach($this->database->query($query) as $data) {
$this->info("created user account for $username");
$this->update_user_token($username, $sha1, false);
// make the user's data directory
$dir = USER_HOME . $username;
if(!mkdir($dir, 0760, true)) { $this->error("could not make user directory $dir"); return false; }
//$this->info("created user directory $dir");
// if there is a callback, call it
if($registration_callback !== false) { $registration_callback($username, $email, $dir); }
if($settings !== 'true' && $settings !== true) { $this->login_user($username, $sha1, true, '', false); }
return true; }
$this->error = "unknown database error occured.";
$this->error("unknown database error occured.");
return false;
}
/**
* Log a user in
*/
function login_user($username, $sha1, $remember, $password, $surface = true) {
$buildLog = function($username, $authType) {
if(file_exists(FAIL_LOG)) {
$getFailLog = str_replace("\r\ndate", "date", file_get_contents(FAIL_LOG));
$gotFailLog = json_decode($getFailLog, true);
}
$failLogEntryFirst = array('logType' => 'login_log', 'auth' => array(array('date' => date("Y-m-d H:i:s"), 'username' => $username, 'ip' => $_SERVER['REMOTE_ADDR'], 'auth_type' => $authType)));
$failLogEntry = array('date' => date("Y-m-d H:i:s"), 'username' => $username, 'ip' => $_SERVER['REMOTE_ADDR'], 'auth_type' => $authType);
if(isset($gotFailLog)) {
array_push($gotFailLog["auth"], $failLogEntry);
$writeFailLog = str_replace("date", "\r\ndate", json_encode($gotFailLog));
} else {
$writeFailLog = str_replace("date", "\r\ndate", json_encode($failLogEntryFirst));
}
return $writeFailLog;
};
// External Authentication
$authSuccess = false;
$function = 'plugin_auth_'.AUTHBACKEND;
switch (AUTHTYPE) {
case 'external':
if (function_exists($function)) {
$authSuccess = $function($username, $password);
}
break;
case 'both':
if (function_exists($function)) {
$authSuccess = $function($username, $password);
}
default: // Internal
if (!$authSuccess) {
// perform the internal authentication step
$query = "SELECT password FROM users WHERE username = '$username'";
foreach($this->database->query($query) as $data) {
if (password_verify($password, $data["password"])) { // Better
$authSuccess = true;
} else {
// Legacy - Less Secure
$dbpassword = $this->token_hash_password($username, $sha1, $this->get_user_token($username));
if($dbpassword==$data["password"]) {
$authSuccess = true;
}
}
}
}
}
if ($authSuccess) {
// Make sure user exists in database
$query = "SELECT username FROM users WHERE username = '$username'";
$userExists = false;
foreach($this->database->query($query) as $data) {
if ($data['username'] == $username) {
$userExists = true;
break;
}
}
if ($userExists) {
// authentication passed - 1) mark active and update token
$this->mark_user_active($username);
$this->setSession($username, $this->update_user_token($username, $sha1, false));
// authentication passed - 2) signal authenticated
if($remember == "true") {
setcookie("Organizr", $this->get_user_token($username), time() + (86400 * 7), "/", DOMAIN);
setcookie("OrganizrU", $username, time() + (86400 * 7), "/", DOMAIN);
}
$this->info("Welcome $username");
file_put_contents(FAIL_LOG, $buildLog($username, "good_auth"));
chmod(FAIL_LOG, 0660);
setcookie("cookiePassword", COOKIEPASSWORD, time() + (86400 * 7), "/", DOMAIN);
return true;
} else if (AUTHBACKENDCREATE !== 'false' && $surface) {
// Create User
$falseByRef = false;
$this->register_user($username, "", $sha1, $falseByRef, !$remember);
} else {
// authentication failed
//$this->info("Successful Backend Auth, No User in DB, Create Set to False");
file_put_contents(FAIL_LOG, $buildLog($username, "bad_auth"));
chmod(FAIL_LOG, 0660);
if(User::unsafe_reporting) { $this->error = "Successful Backend Auth, $username not in DB, Create Set to False."; $this->error("Successful Backend Auth, $username not in DB, Create Set to False."); }
else { $this->error = "Not permitted to login as this user, please contact an administrator."; $this->error("Not permitted to login as this user, please contact an administrator"); }
return false;
}
} else if (!$authSuccess) {
// authentication failed
//$this->info("password mismatch for $username");
file_put_contents(FAIL_LOG, $buildLog($username, "bad_auth"));
chmod(FAIL_LOG, 0660);
if(User::unsafe_reporting) { $this->error = "incorrect password for $username."; $this->error("incorrect password for $username."); }
else { $this->error = "the specified username/password combination is incorrect."; $this->error("the specified username/password combination is incorrect."); }
return false;
} else {
// authentication could not take place
//$this->info("there was no user $username in the database");
file_put_contents(FAIL_LOG, $buildLog($username, "bad_auth"));
chmod(FAIL_LOG, 0660);
if(User::unsafe_reporting) { $this->error = "user $username is unknown."; $this->error("user $username is unknown."); }
else { $this->error = "you either did not correctly input your username, or password (... or both)."; $this->error("you either did not correctly input your username, or password (... or both)."); }
return false;
}
}
/**
* Update a user's information
*/
function update_user($username, $email, $sha1, $role)
{
if($email !="") {
$update = "UPDATE users SET email = '$email' WHERE username = '$username'";
$this->database->exec($update); }
if($role !="") {
$update = "UPDATE users SET role = '$role' WHERE username = '$username'";
$this->database->exec($update); }
if($sha1 !="") {
$dbpassword = $this->token_hash_password($username, $sha1, $this->get_user_token($username));
$update = "UPDATE users SET password = '$dbpassword' WHERE username = '$username'";
$this->database->exec($update); }
$this->info("updated the information for <strong>$username</strong>");
}
/**
* Log a user out.
*/
function logout_user($username)
{
$update = "UPDATE users SET active = 'false' WHERE username = '$username'";
$this->database->exec($update);
$this->resetSession();
$this->info("Buh-Bye <strong>$username</strong>!");
unset($_COOKIE['Organizr']);
setcookie('Organizr', '', time() - 3600, '/', DOMAIN);
setcookie('Organizr', '', time() - 3600, '/');
unset($_COOKIE['OrganizrU']);
setcookie('OrganizrU', '', time() - 3600, '/', DOMAIN);
setcookie('OrganizrU', '', time() - 3600, '/');
unset($_COOKIE['cookiePassword']);
setcookie("cookiePassword", '', time() - 3600, '/', DOMAIN);
setcookie("cookiePassword", '', time() - 3600, '/');
return true;
}
/**
* Drop a user from the system
*/
function unregister_user($username)
{
$delete = "DELETE FROM users WHERE username = '$username'";
$this->database->exec($delete);
$this->info("<strong>$username</strong> has been kicked out of Organizr");
//$this->resetSession();
$dir = USER_HOME . $username;
if(!rmdir($dir)) { $this->error("could not delete user directory $dir"); }
$this->info("and we deleted user directory $dir");
return true;
}
/**
* The incoming password will already be a sha1 print (40 bytes) long,
* but for the database we want it to be hased as sha256 (using 64 bytes).
*/
function token_hash_password($username, $sha1, $token)
{
return hash("sha256", $username . $sha1 . $token);
}
/**
* Get a user's email address
*/
function get_user_email($username)
{
if($username && $username !="" && $username !=User::GUEST_USER) {
$query = "SELECT email FROM users WHERE username = '$username'";
foreach($this->database->query($query) as $data) { return $data["email"]; }}
return "";
}
/**
* Get a user's role
*/
function get_user_role($username)
{
if($username && $username !="" && $username !=User::GUEST_USER) {
$query = "SELECT role FROM users WHERE username = '$username'";
foreach($this->database->query($query) as $data) { return $data["role"]; }}
return User::GUEST_USER;
}
/* function get_user_group($username)
{
if($username && $username !="" && $username !=User::GUEST_USER) {
$query = "SELECT group FROM users WHERE username = '$username'";
foreach($this->database->query($query) as $data) { return $data["group"]; }}
return User::GUEST_USER;
}*/
/**
* Get the user token
*/
function get_user_token($username)
{
$query = "SELECT token FROM users WHERE username = '$username'";
foreach($this->database->query($query) as $data) { return $data["token"]; }
return false;
}
/**
* Update the user's token and password upon successful login
*/
function update_user_token($username, $sha1, $noMsg)
{
// update the user's token
$token = $this->random_hex_string(32);
$update = "UPDATE users SET token = '$token' WHERE username = '$username'";
$this->database->exec($update);
// update the user's password
$newpassword = $this->token_hash_password($username, $sha1, $token);
$update = "UPDATE users SET password = '$newpassword' WHERE username = '$username'";
$this->database->exec($update);
if($noMsg == "false"){
$this->info("token and password updated for <strong>$username</strong>");
}
return $token;
}
/**
* Mark a user as active.
*/
function mark_user_active($username)
{
$update = "UPDATE users SET active = 'true', last = '" . time() . "' WHERE username = '$username'";
$this->database->exec($update);
//$this->info("$username has been marked currently active.");
return true;
}
/**
* Check if user can be considered active
*/
function is_user_active($username)
{
$last = 0;
$active = "false";
$query = "SELECT last, active FROM users WHERE username = '$username'";
foreach($this->database->query($query) as $data) {
$last = intval($data["last"]);
$active = $data["active"];
break; }
if($active=="true") {
$diff = time() - $last;
if($diff >= User::time_out) {
$this->logout_user($username);
$this->error("$username was active but timed out (timeout set at " . User::time_out . " seconds, difference was $diff seconds)");
return false; }
//$this->info("$username is active");
return true; }
$this->error("<strong>$username</strong> is not active");
$this->resetSession();
return false;
}
/**
* Random hex string generator
*/
function random_hex_string($len)
{
$string = "";
$max = strlen($this->hex)-1;
while($len-->0) { $string .= $this->hex[mt_rand(0, $max)]; }
return $string;
}
/**
* Random password string generator
*/
function random_ascii_string($len)
{
$string = "";
$max = strlen($this->ascii)-1;
while($len-->0) { $string .= $this->ascii[mt_rand(0, $max)]; }
return $string;
}
}
?>