3 * FusionForge user management
5 * Copyright 1999-2001, VA Linux Systems, Inc.
6 * Copyright 2009-2010, Roland Mas
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with FusionForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 * user_get_object_by_name() - Get User object by username.
30 * user_get_object is useful so you can pool user objects/save database queries
31 * You should always use this instead of instantiating the object directly
33 * @param string The unix username - required
34 * @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
35 * @return a user object or false on failure
38 function &user_get_object_by_name($user_name,$res=false) {
39 $user_name = strtolower($user_name);
41 $res = db_query_params ('SELECT * FROM users WHERE user_name=$1',
44 return user_get_object(db_result($res,0,'user_id'),$res);
48 * user_get_object_by_email() - Get User object by email address
49 * Only works if sys_require_unique_email is true
51 * @param string The unix username - required
52 * @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
53 * @return a user object or false on failure
56 function user_get_object_by_email($email,$res=false) {
57 if (!validate_email($email)
58 || !forge_get_config('require_unique_email')) {
62 $res=db_query_params('SELECT * FROM users WHERE email=$1',
65 return user_get_object(db_result($res,0,'user_id'),$res);
69 * user_get_object() - Get User object by user ID.
70 * user_get_object is useful so you can pool user objects/save database queries
71 * You should always use this instead of instantiating the object directly
73 * @param int The ID of the user - required
74 * @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
75 * @return 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_active_users() {
136 $res=db_query_params ('SELECT user_id FROM users WHERE status=$1',
138 return user_get_objects (util_result_column_to_array($res,0)) ;
141 class GFUser extends Error {
143 * Associative array of data from db.
145 * @var array $data_array.
150 * Is this person a site super-admin?
152 * @var bool $is_super_user
157 * Is this person the logged in user?
159 * @var bool $is_logged_in
164 * Array of preferences
166 * @var array $user_pref
174 * GFUser($id,$res) - CONSTRUCTOR - GENERALLY DON'T USE THIS
176 * instead use the user_get_object() function call
178 * @param int The user_id
179 * @param int The database result set OR array of data
181 function GFUser($id=false,$res=false) {
184 //setting up an empty object
185 //probably going to call create()
189 $this->fetchData($id);
191 if (is_array($res)) {
192 $this->data_array =& $res;
193 } elseif (db_numrows($res) < 1) {
194 //function in class we extended
195 $this->setError('User Not Found');
196 $this->data_array=array();
199 //set up an associative array for use by other functions
200 $this->data_array = db_fetch_array_by_row($res, 0);
203 $this->is_super_user=false;
204 $this->is_logged_in=false;
209 * create() - Create a new user.
211 * @param string The unix username.
212 * @param string The real firstname.
213 * @param string The real lastname.
214 * @param string The first password.
215 * @param string The confirmation password.
216 * @param string The users email address.
217 * @param string The users preferred default language.
218 * @param string The users preferred default timezone.
219 * @param string The users preference for receiving site updates by email.
220 * @param string The users preference for receiving community updates by email.
221 * @param int The ID of the language preference.
222 * @param string The users preferred timezone.
223 * @param string The users Jabber address.
224 * @param int The users Jabber preference.
225 * @param int The users theme_id.
226 * @param string The users unix_box.
227 * @param string The users address.
228 * @param string The users address part 2.
229 * @param string The users phone.
230 * @param string The users fax.
231 * @param string The users title.
232 * @param char(2) The users ISO country_code.
233 * @param bool Whether to send an email or not
234 * @returns The newly created user ID
237 function create($unix_name,$firstname,$lastname,$password1,$password2,$email,
238 $mail_site,$mail_va,$language_id,$timezone,$jabber_address,$jabber_only,$theme_id,
239 $unix_box='shell',$address='',$address2='',$phone='',$fax='',$title='',$ccode='US',$send_mail=true) {
242 $this->setError(_('You must supply a theme'));
245 if (! forge_get_config('require_unique_email')) {
247 $this->setError(_('You must supply a username'));
252 $this->setError(_('You must supply a first name'));
256 $this->setError(_('You must supply a last name'));
260 $this->setError(_('You must supply a password'));
263 if ($password1 != $password2) {
264 $this->setError(_('Passwords do not match'));
267 if (!account_pwvalid($password1)) {
268 $this->setError(_('Invalid Password:'));
271 $unix_name=strtolower($unix_name);
272 if (!account_namevalid($unix_name)) {
273 $this->setError(_('Invalid Unix Name.'));
276 if (!$SYS->sysUseUnixName($unix_name)) {
277 $this->setError(_('Unix name already taken'));
280 if (!validate_email($email)) {
281 $this->setError(_('Invalid Email Address') . $email);
284 if ($jabber_address && !validate_email($jabber_address)) {
285 $this->setError(_('Invalid Jabber Address'));
293 if ($unix_name && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name LIKE $1',
294 array ($unix_name))) > 0) {
295 $this->setError(_('That username already exists.'));
298 if (forge_get_config('require_unique_email')) {
299 if (user_get_object_by_email ('$email')) {
300 $this->setError(_('User with this email already exists - use people search to recover your login.'));
304 if (forge_get_config('require_unique_email') && !$unix_name) {
305 // Let's generate a loginname for the user
306 // ...based on the email address:
307 $email_array = explode ('@', $email, 2) ;
308 $email_u = $email_array [0] ;
309 $l = ereg_replace ('[^a-z0-9]', '', $email_u) ;
310 $l = substr ($l, 0, 15) ;
311 // Is the user part of the email address okay?
312 if (account_namevalid($l)
313 && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name = $1',
317 // No? What if we add a number at the end?
320 $c = substr ($l, 0, 15-strlen ("$i")) . "$i" ;
321 if (account_namevalid($c)
322 && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name = $1',
330 // If we're really unlucky, then let's go brute-force
331 while (!$unix_name) {
332 $c = substr (md5($email . util_randbytes()), 0, 15) ;
333 if (account_namevalid($c)
334 && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name = $1',
340 $unix_name=strtolower($unix_name);
341 if (!account_namevalid($unix_name)) {
342 $this->setError(_('Invalid Unix Name.'));
345 // if we got this far, it must be good
346 $confirm_hash = substr(md5($password1 . util_randbytes() . microtime()),0,16);
348 $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) 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)',
351 account_genunixpw($password1),
352 htmlspecialchars($firstname.' '.$lastname),
353 htmlspecialchars($firstname),
354 htmlspecialchars($lastname),
359 (($mail_site)?"1":"0"),
360 (($mail_va)?"1":"0"),
366 htmlspecialchars($address),
367 htmlspecialchars($address2),
368 htmlspecialchars($phone),
369 htmlspecialchars($fax),
370 htmlspecialchars($title),
374 $this->setError(_('Insert Failed: ') . db_error());
379 $id = db_insertid($result,'users','user_id');
381 $this->setError('Could Not Get USERID: ' .db_error());
386 if (!$this->fetchData($id)) {
391 $hook_params = array ();
392 $hook_params['user'] = $this;
393 $hook_params['user_id'] = $this->getID();
394 $hook_params['user_name'] = $unix_name;
395 $hook_params['user_password'] = $password1;
396 plugin_hook ("user_create", $hook_params);
399 setup_gettext_from_lang_id($language_id);
400 $this->sendRegistrationEmail();
401 setup_gettext_from_context();
410 * sendRegistrationEmail() - Send email for registration verification
412 * @return true or false
414 function sendRegistrationEmail() {
415 $message=stripcslashes(sprintf(_('Thank you for registering on the %3$s web site. You have
416 account with username %1$s created for you. In order
417 to complete your registration, visit the following url:
421 You have 1 week to confirm your account. After this time, your account will be deleted.
423 (If you don\'t see any URL above, it is likely due to a bug in your mail client.
424 Use one below, but make sure it is entered as the single line.)
432 $this->getUnixName(),
433 util_make_url ('/account/verify.php?confirm_hash=_'.$this->getConfirmHash()),
434 forge_get_config ('forge_name')));
437 sprintf(_('%1$s Account Registration'), forge_get_config ('forge_name')),
443 * delete() - remove the User from all his groups.
445 * Remove the User from all his groups and set his status to D.
447 * @param boolean Confirmation of deletion.
448 * @return true or false
450 function delete($sure) {
454 $groups = &$this->getGroups();
455 if (is_array($groups)) {
456 foreach ($groups as $group) {
457 $group->removeUser($this->getID());
462 $res = db_query_params ('DELETE FROM artifact_monitor WHERE user_id=$1',
463 array ($this->getID())) ;
465 $this->setError('ERROR - Could Not Delete From artifact_monitor: '.db_error());
469 $res = db_query_params ('DELETE FROM artifact_type_monitor WHERE user_id=$1',
470 array ($this->getID())) ;
472 $this->setError('ERROR - Could Not Delete From artifact_type_monitor: '.db_error());
476 $res = db_query_params ('DELETE FROM forum_monitored_forums WHERE user_id=$1',
477 array ($this->getID())) ;
479 $this->setError('ERROR - Could Not Delete From forum_monitored_forums: '.db_error());
483 $res = db_query_params ('DELETE FROM filemodule_monitor WHERE user_id=$1',
484 array ($this->getID())) ;
486 $this->setError('ERROR - Could Not Delete From filemodule_monitor: '.db_error());
491 $hook_params = array ();
492 $hook_params['user'] = $this;
493 $hook_params['user_id'] = $this->getID();
494 plugin_hook ("user_delete", $hook_params);
496 $this->setStatus('D');
503 * update() - update *common* properties of GFUser object.
505 * Use specific setter to change other properties.
507 * @param string The users first name.
508 * @param string The users last name.
509 * @param int The ID of the users language preference.
510 * @param string The useres timezone preference.
511 * @param string The users preference for receiving site updates by email.
512 * @param string The users preference for receiving community updates by email.
513 * @param string The users preference for being participating in "peer ratings".
514 * @param string The users Jabber account address.
515 * @param int The users Jabber preference.
516 * @param int The users theme_id preference.
517 * @param string The users address.
518 * @param string The users address2.
519 * @param string The users phone.
520 * @param string The users fax.
521 * @param string The users title.
522 * @param string The users ccode.
524 function update($firstname,$lastname,$language_id,$timezone,$mail_site,$mail_va,$use_ratings,
525 $jabber_address,$jabber_only,$theme_id,$address,$address2,$phone,$fax,$title,$ccode) {
526 $mail_site = $mail_site ? 1 : 0;
527 $mail_va = $mail_va ? 1 : 0;
528 $block_ratings = $use_ratings ? 0 : 1;
530 if ($jabber_address && !validate_email($jabber_address)) {
531 $this->setError(_('Invalid Jabber Address'));
542 $res = db_query_params ('
564 htmlspecialchars($firstname . ' ' .$lastname),
565 htmlspecialchars($firstname),
566 htmlspecialchars($lastname),
574 htmlspecialchars($address) ,
575 htmlspecialchars($address2) ,
576 htmlspecialchars($phone) ,
577 htmlspecialchars($fax) ,
578 htmlspecialchars($title) ,
584 $this->setError(_('ERROR - Could Not Update User Object:'). ' ' .db_error());
588 // If there's a transaction from using to not
589 // using ratings, remove all rating made by the
590 // user (ratings by others should not be removed,
591 // as it opens possibility to abuse rate system)
592 if (!$use_ratings && $this->usesRatings()) {
593 db_query_params ('DELETE FROM user_ratings WHERE rated_by=$1',
596 if (!$this->fetchData($this->getID())) {
601 $hook_params = array ();
602 $hook_params['user'] = $this;
603 $hook_params['user_id'] = $this->getID();
604 plugin_hook ("user_update", $hook_params);
612 * fetchData - May need to refresh database fields.
614 * If an update occurred and you need to access the updated info.
616 * @return boolean success;
618 function fetchData($user_id) {
619 $res = db_query_params ('SELECT * FROM users WHERE user_id=$1',
621 if (!$res || db_numrows($res) < 1) {
622 $this->setError('GFUser::fetchData():: '.db_error());
625 $this->data_array = db_fetch_array($res);
630 * getID - Simply return the user_id for this object.
632 * @return int This user's user_id number.
635 return $this->data_array['user_id'];
639 * getStatus - get the status of this user.
641 * Statuses include (A)ctive, (P)ending, (S)uspended ,(D)eleted.
643 * @return char This user's status flag.
645 function getStatus() {
646 return $this->data_array['status'];
650 * setStatus - set this user's status.
652 * @param string Status - P, A, S, or D.
653 * @return boolean success.
655 function setStatus($status) {
657 if ($status != 'P' && $status != 'A'
658 && $status != 'S' && $status != 'D') {
659 $this->setError(_('ERROR: Invalid status value'));
662 if ($this->getStatus() != 'P' && $status == 'P') {
663 $this->setError('ERROR: You can\'t set pending status if user is suspend or active');
668 $res = db_query_params ('UPDATE users SET status=$1 WHERE user_id=$2',
673 $this->setError(_('ERROR - Could Not Update User Status:') . ' ' .db_error());
677 $this->data_array['status']=$status;
678 if ($status == 'D') {
679 $projects = $this->getGroups() ;
680 foreach ($projects as $p) {
681 $p->removeUser ($this->getID()) ;
685 $hook_params = array ();
686 $hook_params['user'] = $this;
687 $hook_params['user_id'] = $this->getID();
688 $hook_params['status'] = $status;
689 plugin_hook ("user_setstatus", $hook_params);
698 * isActive - whether this user is confirmed and active.
700 * Database field status of 'A' returns true.
701 * @return boolean is_active.
703 function isActive() {
704 if ($this->getStatus()=='A') {
712 * getUnixStatus - Status of activation of unix account.
714 * @return char (N)one, (A)ctive, (S)uspended or (D)eleted
716 function getUnixStatus() {
717 return $this->data_array['unix_status'];
721 * setUnixStatus - Sets status of activation of unix account.
723 * @param string The unix status.
729 * @return boolean success.
731 function setUnixStatus($status) {
734 $res = db_query_params ('UPDATE users SET unix_status=$1 WHERE user_id=$2',
739 $this->setError('ERROR - Could Not Update User Unix Status: '.db_error());
743 if ($status == 'A') {
744 if (!$SYS->sysCheckCreateUser($this->getID())) {
745 $this->setError($SYS->getErrorMessage());
750 if ($SYS->sysCheckUser($this->getID())) {
751 if (!$SYS->sysRemoveUser($this->getID())) {
752 $this->setError($SYS->getErrorMessage());
759 $this->data_array['unix_status']=$status;
766 * getUnixName - the user's unix_name.
768 * @return string This user's unix/login name.
770 function getUnixName() {
771 return strtolower($this->data_array['user_name']);
775 * getUnixPasswd - get the user's password.
777 * @return string This user's unix crypted passwd.
779 function getUnixPasswd() {
780 return $this->data_array['unix_pw'];
784 * getUnixBox - the hostname of the unix box this user has an account on.
786 * @return string This user's shell login machine.
788 function getUnixBox() {
789 return $this->data_array['unix_box'];
793 * getMD5Passwd - the password.
795 * @return string This user's MD5-crypted passwd.
797 function getMD5Passwd() {
798 return $this->data_array['user_pw'];
801 //Added to be compatible with codendi getUserPw function
802 function getUserPw() {
803 return $this->data_array['user_pw'];
807 * getConfirmHash - the confirm hash in the db.
809 * @return string This user's confirmation hash.
811 function getConfirmHash() {
812 return $this->data_array['confirm_hash'];
816 * getEmail - the user's email address.
818 * @return string This user's email address.
820 function getEmail() {
821 return str_replace("\n", "", $this->data_array['email']);
825 * getSha1Email - a SHA1 encoded hash of the email URI (including mailto: prefix)
827 * @return string The SHA1 encoded value for the email
829 function getSha1Email() {
830 return sha1('mailto:'.$this->getEmail());
834 * getNewEmail - while changing an email address, it is stored here until confirmation.
836 * getNewEmail is a private operation for email change.
838 * @return string This user's new (not yet confirmed) email address.
841 function getNewEmail() {
842 return $this->data_array['email_new'];
846 * setEmail - set a new email address, which must be confirmed.
848 * @param string The email address.
849 * @return boolean success.
851 function setEmail($email) {
853 if (!strcasecmp($this->getEmail(), $email)) {
857 if (!$email || !validate_email($email)) {
858 $this->setError('ERROR: Invalid Email');
862 if (forge_get_config('require_unique_email')) {
863 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)',
864 array ($this->getID(),
865 strtolower($email)))) > 0) {
866 $this->setError(_('User with this email already exists.'));
872 $res = db_query_params ('UPDATE users SET email=$1 WHERE user_id=$2',
877 $this->setError('ERROR - Could Not Update User Email: '.db_error());
881 $hook_params = array ();
882 $hook_params['user'] = $this;
883 $hook_params['user_id'] = $this->getID();
884 $hook_params['user_email'] = $email;
885 plugin_hook ("user_setemail", $hook_params);
887 if (!$this->fetchData($this->getId())) {
898 * setNewEmailAndHash - setNewEmailAndHash is a private operation for email change.
900 * @param string The email address.
901 * @param string The email hash.
902 * @return boolean success.
904 function setNewEmailAndHash($email, $hash='') {
907 $hash = substr(md5(strval(time()) . strval(util_randbytes())), 0, 16);
910 if (!$email || !validate_email($email)) {
911 $this->setError('ERROR - Invalid Email');
915 if (forge_get_config('require_unique_email')) {
916 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)',
917 array ($this->getID(),
918 strtolower($email)))) > 0) {
919 $this->setError(_('User with this email already exists.'));
923 $res = db_query_params ('UPDATE users SET confirm_hash=$1, email_new=$2 WHERE user_id=$3',
928 $this->setError('ERROR - Could Not Update User Email And Hash: '.db_error());
931 $this->data_array['email_new'] = $email;
932 $this->data_array['confirm_hash'] = $hash;
938 * getRealName - get the user's real name.
940 * @return string This user's real name.
942 function getRealName() {
943 $last_name = $this->getLastName();
944 return $this->getFirstName(). ($last_name ? ' ' .$last_name:'');
948 * getFirstName - get the user's first name.
950 * @return string This user's first name.
952 function getFirstName() {
953 return $this->data_array['firstname'];
957 * getLastName - get the user's last name.
959 * @return string This user's last name.
961 function getLastName() {
962 return $this->data_array['lastname'];
966 * getAddDate - this user's unix time when account was opened.
968 * @return int This user's unix time when account was opened.
970 function getAddDate() {
971 return $this->data_array['add_date'];
975 * getTimeZone - this user's timezone setting.
977 * @return string This user's timezone setting.
979 function getTimeZone() {
980 return $this->data_array['timezone'];
984 * getCountryCode - this user's ccode setting.
986 * @return string This user's ccode setting.
988 function getCountryCode() {
989 return $this->data_array['ccode'];
993 * getShell - this user's preferred shell.
995 * @return string This user's preferred shell.
997 function getShell() {
998 return $this->data_array['shell'];
1002 * setShell - sets user's preferred shell.
1004 * @param string The users preferred shell.
1005 * @return boolean success.
1007 function setShell($shell) {
1009 $shells = file('/etc/shells');
1010 $shells[count($shells)] = "/bin/cvssh";
1011 $out_shells = array();
1012 foreach ($shells as $s) {
1013 if (substr($s, 0, 1) == '#') {
1016 $out_shells[] = chop($s);
1018 if (!in_array($shell, $out_shells)) {
1019 $this->setError(_('ERROR: Invalid Shell'));
1024 $res = db_query_params ('UPDATE users SET shell=$1 WHERE user_id=$2',
1028 $this->setError(_('ERROR - Could Not Update User Unix Shell:') . ' ' .db_error());
1032 // Now change LDAP attribute, but only if corresponding
1033 // entry exists (i.e. if user have shell access)
1034 if ($SYS->sysCheckUser($this->getID()))
1036 if (!$SYS->sysUserSetAttribute($this->getID(),"loginShell",$shell)) {
1037 $this->setError($SYS->getErrorMessage());
1042 $this->data_array['shell']=$shell;
1049 * getUnixUID() - Get the unix UID of the user
1051 * @return int This user's UID.
1053 function getUnixUID() {
1054 return $this->data_array['unix_uid'];
1058 * getUnixGID() - Get the unix GID of the user
1060 * @return int This user's GID.
1062 function getUnixGID() {
1063 return $this->data_array['unix_gid'];
1067 * getLanguage - this user's language_id from supported_languages table.
1069 * @return int This user's language_id.
1071 function getLanguage() {
1072 return $this->data_array['language'];
1076 * getJabberAddress - this user's optional jabber address.
1078 * @return string This user's jabber address.
1080 function getJabberAddress() {
1081 return $this->data_array['jabber_address'];
1085 * getJabberOnly - whether this person wants updates sent ONLY to jabber.
1087 * @return boolean This user's jabber preference.
1089 function getJabberOnly() {
1090 return $this->data_array['jabber_only'];
1094 * getAddress - get this user's address.
1096 * @return text This user's address.
1098 function getAddress() {
1099 return $this->data_array['address'];
1103 * getAddress2 - get this user's address2.
1105 * @return text This user's address2.
1107 function getAddress2() {
1108 return $this->data_array['address2'];
1112 * getPhone - get this person's phone number.
1114 * @return text This user's phone number.
1116 function getPhone() {
1117 return $this->data_array['phone'];
1121 * getFax - get this person's fax number.
1123 * @return text This user's fax.
1126 return $this->data_array['fax'];
1130 * getTitle - get this person's title.
1132 * @return text This user's title.
1134 function getTitle() {
1135 return $this->data_array['title'];
1139 * getGroups - get an array of groups this user is a member of.
1141 * @return array Array of groups.
1143 function &getGroups($onlylocal = true) {
1145 foreach ($this->getRoles() as $r) {
1147 if ($r instanceof RoleExplicit
1148 && $r->getHomeProject() != NULL) {
1149 $ids[] = $r->getHomeProject()->getID() ;
1152 foreach ($r->getLinkedProjects() as $p) {
1153 $ids[] = $p->getID() ;
1157 return group_get_objects(array_values(array_unique($ids))) ;
1161 * getAuthorizedKeys - the SSH authorized keys set by the user.
1163 * @return string This user's SSH authorized (public) keys.
1165 function getAuthorizedKeys() {
1166 return preg_replace("/###/", "\n", $this->data_array['authorized_keys']);
1170 * setAuthorizedKeys - set the SSH authorized keys for the user.
1172 * @param string The users public keys.
1173 * @return boolean success.
1175 function setAuthorizedKeys($keys) {
1176 $keys = trim($keys);
1177 $keys = preg_replace("/\r\n/", "\n", $keys); // Convert to Unix EOL
1178 $keys = preg_replace("/\n+/", "\n", $keys); // Remove empty lines
1179 $keys = preg_replace("/\n/", "###", $keys); // Convert EOL to marker
1181 $res = db_query_params ('UPDATE users SET authorized_keys=$1 WHERE user_id=$2',
1185 $this->setError(_('ERROR - Could Not Update User SSH Keys'));
1188 $this->data_array['authorized_keys'] = $keys;
1194 * setLoggedIn($val) - Really only used by session code.
1196 * @param boolean The session value.
1198 function setLoggedIn ($val=true) {
1199 $this->is_logged_in = $val;
1202 $this->is_super_user = forge_check_global_perm_for_user ($this, 'forge_admin') ;
1207 * isLoggedIn - only used by session code.
1209 * @return boolean is_logged_in.
1211 function isLoggedIn() {
1212 return $this->is_logged_in;
1216 * deletePreference - delete a preference for this user.
1218 * @param string The unique field name for this preference.
1219 * @return boolean success.
1221 function deletePreference($preference_name) {
1222 $preference_name=strtolower(trim($preference_name));
1223 unset($this->user_pref["$preference_name"]);
1224 $res = db_query_params ('DELETE FROM user_preferences WHERE user_id=$1 AND preference_name=$2',
1225 array ($this->getID(),
1226 $preference_name)) ;
1231 * setPreference - set a new preference for this user.
1233 * @param string The unique field name for this preference.
1234 * @param string The value you are setting this preference to.
1235 * @return boolean success.
1237 function setPreference($preference_name,$value) {
1238 $preference_name=strtolower(trim($preference_name));
1239 //delete pref if not value passed in
1240 unset($this->user_pref);
1241 if (!isset($value)) {
1242 $result = db_query_params ('DELETE FROM user_preferences WHERE user_id=$1 AND preference_name=$2',
1243 array ($this->getID(),
1244 $preference_name)) ;
1246 $result = db_query_params ('UPDATE user_preferences SET preference_value=$1,set_date=$2 WHERE user_id=$3 AND preference_name=$4',
1250 $preference_name)) ;
1251 if (db_affected_rows($result) < 1) {
1253 $result = db_query_params ('INSERT INTO user_preferences (user_id,preference_name,preference_value,set_date) VALUES ($1,$2,$3,$4)',
1254 array ($this->getID(),
1264 * getPreference - get a specific preference.
1266 * @param string The unique field name for this preference.
1267 * @return the preference string or false on failure.
1269 function getPreference($preference_name) {
1270 $preference_name=strtolower(trim($preference_name));
1272 First check to see if we have already fetched the preferences
1274 if (isset($this->user_pref)) {
1275 //echo "\n\nPrefs were fetched already";
1276 if (isset($this->user_pref["$preference_name"])) {
1277 //we have fetched prefs - return part of array
1278 return $this->user_pref["$preference_name"];
1280 //we have fetched prefs, but this pref hasn't been set
1284 //we haven't returned prefs - go to the db
1285 $result = db_query_params ('SELECT preference_name,preference_value FROM user_preferences WHERE user_id=$1',
1286 array ($this->getID())) ;
1287 if (db_numrows($result) < 1) {
1288 //echo "\n\nNo Prefs Found";
1292 //iterate and put the results into an array
1293 for ($i=0; $i<db_numrows($result); $i++) {
1294 $pref["".db_result($result,$i,'preference_name').""]=db_result($result,$i,'preference_value');
1296 $this->user_pref = $pref;
1298 if (array_key_exists($preference_name,$this->user_pref)) {
1299 //we have fetched prefs - return part of array
1300 return $this->user_pref["$preference_name"];
1302 //we have fetched prefs, but this pref hasn't been set
1310 * setPasswd - Changes user's password.
1312 * @param string The plaintext password.
1313 * @return boolean success.
1315 function setPasswd($passwd) {
1317 if (!account_pwvalid($passwd)) {
1318 $this->setError('Error: '.$GLOBALS['register_error']);
1323 $md5_pw = md5 ($passwd) ;
1324 $unix_pw = account_genunixpw ($passwd) ;
1326 $res = db_query_params ('UPDATE users SET user_pw=$1, unix_pw=$2 WHERE user_id=$3',
1331 if (!$res || db_affected_rows($res) < 1) {
1332 $this->setError(_('ERROR - Could Not Change User Password:') . ' ' .db_error());
1336 // Now change LDAP password, but only if corresponding
1337 // entry exists (i.e. if user have shell access)
1338 if ($SYS->sysCheckUser($this->getID())) {
1339 if (!$SYS->sysUserSetAttribute($this->getID(),"userPassword",'{crypt}'.$unix_pw)) {
1340 $this->setError($SYS->getErrorMessage());
1346 $hook_params = array ();
1347 $hook_params['user'] = $this;
1348 $hook_params['user_id'] = $this->getID();
1349 $hook_params['user_password'] = $passwd;
1350 plugin_hook ("user_setpasswd", $hook_params);
1356 * usesRatings - whether user participates in rating system.
1358 * @return boolean success.
1360 function usesRatings() {
1361 return !$this->data_array['block_ratings'];
1365 * getPlugins - get a list of all available user plugins
1367 * @return array array containing plugin_id => plugin_name
1369 function getPlugins() {
1370 if (!isset($this->plugins_data)) {
1371 $this->plugins_data = array () ;
1372 $res = db_query_params ('SELECT user_plugin.plugin_id, plugins.plugin_name
1373 FROM user_plugin, plugins
1374 WHERE user_plugin.user_id=$1
1375 AND user_plugin.plugin_id=plugins.plugin_id',
1376 array ($this->getID())) ;
1377 $rows = db_numrows($res);
1379 for ($i=0; $i<$rows; $i++) {
1380 $plugin_id = db_result($res,$i,'plugin_id');
1381 $this->plugins_data[$plugin_id] = db_result($res,$i,'plugin_name');
1384 return $this->plugins_data ;
1388 * usesPlugin - returns true if the user uses a particular plugin
1390 * @param string name of the plugin
1391 * @return boolean whether plugin is being used or not
1393 function usesPlugin($pluginname) {
1394 $plugins_data = $this->getPlugins() ;
1395 foreach ($plugins_data as $p_name) {
1396 if ($p_name == $pluginname) {
1404 * setPluginUse - enables/disables plugins for the user
1406 * @param string name of the plugin
1407 * @param boolean the new state
1408 * @return string database result
1410 function setPluginUse($pluginname, $val=true) {
1411 if ($val == $this->usesPlugin($pluginname)) {
1412 // State is already good, returning
1415 $res = db_query_params ('SELECT plugin_id FROM plugins WHERE plugin_name=$1',
1416 array ($pluginname)) ;
1417 $rows = db_numrows($res);
1419 // Error: no plugin by that name
1422 $plugin_id = db_result($res,0,'plugin_id');
1424 unset ($this->plugins_data) ;
1426 return db_query_params ('INSERT INTO user_plugin (user_id,plugin_id) VALUES ($1,$2)',
1427 array ($this->getID(),
1430 return db_query_params ('DELETE FROM user_plugin WHERE user_id=$1 AND plugin_id=$2',
1431 array ($this->getID(),
1437 * getMailingsPrefs - Get activity status for one of the site mailings.
1439 * @param string The id of mailing ('mail_va' for community mailings, 'mail_siteupdates' for site mailings)
1440 * @return boolean success.
1442 function getMailingsPrefs($mailing_id) {
1443 if ($mailing_id=='va') {
1444 return $this->data_array['mail_va'];
1445 } else if ($mailing_id=='site') {
1446 return $this->data_array['mail_siteupdates'];
1453 * unsubscribeFromMailings - Disable email notifications for user.
1455 * @param boolean If false, disable general site mailings, else - all.
1456 * @return boolean success.
1458 function unsubscribeFromMailings($all=false) {
1459 $res1 = $res2 = $res3 = true;
1460 $res1 = db_query_params ('UPDATE users SET mail_siteupdates=0, mail_va=0 WHERE user_id=$1',
1461 array ($this->getID())) ;
1463 $res2 = db_query_params ('DELETE FROM forum_monitored_forums WHERE user_id=$1',
1464 array ($this->getID())) ;
1465 $res3 = db_query_params ('DELETE FROM filemodule_monitor WHERE user_id=$1',
1466 array ($this->getID())) ;
1469 return $res1 && $res2 && $res3;
1473 * getThemeID - get the theme_id for this user.
1475 * @return int The theme_id.
1477 function getThemeID() {
1478 return $this->data_array['theme_id'];
1482 * getThemeID - get the theme_id for this user from the theme_prefs table.
1484 * @return int The theme_id.
1486 function setUpTheme() {
1488 // An optimization in session_getdata lets us pre-fetch this in most cases.....
1490 if (!isset($this->data_array['dirname']) || !$this->data_array['dirname']) {
1491 $res = db_query_params ('SELECT dirname FROM themes WHERE theme_id=$1',
1492 array ($this->getThemeID())) ;
1493 $this->theme=db_result($res,0,'dirname');
1495 $this->theme=$this->data_array['dirname'];
1497 if (is_file(forge_get_config('themes_root').'/'.$this->theme.'/Theme.class.php')) {
1498 $GLOBALS['sys_theme']=$this->theme;
1500 $this->theme=forge_get_config('default_theme');
1502 return $this->theme;
1506 * getRole() - Get user Role object.
1508 * @param object group object
1509 * @return object Role object
1511 function getRole(&$group) {
1512 foreach ($this->getRoles () as $r) {
1513 if ($r instanceof RoleExplicit
1514 && $r->getHomeProject() != NULL
1515 && $r->getHomeProject()->getID() == $group->getID()) {
1522 function getRoles () {
1523 return RBACEngine::getInstance()->getAvailableRolesForUser($this) ;
1527 function isMember($g,$type=0){
1528 if (is_int ($g) || is_string($g)) {
1529 $group = group_get_object ($g) ;
1533 $group_id = $group->getID() ;
1539 return forge_check_perm_for_user($this,'pm_admin',$group_id) ;
1543 return forge_check_perm_for_user($this,'forum_admin',$group_id) ;
1546 //admin for this group
1547 return forge_check_perm_for_user($this,'project_admin',$group_id) ;
1551 return forge_check_perm_for_user($this,'docman',$group_id,'admin') ;
1555 foreach ($this->getGroups() as $p) {
1556 if ($p->getID() == $group_id) {
1571 EVERYTHING BELOW HERE IS DEPRECATED
1574 DO NOT USE FOR ANY NEW CODE
1583 * user_ismember() - DEPRECATED; DO NOT USE!
1585 * @param int The Group ID
1586 * @param int The Type
1590 function user_ismember($group_id,$type=0) {
1591 if (!session_loggedin()) {
1595 return session_get_user()->isMember($group_id, $type) ;
1599 * user_getname() - DEPRECATED; DO NOT USE!
1601 * @param int The User ID
1605 function user_getname($user_id = false) {
1606 // use current user if one is not passed in
1608 if (session_loggedin()) {
1609 $user=&user_get_object(user_getid());
1611 return $user->getUnixName();
1613 return 'Error getting user';
1616 return 'No User Id';
1619 $user=&user_get_object($user_id);
1621 return $user->getUnixName();
1623 return 'Invalid User';
1628 class UserComparator {
1629 var $criterion = 'name' ;
1631 function Compare ($a, $b) {
1632 switch ($this->criterion) {
1635 $namecmp = strcoll ($a->getRealName(), $b->getRealName()) ;
1636 if ($namecmp != 0) {
1639 /* If several projects share a same real name */
1640 return strcoll ($a->getUnixName(), $b->getUnixName()) ;
1643 return strcmp ($a->getUnixName(), $b->getUnixName()) ;
1646 $aid = $a->getID() ;
1647 $bid = $b->getID() ;
1651 return ($a < $b) ? -1 : 1;
1657 function sortUserList (&$list, $criterion='name') {
1658 $cmp = new UserComparator () ;
1659 $cmp->criterion = $criterion ;
1661 return usort ($list, array ($cmp, 'Compare')) ;
1666 // c-file-style: "bsd"