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 || !$GLOBALS['sys_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 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 db_reset_result($res);
199 $this->data_array = db_fetch_array($res);
202 $this->is_super_user=false;
203 $this->is_logged_in=false;
208 * create() - Create a new user.
210 * @param string The unix username.
211 * @param string The real firstname.
212 * @param string The real lastname.
213 * @param string The first password.
214 * @param string The confirmation password.
215 * @param string The users email address.
216 * @param string The users preferred default language.
217 * @param string The users preferred default timezone.
218 * @param string The users preference for receiving site updates by email.
219 * @param string The users preference for receiving community updates by email.
220 * @param int The ID of the language preference.
221 * @param string The users preferred timezone.
222 * @param string The users Jabber address.
223 * @param int The users Jabber preference.
224 * @param int The users theme_id.
225 * @param string The users unix_box.
226 * @param string The users address.
227 * @param string The users address part 2.
228 * @param string The users phone.
229 * @param string The users fax.
230 * @param string The users title.
231 * @param char(2) The users ISO country_code.
232 * @param bool Whether to send an email or not
233 * @returns The newly created user ID
236 function create($unix_name,$firstname,$lastname,$password1,$password2,$email,
237 $mail_site,$mail_va,$language_id,$timezone,$jabber_address,$jabber_only,$theme_id,
238 $unix_box='shell',$address='',$address2='',$phone='',$fax='',$title='',$ccode='US',$send_mail=true) {
240 $this->setError(_('You must supply a theme'));
243 if (! $GLOBALS['sys_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 if (!validate_email($email)) {
270 $this->setError(_('Invalid Email Address'));
273 if ($jabber_address && !validate_email($jabber_address)) {
274 $this->setError(_('Invalid Jabber Address'));
282 if ($unix_name && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name LIKE $1',
283 array ($unix_name))) > 0) {
284 $this->setError(_('That username already exists.'));
287 if ($GLOBALS['sys_require_unique_email']) {
288 if (user_get_object_by_email ('$email')) {
289 $this->setError(_('User with this email already exists - use people search to recover your login.'));
293 if ($GLOBALS['sys_require_unique_email'] && !$unix_name) {
294 // Let's generate a loginname for the user
295 // ...based on the email address:
296 $email_array = explode ('@', $email, 2) ;
297 $email_u = $email_array [0] ;
298 $l = ereg_replace ('[^a-z0-9]', '', $email_u) ;
299 $l = substr ($l, 0, 15) ;
300 // Is the user part of the email address okay?
301 if (account_namevalid($l)
302 && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name = $1',
306 // No? What if we add a number at the end?
309 $c = substr ($l, 0, 15-strlen ("$i")) . "$i" ;
310 if (account_namevalid($c)
311 && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name = $1',
319 // If we're really unlucky, then let's go brute-force
320 while (!$unix_name) {
321 $c = substr (md5($email . rand()), 0, 15) ;
322 if (account_namevalid($c)
323 && db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name = $1',
329 $unix_name=strtolower($unix_name);
330 if (!account_namevalid($unix_name)) {
331 $this->setError(_('Invalid Unix Name.'));
334 // if we got this far, it must be good
335 $confirm_hash = substr(md5($password1 . rand() . microtime()),0,16);
337 $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)',
340 account_genunixpw($password1),
341 htmlspecialchars($firstname.' '.$lastname),
342 htmlspecialchars($firstname),
343 htmlspecialchars($lastname),
348 (($mail_site)?"1":"0"),
349 (($mail_va)?"1":"0"),
355 htmlspecialchars($address),
356 htmlspecialchars($address2),
357 htmlspecialchars($phone),
358 htmlspecialchars($fax),
359 htmlspecialchars($title),
363 $this->setError(_('Insert Failed') .db_error());
368 $id = db_insertid($result,'users','user_id');
370 $this->setError('Could Not Get USERID: ' .db_error());
375 if (!$this->fetchData($id)) {
380 $hook_params = array ();
381 $hook_params['user'] = $this;
382 $hook_params['user_id'] = $this->getID();
383 $hook_params['user_name'] = $unix_name;
384 $hook_params['user_password'] = $password1;
385 plugin_hook ("user_create", $hook_params);
388 setup_gettext_from_lang_id($language_id);
389 $this->sendRegistrationEmail();
390 setup_gettext_from_context();
399 * sendRegistrationEmail() - Send email for registration verification
401 * @return true or false
403 function sendRegistrationEmail() {
404 $message=stripcslashes(sprintf(_('Thank you for registering on the %3$s web site. You have
405 account with username %1$s created for you. In order
406 to complete your registration, visit the following url:
410 (If you don\'t see any URL above, it is likely due to a bug in your mail client.
411 Use one below, but make sure it is entered as the single line.)
419 $this->getUnixName(),
420 util_make_url ('/account/verify.php?confirm_hash=_'.$this->getConfirmHash()),
421 $GLOBALS['sys_name']));
424 sprintf(_('%1$s Account Registration'), $GLOBALS['sys_name']),
430 * delete() - remove the User from all his groups.
432 * Remove the User from all his groups and set his status to D.
434 * @param boolean Confirmation of deletion.
435 * @return true or false
437 function delete($sure) {
441 $groups = &$this->getGroups();
442 if (is_array($groups)) {
443 foreach ($groups as $group) {
444 $group->removeUser($this->getID());
449 $res = db_query_params ('DELETE FROM artifact_monitor WHERE user_id=$1',
450 array ($this->getID())) ;
452 $this->setError('ERROR - Could Not Delete From artifact_monitor: '.db_error());
456 $res = db_query_params ('DELETE FROM artifact_type_monitor WHERE user_id=$1',
457 array ($this->getID())) ;
459 $this->setError('ERROR - Could Not Delete From artifact_type_monitor: '.db_error());
463 $res = db_query_params ('DELETE FROM forum_monitored_forums WHERE user_id=$1',
464 array ($this->getID())) ;
466 $this->setError('ERROR - Could Not Delete From forum_monitored_forums: '.db_error());
470 $res = db_query_params ('DELETE FROM filemodule_monitor WHERE user_id=$1',
471 array ($this->getID())) ;
473 $this->setError('ERROR - Could Not Delete From filemodule_monitor: '.db_error());
478 $hook_params = array ();
479 $hook_params['user'] = $this;
480 $hook_params['user_id'] = $this->getID();
481 plugin_hook ("user_delete", $hook_params);
483 $this->setStatus('D');
490 * update() - update *common* properties of GFUser object.
492 * Use specific setter to change other properties.
494 * @param string The users first name.
495 * @param string The users last name.
496 * @param int The ID of the users language preference.
497 * @param string The useres timezone preference.
498 * @param string The users preference for receiving site updates by email.
499 * @param string The users preference for receiving community updates by email.
500 * @param string The users preference for being participating in "peer ratings".
501 * @param string The users Jabber account address.
502 * @param int The users Jabber preference.
503 * @param int The users theme_id preference.
504 * @param string The users address.
505 * @param string The users address2.
506 * @param string The users phone.
507 * @param string The users fax.
508 * @param string The users title.
509 * @param string The users ccode.
511 function update($firstname,$lastname,$language_id,$timezone,$mail_site,$mail_va,$use_ratings,
512 $jabber_address,$jabber_only,$theme_id,$address,$address2,$phone,$fax,$title,$ccode) {
513 $mail_site = $mail_site ? 1 : 0;
514 $mail_va = $mail_va ? 1 : 0;
515 $block_ratings = $use_ratings ? 0 : 1;
517 if ($jabber_address && !validate_email($jabber_address)) {
518 $this->setError(_('Invalid Jabber Address'));
529 $res = db_query_params ('
551 htmlspecialchars($firstname . ' ' .$lastname),
552 htmlspecialchars($firstname),
553 htmlspecialchars($lastname),
561 htmlspecialchars($address) ,
562 htmlspecialchars($address2) ,
563 htmlspecialchars($phone) ,
564 htmlspecialchars($fax) ,
565 htmlspecialchars($title) ,
571 $this->setError('ERROR - Could Not Update User Object: '.db_error());
575 // If there's a transaction from using to not
576 // using ratings, remove all rating made by the
577 // user (ratings by others should not be removed,
578 // as it opens possibility to abuse rate system)
579 if (!$use_ratings && $this->usesRatings()) {
580 vote_remove_all_ratings_by($this->getID());
582 if (!$this->fetchData($this->getID())) {
587 $hook_params = array ();
588 $hook_params['user'] = $this;
589 $hook_params['user_id'] = $this->getID();
590 plugin_hook ("user_update", $hook_params);
598 * fetchData - May need to refresh database fields.
600 * If an update occurred and you need to access the updated info.
602 * @return boolean success;
604 function fetchData($user_id) {
605 $res = db_query_params ('SELECT * FROM users WHERE user_id=$1',
607 if (!$res || db_numrows($res) < 1) {
608 $this->setError('GFUser::fetchData()::'.db_error());
611 $this->data_array =& db_fetch_array($res);
616 * getID - Simply return the user_id for this object.
618 * @return int This user's user_id number.
621 return $this->data_array['user_id'];
625 * getStatus - get the status of this user.
627 * Statuses include (A)ctive, (P)ending, (S)uspended ,(D)eleted.
629 * @return char This user's status flag.
631 function getStatus() {
632 return $this->data_array['status'];
636 * setStatus - set this user's status.
638 * @param string Status - P, A, S, or D.
639 * @return boolean success.
641 function setStatus($status) {
643 if ($status != 'P' && $status != 'A'
644 && $status != 'S' && $status != 'D') {
645 $this->setError('ERROR: Invalid status value');
650 $res = db_query_params ('UPDATE users SET status=$1 WHERE user_id=$2',
655 $this->setError('ERROR - Could Not Update User Status: '.db_error());
659 $this->data_array['status']=$status;
660 if ($status == 'D') {
661 // Remove this user from all groups
662 $res = db_query_params ('DELETE FROM user_group WHERE user_id=$1',
663 array ($this->getID())) ;
665 $this->setError('ERROR - Could Not Propogate Deleted Status: '.db_error());
670 $hook_params = array ();
671 $hook_params['user'] = $this;
672 $hook_params['user_id'] = $this->getID();
673 $hook_params['status'] = $status;
674 plugin_hook ("user_setstatus", $hook_params);
678 //plugin webcalendar, create cal_user
679 plugin_hook('add_cal_user',$this->getID());
686 * isActive - whether this user is confirmed and active.
688 * Database field status of 'A' returns true.
689 * @return boolean is_active.
691 function isActive() {
692 if ($this->getStatus()=='A') {
700 * getUnixStatus - Status of activation of unix account.
702 * @return char (N)one, (A)ctive, (S)uspended or (D)eleted
704 function getUnixStatus() {
705 return $this->data_array['unix_status'];
709 * setUnixStatus - Sets status of activation of unix account.
711 * @param string The unix status.
717 * @return boolean success.
719 function setUnixStatus($status) {
722 $res = db_query_params ('UPDATE users SET unix_status=$1 WHERE user_id=$2',
727 $this->setError('ERROR - Could Not Update User Unix Status: '.db_error());
731 if ($status == 'A') {
732 if (!$SYS->sysCheckCreateUser($this->getID())) {
733 $this->setError($SYS->getErrorMessage());
738 if ($SYS->sysCheckUser($this->getID())) {
739 if (!$SYS->sysRemoveUser($this->getID())) {
740 $this->setError($SYS->getErrorMessage());
747 $this->data_array['unix_status']=$status;
754 * getUnixName - the user's unix_name.
756 * @return string This user's unix/login name.
758 function getUnixName() {
759 return strtolower($this->data_array['user_name']);
763 * getUnixPasswd - get the user's password.
765 * @return string This user's unix crypted passwd.
767 function getUnixPasswd() {
768 return $this->data_array['unix_pw'];
772 * getUnixBox - the hostname of the unix box this user has an account on.
774 * @return string This user's shell login machine.
776 function getUnixBox() {
777 return $this->data_array['unix_box'];
781 * getMD5Passwd - the password.
783 * @return string This user's MD5-crypted passwd.
785 function getMD5Passwd() {
786 return $this->data_array['user_pw'];
790 * getConfirmHash - the confirm hash in the db.
792 * @return string This user's confirmation hash.
794 function getConfirmHash() {
795 return $this->data_array['confirm_hash'];
799 * getEmail - the user's email address.
801 * @return string This user's email address.
803 function getEmail() {
804 return $this->data_array['email'];
808 * getNewEmail - while changing an email address, it is stored here until confirmation.
810 * getNewEmail is a private operation for email change.
812 * @return string This user's new (not yet confirmed) email address.
815 function getNewEmail() {
816 return $this->data_array['email_new'];
820 * setEmail - set a new email address, which must be confirmed.
822 * @param string The email address.
823 * @return boolean success.
825 function setEmail($email) {
827 if (!strcasecmp($this->getEmail(), stripslashes($email))) {
831 if (!$email || !validate_email($email)) {
832 $this->setError('ERROR: Invalid Email');
836 if ($GLOBALS['sys_require_unique_email']) {
837 if (db_numrows(db_query_params('SELECT user_id FROM users WHERE email ILIKE $1 OR email_new ILIKE $2',
838 array ($email, $email))) > 0) {
839 $this->setError(_('User with this email already exists.'));
845 $res = db_query_params ('UPDATE users SET email=$1 WHERE user_id=$2',
850 $this->setError('ERROR - Could Not Update User Email: '.db_error());
854 $hook_params = array ();
855 $hook_params['user'] = $this;
856 $hook_params['user_id'] = $this->getID();
857 $hook_params['user_email'] = $email;
858 plugin_hook ("user_setemail", $hook_params);
860 if (!$this->fetchData($this->getId())) {
871 * setNewEmailAndHash - setNewEmailAndHash is a private operation for email change.
873 * @param string The email address.
874 * @param string The email hash.
875 * @return boolean success.
877 function setNewEmailAndHash($email, $hash='') {
880 $hash = substr(md5(strval(time()) . strval(mt_rand())), 0, 16);
883 if (!$email || !validate_email($email)) {
884 $this->setError('ERROR - Invalid Email');
888 if ($GLOBALS['sys_require_unique_email']) {
889 if (db_numrows(db_query_params ('SELECT user_id FROM users WHERE email ILIKE $1 OR email_new ILIKE $1',
890 array ($email))) > 0) {
891 $this->setError(_('User with this email already exists.'));
895 $res = db_query_params ('UPDATE users SET confirm_hash=$1, email_new=$2 WHERE user_id=$3',
900 $this->setError('ERROR - Could Not Update User Email And Hash: '.db_error());
903 $this->data_array['email_new'] = $email;
904 $this->data_array['confirm_hash'] = $hash;
910 * getRealName - get the user's real name.
912 * @return string This user's real name.
914 function getRealName() {
915 return $this->getFirstName(). ' ' .$this->getLastName();
919 * getFirstName - get the user's first name.
921 * @return string This user's first name.
923 function getFirstName() {
924 return $this->data_array['firstname'];
928 * getLastName - get the user's last name.
930 * @return string This user's last name.
932 function getLastName() {
933 return $this->data_array['lastname'];
937 * getAddDate - this user's unix time when account was opened.
939 * @return int This user's unix time when account was opened.
941 function getAddDate() {
942 return $this->data_array['add_date'];
946 * getTimeZone - this user's timezone setting.
948 * @return string This user's timezone setting.
950 function getTimeZone() {
951 return $this->data_array['timezone'];
955 * getCountryCode - this user's ccode setting.
957 * @return string This user's ccode setting.
959 function getCountryCode() {
960 return $this->data_array['ccode'];
964 * getShell - this user's preferred shell.
966 * @return string This user's preferred shell.
968 function getShell() {
969 return $this->data_array['shell'];
973 * setShell - sets user's preferred shell.
975 * @param string The users preferred shell.
976 * @return boolean success.
978 function setShell($shell) {
980 $shells = file('/etc/shells');
981 $shells[count($shells)] = "/bin/cvssh";
982 $out_shells = array();
983 foreach ($shells as $s) {
984 if (substr($s, 0, 1) == '#') {
987 $out_shells[] = chop($s);
989 if (!in_array($shell, $out_shells)) {
990 $this->setError('ERROR: Invalid Shell');
995 $res = db_query_params ('UPDATE users SET shell=$1 WHERE user_id=$2',
999 $this->setError('ERROR - Could Not Update User Unix Shell: '.db_error());
1003 // Now change LDAP attribute, but only if corresponding
1004 // entry exists (i.e. if user have shell access)
1005 if ($SYS->sysCheckUser($this->getID()))
1007 if (!$SYS->sysUserSetAttribute($this->getID(),"loginShell",$shell)) {
1008 $this->setError($SYS->getErrorMessage());
1013 $this->data_array['shell']=$shell;
1020 * getUnixUID() - Get the unix UID of the user
1022 * @return int This user's UID.
1024 function getUnixUID() {
1025 return $this->data_array['unix_uid'];
1029 * getUnixGID() - Get the unix GID of the user
1031 * @return int This user's GID.
1033 function getUnixGID() {
1034 return $this->data_array['unix_gid'];
1038 * getLanguage - this user's language_id from supported_languages table.
1040 * @return int This user's language_id.
1042 function getLanguage() {
1043 return $this->data_array['language'];
1047 * getJabberAddress - this user's optional jabber address.
1049 * @return string This user's jabber address.
1051 function getJabberAddress() {
1052 return $this->data_array['jabber_address'];
1056 * getJabberOnly - whether this person wants updates sent ONLY to jabber.
1058 * @return boolean This user's jabber preference.
1060 function getJabberOnly() {
1061 return $this->data_array['jabber_only'];
1065 * getAddress - get this user's address.
1067 * @return text This user's address.
1069 function getAddress() {
1070 return $this->data_array['address'];
1074 * getAddress2 - get this user's address2.
1076 * @return text This user's address2.
1078 function getAddress2() {
1079 return $this->data_array['address2'];
1083 * getPhone - get this person's phone number.
1085 * @return text This user's phone number.
1087 function getPhone() {
1088 return $this->data_array['phone'];
1092 * getFax - get this person's fax number.
1094 * @return text This user's fax.
1097 return $this->data_array['fax'];
1101 * getTitle - get this person's title.
1103 * @return text This user's title.
1105 function getTitle() {
1106 return $this->data_array['title'];
1110 * getGroups - get an array of groups this user is a member of.
1112 * @return array Array of groups.
1114 function &getGroups() {
1115 $res = db_query_params ('SELECT group_id FROM user_group WHERE user_id=$1',
1116 array ($this->getID())) ;
1117 $arr =& util_result_column_to_array($res,0);
1118 return group_get_objects($arr);
1122 * getAuthorizedKeys - the SSH authorized keys set by the user.
1124 * @return string This user's SSH authorized (public) keys.
1126 function getAuthorizedKeys() {
1127 return ereg_replace("###", "\n", $this->data_array['authorized_keys']);
1131 * setAuthorizedKeys - set the SSH authorized keys for the user.
1133 * @param string The users public keys.
1134 * @return boolean success.
1136 function setAuthorizedKeys($keys) {
1137 $keys = trim($keys);
1138 $keys = ereg_replace("\r\n", "\n", $keys); // Convert to Unix EOL
1139 $keys = ereg_replace("\n+", "\n", $keys); // Remove empty lines
1140 $keys = ereg_replace("\n", "###", $keys); // Convert EOL to marker
1142 $res = db_query_params ('UPDATE users SET authorized_keys=$1 WHERE user_id=$2',
1146 $this->setError('ERROR - Could Not Update User SSH Keys');
1149 $this->data_array['authorized_keys'] = $keys;
1155 * setLoggedIn($val) - Really only used by session code.
1157 * @param boolean The session value.
1159 function setLoggedIn($val=true) {
1160 $this->is_logged_in=$val;
1162 //if this is the logged in user, see if they are a super user
1163 $result = db_query_params ('SELECT count(*) AS count FROM user_group WHERE user_id=$1 AND group_id=1 AND admin_flags=$2',
1164 array ($this->getID(),
1167 $this->is_super_user=false;
1170 $row_count = db_fetch_array($result);
1171 $this->is_super_user = ($row_count['count'] > 0);
1176 * isLoggedIn - only used by session code.
1178 * @return boolean is_logged_in.
1180 function isLoggedIn() {
1181 return $this->is_logged_in;
1185 * deletePreference - delete a preference for this user.
1187 * @param string The unique field name for this preference.
1188 * @return boolean success.
1190 function deletePreference($preference_name) {
1191 $preference_name=strtolower(trim($preference_name));
1192 unset($this->user_pref["$preference_name"]);
1193 $res = db_query_params ('DELETE FROM user_preferences WHERE user_id=$1 AND preference_name=$2',
1194 array ($this->getID(),
1195 $preference_name)) ;
1200 * setPreference - set a new preference for this user.
1202 * @param string The unique field name for this preference.
1203 * @param string The value you are setting this preference to.
1204 * @return boolean success.
1206 function setPreference($preference_name,$value) {
1207 $preference_name=strtolower(trim($preference_name));
1208 //delete pref if not value passed in
1209 unset($this->user_pref);
1210 if (!isset($value)) {
1211 $result = db_query_params ('DELETE FROM user_preferences WHERE user_id=$1 AND preference_name=$2',
1212 array ($this->getID(),
1213 $preference_name)) ;
1215 $result = db_query_params ('UPDATE user_preferences SET preference_value=$1,set_date=$2 WHERE user_id=$3 AND preference_name=$4',
1219 $preference_name)) ;
1220 if (db_affected_rows($result) < 1) {
1222 $result = db_query_params ('INSERT INTO user_preferences (user_id,preference_name,preference_value,set_date) VALUES ($1,$2,$3,$4)',
1223 array ($this->getID(),
1233 * getPreference - get a specific preference.
1235 * @param string The unique field name for this preference.
1236 * @return the preference string or false on failure.
1238 function getPreference($preference_name) {
1239 $preference_name=strtolower(trim($preference_name));
1241 First check to see if we have already fetched the preferences
1243 if (isset($this->user_pref)) {
1244 //echo "\n\nPrefs were fetched already";
1245 if (isset($this->user_pref["$preference_name"])) {
1246 //we have fetched prefs - return part of array
1247 return $this->user_pref["$preference_name"];
1249 //we have fetched prefs, but this pref hasn't been set
1253 //we haven't returned prefs - go to the db
1254 $result = db_query_params ('SELECT preference_name,preference_value FROM user_preferences WHERE user_id=$1',
1255 array ($this->getID())) ;
1256 if (db_numrows($result) < 1) {
1257 //echo "\n\nNo Prefs Found";
1261 //iterate and put the results into an array
1262 for ($i=0; $i<db_numrows($result); $i++) {
1263 $pref["".db_result($result,$i,'preference_name').""]=db_result($result,$i,'preference_value');
1265 $this->user_pref = $pref;
1267 if (array_key_exists($preference_name,$this->user_pref)) {
1268 //we have fetched prefs - return part of array
1269 return $this->user_pref["$preference_name"];
1271 //we have fetched prefs, but this pref hasn't been set
1279 * setPasswd - Changes user's password.
1281 * @param string The plaintext password.
1282 * @return boolean success.
1284 function setPasswd($passwd) {
1286 if (!account_pwvalid($passwd)) {
1287 $this->setError('Error: '.$GLOBALS['register_error']);
1292 $md5_pw = md5 ($passwd) ;
1293 $unix_pw = account_genunixpw ($passwd) ;
1295 $res = db_query_params ('UPDATE users SET user_pw=$1, unix_pw=$2 WHERE user_id=$3',
1300 if (!$res || db_affected_rows($res) < 1) {
1301 $this->setError('ERROR - Could Not Change User Password: '.db_error());
1305 // Now change LDAP password, but only if corresponding
1306 // entry exists (i.e. if user have shell access)
1307 if ($SYS->sysCheckUser($this->getID())) {
1308 if (!$SYS->sysUserSetAttribute($this->getID(),"userPassword",'{crypt}'.$unix_pw)) {
1309 $this->setError($SYS->getErrorMessage());
1315 $hook_params = array ();
1316 $hook_params['user'] = $this;
1317 $hook_params['user_id'] = $this->getID();
1318 $hook_params['user_password'] = $passwd;
1319 plugin_hook ("user_setpasswd", $hook_params);
1325 * usesRatings - whether user participates in rating system.
1327 * @return boolean success.
1329 function usesRatings() {
1330 return !$this->data_array['block_ratings'];
1334 * getPlugins - get a list of all available user plugins
1336 * @return array array containing plugin_id => plugin_name
1338 function getPlugins() {
1339 if (!isset($this->plugins_data)) {
1340 $this->plugins_data = array () ;
1342 $res = db_query_params ('SELECT user_plugin.plugin_id, plugins.plugin_name
1343 FROM user_plugin, plugins
1344 WHERE user_plugin.user_id=$1
1345 AND user_plugin.plugin_id=plugins.plugin_id',
1346 array ($this->getID())) ;
1347 $rows = db_numrows($res);
1349 for ($i=0; $i<$rows; $i++) {
1350 $plugin_id = db_result($res,$i,'plugin_id');
1351 $this->plugins_data[$plugin_id] = db_result($res,$i,'plugin_name');
1354 return $this->plugins_data ;
1358 * usesPlugin - returns true if the user uses a particular plugin
1360 * @param string name of the plugin
1361 * @return boolean whether plugin is being used or not
1363 function usesPlugin($pluginname) {
1364 $plugins_data = $this->getPlugins() ;
1365 foreach ($plugins_data as $p_name) {
1366 if ($p_name == $pluginname) {
1374 * setPluginUse - enables/disables plugins for the user
1376 * @param string name of the plugin
1377 * @param boolean the new state
1378 * @return string database result
1380 function setPluginUse($pluginname, $val=true) {
1381 if ($val == $this->usesPlugin($pluginname)) {
1382 // State is already good, returning
1385 $res = db_query_params ('SELECT plugin_id FROM plugins WHERE plugin_name=$1',
1386 array ($pluginname)) ;
1387 $rows = db_numrows($res);
1389 // Error: no plugin by that name
1392 $plugin_id = db_result($res,0,'plugin_id');
1394 unset ($this->plugins_data) ;
1396 return db_query_params ('INSERT INTO user_plugin (user_id,plugin_id) VALUES ($1,$2)',
1397 array ($this->getID(),
1400 return db_query_params ('DELETE FROM user_plugin WHERE user_id=$1 AND plugin_id=$2',
1401 array ($this->getID(),
1407 * getMailingsPrefs - Get activity status for one of the site mailings.
1409 * @param string The id of mailing ('mail_va' for community mailings, 'mail_siteupdates' for site mailings)
1410 * @return boolean success.
1412 function getMailingsPrefs($mailing_id) {
1413 if ($mailing_id=='va') {
1414 return $this->data_array['mail_va'];
1415 } else if ($mailing_id=='site') {
1416 return $this->data_array['mail_siteupdates'];
1423 * unsubscribeFromMailings - Disable email notifications for user.
1425 * @param boolean If false, disable general site mailings, else - all.
1426 * @return boolean success.
1428 function unsubscribeFromMailings($all=false) {
1429 $res1 = $res2 = $res3 = true;
1430 $res1 = db_query_params ('UPDATE users SET mail_siteupdates=0, mail_va=0 WHERE user_id=$1',
1431 array ($this->getID())) ;
1433 $res2 = db_query_params ('DELETE FROM forum_monitored_forums WHERE user_id=$1',
1434 array ($this->getID())) ;
1435 $res3 = db_query_params ('DELETE FROM filemodule_monitor WHERE user_id=$1',
1436 array ($this->getID())) ;
1439 return $res1 && $res2 && $res3;
1443 * getThemeID - get the theme_id for this user.
1445 * @return int The theme_id.
1447 function getThemeID() {
1448 return $this->data_array['theme_id'];
1452 * getThemeID - get the theme_id for this user from the theme_prefs table.
1454 * @return int The theme_id.
1456 function setUpTheme() {
1458 // An optimization in session_getdata lets us pre-fetch this in most cases.....
1460 if (!$this->data_array['dirname']) {
1461 $res = db_query_params ('SELECT dirname FROM themes WHERE theme_id=$1',
1462 array ($this->getThemeID())) ;
1463 $this->theme=db_result($res,0,'dirname');
1465 $this->theme=$this->data_array['dirname'];
1467 if (is_file($GLOBALS['sys_themeroot'].$this->theme.'/Theme.class.php')) {
1468 $GLOBALS['sys_theme']=$this->theme;
1470 $this->theme=$GLOBALS['sys_theme'];
1472 return $this->theme;
1476 * getRole() - Get user Role object.
1478 * @param object group object
1479 * @return object Role object
1481 function getRole(&$group) {
1482 if (!$group || !is_object($group)) {
1483 $this->setError('User::getRole : Unable to get group object');
1486 $res = db_query_params ('SELECT role_id FROM user_group WHERE user_id=$1 AND group_id=$2',
1487 array ($this->getID(),
1490 $this->setError('User::getRole::DB - Could Not get role_id '.db_error());
1493 $role_id = db_result($res,0,'role_id');
1497 $role = new Role($group,$role_id);
1498 if (!$role || !is_object($role)) {
1499 $this->setError('Error Getting Role Object');
1501 } elseif ($role->isError()) {
1502 $this->setError('User::getRole::roleget::'.$role->getErrorMessage());
1514 EVERYTHING BELOW HERE IS DEPRECATED
1517 DO NOT USE FOR ANY NEW CODE
1526 * user_ismember() - DEPRECATED; DO NOT USE!
1528 * @param int The Group ID
1529 * @param int The Type
1533 function user_ismember($group_id,$type=0) {
1534 if (!session_loggedin()) {
1538 $project =& group_get_object($group_id);
1540 if (!$project || !is_object($project)) {
1544 $perm =& $project->getPermission( session_get_user() );
1545 if (!$perm || !is_object($perm) || !$perm->isMember()) {
1549 $type=strtoupper($type);
1554 return $perm->isPMAdmin();
1559 return $perm->isForumAdmin();
1563 //just in this group
1564 return $perm->isMember();
1568 //admin for this group
1569 return $perm->isAdmin();
1574 return $perm->isDocEditor();
1586 * user_getname() - DEPRECATED; DO NOT USE!
1588 * @param int The User ID
1592 function user_getname($user_id = false) {
1593 // use current user if one is not passed in
1595 if (session_loggedin()) {
1596 $user=&user_get_object(user_getid());
1598 return $user->getUnixName();
1600 return 'Error getting user';
1603 return 'No User Id';
1606 $user=&user_get_object($user_id);
1608 return $user->getUnixName();
1610 return 'Invalid User';
1617 // c-file-style: "bsd"