/
user_api.php
1709 lines (1458 loc) · 50.9 KB
/
user_api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
# MantisBT - A PHP based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* User API
*
* @package CoreAPI
* @subpackage UserAPI
* @copyright Copyright 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*
* @uses access_api.php
* @uses authentication_api.php
* @uses config_api.php
* @uses constant_inc.php
* @uses database_api.php
* @uses email_api.php
* @uses error_api.php
* @uses filter_api.php
* @uses helper_api.php
* @uses lang_api.php
* @uses ldap_api.php
* @uses project_api.php
* @uses project_hierarchy_api.php
* @uses string_api.php
* @uses user_pref_api.php
* @uses utility_api.php
*/
require_api( 'access_api.php' );
require_api( 'authentication_api.php' );
require_api( 'config_api.php' );
require_api( 'constant_inc.php' );
require_api( 'database_api.php' );
require_api( 'email_api.php' );
require_api( 'error_api.php' );
require_api( 'filter_api.php' );
require_api( 'helper_api.php' );
require_api( 'lang_api.php' );
require_api( 'ldap_api.php' );
require_api( 'project_api.php' );
require_api( 'project_hierarchy_api.php' );
require_api( 'string_api.php' );
require_api( 'user_pref_api.php' );
require_api( 'utility_api.php' );
$g_cache_user = array();
$g_user_accessible_subprojects_cache = null;
/**
* Cache a user row if necessary and return the cached copy
* If the second parameter is true (default), trigger an error
* if the user can't be found. If the second parameter is
* false, return false if the user can't be found.
*
* @param integer $p_user_id A valid user identifier.
* @param boolean $p_trigger_errors Trigger an error is the user does not exist.
* @return array|boolean array of database data or false if not found
*/
function user_cache_row( $p_user_id, $p_trigger_errors = true ) {
global $g_cache_user;
if( isset( $g_cache_user[$p_user_id] ) ) {
return $g_cache_user[$p_user_id];
}
$t_query = 'SELECT * FROM {user} WHERE id=' . db_param();
$t_result = db_query( $t_query, array( $p_user_id ) );
if( 0 == db_num_rows( $t_result ) ) {
$g_cache_user[$p_user_id] = false;
if( $p_trigger_errors ) {
error_parameters( (integer)$p_user_id );
trigger_error( ERROR_USER_BY_ID_NOT_FOUND, ERROR );
}
return false;
}
$t_row = db_fetch_array( $t_result );
$g_cache_user[$p_user_id] = $t_row;
return $t_row;
}
/**
* Generate an array of User objects from given User ID's
*
* @param array $p_user_id_array An array of user identifiers.
* @return void
*/
function user_cache_array_rows( array $p_user_id_array ) {
global $g_cache_user;
$c_user_id_array = array();
foreach( $p_user_id_array as $t_user_id ) {
if( !isset( $g_cache_user[(int)$t_user_id] ) ) {
$c_user_id_array[] = (int)$t_user_id;
}
}
if( empty( $c_user_id_array ) ) {
return;
}
$t_query = 'SELECT * FROM {user} WHERE id IN (' . implode( ',', $c_user_id_array ) . ')';
$t_result = db_query( $t_query );
while( $t_row = db_fetch_array( $t_result ) ) {
$g_cache_user[(int)$t_row['id']] = $t_row;
}
return;
}
/**
* Cache an object as a bug.
* @param array $p_user_database_result A user row to cache.
* @return array|null
*/
function user_cache_database_result( array $p_user_database_result ) {
global $g_cache_user;
if( isset( $g_cache_user[$p_user_database_result['id']] ) ) {
return $g_cache_user[$p_user_database_result['id']];
}
$g_cache_user[$p_user_database_result['id']] = $p_user_database_result;
}
/**
* Clear the user cache (or just the given id if specified)
* @param integer $p_user_id A valid user identifier or the default of null to clear cache for all users.
* @return boolean
*/
function user_clear_cache( $p_user_id = null ) {
global $g_cache_user;
if( null === $p_user_id ) {
$g_cache_user = array();
} else {
unset( $g_cache_user[$p_user_id] );
}
return true;
}
/**
* Update Cache entry for a given user and field
* @param integer $p_user_id A valid user id to update.
* @param string $p_field The name of the field on the user object to update.
* @param mixed $p_value The updated value for the user object field.
* @return void
*/
function user_update_cache( $p_user_id, $p_field, $p_value ) {
global $g_cache_user;
if( isset( $g_cache_user[$p_user_id] ) && isset( $g_cache_user[$p_user_id][$p_field] ) ) {
$g_cache_user[$p_user_id][$p_field] = $p_value;
} else {
user_clear_cache( $p_user_id );
}
}
/**
* Searches the cache for a given field and value pair against any user,
* and returns the first user id that matches
*
* @param string $p_field The user object field name to search the cache for.
* @param mixed $p_value The field value to look for in the cache.
* @return integer|boolean
*/
function user_search_cache( $p_field, $p_value ) {
global $g_cache_user;
if( isset( $g_cache_user ) ) {
foreach( $g_cache_user as $t_user ) {
if( $t_user[$p_field] == $p_value ) {
return $t_user;
}
}
}
return false;
}
/**
* check to see if user exists by id
* return true if it does, false otherwise
*
* @param integer $p_user_id A valid user identifier.
* @return boolean
*/
function user_exists( $p_user_id ) {
$t_row = user_cache_row( $p_user_id, false );
if( false === $t_row ) {
return false;
} else {
return true;
}
}
/**
* check to see if user exists by id
* if the user does not exist, trigger an error
*
* @param integer $p_user_id A valid user identifier.
* @return void
*/
function user_ensure_exists( $p_user_id ) {
$c_user_id = (integer)$p_user_id;
if( !user_exists( $c_user_id ) ) {
error_parameters( $c_user_id );
trigger_error( ERROR_USER_BY_ID_NOT_FOUND, ERROR );
}
}
/**
* return true if the username is unique, false if there is already a user with that username
* @param string $p_username The username to check.
* @return boolean
*/
function user_is_name_unique( $p_username ) {
$t_query = 'SELECT username FROM {user} WHERE username=' . db_param();
$t_result = db_query( $t_query, array( $p_username ), 1 );
return !db_result( $t_result );
}
/**
* Check if the username is unique and trigger an ERROR if it isn't
* @param string $p_username The username to check.
* @return void
*/
function user_ensure_name_unique( $p_username ) {
if( !user_is_name_unique( $p_username ) ) {
trigger_error( ERROR_USER_NAME_NOT_UNIQUE, ERROR );
}
}
/**
* Checks if the email address is unique.
*
* @param string $p_email The email to check.
* @param integer $p_user_id The user id that we are validating for or null for
* the case of a new user.
*
* @return boolean true: unique or blank, false: otherwise
*/
function user_is_email_unique( $p_email, $p_user_id = null ) {
if( is_blank( $p_email ) ) {
return true;
}
$p_email = trim( $p_email );
if ( $p_user_id === null ) {
$t_query = 'SELECT email FROM {user} WHERE email=' . db_param();
$t_result = db_query( $t_query, array( $p_email ), 1 );
} else {
$t_query = 'SELECT email FROM {user} WHERE id<>' . db_param() .
' AND email=' . db_param();
$t_result = db_query( $t_query, array( $p_user_id, $p_email ), 1 );
}
return !db_result( $t_result );
}
/**
* Check if the email is unique and trigger an ERROR if it isn't
*
* @param string $p_email The email address to check.
* @param integer $p_user_id The user id that we are validating for or null for
* the case of a new user.
*
* @return void
*/
function user_ensure_email_unique( $p_email, $p_user_id = null ) {
if( !config_get_global( 'email_ensure_unique' ) ) {
return;
}
if( !user_is_email_unique( $p_email, $p_user_id ) ) {
trigger_error( ERROR_USER_EMAIL_NOT_UNIQUE, ERROR );
}
}
/**
* Check if the realname is a valid username (does not account for uniqueness)
* Return 0 if it is invalid, The number of matches + 1
*
* @param string $p_username The username to check.
* @param string $p_realname The realname to check.
* @return integer
*/
function user_is_realname_unique( $p_username, $p_realname ) {
if( is_blank( $p_realname ) ) {
# don't bother checking if realname is blank
return 1;
}
$p_username = trim( $p_username );
$p_realname = trim( $p_realname );
# allow realname to match username
$t_duplicate_count = 0;
if( $p_realname !== $p_username ) {
# check realname does not match an existing username
# but allow it to match the current user
$t_target_user = user_get_id_by_name( $p_username );
$t_other_user = user_get_id_by_name( $p_realname );
if( ( false !== $t_other_user ) && ( $t_target_user !== $t_other_user ) ) {
return 0;
}
# check to see if the realname is unique
$t_query = 'SELECT id FROM {user} WHERE realname=' . db_param();
$t_result = db_query( $t_query, array( $p_realname ) );
$t_users = array();
while( $t_row = db_fetch_array( $t_result ) ) {
$t_users[] = $t_row;
}
$t_duplicate_count = count( $t_users );
if( $t_duplicate_count > 0 ) {
# set flags for non-unique realnames
if( config_get( 'differentiate_duplicates' ) ) {
for( $i = 0;$i < $t_duplicate_count;$i++ ) {
$t_user_id = $t_users[$i]['id'];
user_set_field( $t_user_id, 'duplicate_realname', ON );
}
}
}
}
return $t_duplicate_count + 1;
}
/**
* Check if the realname is a unique
* Trigger an error if the username is not valid
*
* @param string $p_username The username to check.
* @param string $p_realname The realname to check.
* @return void
*/
function user_ensure_realname_unique( $p_username, $p_realname ) {
if( 1 > user_is_realname_unique( $p_username, $p_realname ) ) {
trigger_error( ERROR_USER_REAL_MATCH_USER, ERROR );
}
}
/**
* Check if the username is a valid username (does not account for uniqueness) realname can match
* @param string $p_username The username to check.
* @return boolean return true if user name is valid, false otherwise
*/
function user_is_name_valid( $p_username ) {
# The DB field is hard-coded. DB_FIELD_SIZE_USERNAME should not be modified.
if( utf8_strlen( $p_username ) > DB_FIELD_SIZE_USERNAME ) {
return false;
}
# username must consist of at least one character
if( is_blank( $p_username ) ) {
return false;
}
# Only allow a basic set of characters
if( 0 == preg_match( config_get( 'user_login_valid_regex' ), $p_username ) ) {
return false;
}
# We have a valid username
return true;
}
/**
* Check if the username is a valid username (does not account for uniqueness)
* Trigger an error if the username is not valid
* @param string $p_username The username to check.
* @return void
*/
function user_ensure_name_valid( $p_username ) {
if( !user_is_name_valid( $p_username ) ) {
trigger_error( ERROR_USER_NAME_INVALID, ERROR );
}
}
/**
* return whether user is monitoring bug for the user id and bug id
* @param integer $p_user_id A valid user identifier.
* @param integer $p_bug_id A valid bug identifier.
* @return boolean
*/
function user_is_monitoring_bug( $p_user_id, $p_bug_id ) {
$t_query = 'SELECT COUNT(*) FROM {bug_monitor}
WHERE user_id=' . db_param() . ' AND bug_id=' . db_param();
$t_result = db_query( $t_query, array( (int)$p_user_id, (int)$p_bug_id ) );
if( 0 == db_result( $t_result ) ) {
return false;
} else {
return true;
}
}
/**
* Check if the specified user is an enabled user with admin access level or above.
* @param integer $p_user_id A valid user identifier.
* @return boolean true: admin, false: otherwise.
*/
function user_is_administrator( $p_user_id ) {
if( !user_is_enabled( $p_user_id ) ) {
return false;
}
$t_access_level = user_get_field( $p_user_id, 'access_level' );
return $t_access_level >= config_get_global( 'admin_site_threshold' );
}
/**
* Check if a user has a protected user account.
* Protected user accounts cannot be updated without manage_user_threshold
* permission. If the user ID supplied is that of the anonymous user, this
* function will always return true. The anonymous user account is always
* considered to be protected.
*
* @param integer $p_user_id A valid user identifier.
* @return boolean true: user is protected; false: user is not protected.
* @access public
*/
function user_is_protected( $p_user_id ) {
if( user_is_anonymous( $p_user_id ) || ON == user_get_field( $p_user_id, 'protected' ) ) {
return true;
}
return false;
}
/**
* Check if a user is the anonymous user account.
* When anonymous logins are disabled this function will always return false.
*
* @param integer $p_user_id A valid user identifier.
* @return boolean true: user is the anonymous user; false: user is not the anonymous user.
* @access public
*/
function user_is_anonymous( $p_user_id ) {
if( ON == config_get( 'allow_anonymous_login' ) && user_get_field( $p_user_id, 'username' ) == config_get( 'anonymous_account' ) ) {
return true;
}
return false;
}
/**
* Trigger an ERROR if the user account is protected
*
* @param integer $p_user_id A valid user identifier.
* @return void
*/
function user_ensure_unprotected( $p_user_id ) {
if( user_is_protected( $p_user_id ) ) {
trigger_error( ERROR_PROTECTED_ACCOUNT, ERROR );
}
}
/**
* return true is the user account is enabled, false otherwise
*
* @param integer $p_user_id A valid user identifier.
* @return boolean
*/
function user_is_enabled( $p_user_id ) {
if( ON == user_get_field( $p_user_id, 'enabled' ) ) {
return true;
} else {
return false;
}
}
/**
* Count the number of users at or greater than a specific level
*
* @param integer $p_level Access Level to count users. The default is to include ANYBODY.
* @param bool $p_enabled true: must be enabled, false: must be disabled, null: don't care.
* @return integer The number of users.
*/
function user_count_level( $p_level = ANYBODY, $p_enabled = null ) {
$t_query = 'SELECT COUNT(id) FROM {user} WHERE access_level>=' . db_param();
if( $p_enabled === true ) {
$t_query .= ' AND enabled = 1';
} else if( $p_enabled === false ) {
$t_query .= ' AND enabled = 0';
}
$t_result = db_query( $t_query, array( $p_level ) );
# Get the list of connected users
$t_users = db_result( $t_result );
return $t_users;
}
/**
* Return an array of user ids that are logged in.
* A user is considered logged in if the last visit timestamp is within the
* specified session duration.
* If the session duration is 0, then no users will be returned.
* @param integer $p_session_duration_in_minutes The duration to return logged in users for.
* @return array
*/
function user_get_logged_in_user_ids( $p_session_duration_in_minutes ) {
$t_session_duration_in_minutes = (integer)$p_session_duration_in_minutes;
# if session duration is 0, then there is no logged in users.
if( $t_session_duration_in_minutes == 0 ) {
return array();
}
# Generate timestamp
$t_last_timestamp_threshold = mktime( date( 'H' ), date( 'i' ) - 1 * $t_session_duration_in_minutes, date( 's' ), date( 'm' ), date( 'd' ), date( 'Y' ) );
# Execute query
$t_query = 'SELECT id FROM {user} WHERE last_visit > ' . db_param();
$t_result = db_query( $t_query, array( $t_last_timestamp_threshold ), 1 );
# Get the list of connected users
$t_users_connected = array();
while( $t_row = db_fetch_array( $t_result ) ) {
$t_users_connected[] = $t_row['id'];
}
return $t_users_connected;
}
/**
* Create a user.
* returns false if error, the generated cookie string if valid
*
* @param string $p_username A valid username.
* @param string $p_password The password to set for the user.
* @param string $p_email The Email Address of the user.
* @param integer $p_access_level The global access level for the user.
* @param boolean $p_protected Whether the account is protected from modifications (default false).
* @param boolean $p_enabled Whether the account is enabled.
* @param string $p_realname The realname of the user.
* @param string $p_admin_name The name of the administrator creating the account.
* @return string Cookie String
*/
function user_create( $p_username, $p_password, $p_email = '',
$p_access_level = null, $p_protected = false, $p_enabled = true,
$p_realname = '', $p_admin_name = '' ) {
if( null === $p_access_level ) {
$p_access_level = config_get( 'default_new_account_access_level' );
}
$t_password = auth_process_plain_password( $p_password );
$c_enabled = (bool)$p_enabled;
user_ensure_name_valid( $p_username );
user_ensure_name_unique( $p_username );
user_ensure_email_unique( $p_email );
user_ensure_realname_unique( $p_username, $p_realname );
email_ensure_valid( $p_email );
$t_cookie_string = auth_generate_unique_cookie_string();
$t_query = 'INSERT INTO {user}
( username, email, password, date_created, last_visit,
enabled, access_level, login_count, cookie_string, realname )
VALUES
( ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ', ' . db_param() . ',
' . db_param() . ',' . db_param() . ',' . db_param() . ',' . db_param() . ', ' . db_param() . ')';
db_query( $t_query, array( $p_username, $p_email, $t_password, db_now(), db_now(), $c_enabled, (int)$p_access_level, 0, $t_cookie_string, $p_realname ) );
# Create preferences for the user
$t_user_id = db_insert_id( db_get_table( 'user' ) );
# Users are added with protected set to FALSE in order to be able to update
# preferences. Now set the real value of protected.
if( $p_protected ) {
user_set_field( $t_user_id, 'protected', (bool)$p_protected );
}
# Send notification email
if( !is_blank( $p_email ) ) {
$t_confirm_hash = auth_generate_confirm_hash( $t_user_id );
email_signup( $t_user_id, $t_confirm_hash, $p_admin_name );
}
event_signal( 'EVENT_MANAGE_USER_CREATE', array( $t_user_id ) );
return $t_cookie_string;
}
/**
* Signup a user.
* If the use_ldap_email config option is on then tries to find email using
* ldap. $p_email may be empty, but the user wont get any emails.
* returns false if error, the generated cookie string if ok
* @param string $p_username The username to sign up.
* @param string $p_email The email address of the user signing up.
* @return string|boolean cookie string or false on error
*/
function user_signup( $p_username, $p_email = null ) {
if( null === $p_email ) {
$p_email = '';
# @@@ I think the ldap_email stuff is a bit borked
# Where is it being set? When is it being used?
# Shouldn't we override an email that is passed in here?
# If the user doesn't exist in ldap, is the account created?
# If so, there password won't get set anywhere... (etc)
# RJF: I was going to check for the existence of an LDAP email.
# however, since we can't create an LDAP account at the moment,
# and we don't know the user password in advance, we may not be able
# to retrieve it anyway.
# I'll re-enable this once a plan has been properly formulated for LDAP
# account management and creation.
# $t_email = '';
# if( ON == config_get( 'use_ldap_email' ) ) {
# $t_email = ldap_email_from_username( $p_username );
# }
# if( !is_blank( $t_email ) ) {
# $p_email = $t_email;
# }
}
$p_email = trim( $p_email );
email_ensure_not_disposable( $p_email );
email_ensure_valid( $p_email );
user_ensure_email_unique( $p_email );
# Create random password
$t_password = auth_generate_random_password();
return user_create( $p_username, $t_password, $p_email );
}
/**
* delete project-specific user access levels.
* returns true when successfully deleted
*
* @param integer $p_user_id A valid user identifier.
* @return boolean Always true
*/
function user_delete_project_specific_access_levels( $p_user_id ) {
user_ensure_unprotected( $p_user_id );
$t_query = 'DELETE FROM {project_user_list} WHERE user_id=' . db_param();
db_query( $t_query, array( (int)$p_user_id ) );
user_clear_cache( $p_user_id );
return true;
}
/**
* delete profiles for the specified user
* returns true when successfully deleted
* @param integer $p_user_id A valid user identifier.
* @return boolean
*/
function user_delete_profiles( $p_user_id ) {
user_ensure_unprotected( $p_user_id );
# Remove associated profiles
$t_query = 'DELETE FROM {user_profile} WHERE user_id=' . db_param();
db_query( $t_query, array( (int)$p_user_id ) );
user_clear_cache( $p_user_id );
return true;
}
/**
* delete a user account (account, profiles, preferences, project-specific access levels)
* returns true when the account was successfully deleted
*
* @param integer $p_user_id A valid user identifier.
* @return boolean Always true
*/
function user_delete( $p_user_id ) {
$c_user_id = (int)$p_user_id;
user_ensure_unprotected( $p_user_id );
event_signal( 'EVENT_MANAGE_USER_DELETE', array( $p_user_id ) );
# Remove associated profiles
user_delete_profiles( $p_user_id );
# Remove associated preferences
user_pref_delete_all( $p_user_id );
# Remove project specific access levels
user_delete_project_specific_access_levels( $p_user_id );
# unset non-unique realname flags if necessary
if( config_get( 'differentiate_duplicates' ) ) {
$c_realname = user_get_field( $p_user_id, 'realname' );
$t_query = 'SELECT id FROM {user} WHERE realname=' . db_param();
$t_result = db_query( $t_query, array( $c_realname ) );
$t_users = array();
while( $t_row = db_fetch_array( $t_result ) ) {
$t_users[] = $t_row;
}
$t_user_count = count( $t_users );
if( $t_user_count == 2 ) {
# unset flags if there are now only 2 unique names
for( $i = 0;$i < $t_user_count;$i++ ) {
$t_user_id = $t_users[$i]['id'];
user_set_field( $t_user_id, 'duplicate_realname', OFF );
}
}
}
user_clear_cache( $p_user_id );
# Remove account
$t_query = 'DELETE FROM {user} WHERE id=' . db_param();
db_query( $t_query, array( $c_user_id ) );
return true;
}
/**
* get a user id from a username
* return false if the username does not exist
*
* @param string $p_username The username to retrieve data for.
* @return integer|boolean
*/
function user_get_id_by_name( $p_username ) {
if( $t_user = user_search_cache( 'username', $p_username ) ) {
return $t_user['id'];
}
$t_query = 'SELECT * FROM {user} WHERE username=' . db_param();
$t_result = db_query( $t_query, array( $p_username ) );
$t_row = db_fetch_array( $t_result );
if( $t_row ) {
user_cache_database_result( $t_row );
return $t_row['id'];
}
return false;
}
/**
* Get a user id from their email address
*
* @param string $p_email The email address to retrieve data for.
* @return array
*/
function user_get_id_by_email( $p_email ) {
global $g_cache_user;
if( $t_user = user_search_cache( 'email', $p_email ) ) {
return $t_user['id'];
}
$t_query = 'SELECT * FROM {user} WHERE email=' . db_param();
$t_result = db_query( $t_query, array( $p_email ) );
$t_row = db_fetch_array( $t_result );
if( $t_row ) {
user_cache_database_result( $t_row );
return $t_row['id'];
}
return false;
}
/**
* Given an email address, this method returns the ids of the enabled users with
* that address.
*
* The returned list will be sorted by higher access level first.
*
* @param string $p_email The email address, can be an empty string to get users
* without an email address.
*
* @return array The user ids or an empty array.
*/
function user_get_enabled_ids_by_email( $p_email ) {
$t_query = 'SELECT * FROM {user} WHERE email=' . db_param() .
' AND enabled=' . db_param() . ' ORDER BY access_level DESC';
$t_result = db_query( $t_query, array( $p_email, 1 ) );
$t_user_ids = array();
while ( $t_row = db_fetch_array( $t_result ) ) {
user_cache_database_result( $t_row );
$t_user_ids[] = (int)$t_row['id'];
}
return $t_user_ids;
}
/**
* Get a user id from their real name
*
* @param string $p_realname The realname to retrieve data for.
* @return array
*/
function user_get_id_by_realname( $p_realname ) {
global $g_cache_user;
if( $t_user = user_search_cache( 'realname', $p_realname ) ) {
return $t_user['id'];
}
$t_query = 'SELECT * FROM {user} WHERE realname=' . db_param();
$t_result = db_query( $t_query, array( $p_realname ) );
$t_row = db_fetch_array( $t_result );
if( !$t_row ) {
return false;
} else {
user_cache_database_result( $t_row );
return $t_row['id'];
}
}
/**
* return all data associated with a particular user name
* return false if the username does not exist
*
* @param integer $p_username The username to retrieve data for.
* @return array
*/
function user_get_row_by_name( $p_username ) {
$t_user_id = user_get_id_by_name( $p_username );
if( false === $t_user_id ) {
return false;
}
$t_row = user_get_row( $t_user_id );
return $t_row;
}
/**
* return a user row
*
* @param integer $p_user_id A valid user identifier.
* @return array
*/
function user_get_row( $p_user_id ) {
return user_cache_row( $p_user_id );
}
/**
* return the specified user field for the user id
*
* @param integer $p_user_id A valid user identifier.
* @param string $p_field_name The field name to retrieve.
* @return string
*/
function user_get_field( $p_user_id, $p_field_name ) {
if( NO_USER == $p_user_id ) {
error_parameters( NO_USER );
trigger_error( ERROR_USER_BY_ID_NOT_FOUND, WARNING );
return '@null@';
}
$t_row = user_get_row( $p_user_id );
if( isset( $t_row[$p_field_name] ) ) {
switch( $p_field_name ) {
case 'access_level':
return (int)$t_row[$p_field_name];
default:
return $t_row[$p_field_name];
}
} else {
error_parameters( $p_field_name );
trigger_error( ERROR_DB_FIELD_NOT_FOUND, WARNING );
return '';
}
}
/**
* lookup the user's email in LDAP or the db as appropriate
*
* @param integer $p_user_id A valid user identifier.
* @return string
*/
function user_get_email( $p_user_id ) {
$t_email = '';
if( LDAP == config_get( 'login_method' ) && ON == config_get( 'use_ldap_email' ) ) {
$t_email = ldap_email( $p_user_id );
}
if( is_blank( $t_email ) ) {
$t_email = user_get_field( $p_user_id, 'email' );
}
return $t_email;
}
/**
* lookup the user's realname
*
* @param integer $p_user_id A valid user identifier.
* @return string
*/
function user_get_realname( $p_user_id ) {
$t_realname = '';
if( LDAP == config_get( 'login_method' ) && ON == config_get( 'use_ldap_realname' ) ) {
$t_realname = ldap_realname( $p_user_id );
}
if( is_blank( $t_realname ) ) {
$t_realname = user_get_field( $p_user_id, 'realname' );
}
return $t_realname;
}
/**
* return the username or a string "user<id>" if the user does not exist
* if show_user_realname_threshold is set and real name is not empty, return it instead
*
* @param integer $p_user_id A valid user identifier.
* @return string
*/
function user_get_name( $p_user_id ) {
$t_row = user_cache_row( $p_user_id, false );
if( false == $t_row ) {
return lang_get( 'prefix_for_deleted_users' ) . (int)$p_user_id;
} else {
if( ON == config_get( 'show_realname' ) ) {
if( is_blank( $t_row['realname'] ) ) {
return $t_row['username'];
} else {
if( isset( $t_row['duplicate_realname'] ) && ( ON == $t_row['duplicate_realname'] ) ) {
return $t_row['realname'] . ' (' . $t_row['username'] . ')';
} else {
return $t_row['realname'];
}
}
} else {
return $t_row['username'];
}
}
}
/**
* Return the user avatar image URL
* in this first implementation, only gravatar.com avatars are supported
*
* This function returns an array( URL, width, height ) or an empty array when the given user has no avatar.
*
* @param integer $p_user_id A valid user identifier.
* @param integer $p_size The required number of pixel in the image to retrieve the link for.
* @return array
*/
function user_get_avatar( $p_user_id, $p_size = 80 ) {
$t_default_avatar = config_get( 'show_avatar' );
if( OFF === $t_default_avatar ) {
# Avatars are not used
return array();
}
# Set default avatar for legacy configuration
if( ON === $t_default_avatar ) {
$t_default_avatar = 'identicon';
}
# Default avatar is either one of Gravatar's options, or
# assumed to be an URL to a default avatar image
$t_default_avatar = urlencode( $t_default_avatar );
$t_rating = 'G';
if ( user_exists( $p_user_id ) ) {
$t_email_hash = md5( strtolower( trim( user_get_email( $p_user_id ) ) ) );
} else {
$t_email_hash = md5( 'generic-avatar-since-user-not-found' );
}