3 * FusionForge user management
5 * Copyright 1999-2001, VA Linux Systems, Inc.
6 * Copyright 2009-2010, Roland Mas
7 * Copyright 2011, Franck Villaume - Capgemini
8 * http://fusionforge.org
10 * This file is part of FusionForge.
12 * FusionForge is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published
14 * by the Free Software Foundation; either version 2 of the License,
15 * or (at your option) any later version.
17 * FusionForge is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with FusionForge; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 * user_get_object_by_name() - Get User object by username.
32 * user_get_object is useful so you can pool user objects/save database queries
33 * You should always use this instead of instantiating the object directly
35 * @param string The unix username - required
36 * @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
37 * @return a user object or false on failure
39 function &user_get_object_by_name($user_name, $res = false) {
40 $user_name = strtolower($user_name);
42 $res = db_query_params('SELECT * FROM users WHERE user_name=$1',
45 return user_get_object(db_result($res, 0, 'user_id'), $res);
49 * user_get_object_by_email() - Get User object by email address
50 * Only works if sys_require_unique_email is true
52 * @param string The unix username - required
53 * @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
54 * @return a user object or false on failure
57 function user_get_object_by_email($email ,$res = false) {
58 if (!validate_email($email)
59 || !forge_get_config('require_unique_email')) {
63 $res=db_query_params('SELECT * FROM users WHERE email=$1',
66 return user_get_object(db_result($res, 0, 'user_id'), $res);
70 * user_get_object() - Get User object by user ID.
71 * user_get_object is useful so you can pool user objects/save database queries
72 * You should always use this instead of instantiating the object directly
74 * @param int The ID of the user - required
75 * @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
76 * @return object a user object or false on failure
78 function &user_get_object($user_id, $res = false) {
79 //create a common set of group objects
80 //saves a little wear on the database
82 //automatically checks group_type and
83 //returns appropriate object
86 if (!isset($USER_OBJ["_".$user_id."_"])) {
88 //the db result handle was passed in
90 $res = db_query_params('SELECT * FROM users WHERE user_id=$1',
93 if (!$res || db_numrows($res) < 1) {
94 $USER_OBJ["_".$user_id."_"]=false;
96 $USER_OBJ["_".$user_id."_"]= new GFUser($user_id,$res);
99 return $USER_OBJ["_".$user_id."_"];
102 function &user_get_objects($id_arr) {
107 foreach ($id_arr as $id) {
109 // See if this ID already has been fetched in the cache
111 if (!isset($USER_OBJ["_".$id."_"])) {
115 if (count($fetch) > 0) {
116 $res = db_query_params('SELECT * FROM users WHERE user_id = ANY ($1)',
117 array(db_int_array_to_any_clause ($fetch)));
118 while ($arr = db_fetch_array($res)) {
119 $USER_OBJ["_".$arr['user_id']."_"] = new GFUser($arr['user_id'],$arr);
122 foreach ($id_arr as $id) {
123 $return[] =& $USER_OBJ["_".$id."_"];
128 function &user_get_objects_by_name($username_arr) {
129 $res = db_query_params('SELECT user_id FROM users WHERE lower(user_name) = ANY ($1)',
130 array(db_string_array_to_any_clause ($username_arr)));
131 $arr =& util_result_column_to_array($res, 0);
132 return user_get_objects($arr);
135 function &user_get_objects_by_email($email_arr) {
136 $res=db_query_params('SELECT user_id FROM users WHERE lower(email) = ANY ($1)',
137 array(db_string_array_to_any_clause ($email_arr)));
138 $arr =& util_result_column_to_array($res, 0);
139 return user_get_objects($arr);
142 function &user_get_active_users() {
143 $res=db_query_params('SELECT user_id FROM users WHERE status=$1',
145 return user_get_objects(util_result_column_to_array($res, 0));
148 class GFUser extends Error {
150 * Associative array of data from db.
152 * @var array $data_array.
157 * Is this person a site super-admin?
159 * @var bool $is_super_user
164 * Is this person the logged in user?
166 * @var bool $is_logged_in
171 * Array of preferences
173 * @var array $user_pref
181 * GFUser($id,$res) - CONSTRUCTOR - GENERALLY DON'T USE THIS
183 * instead use the user_get_object() function call
185 * @param int The user_id
186 * @param int The database result set OR array of data
188 function GFUser($id=false,$res=false) {
191 //setting up an empty object
192 //probably going to call create()
196 $this->fetchData($id);
198 if (is_array($res)) {
199 $this->data_array =& $res;
200 } elseif (db_numrows($res) < 1) {
201 //function in class we extended
202 $this->setError(_('User Not Found'));
203 $this->data_array=array();
206 //set up an associative array for use by other functions
207 $this->data_array = db_fetch_array_by_row($res, 0);
210 $this->is_super_user=false;
211 $this->is_logged_in=false;
216 * create() - Create a new user.
218 * @param string The unix username.
219 * @param string The real firstname.
220 * @param string The real lastname.
221 * @param string The first password.
222 * @param string The confirmation password.
223 * @param string The users email address.
224 * @param string The users preferred default language.
225 * @param string The users preferred default timezone.
226 * @param string The users preference for receiving site updates by email.
227 * @param string The users preference for receiving community updates by email.
228 * @param int The ID of the language preference.
229 * @param string The users preferred timezone.
230 * @param string The users Jabber address.
231 * @param int The users Jabber preference.
232 * @param int The users theme_id.
233 * @param string The users unix_box.
234 * @param string The users address.
235 * @param string The users address part 2.
236 * @param string The users phone.
237 * @param string The users fax.
238 * @param string The users title.
239 * @param char(2) The users ISO country_code.
240 * @param bool Whether to send an email or not
241 * @param int The users preference for tooltips
242 * @returns The newly created user ID
245 function create($unix_name, $firstname, $lastname, $password1, $password2, $email,
246 $mail_site, $mail_va, $language_id, $timezone, $jabber_address, $jabber_only, $theme_id,
247 $unix_box = 'shell', $address = '', $address2 = '', $phone = '', $fax = '', $title = '', $ccode = 'US', $send_mail = true, $tooltips = true) {
250 $this->setError(_('You must supply a theme'));
253 if (! forge_get_config('require_unique_email')) {
255 $this->setError(_('You must supply a username'));
260 $this->setError(_('You must supply a first name'));
264 $this->setError(_('You must supply a last name'));
268 $this->setError(_('You must supply a password'));
271 if ($password1 != $password2) {
272 $this->setError(_('Passwords do not match'));
275 if (!account_pwvalid($password1)) {
276 $this->setError(_('Invalid Password:'));
279 $unix_name=strtolower($unix_name);
280 if (!account_namevalid($unix_name)) {
281 $this->setError(_('Invalid Unix Name.'));
284 if (!$SYS->sysUseUnixName($unix_name)) {
285 $this->setError(_('Unix name already taken'));
288 if (!validate_email($email)) {
289 $this->setError(_('Invalid Email Address:') .' '. $email);
292 if ($jabber_address && !validate_email($jabber_address)) {
293 $this->setError(_('Invalid Jabber Address'));
301 if ($unix_name && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name LIKE $1',
302 array($unix_name))) > 0) {
303 $this->setError(_('That username already exists.'));
306 if (forge_get_config('require_unique_email')) {
307 if (user_get_object_by_email('$email')) {
308 $this->setError(_('User with this email already exists - use people search to recover your login.'));
312 if (forge_get_config('require_unique_email') && !$unix_name) {
313 // Let's generate a loginname for the user
314 // ...based on the email address:
315 $email_array = explode ('@', $email, 2) ;
316 $email_u = $email_array [0];
317 $l = ereg_replace('[^a-z0-9]', '', $email_u);
318 $l = substr ($l, 0, 15);
319 // Is the user part of the email address okay?
320 if (account_namevalid($l)
321 && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name = $1',
325 // No? What if we add a number at the end?
328 $c = substr ($l, 0, 15-strlen ("$i")) . "$i" ;
329 if (account_namevalid($c)
330 && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name = $1',
338 // If we're really unlucky, then let's go brute-force
339 while (!$unix_name) {
340 $c = substr (md5($email . util_randbytes()), 0, 15);
341 if (account_namevalid($c)
342 && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name = $1',
348 $unix_name = strtolower($unix_name);
349 if (!account_namevalid($unix_name)) {
350 $this->setError(_('Invalid Unix Name.'));
353 // if we got this far, it must be good
354 $confirm_hash = substr(md5($password1 . util_randbytes() . microtime()),0,16);
356 $result = db_query_params('INSERT INTO users (user_name,user_pw,unix_pw,realname,firstname,lastname,email,add_date,status,confirm_hash,mail_siteupdates,mail_va,language,timezone,jabber_address,jabber_only,unix_box,address,address2,phone,fax,title,ccode,theme_id,tooltips)
357 VALUES ($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)',
360 account_genunixpw($password1),
361 htmlspecialchars($firstname.' '.$lastname),
362 htmlspecialchars($firstname),
363 htmlspecialchars($lastname),
368 (($mail_site)?"1":"0"),
369 (($mail_va)?"1":"0"),
375 htmlspecialchars($address),
376 htmlspecialchars($address2),
377 htmlspecialchars($phone),
378 htmlspecialchars($fax),
379 htmlspecialchars($title),
384 $this->setError(_('Insert Failed: ') . db_error());
389 $id = db_insertid($result, 'users', 'user_id');
391 $this->setError('Could Not Get USERID: ' .db_error());
396 if (!$this->fetchData($id)) {
401 $hook_params = array();
402 $hook_params['user'] = $this;
403 $hook_params['user_id'] = $this->getID();
404 $hook_params['user_name'] = $unix_name;
405 $hook_params['user_password'] = $password1;
406 plugin_hook("user_create", $hook_params);
409 setup_gettext_from_lang_id($language_id);
410 $this->sendRegistrationEmail();
411 setup_gettext_from_context();
420 * sendRegistrationEmail() - Send email for registration verification
422 * @return bool success or not
424 function sendRegistrationEmail() {
425 $message=stripcslashes(sprintf(_('Thank you for registering on the %3$s web site. You have
426 account with username %1$s created for you. In order
427 to complete your registration, visit the following url:
431 You have 1 week to confirm your account. After this time, your account will be deleted.
433 (If you don\'t see any URL above, it is likely due to a bug in your mail client.
434 Use one below, but make sure it is entered as the single line.)
442 $this->getUnixName(),
443 util_make_url ('/account/verify.php?confirm_hash=_'.$this->getConfirmHash()),
444 forge_get_config ('forge_name')));
447 sprintf(_('%1$s Account Registration'), forge_get_config ('forge_name')),
453 * delete() - remove the User from all his groups.
455 * Remove the User from all his groups and set his status to D.
457 * @param boolean Confirmation of deletion.
458 * @return boolean success or not
460 function delete($sure) {
464 $groups = &$this->getGroups();
465 if (is_array($groups)) {
466 foreach ($groups as $group) {
467 $group->removeUser($this->getID());
472 $res = db_query_params('DELETE FROM artifact_monitor WHERE user_id=$1',
473 array($this->getID()));
475 $this->setError('ERROR - ' . _('Could Not Delete From artifact_monitor:') . ' '.db_error());
479 $res = db_query_params('DELETE FROM artifact_type_monitor WHERE user_id=$1',
480 array($this->getID()));
482 $this->setError('ERROR - ' . _('Could Not Delete From artifact_type_monitor:') . ' ' .db_error());
486 $res = db_query_params('DELETE FROM forum_monitored_forums WHERE user_id=$1',
487 array($this->getID()));
489 $this->setError('ERROR - ' . _('Could Not Delete From forum_monitored_forums:') . ' '.db_error());
493 $res = db_query_params('DELETE FROM filemodule_monitor WHERE user_id=$1',
494 array($this->getID()));
496 $this->setError('ERROR - ' . _('Could Not Delete From filemodule_monitor:') . ' '.db_error());
501 $hook_params = array ();
502 $hook_params['user'] = $this;
503 $hook_params['user_id'] = $this->getID();
504 plugin_hook("user_delete", $hook_params);
506 $this->setStatus('D');
513 * update() - update *common* properties of GFUser object.
515 * Use specific setter to change other properties.
517 * @param string The users first name.
518 * @param string The users last name.
519 * @param int The ID of the users language preference.
520 * @param string The useres timezone preference.
521 * @param string The users preference for receiving site updates by email.
522 * @param string The users preference for receiving community updates by email.
523 * @param string The users preference for being participating in "peer ratings".
524 * @param string The users Jabber account address.
525 * @param int The users Jabber preference.
526 * @param int The users theme_id preference.
527 * @param string The users address.
528 * @param string The users address2.
529 * @param string The users phone.
530 * @param string The users fax.
531 * @param string The users title.
532 * @param string The users ccode.
533 * @param int The users preference for tooltips.
534 * @param string The users email.
536 function update($firstname, $lastname, $language_id, $timezone, $mail_site, $mail_va, $use_ratings,
537 $jabber_address, $jabber_only, $theme_id, $address, $address2, $phone, $fax, $title, $ccode, $tooltips, $email='') {
538 $mail_site = $mail_site ? 1 : 0;
539 $mail_va = $mail_va ? 1 : 0;
540 $block_ratings = $use_ratings ? 0 : 1;
542 if ($jabber_address && !validate_email($jabber_address)) {
543 $this->setError(_('Invalid Jabber Address'));
554 $res = db_query_params('
577 htmlspecialchars($firstname . ' ' .$lastname),
578 htmlspecialchars($firstname),
579 htmlspecialchars($lastname),
587 htmlspecialchars($address) ,
588 htmlspecialchars($address2) ,
589 htmlspecialchars($phone) ,
590 htmlspecialchars($fax) ,
591 htmlspecialchars($title) ,
598 $this->setError(_('ERROR - Could Not Update User Object:'). ' ' .db_error());
603 if ($email && $email != $this->getEmail()
604 && !$this->setEmail($email)) {
608 // If there's a transaction from using to not
609 // using ratings, remove all rating made by the
610 // user (ratings by others should not be removed,
611 // as it opens possibility to abuse rate system)
612 if (!$use_ratings && $this->usesRatings()) {
613 db_query_params('DELETE FROM user_ratings WHERE rated_by=$1',
616 if (!$this->fetchData($this->getID())) {
621 $hook_params = array ();
622 $hook_params['user'] = $this;
623 $hook_params['user_id'] = $this->getID();
624 plugin_hook ("user_update", $hook_params);
631 * fetchData - May need to refresh database fields.
633 * If an update occurred and you need to access the updated info.
635 * @param int the User ID data to be fecthed
636 * @return boolean success;
638 function fetchData($user_id) {
639 $res = db_query_params ('SELECT * FROM users WHERE user_id=$1',
641 if (!$res || db_numrows($res) < 1) {
642 $this->setError('GFUser::fetchData():: '.db_error());
645 $this->data_array = db_fetch_array($res);
650 * getID - Simply return the user_id for this object.
652 * @return int This user's user_id number.
655 return $this->data_array['user_id'];
659 * getStatus - get the status of this user.
661 * Statuses include (A)ctive, (P)ending, (S)uspended ,(D)eleted.
663 * @return char This user's status flag.
665 function getStatus() {
666 return $this->data_array['status'];
670 * setStatus - set this user's status.
672 * @param string Status - P, A, S, or D.
673 * @return boolean success.
675 function setStatus($status) {
677 if ($status != 'P' && $status != 'A'
678 && $status != 'S' && $status != 'D') {
679 $this->setError(_('ERROR: Invalid status value'));
682 if ($this->getStatus() != 'P' && $status == 'P') {
683 $this->setError('ERROR: You can\'t set pending status if user is suspend or active');
688 $res = db_query_params ('UPDATE users SET status=$1 WHERE user_id=$2',
693 $this->setError(_('ERROR - Could Not Update User Status:') . ' ' .db_error());
697 $this->data_array['status']=$status;
698 if ($status == 'D') {
699 $projects = $this->getGroups() ;
700 foreach ($projects as $p) {
701 $p->removeUser ($this->getID()) ;
705 $hook_params = array ();
706 $hook_params['user'] = $this;
707 $hook_params['user_id'] = $this->getID();
708 $hook_params['status'] = $status;
709 plugin_hook ("user_setstatus", $hook_params);
718 * isActive - whether this user is confirmed and active.
720 * Database field status of 'A' returns true.
721 * @return boolean is_active.
723 function isActive() {
724 if ($this->getStatus()=='A') {
732 * getUnixStatus - Status of activation of unix account.
734 * @return char (N)one, (A)ctive, (S)uspended or (D)eleted
736 function getUnixStatus() {
737 return $this->data_array['unix_status'];
741 * setUnixStatus - Sets status of activation of unix account.
743 * @param string The unix status.
749 * @return boolean success.
751 function setUnixStatus($status) {
754 $res = db_query_params ('UPDATE users SET unix_status=$1 WHERE user_id=$2',
759 $this->setError('ERROR - Could Not Update User Unix Status: '.db_error());
763 if ($status == 'A') {
764 if (!$SYS->sysCheckCreateUser($this->getID())) {
765 $this->setError($SYS->getErrorMessage());
770 if ($SYS->sysCheckUser($this->getID())) {
771 if (!$SYS->sysRemoveUser($this->getID())) {
772 $this->setError($SYS->getErrorMessage());
779 $this->data_array['unix_status']=$status;
786 * getUnixName - the user's unix_name.
788 * @return string This user's unix/login name.
790 function getUnixName() {
791 return strtolower($this->data_array['user_name']);
795 * getUnixPasswd - get the user's password.
797 * @return string This user's unix crypted passwd.
799 function getUnixPasswd() {
800 return $this->data_array['unix_pw'];
804 * getUnixBox - the hostname of the unix box this user has an account on.
806 * @return string This user's shell login machine.
808 function getUnixBox() {
809 return $this->data_array['unix_box'];
813 * getMD5Passwd - the password.
815 * @return string This user's MD5-crypted passwd.
817 function getMD5Passwd() {
818 return $this->data_array['user_pw'];
821 //Added to be compatible with codendi getUserPw function
822 function getUserPw() {
823 return $this->data_array['user_pw'];
827 * getConfirmHash - the confirm hash in the db.
829 * @return string This user's confirmation hash.
831 function getConfirmHash() {
832 return $this->data_array['confirm_hash'];
836 * getEmail - the user's email address.
838 * @return string This user's email address.
840 function getEmail() {
841 return str_replace("\n", "", $this->data_array['email']);
845 * getSha1Email - a SHA1 encoded hash of the email URI (including mailto: prefix)
847 * @return string The SHA1 encoded value for the email
849 function getSha1Email() {
850 return sha1('mailto:'.$this->getEmail());
854 * getNewEmail - while changing an email address, it is stored here until confirmation.
856 * getNewEmail is a private operation for email change.
858 * @return string This user's new (not yet confirmed) email address.
861 function getNewEmail() {
862 return $this->data_array['email_new'];
866 * setEmail - set a new email address, which must be confirmed.
868 * @param string The email address.
869 * @return boolean success.
871 function setEmail($email) {
873 if (!strcasecmp($this->getEmail(), $email)) {
877 if (!$email || !validate_email($email)) {
878 $this->setError('ERROR: Invalid Email');
882 if (forge_get_config('require_unique_email')) {
883 if (db_numrows(db_query_params('SELECT user_id FROM users WHERE user_id!=$1 AND (lower(email) LIKE $2 OR lower(email_new) LIKE $2)',
884 array ($this->getID(),
885 strtolower($email)))) > 0) {
886 $this->setError(_('User with this email already exists.'));
892 $res = db_query_params ('UPDATE users SET email=$1 WHERE user_id=$2',
897 $this->setError('ERROR - Could Not Update User Email: '.db_error());
901 $hook_params = array ();
902 $hook_params['user'] = $this;
903 $hook_params['user_id'] = $this->getID();
904 $hook_params['user_email'] = $email;
905 plugin_hook("user_setemail", $hook_params);
907 if (!$this->fetchData($this->getId())) {
918 * setNewEmailAndHash - setNewEmailAndHash is a private operation for email change.
920 * @param string The email address.
921 * @param string The email hash.
922 * @return boolean success.
924 function setNewEmailAndHash($email, $hash='') {
927 $hash = substr(md5(strval(time()) . strval(util_randbytes())), 0, 16);
930 if (!$email || !validate_email($email)) {
931 $this->setError('ERROR - Invalid Email');
935 if (forge_get_config('require_unique_email')) {
936 if (db_numrows(db_query_params('SELECT user_id FROM users WHERE user_id!=$1 AND (lower(email) LIKE $2 OR lower(email_new) LIKE $2)',
937 array ($this->getID(),
938 strtolower($email)))) > 0) {
939 $this->setError(_('User with this email already exists.'));
943 $res = db_query_params ('UPDATE users SET confirm_hash=$1, email_new=$2 WHERE user_id=$3',
948 $this->setError('ERROR - Could Not Update User Email And Hash: '.db_error());
951 $this->data_array['email_new'] = $email;
952 $this->data_array['confirm_hash'] = $hash;
958 * getRealName - get the user's real name.
960 * @return string This user's real name.
962 function getRealName() {
963 $last_name = $this->getLastName();
964 return $this->getFirstName(). ($last_name ? ' ' .$last_name:'');
968 * getFirstName - get the user's first name.
970 * @return string This user's first name.
972 function getFirstName() {
973 return $this->data_array['firstname'];
977 * getLastName - get the user's last name.
979 * @return string This user's last name.
981 function getLastName() {
982 return $this->data_array['lastname'];
986 * getAddDate - this user's unix time when account was opened.
988 * @return int This user's unix time when account was opened.
990 function getAddDate() {
991 return $this->data_array['add_date'];
995 * getTimeZone - this user's timezone setting.
997 * @return string This user's timezone setting.
999 function getTimeZone() {
1000 return $this->data_array['timezone'];
1004 * getCountryCode - this user's ccode setting.
1006 * @return string This user's ccode setting.
1008 function getCountryCode() {
1009 return $this->data_array['ccode'];
1013 * getShell - this user's preferred shell.
1015 * @return string This user's preferred shell.
1017 function getShell() {
1018 return $this->data_array['shell'];
1022 * setShell - sets user's preferred shell.
1024 * @param string The users preferred shell.
1025 * @return boolean success.
1027 function setShell($shell) {
1029 $shells = file('/etc/shells');
1030 $shells[count($shells)] = "/bin/cvssh";
1031 $out_shells = array();
1032 foreach ($shells as $s) {
1033 if (substr($s, 0, 1) == '#') {
1036 $out_shells[] = chop($s);
1038 if (!in_array($shell, $out_shells)) {
1039 $this->setError(_('ERROR: Invalid Shell'));
1044 $res = db_query_params ('UPDATE users SET shell=$1 WHERE user_id=$2',
1048 $this->setError(_('ERROR - Could Not Update User Unix Shell:') . ' ' .db_error());
1052 // Now change LDAP attribute, but only if corresponding
1053 // entry exists (i.e. if user have shell access)
1054 if ($SYS->sysCheckUser($this->getID()))
1056 if (!$SYS->sysUserSetAttribute($this->getID(),"loginShell",$shell)) {
1057 $this->setError($SYS->getErrorMessage());
1062 $this->data_array['shell']=$shell;
1069 * getUnixUID() - Get the unix UID of the user
1071 * @return int This user's UID.
1073 function getUnixUID() {
1074 return $this->data_array['unix_uid'];
1078 * getUnixGID() - Get the unix GID of the user
1080 * @return int This user's GID.
1082 function getUnixGID() {
1083 return $this->data_array['unix_gid'];
1087 * getLanguage - this user's language_id from supported_languages table.
1089 * @return int This user's language_id.
1091 function getLanguage() {
1092 return $this->data_array['language'];
1096 * getJabberAddress - this user's optional jabber address.
1098 * @return string This user's jabber address.
1100 function getJabberAddress() {
1101 return $this->data_array['jabber_address'];
1105 * getJabberOnly - whether this person wants updates sent ONLY to jabber.
1107 * @return boolean This user's jabber preference.
1109 function getJabberOnly() {
1110 return $this->data_array['jabber_only'];
1114 * getAddress - get this user's address.
1116 * @return text This user's address.
1118 function getAddress() {
1119 return $this->data_array['address'];
1123 * getAddress2 - get this user's address2.
1125 * @return text This user's address2.
1127 function getAddress2() {
1128 return $this->data_array['address2'];
1132 * getPhone - get this person's phone number.
1134 * @return text This user's phone number.
1136 function getPhone() {
1137 return $this->data_array['phone'];
1141 * getFax - get this person's fax number.
1143 * @return text This user's fax.
1146 return $this->data_array['fax'];
1150 * getTitle - get this person's title.
1152 * @return text This user's title.
1154 function getTitle() {
1155 return $this->data_array['title'];
1159 * getGroups - get an array of groups this user is a member of.
1161 * @return array Array of groups.
1163 function &getGroups($onlylocal = true) {
1165 foreach ($this->getRoles() as $r) {
1167 if ($r instanceof RoleExplicit
1168 && $r->getHomeProject() != NULL) {
1169 $ids[] = $r->getHomeProject()->getID() ;
1172 foreach ($r->getLinkedProjects() as $p) {
1173 $ids[] = $p->getID() ;
1177 return group_get_objects(array_values(array_unique($ids))) ;
1181 * getAuthorizedKeys - the SSH authorized keys set by the user.
1183 * @return string This user's SSH authorized (public) keys.
1185 function getAuthorizedKeys() {
1186 return preg_replace("/###/", "\n", $this->data_array['authorized_keys']);
1190 * setAuthorizedKeys - set the SSH authorized keys for the user.
1192 * @param string The users public keys.
1193 * @return boolean success.
1195 function setAuthorizedKeys($keys) {
1196 $keys = trim($keys);
1197 $keys = preg_replace("/\r\n/", "\n", $keys); // Convert to Unix EOL
1198 $keys = preg_replace("/\n+/", "\n", $keys); // Remove empty lines
1199 $keys = preg_replace("/\n/", "###", $keys); // Convert EOL to marker
1201 $res = db_query_params('UPDATE users SET authorized_keys=$1 WHERE user_id=$2',
1205 $this->setError(_('ERROR - Could Not Update User SSH Keys'));
1208 $this->data_array['authorized_keys'] = $keys;
1214 * setLoggedIn($val) - Really only used by session code.
1216 * @param boolean The session value.
1218 function setLoggedIn($val=true) {
1219 $this->is_logged_in = $val;
1222 $this->is_super_user = forge_check_global_perm_for_user($this, 'forge_admin') ;
1227 * isLoggedIn - only used by session code.
1229 * @return boolean is_logged_in.
1231 function isLoggedIn() {
1232 return $this->is_logged_in;
1236 * deletePreference - delete a preference for this user.
1238 * @param string The unique field name for this preference.
1239 * @return boolean success.
1241 function deletePreference($preference_name) {
1242 $preference_name=strtolower(trim($preference_name));
1243 unset($this->user_pref["$preference_name"]);
1244 $res = db_query_params('DELETE FROM user_preferences WHERE user_id=$1 AND preference_name=$2',
1245 array ($this->getID(),
1251 * setPreference - set a new preference for this user.
1253 * @param string The unique field name for this preference.
1254 * @param string The value you are setting this preference to.
1255 * @return boolean success.
1257 function setPreference($preference_name,$value) {
1258 $preference_name=strtolower(trim($preference_name));
1259 //delete pref if not value passed in
1260 unset($this->user_pref);
1261 if (!isset($value)) {
1262 $result = db_query_params ('DELETE FROM user_preferences WHERE user_id=$1 AND preference_name=$2',
1263 array ($this->getID(),
1264 $preference_name)) ;
1266 $result = db_query_params ('UPDATE user_preferences SET preference_value=$1,set_date=$2 WHERE user_id=$3 AND preference_name=$4',
1270 $preference_name)) ;
1271 if (db_affected_rows($result) < 1) {
1273 $result = db_query_params ('INSERT INTO user_preferences (user_id,preference_name,preference_value,set_date) VALUES ($1,$2,$3,$4)',
1274 array ($this->getID(),
1284 * getPreference - get a specific preference.
1286 * @param string The unique field name for this preference.
1287 * @return string|bool the preference string or false on failure.
1289 function getPreference($preference_name) {
1290 $preference_name=strtolower(trim($preference_name));
1292 First check to see if we have already fetched the preferences
1294 if (isset($this->user_pref)) {
1295 //echo "\n\nPrefs were fetched already";
1296 if (isset($this->user_pref["$preference_name"])) {
1297 //we have fetched prefs - return part of array
1298 return $this->user_pref["$preference_name"];
1300 //we have fetched prefs, but this pref hasn't been set
1304 //we haven't returned prefs - go to the db
1305 $result = db_query_params ('SELECT preference_name,preference_value FROM user_preferences WHERE user_id=$1',
1306 array ($this->getID())) ;
1307 if (db_numrows($result) < 1) {
1308 //echo "\n\nNo Prefs Found";
1312 //iterate and put the results into an array
1313 for ($i=0; $i<db_numrows($result); $i++) {
1314 $pref["".db_result($result,$i,'preference_name').""]=db_result($result,$i,'preference_value');
1316 $this->user_pref = $pref;
1318 if (array_key_exists($preference_name,$this->user_pref)) {
1319 //we have fetched prefs - return part of array
1320 return $this->user_pref["$preference_name"];
1322 //we have fetched prefs, but this pref hasn't been set
1330 * setPasswd - Changes user's password.
1332 * @param string The plaintext password.
1333 * @return boolean success.
1335 function setPasswd($passwd) {
1337 if (!account_pwvalid($passwd)) {
1338 $this->setError('Error: '.$GLOBALS['register_error']);
1343 $md5_pw = md5 ($passwd) ;
1344 $unix_pw = account_genunixpw ($passwd) ;
1346 $res = db_query_params ('UPDATE users SET user_pw=$1, unix_pw=$2 WHERE user_id=$3',
1351 if (!$res || db_affected_rows($res) < 1) {
1352 $this->setError(_('ERROR - Could Not Change User Password:') . ' ' .db_error());
1356 // Now change LDAP password, but only if corresponding
1357 // entry exists (i.e. if user have shell access)
1358 if ($SYS->sysCheckUser($this->getID())) {
1359 if (!$SYS->sysUserSetAttribute($this->getID(),"userPassword",'{crypt}'.$unix_pw)) {
1360 $this->setError($SYS->getErrorMessage());
1366 $hook_params = array ();
1367 $hook_params['user'] = $this;
1368 $hook_params['user_id'] = $this->getID();
1369 $hook_params['user_password'] = $passwd;
1370 plugin_hook ("user_setpasswd", $hook_params);
1376 * setMD5Passwd - Changes user's MD5 password.
1378 * @param string The MD5-hashed password.
1379 * @return boolean success.
1381 function setMD5Passwd($md5) {
1387 $res = db_query_params ('UPDATE users SET user_pw=$1 WHERE user_id=$2',
1391 if (!$res || db_affected_rows($res) < 1) {
1392 $this->setError(_('ERROR - Could Not Change User Password:') . ' ' .db_error());
1402 * setUnixPasswd - Changes user's Unix-hashed password.
1404 * @param string The Unix-hashed password.
1405 * @return boolean success.
1407 function setUnixPasswd($unix) {
1413 $res = db_query_params ('UPDATE users SET unix_pw=$1 WHERE user_id=$1',
1417 if (!$res || db_affected_rows($res) < 1) {
1418 $this->setError(_('ERROR - Could Not Change User Password:') . ' ' .db_error());
1423 // Now change system password, but only if corresponding
1424 // entry exists (i.e. if user have shell access)
1425 if ($SYS->sysCheckUser($this->getID())) {
1426 if (!$SYS->sysUserSetAttribute($this->getID(),"userPassword",'{crypt}'.$unix)) {
1427 $this->setError($SYS->getErrorMessage());
1438 * usesRatings - whether user participates in rating system.
1440 * @return boolean success.
1442 function usesRatings() {
1443 return !$this->data_array['block_ratings'];
1447 * usesTooltips - whether user enables or not tooltips.
1449 * @return boolean success.
1451 function usesTooltips() {
1452 return $this->data_array['tooltips'];
1456 * getPlugins - get a list of all available user plugins
1458 * @return array array containing plugin_id => plugin_name
1460 function getPlugins() {
1461 if (!isset($this->plugins_data)) {
1462 $this->plugins_data = array () ;
1463 $res = db_query_params ('SELECT user_plugin.plugin_id, plugins.plugin_name
1464 FROM user_plugin, plugins
1465 WHERE user_plugin.user_id=$1
1466 AND user_plugin.plugin_id=plugins.plugin_id',
1467 array ($this->getID())) ;
1468 $rows = db_numrows($res);
1470 for ($i=0; $i<$rows; $i++) {
1471 $plugin_id = db_result($res,$i,'plugin_id');
1472 $this->plugins_data[$plugin_id] = db_result($res,$i,'plugin_name');
1475 return $this->plugins_data ;
1479 * usesPlugin - returns true if the user uses a particular plugin
1481 * @param string name of the plugin
1482 * @return boolean whether plugin is being used or not
1484 function usesPlugin($pluginname) {
1485 $plugins_data = $this->getPlugins() ;
1486 foreach ($plugins_data as $p_name) {
1487 if ($p_name == $pluginname) {
1495 * setPluginUse - enables/disables plugins for the user
1497 * @param string name of the plugin
1498 * @param boolean the new state
1499 * @return string database result
1501 function setPluginUse($pluginname, $val=true) {
1502 if ($val == $this->usesPlugin($pluginname)) {
1503 // State is already good, returning
1506 $res = db_query_params('SELECT plugin_id FROM plugins WHERE plugin_name=$1',
1507 array($pluginname));
1508 $rows = db_numrows($res);
1510 // Error: no plugin by that name
1513 $plugin_id = db_result($res,0,'plugin_id');
1515 unset ($this->plugins_data);
1517 return db_query_params('INSERT INTO user_plugin (user_id,plugin_id) VALUES ($1,$2)',
1518 array($this->getID(),
1521 return db_query_params('DELETE FROM user_plugin WHERE user_id=$1 AND plugin_id=$2',
1522 array($this->getID(),
1528 * getMailingsPrefs - Get activity status for one of the site mailings.
1530 * @param string The id of mailing ('mail_va' for community mailings, 'mail_siteupdates' for site mailings)
1531 * @return boolean success.
1533 function getMailingsPrefs($mailing_id) {
1534 if ($mailing_id=='va') {
1535 return $this->data_array['mail_va'];
1536 } else if ($mailing_id=='site') {
1537 return $this->data_array['mail_siteupdates'];
1544 * unsubscribeFromMailings - Disable email notifications for user.
1546 * @param boolean If false, disable general site mailings, else - all.
1547 * @return boolean success.
1549 function unsubscribeFromMailings($all=false) {
1550 $res1 = $res2 = $res3 = true;
1551 $res1 = db_query_params ('UPDATE users SET mail_siteupdates=0, mail_va=0 WHERE user_id=$1',
1552 array ($this->getID())) ;
1554 $res2 = db_query_params ('DELETE FROM forum_monitored_forums WHERE user_id=$1',
1555 array ($this->getID())) ;
1556 $res3 = db_query_params ('DELETE FROM filemodule_monitor WHERE user_id=$1',
1557 array ($this->getID())) ;
1560 return $res1 && $res2 && $res3;
1564 * getThemeID - get the theme_id for this user.
1566 * @return int The theme_id.
1568 function getThemeID() {
1569 return $this->data_array['theme_id'];
1573 * setUpTheme - get the theme path
1575 * @return string The theme path.
1577 function setUpTheme() {
1579 // An optimization in session_getdata lets us pre-fetch this in most cases.....
1581 if (!isset($this->data_array['dirname']) || !$this->data_array['dirname']) {
1582 $res = db_query_params ('SELECT dirname FROM themes WHERE theme_id=$1',
1583 array ($this->getThemeID())) ;
1584 $this->theme=db_result($res,0,'dirname');
1586 $this->theme=$this->data_array['dirname'];
1588 if (is_file(forge_get_config('themes_root').'/'.$this->theme.'/Theme.class.php')) {
1589 $GLOBALS['sys_theme']=$this->theme;
1591 $this->theme=forge_get_config('default_theme');
1593 return $this->theme;
1597 * getRole() - Get user Role object.
1599 * @param object group object
1600 * @return object Role object
1602 function getRole(&$g) {
1603 if (is_int ($g) || is_string($g)) {
1606 $group_id = $g->getID();
1608 foreach ($this->getRoles () as $r) {
1609 if ($r instanceof RoleExplicit ) {
1610 $homeproj = $r->getHomeProject();
1612 if ($homeproj->getID() == $group_id) {
1621 function getRoles () {
1622 return RBACEngine::getInstance()->getAvailableRolesForUser($this) ;
1626 function isMember($g,$type=0){
1627 if (is_int ($g) || is_string($g)) {
1628 $group = group_get_object ($g) ;
1632 $group_id = $group->getID() ;
1638 return forge_check_perm_for_user($this,'pm_admin',$group_id) ;
1642 return forge_check_perm_for_user($this,'forum_admin',$group_id) ;
1645 //admin for this group
1646 return forge_check_perm_for_user($this,'project_admin',$group_id) ;
1650 return forge_check_perm_for_user($this,'docman',$group_id,'admin') ;
1654 foreach ($this->getGroups() as $p) {
1655 if ($p->getID() == $group_id) {
1670 EVERYTHING BELOW HERE IS DEPRECATED
1673 DO NOT USE FOR ANY NEW CODE
1682 * user_ismember() - DEPRECATED; DO NOT USE! (TODO: document what should be used instead)
1683 * May need to be replaced by forge_check_perm ('project_admin', $group_id) if checking for project admin
1684 * @param int The Group ID
1685 * @param int The Type
1689 function user_ismember($group_id,$type=0) {
1690 if (!session_loggedin()) {
1694 return session_get_user()->isMember($group_id, $type) ;
1698 * user_getname() - DEPRECATED; DO NOT USE! (TODO: document what should be used instead)
1700 * @param int The User ID
1704 function user_getname($user_id = false) {
1705 // use current user if one is not passed in
1707 if (session_loggedin()) {
1708 $user=&user_get_object(user_getid());
1710 return $user->getUnixName();
1712 return 'Error getting user';
1715 return 'No User Id';
1718 $user=&user_get_object($user_id);
1720 return $user->getUnixName();
1722 return 'Invalid User';
1727 class UserComparator {
1728 var $criterion = 'name' ;
1730 function Compare ($a, $b) {
1731 switch ($this->criterion) {
1734 $namecmp = strcoll ($a->getRealName(), $b->getRealName()) ;
1735 if ($namecmp != 0) {
1738 /* If several projects share a same real name */
1739 return strcoll ($a->getUnixName(), $b->getUnixName()) ;
1742 return strcmp ($a->getUnixName(), $b->getUnixName()) ;
1745 $aid = $a->getID() ;
1746 $bid = $b->getID() ;
1750 return ($a < $b) ? -1 : 1;
1756 function sortUserList (&$list, $criterion='name') {
1757 $cmp = new UserComparator () ;
1758 $cmp->criterion = $criterion ;
1760 return usort ($list, array ($cmp, 'Compare')) ;
1765 // c-file-style: "bsd"