-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathuser.class.php
More file actions
4147 lines (3679 loc) · 157 KB
/
Copy pathuser.class.php
File metadata and controls
4147 lines (3679 loc) · 157 KB
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
/**
* EfrontUser Class file
*
* @package eFront
*/
//This file cannot be called directly, only included.
if (str_replace(DIRECTORY_SEPARATOR, "/", __FILE__) == $_SERVER['SCRIPT_FILENAME']) {
exit;
}
/**
* User exceptions class
*
* @package eFront
*/
class EfrontUserException extends Exception
{
const NO_ERROR = 0;
const INVALID_LOGIN = 401;
const USER_NOT_EXISTS = 402;
const INVALID_PARAMETER = 403;
const USER_EXISTS = 404;
const DATABASE_ERROR = 405;
const USER_FILESYSTEM_ERROR = 406;
const INVALID_TYPE = 407;
const ALREADY_IN = 408;
const INVALID_PASSWORD = 409;
const USER_NOT_HAVE_LESSON = 410;
const WRONG_INPUT_TYPE = 411;
const USER_PENDING = 412;
const TYPE_NOT_EXISTS = 414;
const MAXIMUM_REACHED = 415;
const RESTRICTED_USER_TYPE = 416;
const USER_INACTIVE = 417;
const USER_NOT_LOGGED_IN = 418;
const USER_NOT_HAVE_COURSE = 419;
const GENERAL_ERROR = 499;
}
/**
* Abstract class for users
*
* @package eFront
* @abstract
*/
abstract class EfrontUser
{
/**
* Percentage above which we notify the account holder for license reasons
*/
const NOTIFY_THRESHOLD = 0.8;
/**
* A caching variable for user types
*
* @since 3.5.3
* @var array
* @access private
* @static
*/
private static $userRoles;
/**
* The basic user types.
*
* @since 3.5.0
* @var array
* @access public
* @static
*/
public static $basicUserTypes = array('student', 'professor', 'administrator');
/**
* The basic user types.
*
* @since 3.5.0
* @var array
* @access public
* @static
*/
public static $basicUserTypesTranslations = array('student' => _STUDENT, 'professor' => _PROFESSOR, 'administrator' => _ADMINISTRATOR);
/**
* The user array.
*
* @since 3.5.0
* @var array
* @access public
*/
public $user = array();
/**
* The user login.
*
* @since 3.5.0
* @var string
* @access public
*/
public $login = '';
/**
* The user groups.
*
* @since 3.5.0
* @var string
* @access public
*/
public $groups = array();
/**
* The user login.
*
* @since 3.5.0
* @var string
* @access public
*/
public $aspects = array();
/**
* Whether this user authenitactes through LDAP.
*
* @since 3.5.0
* @var boolean
* @access public
*/
public $isLdapUser = false;
/**
* The core_access sets where each user has access to
* @var array
* @since 3.5.0
* @access public
*/
public $core_access = array();
/**
* Cache for modules
* @var array
* @since 3.6.1
* @access public
*/
protected static $cached_modules = false;
/**
* Instantiate class
*
* This function instantiates a new EfrontUser sibling object based on the given
* login. If $password is set, then it verifies the given password against
* the stored one. Either the EfrontUserFactory may be used, or directly the
* EfrontX class.
* <br/>Example:
* <code>
* $user = EfrontUserFactory :: factory('jdoe'); //Use factory to instantiate user object with login 'jdoe'
* $user = EfrontUserFactory :: factory('jdoe', 'mypass'); //Use factory to instantiate user object with login 'jdoe' and perform password verification
* $user = new EfrontAdministrator('jdoe') //Instantiate administrator user object with login 'jdoe'
* </code>
*
* @param string $login The user login
* @param string $password An enrypted password to check for the user
* @since 3.5.0
* @access public
*/
function __construct($user, $password = false) {
if (!eF_checkParameter($user['login'], 'login')) {
throw new EfrontUserException(_INVALIDLOGIN.': '.$user['login'], EfrontUserException :: INVALID_LOGIN);
} else if ($password !== false && $password != $user['password']) {
throw new EfrontUserException(_INVALIDPASSWORD.': '.$user, EfrontUserException :: INVALID_PASSWORD);
}
$this -> user = $user;
$this -> login = $user['login'];
$this -> user['directory'] = G_UPLOADPATH.$this -> user['login'];
if (!is_dir($this -> user['directory'])) {
$this -> createUserFolders();
}
$this -> user['password'] == 'ldap' ? $this -> isLdapUser = true : $this -> isLdapUser = false;
//Initialize core access
$this -> coreAccess = array();
}
/**
* Creates user folders
* @since 3.6.4
* @access private
*/
private function createUserFolders() {
$user_dir = G_UPLOADPATH.$this -> user['login'].'/';
mkdir($user_dir, 0755);
mkdir($user_dir.'message_attachments/', 0755);
mkdir($user_dir.'message_attachments/Incoming/', 0755);
mkdir($user_dir.'message_attachments/Sent/', 0755);
mkdir($user_dir.'message_attachments/Drafts/', 0755);
mkdir($user_dir.'avatars/', 0755);
try {
//Create database representations for personal messages folders (it has nothing to do with filsystem database representation)
eF_insertTableDataMultiple("f_folders", array(array('name' => 'Incoming', 'users_LOGIN' => $this -> user['login']),
array('name' => 'Sent', 'users_LOGIN' => $this -> user['login']),
array('name' => 'Drafts', 'users_LOGIN' => $this -> user['login'])));
} catch(Exception $e) {}
}
/**
* Get the user's upload directory
*
* This function returns the path to the user's upload directory. The path always has a trailing
* slash at the end.
* <br/>Example:
* <code>
* $path = $user -> getDirectory(); //returns something like /var/www/efront/upload/admin/
* </code>
*
* @return string The path to the user directory
* @since 3.6.0
* @access public
*/
public function getDirectory() {
return $this -> user['directory'].'/';
}
/**
* Create new user
*
* This function is used to create a new user in the system
* The user is created based on a a properties array, in which
* the user login, name, surname and email must be present, otherwise
* an EfrontUserException is thrown. Apart from these, all the other
* user elements are optional, and defaults will be used if they are left
* blank.
* Once the database representation is created, the constructor tries to create the
* user directories, G_UPLOADPATH.'login/' and message attachments subfolders. Finally
* it assigns a default avatar to the user. The function instantiates the user based on
* its type.
* <br/>Example:
* <code>
* $properties = array('login' => 'jdoe', 'name' => 'john', 'surname' => 'doe', 'email' => '[email protected]');
* $user = EfrontUser :: createUser($properties);
* </code>
*
* @param array $userProperties The new user properties
* @param array $users The list of existing users, with logins and active properties, in the form array($login => $active). It is handy to specify when creating massively users
* @return array with new user settings if the new user was successfully created
* @since 3.5.0
* @access public
*/
public static function createUser($userProperties, $users = array(), $addToDefaultGroup = true) {
$result = eF_getTableData("users", "count(id) as total", "active=1");
$activatedUsers = $result[0]['total'];
if (!isset($userProperties['login']) || !eF_checkParameter($userProperties['login'], 'login')) {
throw new EfrontUserException(_INVALIDLOGIN.': '.$userProperties['login'], EfrontUserException :: INVALID_LOGIN);
}
$result = eF_getTableData("users", "login, archive", "login='{$userProperties['login']}'"); //collation is by default utf8_general_ci, meaning that this search is case-insensitive
if (sizeof($result) > 0) {
if ($result[0]['archive']) {
throw new EfrontUserException(_USERALREADYEXISTSARCHIVED.': '.$userProperties['login'], EfrontUserException :: USER_EXISTS);
} else {
throw new EfrontUserException(_USERALREADYEXISTS.': '.$userProperties['login'], EfrontUserException :: USER_EXISTS);
}
}
/*
$archived_keys = array_combine(array_keys($archived),array_keys($archived));
if (isset($archived_keys[mb_strtolower($userProperties['login'])])) {
//if (in_array(mb_strtolower($userProperties['login']), array_keys($archived), true) !== false) {
throw new EfrontUserException(_USERALREADYEXISTSARCHIVED.': '.$userProperties['login'], EfrontUserException :: USER_EXISTS);
}
$user_keys = array_combine(array_keys($users),array_keys($users));
if (isset($user_keys[mb_strtolower($userProperties['login'])])) {
//if (in_array(mb_strtolower($userProperties['login']), array_keys($users), true) !== false) {
throw new EfrontUserException(_USERALREADYEXISTS.': '.$userProperties['login'], EfrontUserException :: USER_EXISTS);
}
*/
if (G_VERSIONTYPE != 'community') { #cpp#ifndef COMMUNITY
if (G_VERSIONTYPE != 'standard') { #cpp#ifndef STANDARD
//pr($activatedUsers);
if (isset($GLOBALS['configuration']['version_users']) && $activatedUsers > $GLOBALS['configuration']['version_users'] && $GLOBALS['configuration']['version_users'] > 0) {
throw new EfrontUserException(_MAXIMUMUSERSNUMBERREACHED.' ('.$GLOBALS['configuration']['version_users'].'): '.$userProperties['login'], EfrontUserException :: MAXIMUM_REACHED);
}
} #cpp#endif
} #cpp#endif
if ($userProperties['email'] && !eF_checkParameter($userProperties['email'], 'email')) {
throw new EfrontUserException(_INVALIDEMAIL.': '.$userProperties['email'], EfrontUserException :: INVALID_PARAMETER);
}
if (!isset($userProperties['name'])) {
throw new EfrontUserException(_INVALIDNAME.': '.$userProperties['name'], EfrontUserException :: INVALID_PARAMETER);
}
if (!isset($userProperties['surname'])) {
throw new EfrontUserException(_INVALIDSURNAME.': '.$userProperties['login'], EfrontUserException :: INVALID_PARAMETER);
}
$roles = EfrontUser::getRoles();
$rolesTypes = EfrontUser::getRoles(true);
foreach (EfrontUser::getRoles(true) as $key => $value) {
$rolesTypes[$key] = mb_strtolower($value);
}
//If a user type is not specified, by default make the new user student
if (!isset($userProperties['user_type'])) {
$userProperties['user_type'] = 'student';
} else {
if (in_array(mb_strtolower($userProperties['user_type']), $roles)) {
$userProperties['user_type'] = mb_strtolower($userProperties['user_type']);
} else if ($k=array_search(mb_strtolower($userProperties['user_type']), $rolesTypes)) {
$userProperties['user_types_ID'] = $k;
$userProperties['user_type'] = $roles[$k];
} else {
$userProperties['user_type'] = 'student';
}
}
if (!in_array($userProperties['user_type'], EFrontUser::$basicUserTypes)) {
$userProperties['user_type'] = 'student';
$userProperties['user_types_ID'] = 0;
}
//!isset($userProperties['user_type']) || !in_array($userProperties['user_type'], EfrontUser::getRoles()) ? $userProperties['user_type'] = 'student' : null;
isset($userProperties['password']) && $userProperties['password'] != '' ? $passwordNonTransformed = $userProperties['password'] : $passwordNonTransformed = $userProperties['login'];
if ($userProperties['password'] != 'ldap') {
!isset($userProperties['password']) || $userProperties['password'] == '' ? $userProperties['password'] = EfrontUser::createPassword($userProperties['login']) : $userProperties['password'] = self :: createPassword($userProperties['password']);
if ($GLOBALS['configuration']['force_change_password']) {
$userProperties['need_pwd_change'] = 1;
}
}
!isset($userProperties['email']) ? $userProperties['email'] = '' : null; // 0 means not pending, 1 means pending
!isset($userProperties['languages_NAME']) ? $userProperties['languages_NAME'] = $GLOBALS['configuration']['default_language'] : null; //If language is not specified, use default language
!isset($userProperties['active']) || $userProperties['active'] == "" ? $userProperties['active'] = 0 : null; // 0 means inactive, 1 means active
!isset($userProperties['pending']) ? $userProperties['pending'] = 0 : null; // 0 means not pending, 1 means pending
!isset($userProperties['timestamp']) || $userProperties['timestamp'] == "" ? $userProperties['timestamp'] = time() : null;
!isset($userProperties['user_types_ID']) ? $userProperties['user_types_ID'] = 0 : null;
$languages = EfrontSystem :: getLanguages();
if (in_array($userProperties['languages_NAME'], array_keys($languages)) === false) {
$userProperties['languages_NAME'] = $GLOBALS['configuration']['default_language'];
}
if ($userProperties['archive']) {
$userProperties['archive'] = time();
$userProperties['active'] = 0;
}
!isset($userProperties['timezone']) || $userProperties['timezone'] == '' ? $userProperties['timezone'] = $GLOBALS['configuration']['time_zone'] :null;
$userProfile = eF_getTableData("user_profile", "name,options", "active=1 AND type='select'");
foreach ($userProfile as $field) {
if(isset($userProperties[$field['name']])) {
$options = unserialize($field['options']);
$userProperties[$field['name']] = array_search($userProperties[$field['name']], $options);
}
}
eF_insertTableData("users", $userProperties);
// Assign to the new user all skillgap tests that should be automatically assigned to every new student
if (G_VERSIONTYPE != 'community') { #cpp#ifndef COMMUNITY
if (G_VERSIONTYPE != 'standard') { #cpp#ifndef STANDARD
if ($userProperties['user_type'] == 'student') {
$tests = EfrontTest :: getAutoAssignedTests();
foreach ($tests as $test) {
eF_insertTableData("users_to_skillgap_tests", array("users_LOGIN" => $userProperties['login'], "tests_ID" => $test));
}
}
} #cpp#endif
} #cpp#endif
$newUser = EfrontUserFactory :: factory($userProperties['login']);
//$newUser -> user['password'] = $passwordNonTransformed; //commented out because it was not needed any more, and created problems. Will be removed in next pass
global $currentUser; // this is for running eF_loadAllModules ..needs to go somewhere else
if (!$currentUser) {
$currentUser = $newUser;
}
EfrontEvent::triggerEvent(array("type" => EfrontEvent::SYSTEM_JOIN, "users_LOGIN" => $newUser -> user['login'], "users_name" => $newUser -> user['name'], "users_surname" => $newUser -> user['surname'], "entity_name" => $passwordNonTransformed));
EfrontEvent::triggerEvent(array("type" => (-1) * EfrontEvent::SYSTEM_VISITED, "users_LOGIN" =>$newUser -> user['login'], "users_name" => $newUser -> user['name'], "users_surname" => $newUser -> user['surname']));
if (G_VERSIONTYPE != 'community') { #cpp#ifndef COMMUNITY
if (G_VERSIONTYPE != 'standard') { #cpp#ifndef STANDARD
if ($addToDefaultGroup) {
EfrontGroup::addToDefaultGroup($newUser, $newUser -> user['user_types_ID'] ? $newUser -> user['user_types_ID'] : $newUser -> user['user_type']);
}
} #cpp#endif
} #cpp#endif
///MODULES1 - Module user add events
// Get all modules (NOT only the ones that have to do with the user type)
if (!self::$cached_modules) {
self::$cached_modules = eF_loadAllModules();
}
// Trigger all necessary events. If the function has not been re-defined in the derived module class, nothing will happen
foreach (self::$cached_modules as $module) {
$module -> onNewUser($userProperties['login']);
}
if (function_exists('apc_delete')) {
apc_delete(G_DBNAME.':_usernames');
}
if (G_VERSIONTYPE != 'community') { #cpp#ifndef COMMUNITY
if (G_VERSIONTYPE != 'standard') { #cpp#ifndef STANDARD
$threshold = self::NOTIFY_THRESHOLD * $GLOBALS['configuration']['version_users'];
if (isset($GLOBALS['configuration']['version_users']) && $GLOBALS['configuration']['version_users'] > 0 && $activatedUsers < $threshold && $activatedUsers+1 > $threshold) {
$admin = EfrontSystem::getAdministrator();
eF_mail($GLOBALS['configuration']['system_email'], $admin->user['email'], _YOUAREREACHINGYOURSUBSCRIPTIONLIMIT, str_replace(array('%w', '%x', '%y', '%z'), array($admin->user['name'], self::NOTIFY_THRESHOLD*100, $GLOBALS['configuration']['site_name'], G_SERVERNAME), _YOUAREREACHINGYOURSUBSCRIPTIONLIMITBODY));
}
} #cpp#endif
} #cpp#endif
return $newUser;
}
/**
* This function parses an array of users and verifies that they are
* correct and converts it to an array if it's a single entry
*
* @param mixed $users The users to verify
* @return array The array of verified users
* @since 3.6.7
* @access public
* @static
*/
public static function verifyUsersList($users) {
if (!is_array($users)) {
$users = array($users);
}
foreach ($users as $key => $value) {
if ($value instanceOf EfrontUser) {
$users[$key] = $value -> user['login'];
} elseif (is_array($value) && isset($value['login'])) {
$users[$key] = $value['login'];
} elseif (is_array($value) && isset($value['users_LOGIN'])) {
$users[$key] = $value['users_LOGIN'];
} elseif (!eF_checkParameter($value, 'login')) {
unset($users[$key]);
}
}
return array_values(array_unique($users)); //array_values() to reindex array
}
/**
* This function parses an array of roles and verifies that they are
* correct, converts it to an array if it's a single entry and
* pads the array with extra values, if its length is less than the
* desired
*
* @param mixed $roles The roles to verify
* @param int $length The desired length of the roles array
* @return array The array of verified roles
* @since 3.6.7
* @access public
* @static
*/
public static function verifyRolesList($roles, $length) {
if (!is_array($roles)) {
$roles = array($roles);
}
if (sizeof($roles) < $length) {
$roles = array_pad($roles, $length, $roles[0]);
}
return array_values($roles); //array_values() to reindex array
}
/**
* Check whether the specified role is of type 'student'
*
* @param mixed $role The role to check
* @return boolean Whether it's a 'student' role
* @since 3.6.7
* @access public
* @static
*/
public static function isStudentRole($role) {
$courseRoles = EfrontLessonUser :: getLessonsRoles();
if ($courseRoles[$role] == 'student') {
return true;
} else {
return false;
}
}
/**
* Check whether the specified role is of type 'professor'
*
* @param mixed $role The role to check
* @return boolean Whether it's a 'professor' role
* @since 3.6.7
* @access public
* @static
*/
public static function isProfessorRole($role) {
$courseRoles = EfrontLessonUser :: getLessonsRoles();
if ($courseRoles[$role] == 'professor') {
return true;
} else {
return false;
}
}
public static function checkUserAccess($type = false, $forceType = false) {
if ($GLOBALS['configuration']['webserver_auth']) {
$user = EfrontUser :: checkWebserverAuthentication();
} else if (isset($_SESSION['s_login']) && $_SESSION['s_password']) {
$user = EfrontUserFactory :: factory($_SESSION['s_login'], false, $forceType);
} else {
throw new EfrontUserException(_RESOURCEREQUESTEDREQUIRESLOGIN, EfrontUserException::USER_NOT_LOGGED_IN);
}
if (!$user -> isLoggedIn(session_id())) {
throw new EfrontUserException(_RESOURCEREQUESTEDREQUIRESLOGIN, EfrontUserException::USER_NOT_LOGGED_IN);
}
if ($user -> user['timezone']) {
date_default_timezone_set($user -> user['timezone']);
}
$user -> applyRoleOptions($user -> user['user_types_ID']); //Initialize user's role options for this lesson
if ($type && $user -> user['user_type'] != $type) {
throw new Exception(_YOUCANNOTACCESSTHISPAGE, EfrontUserException::INVALID_TYPE);
}
return $user;
}
public static function checkWebserverAuthentication() {
try {
eval('$usernameVar='.$GLOBALS['configuration']['username_variable'].';');
if (!$usernameVar) {
eF_redirect(G_SERVERNAME.$GLOBALS['configuration']['error_page'], true, 'top', true);
exit;
} else {
try {
$user = EfrontUserFactory :: factory($usernameVar);
if (!$_SESSION['s_login'] || $usernameVar != $_SESSION['s_login']) {
$user -> login($user -> user['password'], true);
}
} catch (EfrontUserException $e) {
if ($e -> getCode() == EfrontUserException::USER_NOT_EXISTS && $GLOBALS['configuration']['webserver_registration']) {
try {
include($GLOBALS['configuration']['registration_file']);
$user = EfrontUserFactory :: factory($usernameVar);
if (!$_SESSION['s_login'] || $usernameVar != $_SESSION['s_login']) {
$user -> login($user -> user['password'], true);
}
} catch (Exception $e) {
eF_redirect(G_SERVERNAME.$GLOBALS['configuration']['unauthorized_page'], true, 'top', true);
exit;
}
} else {
eF_redirect(G_SERVERNAME.$GLOBALS['configuration']['unauthorized_page'], true, 'top', true);
exit;
}
}
}
} catch (Exception $e) {
eF_redirect(G_SERVERNAME.$GLOBALS['configuration']['unauthorized_page'], true, 'top', true);
//header("location:".G_SERVERNAME.$GLOBALS['configuration']['unauthorized_page']);
}
return $user;
}
public static function countUsers($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
$from = "users u";
list($where, $limit, $orderby) = EfrontUser :: convertUserConstraintsToSqlParameters($constraints);
$result = eF_countTableData($from, "u.login", implode(" and ", $where));
return $result[0]['count'];
}
public function getUsers($constraints = array()) {
!empty($constraints) OR $constraints = array('archive' => false, 'active' => true);
list($where, $limit, $orderby) = EfrontUser :: convertUserConstraintsToSqlParameters($constraints);
$select = "u.login,u.user_type,u.user_types_ID,u.active,u.timestamp,u.archive, u.last_login";
$from = "users u";
$result = eF_getTableData($from, $select, implode(" and ", $where), $orderby, $groupby, $limit);
return EfrontUser :: convertDatabaseResultToUserArray($result);
}
/**
* Add user profile field
*/
public static function addUserField() {}
/**
* Remove user profile field
*/
public static function removeUserField() {}
/**
* Get user type
*
* This function returns the user basic type, one of 'administrator', 'professor',
* 'student'
* <br/>Example:
* <code>
* $user = EfrontUserFactory :: factory('admin');
* echo $user -> getType(); //Returns 'administrator'
* </code>
*
* @return string The user type
* @since 3.5.0
* @access public
*/
public function getType() {
return $this -> user['user_type'];
}
/**
* Set user password
*
* This function is used to change the user password to something
* new.
* <br/>Example:
* <code>
* $user = EfrontUserFactory :: factory('jdoe');
* $user -> setPassword('somepass');
* </code>
*
* @param string $password The new password
* @return boolean true if everything is ok
* @since 3.5.0
* @access public
*/
public function setPassword($password) {
$password_encrypted = EfrontUser::createPassword($password);
if (eF_updateTableData("users", array("password" => $password_encrypted), "login='".$this -> user['login']."'")) {
$this -> user['password'] = $password;
return true;
} else {
return false;
}
}
/**
* Get user password
*
* This function returns the user password (MD5 encrypted)
* <br/>Example:
* <code>
* $user = EfrontUserFactory :: factory('jdoe');
* echo $user -> getPassword(); //echos something like '36f49e43c662986b838258ab099d0d5a'
* </code>
*
* @return string The user password (encrypted)
* @since 3.5.0
* @access public
*/
public function getPassword() {
return $this -> user['password'];
}
/**
* Set login type
*
* This function is used to set the login type for the user. Currently this
* can be either 'normal' (default) or 'ldap'. Setting the login type to 'ldap'
* erases the user password and forces authentication through ldap server
* <br/>Example:
* <code>
* $user = EfrontUserFactory :: factory('jdoe');
* $user -> setLoginType('ldap'); //Set login type to 'ldap'
* $user -> setLoginType('normal', 'testpass'); //Set login type to 'normal' using password 'testpass'
* $user -> setLoginType(); //Set login type to 'normal' and use default password (the user's login)
* </code>
* If the user was an ldap user and is reverted back to normal, the password is either specified
* or created by default to match the user's login
*
* @param string $loginType The new login type, one of 'ldap' or 'normal'
* @param string $password The new password, only used when converting ldap to normal accounts
* @return boolean True if everything is ok.
* @since 3.5.0
* @access public
*/
public function setLoginType($loginType = 'normal', $password = '') {
//The user login type is specified by the password. If the password is 'ldap', the the login type is also ldap. There is no chance to mistaken normal users for ldap users, since all normal users have passwords stored in md5 format, which can never be 'ldap' (or anything like it)
if ($loginType == 'ldap' && $this -> user['password'] != 'ldap') {
eF_updateTableData("users", array("password" => 'ldap'), "login='".$this -> user['login']."'");
$this -> user['password'] = 'ldap';
} elseif ($loginType == 'normal' && $this -> user['password'] == 'ldap') {
!$password ? $password = EfrontUser::createPassword($this -> user['login']) : null; //If a password is not specified, use the user's login name
eF_updateTableData("users", array("password" => $password), "login='".$this -> user['login']."'");
$this -> user['password'] = $password;
}
return true;
}
/**
* Get the login type
*
* This function is used to check whether the user's login type
* is 'normal' or 'ldap'
* <br/>Example:
* <code>
* $user = EfrontUserFactory :: factory('jdoe');
* $user -> getLoginType(); //Returns either 'normal' or 'ldap'
* </code>
*
* @return string Either 'normal' or 'ldap'
* @since 3.5.0
* @access public
*/
public function getLoginType() {
if ($this -> user['password'] == 'ldap') {
return 'ldap';
} else {
return 'normal';
}
}
/**
* Activate user
*
* This function is used to activate the user
* <br/>Example:
* <code>
* $user = EfrontUserFactory :: factory('jdoe');
* $user -> activate();
* </code>
*
* @return boolean True if everything is ok
* @since 3.5.0
* @access public
*/
public function activate() {
if (G_VERSIONTYPE != 'community') { #cpp#ifndef COMMUNITY
if (G_VERSIONTYPE != 'standard') { #cpp#ifndef STANDARD
$users = eF_countTableData("users", "*", "active=1 and archive=0");
$versionUsers = (int) $GLOBALS['configuration']['version_users'];
if (isset($versionUsers) && $users[0]['count'] > $versionUsers && $versionUsers > 0) {
throw new EfrontUserException(_MAXIMUMUSERSNUMBERREACHED.' ('.$GLOBALS['configuration']['version_users'].'): '.$this -> user['login'], EfrontUserException :: MAXIMUM_REACHED);
}
} #cpp#endif
} #cpp#endif
$this -> user['active'] = 1;
$this -> user['pending'] = 0;
$this -> persist();
return true;
}
/**
* Deactivate user
*
* This function is used to deactivate the user
* <br/>Example:
* <code>
* $user = EfrontUserFactory :: factory('jdoe');
* $user -> deactivate();
* </code>
*
* @return boolean True if everything is ok
* @since 3.5.0
* @access public
*/
public function deactivate() {
$this -> user['active'] = 0;
$this -> persist();
EfrontEvent::triggerEvent(array("type" => EfrontEvent::SYSTEM_USER_DEACTIVATE, "users_LOGIN" => $this -> user['login'], "users_name" => $this -> user['name'], "users_surname" => $this -> user['surname']));
return true;
}
/**
* Set avatar image
*
* This function is used to set the user's avatar image.
* <br/>Example:
* <code>
* $file = new EfrontFile(32); //This is a file uploaded -for example- in the filesystem.
* $user -> setAvatar($file);
* </code>
*
* @param EfrontFile $file The file that will be used as avatar
* @return boolean True if everything is ok
* @since 3.5.0
* @access public
*/
public function setAvatar($file) {
if (eF_updateTableData("users", array("avatar" => $file['id']), "login = '".$this -> user['login']."'")) {
$this -> user['avatar'] = $file['id'];
return true;
} else {
return false;
}
}
/**
* Get avatar image
*
* This function returns the file object corresponding to the user avatar
* <br/>Example:
* <code>
* $user = EfrontUserFactory :: factory('jdoe');
* $user -> getAvatar(); //Returns an EfrontFile object
* </code>
*
* @return EfrontFile The avatar's file object
* @since 3.6.0
* @access public
*/
public function getAvatar() {
if ($this -> user['avatar']) {
$avatar = new EfrontFile($this -> user['avatar']);
} else {
$avatar = new EfrontFile(G_SYSTEMAVATARSURL.'unknown_small.png');
}
return $avatar;
}
/**
* Set user status
*
* This function is used to set the user's status.
* <br/>Example:
* <code>
* $user -> setStatus("Carpe Diem!");
* </code>
*
* @param string to be set as the new status - could be ""
* @return boolean True if everything is ok
* @since 3.6.0
* @access public
*/
public function setStatus($status) {
//echo $status;
if ($_SESSION['facebook_user'] && $_SESSION['facebook_details']['status']['message'] != $status) {
$path = "../libraries/";
require_once $path . "external/facebook/facebook.php";
$facebook = new Facebook(array(
'appId' => $GLOBALS['configuration']['facebook_api_key'],
'secret' => $GLOBALS['configuration']['facebook_secret'],
'cookie' => true
));
//$access_token = $facebook -> getAccessToken();
$fql = "SELECT publish_stream FROM permissions WHERE uid =".$_SESSION['facebook_user'];
$publish_info = $facebook->api(array(
'method' => 'fql.query',
'query' => $fql,
));
$canPublish = 0;
if (!empty($publish_info)) {
$canPublish = $publish_info[0]['publish_stream'];
}
if (!$canPublish) {
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id=". $GLOBALS['configuration']['facebook_api_key']."&redirect_uri=".G_SERVERNAME.$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']."&scope=read_stream,status_update";
echo json_encode(array('redirect' => $dialog_url));
exit;
}
//$perms = json_decode(file_get_contents('https://graph.facebook.com/me/permissions?access_token=' . $access_token));
//$_SESSION['facebook_can_update'] = $perms->data[0]->status_update;
$facebook->api ( array(
'method' => 'users.setStatus',
'status' => $status,
'uid' => $_SESSION['facebook_user'],
'status_includes_verb' => true
) );
// $facebook->api_client->call_method("facebook.users.setStatus", array("status" => $status, "status_includes_verb" => true));
// $temp = $facebook->api_client->fql_query("SELECT status FROM user WHERE uid = " . $_SESSION['facebook_user']);
$_SESSION['facebook_details']['status'] = $status;
}
eF_updateTableData("users", array("status" => $status), "login = '".$this -> user['login']."'");
$this -> user['status'] = $status;
EfrontEvent::triggerEvent(array("type" => EfrontEvent::STATUS_CHANGE, "users_LOGIN" => $this -> user['login'], "users_name" => $this->user['name'], "users_surname" => $this->user['surname'], "entity_name" => $status));
return true;
}
/**
* Logs out user
*
* To log out a user, the function deletes the session information and updates the database
* tables.
* <br/>Example:
* <code>
* $user = EfrontUserFactory :: factory('jdoe');
* $user -> logout();
* </code>
*
* @param $sessionId Which session to logout user from
* @return boolean True if the user was logged out succesfully
* @since 3.5.0
* @access public
*/
public function logout($sessionId = false) {
// Delete FB-connect related cookies - without this code the "Session key invalid problem" appears
if (isset($GLOBALS['configuration']['facebook_api_key']) && $GLOBALS['configuration']['facebook_api_key'] && $_COOKIE['fbsr_'.$GLOBALS['configuration']['facebook_api_key']]) {
foreach ($_COOKIE as $cookie_key => $cookie) {
if (strpos($cookie_key, $GLOBALS['configuration']['facebook_api_key']) !== false) {
unset($_COOKIE[$cookie_key]);
}
}
/* $path = "../libraries/";
require_once $path . "external/facebook/facebook.php";
$facebook = new Facebook(array(
'appId' => $GLOBALS['configuration']['facebook_api_key'],
'secret' => $GLOBALS['configuration']['facebook_secret'],
'cookie' => true
));
$facebook->destroySession();
setcookie('fbsr_' . $GLOBALS['configuration']['facebook_api_key'], $_COOKIE['fbsr_' . $GLOBALS['configuration']['facebook_api_key']], time() - 3600, '/', '.'.$_SERVER['SERVER_NAME']);
setcookie('fbsr_' . $GLOBALS['configuration']['facebook_api_key'], $_COOKIE['fbsr_' . $GLOBALS['configuration']['facebook_api_key']], time() - 3600, '/', $_SERVER['SERVER_NAME']);
*/
}
if ($sessionId) {
//Logout user on a specific session
eF_updateTableData("user_times", array("session_expired" => 1), "session_expired = 0 and users_LOGIN='".$this -> user['login']."' and session_id='$sessionId' ");
} else {
//Logout every user logged under this login
eF_updateTableData("user_times", array("session_expired" => 1), "session_expired = 0 and users_LOGIN='".$this -> user['login']."'");
}
$result = eF_getTableData("user_times", "id", "session_expired=0 and users_LOGIN='".$this -> user['login']."'");
//If this was the last user logged in under this login, create logout entry
if (empty($result)) {
$fields_insert = array('users_LOGIN' => $this -> user['login'],
'timestamp' => time(),
'action' => 'logout',
'comments' => 0,
'session_ip' => eF_encodeIP($_SERVER['REMOTE_ADDR']));
eF_insertTableData("logs", $fields_insert);
}
if ((!$_SESSION['s_login'] || $this -> user['login'] == $_SESSION['s_login']) && ($sessionId == session_id() || !$sessionId)) {
if (isset($_COOKIE['c_request'])) {
setcookie('c_request', '', time() - 86400);
unset($_COOKIE['c_request']);
}
setcookie ("cookie_login", "", time() - 3600);
setcookie ("cookie_password", "", time() - 3600);
unset($_COOKIE['cookie_login']); //These 2 lines are necessary, so that index.php does not think they are set
unset($_COOKIE['cookie_password']);
//Empty session without destroying it
foreach ($_SESSION as $key => $value) {
if ($key != 's_current_branch') {
unset($_SESSION[$key]);
}
}
//session_destroy();
}
return true;
}
/**
* Login user
*
* This function logs the user in the system, using the specified password
* <br/>Example:
* <code>
* $user = EfrontUserFactory :: factory('jdoe');
* $user -> login('mypass');
* </code>
*
* @param string $password The password to login with
* @param boolean $encrypted Whether the password is already encrypted
* @return boolean True if the user logged in successfully
* @since 3.5.0
* @access public
*/
public function login($password, $encrypted = false) {
//If the user is already logged in, log him out