3 * FusionForge user management
5 * Copyright 1999-2001, VA Linux Systems, Inc.
6 * Copyright 2009, 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
26 require_once $gfwww.'include/vote_function.php';
30 * user_get_object_by_name() - Get User object by username.
31 * user_get_object is useful so you can pool user objects/save database queries
32 * You should always use this instead of instantiating the object directly
34 * @param string The unix username - required
35 * @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
36 * @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 a user object or false on failure
79 function &user_get_object($user_id,$res=false) {
80 //create a common set of group objects
81 //saves a little wear on the database
83 //automatically checks group_type and
84 //returns appropriate object
87 if (!isset($USER_OBJ["_".$user_id."_"])) {
89 //the db result handle was passed in
91 $res = db_query_params ('SELECT * FROM users WHERE user_id=$1',
94 if (!$res || db_numrows($res) < 1) {
95 $USER_OBJ["_".$user_id."_"]=false;
97 $USER_OBJ["_".$user_id."_"]= new GFUser($user_id,$res);
100 return $USER_OBJ["_".$user_id."_"];
103 function &user_get_objects($id_arr) {
108 for ($i=0; $i<count($id_arr); $i++) {
110 // See if this ID already has been fetched in the cache
115 if (!isset($USER_OBJ["_".$id_arr[$i]."_"])) {
116 $fetch[]=$id_arr[$i];
118 $return[] =& $USER_OBJ["_".$id_arr[$i]."_"];
121 if (count($fetch) > 0) {
122 $res = db_query_params ('SELECT * FROM users WHERE user_id = ANY ($1)',
123 array (db_int_array_to_any_clause ($fetch))) ;
124 while ($arr =& db_fetch_array($res)) {
125 $USER_OBJ["_".$arr['user_id']."_"] = new GFUser($arr['user_id'],$arr);
126 $return[] =& $USER_OBJ["_".$arr['user_id']."_"];
132 function &user_get_objects_by_name($username_arr) {
133 $res = db_query_params ('SELECT user_id FROM users WHERE lower(user_name) = ANY ($1)',
134 array (db_string_array_to_any_clause ($username_arr))) ;
135 $arr =& util_result_column_to_array($res,0);
136 return user_get_objects($arr);
139 class GFUser extends Error {
141 * Associative array of data from db.
143 * @var array $data_array.
148 * Is this person a site super-admin?
150 * @var bool $is_super_user
155 * Is this person the logged in user?
157 * @var bool $is_logged_in
162 * Array of preferences
164 * @var array $user_pref
172 * GFUser($id,$res) - CONSTRUCTOR - GENERALLY DON'T USE THIS
174 * instead use the user_get_object() function call
176 * @param int The user_id
177 * @param int The database result set OR array of data
179 function GFUser($id=false,$res=false) {
182 //setting up an empty object
183 //probably going to call create()
187 $this->fetchData($id);
189 if (is_array($res)) {
190 $this->data_array =& $res;
191 } elseif (db_numrows($res) < 1) {
192 //function in class we extended
193 $this->setError('User Not Found');
194 $this->data_array=array();
197 //set up an associative array for use by other functions
198 $this->data_array = db_fetch_array_by_row($res, 0);
201 $this->is_super_user=false;
202 $this->is_logged_in=false;
207 * create() - Create a new user.
209 * @param string The unix username.
210 * @param string The real firstname.
211 * @param string The real lastname.
212 * @param string The first password.
213 * @param string The confirmation password.
214 * @param string The users email address.
215 * @param string The users preferred default language.
216 * @param string The users preferred default timezone.
217 * @param string The users preference for receiving site updates by email.
218 * @param string The users preference for receiving community updates by email.
219 * @param int The ID of the language preference.
220 * @param string The users preferred timezone.
221 * @param string The users Jabber address.
222 * @param int The users Jabber preference.
223 * @param int The users theme_id.
224 * @param string The users unix_box.
225 * @param string The users address.
226 * @param string The users address part 2.
227 * @param string The users phone.
228 * @param string The users fax.
229 * @param string The users title.
230 * @param char(2) The users ISO country_code.
231 * @param bool Whether to send an email or not
232 * @returns The newly created user ID
235 function create($unix_name,$firstname,$lastname,$password1,$password2,$email,
236 $mail_site,$mail_va,$language_id,$timezone,$jabber_address,$jabber_only,$theme_id,
237 $unix_box='shell',$address='',$address2='',$phone='',$fax='',$title='',$ccode='US',$send_mail=true) {
240 $this->setError(_('You must supply a theme'));
243 if (! forge_get_config('require_unique_email')) {
245 $this->setError(_('You must supply a username'));
250 $this->setError(_('You must supply a first name'));
254 $this->setError(_('You must supply a last name'));
258 $this->setError(_('You must supply a password'));
261 if ($password1 != $password2) {
262 $this->setError(_('Passwords do not match'));
265 if (!account_pwvalid($password1)) {
266 $this->setError(_('Invalid Password:'));
269 $unix_name=strtolower($unix_name);
270 if (!account_namevalid($unix_name)) {
271 $this->setError(_('Invalid Unix Name.'));
274 if (!$SYS->sysUseUnixName($unix_name)) {
275 $this->setError(_('Unix name already taken'));
278 if (!validate_email($email)) {
279 $this->setError(_('Invalid Email Address'));
282 if ($jabber_address && !validate_email($jabber_address)) {
283 $this->setError(_('Invalid Jabber Address'));
291 if ($unix_name && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name LIKE $1',
292 array ($unix_name))) > 0) {
293 $this->setError(_('That username already exists.'));
296 if (forge_get_config('require_unique_email')) {
297 if (user_get_object_by_email ('$email')) {
298 $this->setError(_('User with this email already exists - use people search to recover your login.'));
302 if (forge_get_config('require_unique_email') && !$unix_name) {
303 // Let's generate a loginname for the user
304 // ...based on the email address:
305 $email_array = explode ('@', $email, 2) ;
306 $email_u = $email_array [0] ;
307 $l = ereg_replace ('[^a-z0-9]', '', $email_u) ;
308 $l = substr ($l, 0, 15) ;
309 // Is the user part of the email address okay?
310 if (account_namevalid($l)
311 && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name = $1',
315 // No? What if we add a number at the end?
318 $c = substr ($l, 0, 15-strlen ("$i")) . "$i" ;
319 if (account_namevalid($c)
320 && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name = $1',
328 // If we're really unlucky, then let's go brute-force
329 while (!$unix_name) {
330 $c = substr (md5($email . rand()), 0, 15) ;
331 if (account_namevalid($c)
332 && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name = $1',
338 $unix_name=strtolower($unix_name);
339 if (!account_namevalid($unix_name)) {
340 $this->setError(_('Invalid Unix Name.'));
343 // if we got this far, it must be good
344 $confirm_hash = substr(md5($password1 . rand() . microtime()),0,16);
346 $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)',
349 account_genunixpw($password1),
350 htmlspecialchars($firstname.' '.$lastname),
351 htmlspecialchars($firstname),
352 htmlspecialchars($lastname),
357 (($mail_site)?"1":"0"),
358 (($mail_va)?"1":"0"),
364 htmlspecialchars($address),
365 htmlspecialchars($address2),
366 htmlspecialchars($phone),
367 htmlspecialchars($fax),
368 htmlspecialchars($title),
372 $this->setError(_('Insert Failed') . db_error());
377 $id = db_insertid($result,'users','user_id');
379 $this->setError('Could Not Get USERID: ' .db_error());
384 if (!$this->fetchData($id)) {
389 $hook_params = array ();
390 $hook_params['user'] = $this;
391 $hook_params['user_id'] = $this->getID();
392 $hook_params['user_name'] = $unix_name;
393 $hook_params['user_password'] = $password1;
394 plugin_hook ("user_create", $hook_params);
397 setup_gettext_from_lang_id($language_id);
398 $this->sendRegistrationEmail();
399 setup_gettext_from_context();
408 * sendRegistrationEmail() - Send email for registration verification
410 * @return true or false
412 function sendRegistrationEmail() {
413 $message=stripcslashes(sprintf(_('Thank you for registering on the %3$s web site. You have
414 account with username %1$s created for you. In order
415 to complete your registration, visit the following url:
419 You have 1 week to confirm your account. After this time, your account will be deleted.
421 (If you don\'t see any URL above, it is likely due to a bug in your mail client.
422 Use one below, but make sure it is entered as the single line.)
430 $this->getUnixName(),
431 util_make_url ('/account/verify.php?confirm_hash=_'.$this->getConfirmHash()),
432 forge_get_config ('forge_name')));
435 sprintf(_('%1$s Account Registration'), forge_get_config ('forge_name')),
441 * delete() - remove the User from all his groups.
443 * Remove the User from all his groups and set his status to D.
445 * @param boolean Confirmation of deletion.
446 * @return true or false
448 function delete($sure) {
452 $groups = &$this->getGroups();
453 if (is_array($groups)) {
454 foreach ($groups as $group) {
455 $group->removeUser($this->getID());
460 $res = db_query_params ('DELETE FROM artifact_monitor WHERE user_id=$1',
461 array ($this->getID())) ;
463 $this->setError('ERROR - Could Not Delete From artifact_monitor: '.db_error());
467 $res = db_query_params ('DELETE FROM artifact_type_monitor WHERE user_id=$1',
468 array ($this->getID())) ;
470 $this->setError('ERROR - Could Not Delete From artifact_type_monitor: '.db_error());
474 $res = db_query_params ('DELETE FROM forum_monitored_forums WHERE user_id=$1',
475 array ($this->getID())) ;
477 $this->setError('ERROR - Could Not Delete From forum_monitored_forums: '.db_error());
481 $res = db_query_params ('DELETE FROM filemodule_monitor WHERE user_id=$1',
482 array ($this->getID())) ;
484 $this->setError('ERROR - Could Not Delete From filemodule_monitor: '.db_error());
489 $hook_params = array ();
490 $hook_params['user'] = $this;
491 $hook_params['user_id'] = $this->getID();
492 plugin_hook ("user_delete", $hook_params);
494 $this->setStatus('D');
501 * update() - update *common* properties of GFUser object.
503 * Use specific setter to change other properties.
505 * @param string The users first name.
506 * @param string The users last name.
507 * @param int The ID of the users language preference.
508 * @param string The useres timezone preference.
509 * @param string The users preference for receiving site updates by email.
510 * @param string The users preference for receiving community updates by email.
511 * @param string The users preference for being participating in "peer ratings".
512 * @param string The users Jabber account address.
513 * @param int The users Jabber preference.
514 * @param int The users theme_id preference.
515 * @param string The users address.
516 * @param string The users address2.
517 * @param string The users phone.
518 * @param string The users fax.
519 * @param string The users title.
520 * @param string The users ccode.
522 function update($firstname,$lastname,$language_id,$timezone,$mail_site,$mail_va,$use_ratings,
523 $jabber_address,$jabber_only,$theme_id,$address,$address2,$phone,$fax,$title,$ccode) {
524 $mail_site = $mail_site ? 1 : 0;
525 $mail_va = $mail_va ? 1 : 0;
526 $block_ratings = $use_ratings ? 0 : 1;
528 if ($jabber_address && !validate_email($jabber_address)) {
529 $this->setError(_('Invalid Jabber Address'));
540 $res = db_query_params ('
562 htmlspecialchars($firstname . ' ' .$lastname),
563 htmlspecialchars($firstname),
564 htmlspecialchars($lastname),
572 htmlspecialchars($address) ,
573 htmlspecialchars($address2) ,
574 htmlspecialchars($phone) ,
575 htmlspecialchars($fax) ,
576 htmlspecialchars($title) ,
582 $this->setError('ERROR - Could Not Update User Object: '.db_error());
586 // If there's a transaction from using to not
587 // using ratings, remove all rating made by the
588 // user (ratings by others should not be removed,
589 // as it opens possibility to abuse rate system)
590 if (!$use_ratings && $this->usesRatings()) {
591 vote_remove_all_ratings_by($this->getID());
593 if (!$this->fetchData($this->getID())) {
598 $hook_params = array ();
599 $hook_params['user'] = $this;
600 $hook_params['user_id'] = $this->getID();
601 plugin_hook ("user_update", $hook_params);
609 * fetchData - May need to refresh database fields.
611 * If an update occurred and you need to access the updated info.
613 * @return boolean success;
615 function fetchData($user_id) {
616 $res = db_query_params ('SELECT * FROM users WHERE user_id=$1',
618 if (!$res || db_numrows($res) < 1) {
619 $this->setError('GFUser::fetchData()::'.db_error());
622 $this->data_array = db_fetch_array($res);
627 * getID - Simply return the user_id for this object.
629 * @return int This user's user_id number.
632 return $this->data_array['user_id'];
636 * getStatus - get the status of this user.
638 * Statuses include (A)ctive, (P)ending, (S)uspended ,(D)eleted.
640 * @return char This user's status flag.
642 function getStatus() {
643 return $this->data_array['status'];
647 * setStatus - set this user's status.
649 * @param string Status - P, A, S, or D.
650 * @return boolean success.
652 function setStatus($status) {
654 if ($status != 'P' && $status != 'A'
655 && $status != 'S' && $status != 'D') {
656 $this->setError('ERROR: Invalid status value');
661 $res = db_query_params ('UPDATE users SET status=$1 WHERE user_id=$2',
666 $this->setError('ERROR - Could Not Update User Status: '.db_error());
670 $this->data_array['status']=$status;
671 if ($status == 'D') {
672 // Remove this user from all groups
673 $res = db_query_params ('DELETE FROM user_group WHERE user_id=$1',
674 array ($this->getID())) ;
676 $this->setError('ERROR - Could Not Propogate Deleted Status: '.db_error());
681 $hook_params = array ();
682 $hook_params['user'] = $this;
683 $hook_params['user_id'] = $this->getID();
684 $hook_params['status'] = $status;
685 plugin_hook ("user_setstatus", $hook_params);
689 //plugin webcalendar, create cal_user
690 plugin_hook('add_cal_user',$this->getID());
697 * isActive - whether this user is confirmed and active.
699 * Database field status of 'A' returns true.
700 * @return boolean is_active.
702 function isActive() {
703 if ($this->getStatus()=='A') {
711 * getUnixStatus - Status of activation of unix account.
713 * @return char (N)one, (A)ctive, (S)uspended or (D)eleted
715 function getUnixStatus() {
716 return $this->data_array['unix_status'];
720 * setUnixStatus - Sets status of activation of unix account.
722 * @param string The unix status.
728 * @return boolean success.
730 function setUnixStatus($status) {
733 $res = db_query_params ('UPDATE users SET unix_status=$1 WHERE user_id=$2',
738 $this->setError('ERROR - Could Not Update User Unix Status: '.db_error());
742 if ($status == 'A') {
743 if (!$SYS->sysCheckCreateUser($this->getID())) {
744 $this->setError($SYS->getErrorMessage());
749 if ($SYS->sysCheckUser($this->getID())) {
750 if (!$SYS->sysRemoveUser($this->getID())) {
751 $this->setError($SYS->getErrorMessage());
758 $this->data_array['unix_status']=$status;
765 * getUnixName - the user's unix_name.
767 * @return string This user's unix/login name.
769 function getUnixName() {
770 return strtolower($this->data_array['user_name']);
774 * getUnixPasswd - get the user's password.
776 * @return string This user's unix crypted passwd.
778 function getUnixPasswd() {
779 return $this->data_array['unix_pw'];
783 * getUnixBox - the hostname of the unix box this user has an account on.
785 * @return string This user's shell login machine.
787 function getUnixBox() {
788 return $this->data_array['unix_box'];
792 * getMD5Passwd - the password.
794 * @return string This user's MD5-crypted passwd.
796 function getMD5Passwd() {
797 return $this->data_array['user_pw'];
800 //Added to be compatible with codendi getUserPw function
801 function getUserPw() {
802 return $this->data_array['user_pw'];
806 * getConfirmHash - the confirm hash in the db.
808 * @return string This user's confirmation hash.
810 function getConfirmHash() {
811 return $this->data_array['confirm_hash'];
815 * getEmail - the user's email address.
817 * @return string This user's email address.
819 function getEmail() {
820 return $this->data_array['email'];
824 * getSha1Email - a SHA1 encoded hash of the email URI (including mailto: prefix)
826 * @return string The SHA1 encoded value for the email
828 function getSha1Email() {
829 return sha1('mailto:'.$this->getEmail());
833 * getNewEmail - while changing an email address, it is stored here until confirmation.
835 * getNewEmail is a private operation for email change.
837 * @return string This user's new (not yet confirmed) email address.
840 function getNewEmail() {
841 return $this->data_array['email_new'];
845 * setEmail - set a new email address, which must be confirmed.
847 * @param string The email address.
848 * @return boolean success.
850 function setEmail($email) {
852 if (!strcasecmp($this->getEmail(), $email)) {
856 if (!$email || !validate_email($email)) {
857 $this->setError('ERROR: Invalid Email');
861 if (forge_get_config('require_unique_email')) {
862 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)',
863 array ($this->getID(),
864 strtolower($email)))) > 0) {
865 $this->setError(_('User with this email already exists.'));
871 $res = db_query_params ('UPDATE users SET email=$1 WHERE user_id=$2',
876 $this->setError('ERROR - Could Not Update User Email: '.db_error());
880 $hook_params = array ();
881 $hook_params['user'] = $this;
882 $hook_params['user_id'] = $this->getID();
883 $hook_params['user_email'] = $email;
884 plugin_hook ("user_setemail", $hook_params);
886 if (!$this->fetchData($this->getId())) {
897 * setNewEmailAndHash - setNewEmailAndHash is a private operation for email change.
899 * @param string The email address.
900 * @param string The email hash.
901 * @return boolean success.
903 function setNewEmailAndHash($email, $hash='') {
906 $hash = substr(md5(strval(time()) . strval(mt_rand())), 0, 16);
909 if (!$email || !validate_email($email)) {
910 $this->setError('ERROR - Invalid Email');
914 if (forge_get_config('require_unique_email')) {
915 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)',
916 array ($this->getID(),
917 strtolower($email)))) > 0) {
918 $this->setError(_('User with this email already exists.'));
922 $res = db_query_params ('UPDATE users SET confirm_hash=$1, email_new=$2 WHERE user_id=$3',
927 $this->setError('ERROR - Could Not Update User Email And Hash: '.db_error());
930 $this->data_array['email_new'] = $email;
931 $this->data_array['confirm_hash'] = $hash;
937 * getRealName - get the user's real name.
939 * @return string This user's real name.
941 function getRealName() {
942 $last_name = $this->getLastName();
943 return $this->getFirstName(). ($last_name ? ' ' .$last_name:'');
947 * getFirstName - get the user's first name.
949 * @return string This user's first name.
951 function getFirstName() {
952 return $this->data_array['firstname'];
956 * getLastName - get the user's last name.
958 * @return string This user's last name.
960 function getLastName() {
961 return $this->data_array['lastname'];
965 * getAddDate - this user's unix time when account was opened.
967 * @return int This user's unix time when account was opened.
969 function getAddDate() {
970 return $this->data_array['add_date'];
974 * getTimeZone - this user's timezone setting.
976 * @return string This user's timezone setting.
978 function getTimeZone() {
979 return $this->data_array['timezone'];
983 * getCountryCode - this user's ccode setting.
985 * @return string This user's ccode setting.
987 function getCountryCode() {
988 return $this->data_array['ccode'];
992 * getShell - this user's preferred shell.
994 * @return string This user's preferred shell.
996 function getShell() {
997 return $this->data_array['shell'];
1001 * setShell - sets user's preferred shell.
1003 * @param string The users preferred shell.
1004 * @return boolean success.
1006 function setShell($shell) {
1008 $shells = file('/etc/shells');
1009 $shells[count($shells)] = "/bin/cvssh";
1010 $out_shells = array();
1011 foreach ($shells as $s) {
1012 if (substr($s, 0, 1) == '#') {
1015 $out_shells[] = chop($s);
1017 if (!in_array($shell, $out_shells)) {
1018 $this->setError('ERROR: Invalid Shell');
1023 $res = db_query_params ('UPDATE users SET shell=$1 WHERE user_id=$2',
1027 $this->setError('ERROR - Could Not Update User Unix Shell: '.db_error());
1031 // Now change LDAP attribute, but only if corresponding
1032 // entry exists (i.e. if user have shell access)
1033 if ($SYS->sysCheckUser($this->getID()))
1035 if (!$SYS->sysUserSetAttribute($this->getID(),"loginShell",$shell)) {
1036 $this->setError($SYS->getErrorMessage());
1041 $this->data_array['shell']=$shell;
1048 * getUnixUID() - Get the unix UID of the user
1050 * @return int This user's UID.
1052 function getUnixUID() {
1053 return $this->data_array['unix_uid'];
1057 * getUnixGID() - Get the unix GID of the user
1059 * @return int This user's GID.
1061 function getUnixGID() {
1062 return $this->data_array['unix_gid'];
1066 * getLanguage - this user's language_id from supported_languages table.
1068 * @return int This user's language_id.
1070 function getLanguage() {
1071 return $this->data_array['language'];
1075 * getJabberAddress - this user's optional jabber address.
1077 * @return string This user's jabber address.
1079 function getJabberAddress() {
1080 return $this->data_array['jabber_address'];
1084 * getJabberOnly - whether this person wants updates sent ONLY to jabber.
1086 * @return boolean This user's jabber preference.
1088 function getJabberOnly() {
1089 return $this->data_array['jabber_only'];
1093 * getAddress - get this user's address.
1095 * @return text This user's address.
1097 function getAddress() {
1098 return $this->data_array['address'];
1102 * getAddress2 - get this user's address2.
1104 * @return text This user's address2.
1106 function getAddress2() {
1107 return $this->data_array['address2'];
1111 * getPhone - get this person's phone number.
1113 * @return text This user's phone number.
1115 function getPhone() {
1116 return $this->data_array['phone'];
1120 * getFax - get this person's fax number.
1122 * @return text This user's fax.
1125 return $this->data_array['fax'];
1129 * getTitle - get this person's title.
1131 * @return text This user's title.
1133 function getTitle() {
1134 return $this->data_array['title'];
1138 * getGroups - get an array of groups this user is a member of.
1140 * @return array Array of groups.
1142 function &getGroups() {
1143 $res = db_query_params ('SELECT group_id FROM user_group WHERE user_id=$1',
1144 array ($this->getID())) ;
1145 $arr =& util_result_column_to_array($res,0);
1146 return group_get_objects($arr);
1150 * getAuthorizedKeys - the SSH authorized keys set by the user.
1152 * @return string This user's SSH authorized (public) keys.
1154 function getAuthorizedKeys() {
1155 return ereg_replace("###", "\n", $this->data_array['authorized_keys']);
1159 * setAuthorizedKeys - set the SSH authorized keys for the user.
1161 * @param string The users public keys.
1162 * @return boolean success.
1164 function setAuthorizedKeys($keys) {
1165 $keys = trim($keys);
1166 $keys = ereg_replace("\r\n", "\n", $keys); // Convert to Unix EOL
1167 $keys = ereg_replace("\n+", "\n", $keys); // Remove empty lines
1168 $keys = ereg_replace("\n", "###", $keys); // Convert EOL to marker
1170 $res = db_query_params ('UPDATE users SET authorized_keys=$1 WHERE user_id=$2',
1174 $this->setError('ERROR - Could Not Update User SSH Keys');
1177 $this->data_array['authorized_keys'] = $keys;
1183 * setLoggedIn($val) - Really only used by session code.
1185 * @param boolean The session value.
1187 function setLoggedIn($val=true) {
1188 $this->is_logged_in=$val;
1190 //if this is the logged in user, see if they are a super user
1191 $result = db_query_params ('SELECT count(*) AS count FROM user_group WHERE user_id=$1 AND group_id=1 AND admin_flags=$2',
1192 array ($this->getID(),
1195 $this->is_super_user=false;
1198 $row_count = db_fetch_array($result);
1199 $this->is_super_user = ($row_count['count'] > 0);
1204 * isLoggedIn - only used by session code.
1206 * @return boolean is_logged_in.
1208 function isLoggedIn() {
1209 return $this->is_logged_in;
1213 * deletePreference - delete a preference for this user.
1215 * @param string The unique field name for this preference.
1216 * @return boolean success.
1218 function deletePreference($preference_name) {
1219 $preference_name=strtolower(trim($preference_name));
1220 unset($this->user_pref["$preference_name"]);
1221 $res = db_query_params ('DELETE FROM user_preferences WHERE user_id=$1 AND preference_name=$2',
1222 array ($this->getID(),
1223 $preference_name)) ;
1228 * setPreference - set a new preference for this user.
1230 * @param string The unique field name for this preference.
1231 * @param string The value you are setting this preference to.
1232 * @return boolean success.
1234 function setPreference($preference_name,$value) {
1235 $preference_name=strtolower(trim($preference_name));
1236 //delete pref if not value passed in
1237 unset($this->user_pref);
1238 if (!isset($value)) {
1239 $result = db_query_params ('DELETE FROM user_preferences WHERE user_id=$1 AND preference_name=$2',
1240 array ($this->getID(),
1241 $preference_name)) ;
1243 $result = db_query_params ('UPDATE user_preferences SET preference_value=$1,set_date=$2 WHERE user_id=$3 AND preference_name=$4',
1247 $preference_name)) ;
1248 if (db_affected_rows($result) < 1) {
1250 $result = db_query_params ('INSERT INTO user_preferences (user_id,preference_name,preference_value,set_date) VALUES ($1,$2,$3,$4)',
1251 array ($this->getID(),
1261 * getPreference - get a specific preference.
1263 * @param string The unique field name for this preference.
1264 * @return the preference string or false on failure.
1266 function getPreference($preference_name) {
1267 $preference_name=strtolower(trim($preference_name));
1269 First check to see if we have already fetched the preferences
1271 if (isset($this->user_pref)) {
1272 //echo "\n\nPrefs were fetched already";
1273 if (isset($this->user_pref["$preference_name"])) {
1274 //we have fetched prefs - return part of array
1275 return $this->user_pref["$preference_name"];
1277 //we have fetched prefs, but this pref hasn't been set
1281 //we haven't returned prefs - go to the db
1282 $result = db_query_params ('SELECT preference_name,preference_value FROM user_preferences WHERE user_id=$1',
1283 array ($this->getID())) ;
1284 if (db_numrows($result) < 1) {
1285 //echo "\n\nNo Prefs Found";
1289 //iterate and put the results into an array
1290 for ($i=0; $i<db_numrows($result); $i++) {
1291 $pref["".db_result($result,$i,'preference_name').""]=db_result($result,$i,'preference_value');
1293 $this->user_pref = $pref;
1295 if (array_key_exists($preference_name,$this->user_pref)) {
1296 //we have fetched prefs - return part of array
1297 return $this->user_pref["$preference_name"];
1299 //we have fetched prefs, but this pref hasn't been set
1307 * setPasswd - Changes user's password.
1309 * @param string The plaintext password.
1310 * @return boolean success.
1312 function setPasswd($passwd) {
1314 if (!account_pwvalid($passwd)) {
1315 $this->setError('Error: '.$GLOBALS['register_error']);
1320 $md5_pw = md5 ($passwd) ;
1321 $unix_pw = account_genunixpw ($passwd) ;
1323 $res = db_query_params ('UPDATE users SET user_pw=$1, unix_pw=$2 WHERE user_id=$3',
1328 if (!$res || db_affected_rows($res) < 1) {
1329 $this->setError('ERROR - Could Not Change User Password: '.db_error());
1333 // Now change LDAP password, but only if corresponding
1334 // entry exists (i.e. if user have shell access)
1335 if ($SYS->sysCheckUser($this->getID())) {
1336 if (!$SYS->sysUserSetAttribute($this->getID(),"userPassword",'{crypt}'.$unix_pw)) {
1337 $this->setError($SYS->getErrorMessage());
1343 $hook_params = array ();
1344 $hook_params['user'] = $this;
1345 $hook_params['user_id'] = $this->getID();
1346 $hook_params['user_password'] = $passwd;
1347 plugin_hook ("user_setpasswd", $hook_params);
1353 * usesRatings - whether user participates in rating system.
1355 * @return boolean success.
1357 function usesRatings() {
1358 return !$this->data_array['block_ratings'];
1362 * getPlugins - get a list of all available user plugins
1364 * @return array array containing plugin_id => plugin_name
1366 function getPlugins() {
1367 if (!isset($this->plugins_data)) {
1368 $this->plugins_data = array () ;
1370 $res = db_query_params ('SELECT user_plugin.plugin_id, plugins.plugin_name
1371 FROM user_plugin, plugins
1372 WHERE user_plugin.user_id=$1
1373 AND user_plugin.plugin_id=plugins.plugin_id',
1374 array ($this->getID())) ;
1375 $rows = db_numrows($res);
1377 for ($i=0; $i<$rows; $i++) {
1378 $plugin_id = db_result($res,$i,'plugin_id');
1379 $this->plugins_data[$plugin_id] = db_result($res,$i,'plugin_name');
1382 return $this->plugins_data ;
1386 * usesPlugin - returns true if the user uses a particular plugin
1388 * @param string name of the plugin
1389 * @return boolean whether plugin is being used or not
1391 function usesPlugin($pluginname) {
1392 $plugins_data = $this->getPlugins() ;
1393 foreach ($plugins_data as $p_name) {
1394 if ($p_name == $pluginname) {
1402 * setPluginUse - enables/disables plugins for the user
1404 * @param string name of the plugin
1405 * @param boolean the new state
1406 * @return string database result
1408 function setPluginUse($pluginname, $val=true) {
1409 if ($val == $this->usesPlugin($pluginname)) {
1410 // State is already good, returning
1413 $res = db_query_params ('SELECT plugin_id FROM plugins WHERE plugin_name=$1',
1414 array ($pluginname)) ;
1415 $rows = db_numrows($res);
1417 // Error: no plugin by that name
1420 $plugin_id = db_result($res,0,'plugin_id');
1422 unset ($this->plugins_data) ;
1424 return db_query_params ('INSERT INTO user_plugin (user_id,plugin_id) VALUES ($1,$2)',
1425 array ($this->getID(),
1428 return db_query_params ('DELETE FROM user_plugin WHERE user_id=$1 AND plugin_id=$2',
1429 array ($this->getID(),
1435 * getMailingsPrefs - Get activity status for one of the site mailings.
1437 * @param string The id of mailing ('mail_va' for community mailings, 'mail_siteupdates' for site mailings)
1438 * @return boolean success.
1440 function getMailingsPrefs($mailing_id) {
1441 if ($mailing_id=='va') {
1442 return $this->data_array['mail_va'];
1443 } else if ($mailing_id=='site') {
1444 return $this->data_array['mail_siteupdates'];
1451 * unsubscribeFromMailings - Disable email notifications for user.
1453 * @param boolean If false, disable general site mailings, else - all.
1454 * @return boolean success.
1456 function unsubscribeFromMailings($all=false) {
1457 $res1 = $res2 = $res3 = true;
1458 $res1 = db_query_params ('UPDATE users SET mail_siteupdates=0, mail_va=0 WHERE user_id=$1',
1459 array ($this->getID())) ;
1461 $res2 = db_query_params ('DELETE FROM forum_monitored_forums WHERE user_id=$1',
1462 array ($this->getID())) ;
1463 $res3 = db_query_params ('DELETE FROM filemodule_monitor WHERE user_id=$1',
1464 array ($this->getID())) ;
1467 return $res1 && $res2 && $res3;
1471 * getThemeID - get the theme_id for this user.
1473 * @return int The theme_id.
1475 function getThemeID() {
1476 return $this->data_array['theme_id'];
1480 * getThemeID - get the theme_id for this user from the theme_prefs table.
1482 * @return int The theme_id.
1484 function setUpTheme() {
1486 // An optimization in session_getdata lets us pre-fetch this in most cases.....
1488 if (!isset($this->data_array['dirname']) || !$this->data_array['dirname']) {
1489 $res = db_query_params ('SELECT dirname FROM themes WHERE theme_id=$1',
1490 array ($this->getThemeID())) ;
1491 $this->theme=db_result($res,0,'dirname');
1493 $this->theme=$this->data_array['dirname'];
1495 if (is_file(forge_get_config('themes_root').$this->theme.'/Theme.class.php')) {
1496 $GLOBALS['sys_theme']=$this->theme;
1498 $this->theme=forge_get_config('default_theme');
1500 return $this->theme;
1504 * getRole() - Get user Role object.
1506 * @param object group object
1507 * @return object Role object
1509 function getRole(&$group) {
1510 if (!$group || !is_object($group)) {
1511 $this->setError('User::getRole : Unable to get group object');
1514 $res = db_query_params ('SELECT role_id FROM user_group WHERE user_id=$1 AND group_id=$2',
1515 array ($this->getID(),
1517 if (!$res || db_numrows($res) < 1) {
1518 $this->setError('User::getRole::DB - Could Not get role_id '.db_error());
1521 $role_id = db_result($res,0,'role_id');
1525 $role = new Role($group,$role_id);
1526 if (!$role || !is_object($role)) {
1527 $this->setError('Error Getting Role Object');
1529 } elseif ($role->isError()) {
1530 $this->setError('User::getRole::roleget::'.$role->getErrorMessage());
1537 function isMember($group_id,$type){
1538 return user_ismember($group_id,$type);
1547 EVERYTHING BELOW HERE IS DEPRECATED
1550 DO NOT USE FOR ANY NEW CODE
1559 * user_ismember() - DEPRECATED; DO NOT USE!
1561 * @param int The Group ID
1562 * @param int The Type
1566 function user_ismember($group_id,$type=0) {
1567 if (!session_loggedin()) {
1571 $project =& group_get_object($group_id);
1573 if (!$project || !is_object($project)) {
1577 $perm =& $project->getPermission ();
1578 if (!$perm || !is_object($perm) || !$perm->isMember()) {
1582 $type=strtoupper($type);
1587 return $perm->isPMAdmin();
1592 return $perm->isForumAdmin();
1596 //just in this group
1597 return $perm->isMember();
1601 //admin for this group
1602 return $perm->isAdmin();
1607 return $perm->isDocEditor();
1619 * user_getname() - DEPRECATED; DO NOT USE!
1621 * @param int The User ID
1625 function user_getname($user_id = false) {
1626 // use current user if one is not passed in
1628 if (session_loggedin()) {
1629 $user=&user_get_object(user_getid());
1631 return $user->getUnixName();
1633 return 'Error getting user';
1636 return 'No User Id';
1639 $user=&user_get_object($user_id);
1641 return $user->getUnixName();
1643 return 'Invalid User';
1650 // c-file-style: "bsd"