-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.tmp_User.php.11489~
executable file
·1466 lines (1272 loc) · 37.4 KB
/
.tmp_User.php.11489~
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
/* SVN FILE: $Id: User.php 232 2009-05-04 03:09:46Z david@ramaboo.com $ */
/**
* @brief User class.
*
* This class is used to create and manipulate users.
*
* @class Boo_User
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU General Public License
* @link http://code.ramaboo.com/ Boo Framework
* @copyright 2008 David Singer
* @author David Singer <david@ramaboo.com>
* @version 1.9.5
*/
class Boo_User extends Boo_Io {
/**
* @brief Table name.
*
* Used for static functions.
* For non static functions see Boo_Db::setTableName().
*/
const TABLE_NAME = 'boo_users';
/**
* @brief Primary key.
*
* Used for static functions.
* For non static functions see Boo_Db::setPrimaryKey().
*/
const PRIMARY_KEY = 'user_id';
/**
* @brief Maximum length of username.
*/
const MAX_USERNAME_LENGTH = 32;
/**
* @brief Minimum length of username.
*/
const MIN_USERNAME_LENGTH = 4;
/**
* @brief Default constructor.
*
* @param int $user[optional] User to open.
* @return void.
*/
public function __construct($user = false) {
parent::__construct(self::TABLE_NAME, self::PRIMARY_KEY);
// defaults
$this->setIgnoredField('password');
$this->setMagicFields(array('created', 'modified', 'ip'));
$this->setSerializedField('settings');
$this->setStatus(BOO_STATUS_OK);
$this->setGroupId(BOO_GROUP_USER);
$this->setCountryCode('US');
if ($user !== false) {
$this->open($user);
}
}
public function hasAccount() {
return (bool) $this->getAccountId();
}
public function openAccount($accountId = false) {
if ($accountId === false) {
$accountId = $this->getAccountId();
}
if ($accountId === false) {
trigger_error('Account ID is false, user does not have an account', E_USER_NOTICE);
return false;
}
if (class_exists('Account', false) && BOO_CLASS) {
$this->account = new Account;
} else {
$this->account = new Boo_Account;
}
$result = $this->account->open($accountId);
return $result;
}
/**
* @brief Sets the users group ID.
*
* @param int $groupId The group ID.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setGroupId($groupId) {
$groupId = (int) $groupId;
if (Boo_Group::isGroupId($groupId)) {
return $this->set('group_id', $groupId);
} else {
trigger_error("Group ID {$groupId} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users group ID.
*
* @return int Returns the users group ID if set, FALSE otherwise.
*/
public function getGroupId() { return $this->get('group_id'); }
/**
* @brief Sets the users account ID.
*
* @param int $accountId The account id.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setAccountId($accountId) {
$accountId = (int) $accountId;
if (Boo_Account::isAccountId($accountId)) {
return $this->set('account_id', $accountId);
} else {
trigger_error("Account ID {$accountId} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users account ID.
*
* @return int Returns the users account ID if set, FALSE otherwise.
*/
public function getAccountId() { return $this->get('account_id'); }
/**
* @brief Set users status.
*
* Possible values are:
* \li \c BOO_STATUS_OK
* \li \c BOO_STATUS_CLOSED
* \li \c BOO_STATUS_SUSPENDED
* \li \c BOO_STATUS_DISABLED
* \li \c BOO_STATUS_PENDING
* \li \c BOO_STATUS_UNKNOWN
*
* You may use other integer values of your own. Statuses that indicate
* a user is in good standing (and allowed to login) should be greater than or equal to 1. Statuses
* that indicate a users account is closed, suspended, or otherwise inaccesable (and not allowed to login) be less
* than or equal to 0.
*
* @param int $status User status.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setStatus($status) {
$status = (int) $status;
return $this->set('status', $status);
}
/**
* @brief Gets the users status.
* @return int The users status, FALSE on failure.
*/
public function getStatus() { return $this->get('status'); }
/**
* @breif Determins if a users status is OK.
* @return bool Returns TRUE if user is OK, FALSE otherwise.
* @warning This function will return TRUE for any positive status not just \c BOO_STATUS_OK.
*/
public function isStatusOK() { return ($this->get('status') >= 1); }
/**
* @brief Sets users private status.
*
* @param bool $opt Private status.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setPrivate($opt) {
$private = (bool) $private;
return $this->set('private', $opt);
}
/**
* @brief Determins if a users profile is private.
*
* @return bool Returns TRUE if private, FALSE otherwise.
*/
public function isPrivate() { return (bool) $this->get('private'); }
/**
* @brief Sets the users website.
*
* @param string $website The website.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setWebsite($website) {
$website = Boo_Format::formatURL($website);
if (Boo_Validator::isUrl($website)) {
return $this->set('website', $website);
} else {
trigger_error("Website {$website} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users website.
*
* @return string Returns the users website if set, FALSE otherwise.
*/
public function getWebsite() { return $this->get('website'); }
/**
* @brief Gets the users website as a HTML anchor.
*
* @param array $attrs[optional] Array of attributes.
* @return Boo_Html The users link as a HTML anchor, FALSE on failure.
*/
public function htmlWebsiteA(array $attrs = array()) {
$website = $this->get('website');
if ($website) {
$a = new Boo_Html_A;
$a->applyAttrs($attrs);
$a->setAttr('href', Boo_Format::formatURL($website));
$a->setContent($website);
return $a;
} else {
return false;
}
}
/**
* @brief Sets the users first name.
*
* @param string $name The first name.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setFirstName($name) {
$name = strtodb($name);
if (Boo_Validator::isName($name)) {
return $this->set('first_name', $name);
} else {
trigger_error("First name {$name} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users first name.
*
* @return string Returns the users first name if set, FALSE otherwise.
*/
public function getFirstName() { return $this->get('first_name'); }
/**
* @brief Sets the users last name.
*
* @param string $name The last name.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setLastName($name) {
$name = strtodb($name);
if (Boo_Validator::isName($name)) {
return $this->set('last_name', $name);
} else {
trigger_error("Last name {$name} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users last name.
*
* @return string Returns the users last name if set, FALSE otherwise.
*/
public function getLastName() { return $this->get('last_name'); }
/**
* @brief Gets the users name.
*
* Formated as first name space last name if both are present, otherwise just the
* name that is set.
*
* @return string Returns the users name if set, FALSE otherwise.
*/
public function getName() {
$name = trim($this->get('first_name') . ' ' . $this->get('last_name')); // removes extra spaces
if ($name) {
return $name;
} else {
return false;
}
}
/**
* @brief Sets the users company.
*
* @param string $company The company.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setCompany($company) {
$company = strtodb($company);
if (Boo_Validator::isString($company, 2, 32)) {
return $this->set('company', $company);
} else {
trigger_error("Company {$company} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users company.
*
* @return string Returns the users company if set, FALSE otherwise.
*/
public function getCompany() { return $this->get('company'); }
/**
* @brief Sets the users street.
*
* @param string $street The street.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setStreet($street) {
$street = strtodb($street);
if (Boo_Validator::isStreet($street)) {
return $this->set('street', $street);
} else {
trigger_error("Street {$street} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users street.
*
* @return string Returns the users street if set, FALSE otherwise.
*/
public function getStreet() { return $this->get('street'); }
/**
* @brief Sets the users apartment number.
*
* @param string $apt The apartment number.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setApartmentNumber($apt) {
$apt = strtodb($apt);
if (Boo_Validator::isApartmentNumber($apt)) {
return $this->set('apartment_number', $apt);
} else {
trigger_error("Apartment number {$apt} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users apartment number.
*
* @return string Returns the users apartment number if set, FALSE otherwise.
*/
public function getApartmentNumber() { return $this->get('apartment_number'); }
/**
* @brief Determins if user has an apartment number.
*
* @attention Zero is a valid apartment number.
* @return bool Returns TRUE if apartment number is present, FALSE otherwise.
*/
public function hasApartmentNumber() { return !Boo_Validator::isNull($this->getApartmentNumber()); }
/**
* @brief Sets the users city.
*
* @param string $city The city.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setCity($city) {
$city = strtodb($city);
if (Boo_Validator::isCity($city)) {
return $this->set('city', $street);
} else {
trigger_error("City {$city} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users city.
*
* @return string Returns the users city if set, FALSE otherwise.
*/
public function getCity() { return $this->get('city'); }
/**
* @brief Sets the users state.
*
* @param string $state The state as a state code or state name.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setState($state) {
$state = trim($state);
if (!$state) {
trigger_error('State can not be false', E_USER_WARNING);
return false;
}
if (Boo_Validator::isStateCode($state)) {
// $state is a state code
return $this->setStateCode($state);
} elseif (Boo_Validator::isStateName($state)) {
// $state is a state name
return $this->setStateName($state);
} else {
// neither code or name
trigger_error("State {$state} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Sets the users state.
*
* @param string $state The state as a state name.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setStateName($name) {
$name = trim($name);
if (Boo_Validator::isStateName($name)) {
// $state is a state name
return $this->set('state_code', Boo_Helper::getStateCodeByName($state));
} else {
// neither a code or name
trigger_error("State name {$name} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Sets the users state code.
*
* State code is the two letter abbreviation.
*
* @see Boo_Validator::$states.
* @param string $code The state code.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setStateCode($code) {
$code = strtoupper(trim($code));
if (Boo_Validator::isStateCode($code)) {
return $this->set('state_code', $code);
} else {
// not a valid code
trigger_error("State code {$code} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users state code.
*
* Provides API compatibility.
*
* @see Boo_Validator::$states.
* @return string State code in strtoupper.
*/
public function getState() { return $this->get('state_code'); }
/**
* @brief Gets the users state code.
*
* @see Boo_Validator::$states.
* @return string State code in strtoupper.
*/
public function getStateCode() { return $this->get('state_code'); }
/**
* @brief Gets the users state name.
*
* @see Boo_Validator::$states.
* @return string State name.
*/
public function getStateName() { return Boo_Helper::getStateNameByCode($this->get('state_code')); }
/**
* @brief Sets the users country.
*
* @param string $country The country as a country code or country name.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setCountry($country) {
$country = trim($country);
if (!$country) {
if (BOO_WARNING) { trigger_error('Country can not be false', E_USER_WARNING); }
return false;
}
if (Boo_Validator::isCountryCode($country)) {
// $country is a country code
return $this->setCountryCode($country);
} elseif (Boo_Validator::isCountryName($country)) {
// $country is a country name
return $this->setCountryName($country);
} else {
// neither code or name
trigger_error("Country {$country} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Sets the users country.
*
* @param string $country The country as a country name.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setCountryName($name) {
$name = trim($name);
if (Boo_Validator::isCountryName($name)) {
// $country is a country name
return $this->set('country_code', Boo_Helper::getCountryCodeByName($country));
} else {
// neither a code or name
trigger_error("Country name {$name} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Sets the users country code.
*
* Country code is the two letter abbreviation.
*
* @see Boo_Validator::$countries.
* @param string $code The country code.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setCountryCode($code) {
$code = strtoupper(trim($code));
if (Boo_Validator::isCountryCode($code)) {
return $this->set('country_code', $code);
} else {
// not a valid code
trigger_error("Country code {$code} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users country code.
*
* Provides API compatibility.
*
* @see Boo_Validator::$countries.
* @return string Country code in strtoupper.
*/
public function getCountry() { return $this->get('country_code'); }
/**
* @brief Gets the users country code.
*
* @see Boo_Validator::$countries.
* @return string Country code in strtoupper.
*/
public function getCountryCode() { return $this->get('country_code'); }
/**
* @brief Gets the users country name.
*
* @see Boo_Validator::$countries.
* @return string Country name.
*/
public function getCountryName() { return Boo_Helper::getCountryNameByCode($this->get('country_code')); }
/**
* @brief Sets the users ZIP code.
*
* @param string $zip The ZIP code.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setZip($zip) {
$zip = Boo_Format::formatZip($zip);
if (Boo_Validator::isZip($zip)) {
return $this->set('zip', $zip);
} else {
trigger_error("ZIP {$zip} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users ZIP code.
*
* @return string ZIP code.
*/
public function getZip() { return $this->get('zip'); }
/**
* @brief Sets the users carrier ID.
*
* @see Boo_SMS
* @param int $carrierId The carrier ID.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setCarrierId($carrierId) {
$carrierId = (int) $carrierId;
if (Boo_SMS::isCarrierId($carrierId)) {
return $this->set('carrier_id', $carrierId);
} else {
trigger_error("Carrier ID {$carrierId} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users carrier ID.
*
* @return int The carrier ID.
*/
public function getCarrierId() { return $this->carrierId; }
/**
* @brief Sets the users SMS number.
*
* @param string $smsNumber The SMS number
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setSmsNumber($smsNumber) {
$smsNumber = Boo_Format::formatPhoneNumber($smsNumber);
if (Boo_Validator::isPhoneNumber($smsNumber)) {
return $this->set('sms_number', $smsNumber);
} else {
trigger_error("SMS number {$smsNumber} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users SMS number.
*
* @return string The SMS number if available, empty string otherwise.
*/
public function getSmsNumber() { return ($this->get('sms_number')) ? $this->get('sms_number') : ''; }
/**
* @brief Sets the users fax number.
*
* @param string $faxNumber The fax number
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setFaxNumber($faxNumber) {
$faxNumber = Boo_Fromat::formatPhoneNumber($faxNumber);
if (Boo_Validator::isPhoneNumber($faxNumber)) {
return $this->set('fax_number', $faxNumber);
} else {
trigger_error("Fax number {$faxNumber} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users fax number.
*
* @return string The fax number if available, empty string otherwise.
*/
public function getFaxNumber() { return ($this->get('fax_number')) ? $this->get('fax_number') : ''; }
/**
* @brief Sets the users phone number.
*
* @param string $phoneNumber The phone number
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setPhoneNumber($phoneNumber) {
$phoneNumber = Boo_Format::formatPhoneNumber($phoneNumber);
if (Boo_Validator::isPhoneNumber($phoneNumber)) {
return $this->set('phone_number', $phoneNumber);
} else {
trigger_error("Phone number {$phoneNumber} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users phone number.
*
* @return string The phone number if available, empty string otherwise.
*/
public function getPhoneNumber() { return ($this->get('phone_number')) ? $this->get('phone_number') : ''; }
/**
* @brief Gets the users ID.
*
* @return int The users ID.
*/
public function getUserId() { return (int) $this->get('user_id'); }
/**
* @brief Gets the users link as a HTML anchor.
*
* @param array $attrs[optional] Array of attributes.
* @return Boo_Html The users link as a HTML anchor, FALSE on failure.
*/
public function htmlUserA(array $attrs = array()) {
$username = $this->get('username');
if ($username) {
$a = new Boo_Html_A;
$a->applyAttrs($attrs);
$a->setAttr('href', "/user/{$username}/");
$a->setContent($username);
return $a;
} else {
trigger_error('Username can not be false', E_USER_NOTICE);
return false;
}
}
/**
* @brief Sets the users username.
*
* Setting the username to an already existing username will generate an \c E_USER_WARNING
* and return FALSE.
*
* @param string $username The username.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setUsername($username) {
$username = strtolower(trim($username));
if (self::isUsername($username)) {
trigger_error("Username {$username} already exists", E_USER_WARNING);
return false;
} else {
if (Boo_Validator::isUsername($username)) {
return $this->set('username', $username);
} else {
trigger_error("Username {$username} is not valid", E_USER_WARNING);
return false;
}
}
}
/**
* @breif Gets the users username.
*
* @return string The users usersname.
*/
public function getUsername() { return $this->get('username'); }
/**
* @brief Sets the users password.
*
* @param string $password The password.
* @return bool Returns TRUE on success, FALSE on failure.
*/
public function setPassword($password) {
$password = trim($password);
if (!Boo_Validator::isPassword($password)) {
if (BOO_WARNING) { trigger_error("Password (hidden) is not valid", E_USER_WARNING); }
return false;
}
if ($password == '********' && $this->get('password_hash')) {
// password is already set
// ignore this request
return true;
}
$result = true;
$result = $result && $this->set('password', $password);
// generate password hash
$salt = hash('md5', uniqid(rand(), true)); // 32 character salt
$result = $result && $this->set('password_hash', hash('sha256', $password . BOO_SECRET . $salt)); // 64 character password hash
$result = $result && $this->set('salt', $salt); // save salt
return $result;
}
/**
* @brief Gets the users password hash.
* @return string The password hash.
*/
public function getPasswordHash() { return $this->get('password_hash'); }
/**
* @brief Gets the users password salt.
* @return string The password salt.
*/
public function getSalt() { return $this->get('salt'); }
public function isAuthTokenValid($authToken) {
$authToken = strtolower(trim($authToken));
return $this->getAuthToken() == $authToken;
}
public function getAuthToken() {
return hash('sha256', $this->getUserId() . $this->getPasswordHash() . $this->getSalt() . BOO_SECRET);
}
public function htmlAuthA(array $attrs = array()) {
$a = new Boo_Html_A;
$a->applyAttrs($attrs);
if (BOO_SSL) {
$url = 'https://';
} else {
$url = 'http://';
}
$url .= BOO_DOMAIN . '/password/reset/' . $this->getUserId() . '/' . $this->getAuthToken() . '/';
$a->setAttr('src', $url);
return $a;
}
/**
* @brief Sets the users email address.
*
* @param string $email The email address.
* @return bool Returns TRUE on success, FALSE otherwise.
*/
public function setEmail($email) {
$email = Boo_Format::formatEmail($email);
if (Boo_Validator::isEmail($email)) {
return $this->set('email', $email);
} else {
trigger_error("Email address {$email} is not valid", E_USER_WARNING);
return false;
}
}
/**
* @brief Gets the users email address.
*
* @return string The users email address.
*/
public function getEmail() { return $this->get('email'); }
/**
* @brief Gets the users email address link.
*
* @param string $subject[optional] The subject.
* @return string The users email address link as a URL, FALSE on failure.
*/
public function getEmailLink($subject = false) {
$email = $this->get('email');
if ($email) {
$result = "mailto:{$email}";
if ($subject) {
$result .= "?subject={$subject}";
}
return $result;
} else {
return false;
}
}
/**
* @brief Gets the users email address as a HTML anchor.
*
* @param array $attrs[optional] Array of attributes.
* @param string $subject[optional] The subject.
* @return Boo_Html The users link as a HTML anchor, FALSE on failure.
*/
public function htmlEmailA(array $attrs = array(), $subject = false) {
$email = $this->get('email');
if ($email) {
$a = new Boo_Html_A;
$a->applyAttrs($attrs);
$a->setAttr('href', $this->getEmailLink($subject));
$a->addClass('email');
$a->setContent($email);
return $a;
} else {
return false;
}
}
/**
* @brief Gets the time of the users last login.
*
* @return int Unix timestamp of the last login.
*/
public function getLastLogin() { return $this->get('last_login'); }
/**
* @brief Gets the time of the users last IP.
*
* @return int Unix timestamp of the last IP used by the user.
*/
public function getLastIp() { return $this->get('last_ip'); }
/**
* @brief Authorize user.
*
* @return Returns TRUE on success, FALSE otherwise.
*/
public function auth() {
$userId = $this->getUserId();
if (!$userId) {
trigger_error('User ID cannot be zero', E_USER_NOTICE);
return false;
}
$_SESSION['boo_auth'] = true;
$_SESSION['boo_user_id'] = $userId;
$_SESSION['boo_user_id_hash'] = hash('sha256', $userId . _server('HTTP_USER_AGENT') . BOO_SECRET);
return true;
}
/**
* @brief Verifys a session hash to be sure the user ID was not changed without our knowning.
* @return bool Returns TRUE if verified, FALSE otherwise.
*/
public function verifyUserIdHash() {
if (isset($_SESSION['boo_user_id_hash']) && isset($_SESSION['boo_user_id'])) {
return $_SESSION['boo_user_id_hash'] == hash('sha256', $_SESSION['boo_user_id']
. _server('HTTP_USER_AGENT') . BOO_SECRET);
} else {
return false;
}
}
/**
* @brief Determins if a session is authorized.
*
* @return bool Returns TRUE if authorized, FALSE otherwise.
*/
public function isAuth() {
if (isset($_SESSION['boo_auth'])) {
session_regenerate_id(true);
return $this->verifyUserIdHash();
}
return false;
}
/**
* @brief Login the user.
*
* @param string $username[optional] The username. If it is not set then Boo_User::getUsername() will be used.
* @param string $password[optional] The password. If it is not set then Boo_User::getPassword() will be used.
*
* @return Returns TRUE if login was successful, FALSE otherwise.
*/
public function login($username = false, $password = false) {
$passwordHash = ''; $salt = ''; $userId = 0;
if ($username === false) {
$username = $this->getUsername();
} else {
$username = strtolower(trim($username));
}
if ($password === false) {
$password = $this->getPassword();
} else {
$password = trim($password);
}