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() - Get User object by user ID.
50 * user_get_object is useful so you can pool user objects/save database queries
51 * You should always use this instead of instantiating the object directly
53 * @param int The ID of the user - required
54 * @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
55 * @return a user object or false on failure
58 function &user_get_object($user_id,$res=false) {
59 //create a common set of group objects
60 //saves a little wear on the database
62 //automatically checks group_type and
63 //returns appropriate object
66 if (!isset($USER_OBJ["_".$user_id."_"])) {
68 //the db result handle was passed in
70 $res = db_query_params ('SELECT * FROM users WHERE user_id=$1',
73 if (!$res || db_numrows($res) < 1) {
74 $USER_OBJ["_".$user_id."_"]=false;
76 $USER_OBJ["_".$user_id."_"]= new GFUser($user_id,$res);
79 return $USER_OBJ["_".$user_id."_"];
82 function &user_get_objects($id_arr) {
87 for ($i=0; $i<count($id_arr); $i++) {
89 // See if this ID already has been fetched in the cache
94 if (!isset($USER_OBJ["_".$id_arr[$i]."_"])) {
97 $return[] =& $USER_OBJ["_".$id_arr[$i]."_"];
100 if (count($fetch) > 0) {
101 $res = db_query_params ('SELECT * FROM users WHERE user_id = ANY ($1)',
102 array (db_int_array_to_any_clause ($fetch))) ;
103 while ($arr =& db_fetch_array($res)) {
104 $USER_OBJ["_".$arr['user_id']."_"] = new GFUser($arr['user_id'],$arr);
105 $return[] =& $USER_OBJ["_".$arr['user_id']."_"];
111 function &user_get_objects_by_name($username_arr) {
112 $res = db_query_params ('SELECT user_id FROM users WHERE user_name = ANY ($1)',
113 array (db_string_array_to_any_clause ($username_arr))) ;
114 $arr =& util_result_column_to_array($res,0);
115 return user_get_objects($arr);
118 class GFUser extends Error {
120 * Associative array of data from db.
122 * @var array $data_array.
127 * Is this person a site super-admin?
129 * @var bool $is_super_user
134 * Is this person the logged in user?
136 * @var bool $is_logged_in
141 * Array of preferences
143 * @var array $user_pref
151 * GFUser($id,$res) - CONSTRUCTOR - GENERALLY DON'T USE THIS
153 * instead use the user_get_object() function call
155 * @param int The user_id
156 * @param int The database result set OR array of data
158 function GFUser($id=false,$res=false) {
161 //setting up an empty object
162 //probably going to call create()
166 $this->fetchData($id);
168 if (is_array($res)) {
169 $this->data_array =& $res;
170 } elseif (db_numrows($res) < 1) {
171 //function in class we extended
172 $this->setError('User Not Found');
173 $this->data_array=array();
176 //set up an associative array for use by other functions
177 db_reset_result($res);
178 $this->data_array = db_fetch_array($res);
181 $this->is_super_user=false;
182 $this->is_logged_in=false;
187 * create() - Create a new user.
189 * @param string The unix username.
190 * @param string The real firstname.
191 * @param string The real lastname.
192 * @param string The first password.
193 * @param string The confirmation password.
194 * @param string The users email address.
195 * @param string The users preferred default language.
196 * @param string The users preferred default timezone.
197 * @param string The users preference for receiving site updates by email.
198 * @param string The users preference for receiving community updates by email.
199 * @param int The ID of the language preference.
200 * @param string The users preferred timezone.
201 * @param string The users Jabber address.
202 * @param int The users Jabber preference.
203 * @param int The users theme_id.
204 * @param string The users unix_box.
205 * @param string The users address.
206 * @param string The users address part 2.
207 * @param string The users phone.
208 * @param string The users fax.
209 * @param string The users title.
210 * @param char(2) The users ISO country_code.
211 * @param bool Whether to send an email or not
212 * @returns The newly created user ID
215 function create($unix_name,$firstname,$lastname,$password1,$password2,$email,
216 $mail_site,$mail_va,$language_id,$timezone,$jabber_address,$jabber_only,$theme_id,
217 $unix_box='shell',$address='',$address2='',$phone='',$fax='',$title='',$ccode='US',$send_mail=true) {
219 $this->setError(_('You must supply a theme'));
223 $this->setError(_('You must supply a username'));
227 $this->setError(_('You must supply a first name'));
231 $this->setError(_('You must supply a last name'));
235 $this->setError(_('You must supply a password'));
238 if ($password1 != $password2) {
239 $this->setError(_('Passwords do not match'));
242 if (!account_pwvalid($password1)) {
243 $this->setError(_('Invalid Password:'));
246 $unix_name=strtolower($unix_name);
247 if (!account_namevalid($unix_name)) {
248 $this->setError(_('Invalid Unix Name.'));
251 if (!validate_email($email)) {
252 $this->setError(_('Invalid Email Address'));
255 if ($jabber_address && !validate_email($jabber_address)) {
256 $this->setError(_('Invalid Jabber Address'));
264 if (db_numrows(db_query_params('SELECT user_id FROM users WHERE user_name LIKE $1',
265 array ($unix_name))) > 0) {
266 $this->setError(_('That username already exists.'));
269 if ($GLOBALS['sys_require_unique_email']) {
270 if (db_numrows(db_query_params('SELECT user_id FROM users WHERE email ILIKE $1 OR email_new ILIKE $1',
271 array ($email))) > 0) {
272 $this->setError(_('User with this email already exists - use people search to recover your login.'));
276 // if we got this far, it must be good
277 $confirm_hash = substr(md5($password1 . rand() . microtime()),0,16);
279 $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)',
282 account_genunixpw($password1),
283 htmlspecialchars($firstname.' '.$lastname),
284 htmlspecialchars($firstname),
285 htmlspecialchars($lastname),
290 (($mail_site)?"1":"0"),
291 (($mail_va)?"1":"0"),
297 htmlspecialchars($address),
298 htmlspecialchars($address2),
299 htmlspecialchars($phone),
300 htmlspecialchars($fax),
301 htmlspecialchars($title),
305 $this->setError(_('Insert Failed') .db_error().$sql);
310 $id = db_insertid($result,'users','user_id');
312 $this->setError('Could Not Get USERID: ' .db_error());
317 if (!$this->fetchData($id)) {
322 $hook_params = array ();
323 $hook_params['user'] = $this;
324 $hook_params['user_id'] = $this->getID();
325 $hook_params['user_name'] = $unix_name;
326 $hook_params['user_password'] = $password1;
327 plugin_hook ("user_create", $hook_params);
330 setup_gettext_from_lang_id($language_id);
331 $this->sendRegistrationEmail();
332 setup_gettext_from_context();
341 * sendRegistrationEmail() - Send email for registration verification
343 * @return true or false
345 function sendRegistrationEmail() {
346 $message=stripcslashes(sprintf(_('Thank you for registering on the %3$s web site. You have
347 account with username %1$s created for you. In order
348 to complete your registration, visit the following url:
352 (If you don\'t see any URL above, it is likely due to a bug in your mail client.
353 Use one below, but make sure it is entered as the single line.)
361 $this->getUnixName(),
362 util_make_url ('/account/verify.php?confirm_hash=_'.$this->getConfirmHash()),
363 $GLOBALS['sys_name']));
366 sprintf(_('%1$s Account Registration'), $GLOBALS['sys_name']),
372 * delete() - remove the User from all his groups.
374 * Remove the User from all his groups and set his status to D.
376 * @param boolean Confirmation of deletion.
377 * @return true or false
379 function delete($sure) {
383 $groups = &$this->getGroups();
384 if (is_array($groups)) {
385 foreach ($groups as $group) {
386 $group->removeUser($this->getID());
391 $res = db_query_params ('DELETE FROM artifact_monitor WHERE user_id=$1',
392 array ($this->getID())) ;
394 $this->setError('ERROR - Could Not Delete From artifact_monitor: '.db_error());
398 $res = db_query_params ('DELETE FROM artifact_type_monitor WHERE user_id=$1',
399 array ($this->getID())) ;
401 $this->setError('ERROR - Could Not Delete From artifact_type_monitor: '.db_error());
405 $res = db_query_params ('DELETE FROM forum_monitored_forums WHERE user_id=$1',
406 array ($this->getID())) ;
408 $this->setError('ERROR - Could Not Delete From forum_monitored_forums: '.db_error());
412 $res = db_query_params ('DELETE FROM filemodule_monitor WHERE user_id=$1',
413 array ($this->getID())) ;
415 $this->setError('ERROR - Could Not Delete From filemodule_monitor: '.db_error());
420 $hook_params = array ();
421 $hook_params['user'] = $this;
422 $hook_params['user_id'] = $this->getID();
423 plugin_hook ("user_delete", $hook_params);
425 $this->setStatus('D');
432 * update() - update *common* properties of GFUser object.
434 * Use specific setter to change other properties.
436 * @param string The users first name.
437 * @param string The users last name.
438 * @param int The ID of the users language preference.
439 * @param string The useres timezone preference.
440 * @param string The users preference for receiving site updates by email.
441 * @param string The users preference for receiving community updates by email.
442 * @param string The users preference for being participating in "peer ratings".
443 * @param string The users Jabber account address.
444 * @param int The users Jabber preference.
445 * @param int The users theme_id preference.
446 * @param string The users address.
447 * @param string The users address2.
448 * @param string The users phone.
449 * @param string The users fax.
450 * @param string The users title.
451 * @param string The users ccode.
453 function update($firstname,$lastname,$language_id,$timezone,$mail_site,$mail_va,$use_ratings,
454 $jabber_address,$jabber_only,$theme_id,$address,$address2,$phone,$fax,$title,$ccode) {
455 $mail_site = $mail_site ? 1 : 0;
456 $mail_va = $mail_va ? 1 : 0;
457 $block_ratings = $use_ratings ? 0 : 1;
459 if ($jabber_address && !validate_email($jabber_address)) {
460 $this->setError(_('Invalid Jabber Address'));
471 $res = db_query_params ('
493 htmlspecialchars($firstname . ' ' .$lastname),
494 htmlspecialchars($firstname),
495 htmlspecialchars($lastname),
503 htmlspecialchars($address) ,
504 htmlspecialchars($address2) ,
505 htmlspecialchars($phone) ,
506 htmlspecialchars($fax) ,
507 htmlspecialchars($title) ,
513 $this->setError('ERROR - Could Not Update User Object: '.db_error());
517 // If there's a transaction from using to not
518 // using ratings, remove all rating made by the
519 // user (ratings by others should not be removed,
520 // as it opens possibility to abuse rate system)
521 if (!$use_ratings && $this->usesRatings()) {
522 vote_remove_all_ratings_by($this->getID());
524 if (!$this->fetchData($this->getID())) {
529 $hook_params = array ();
530 $hook_params['user'] = $this;
531 $hook_params['user_id'] = $this->getID();
532 plugin_hook ("user_update", $hook_params);
540 * fetchData - May need to refresh database fields.
542 * If an update occurred and you need to access the updated info.
544 * @return boolean success;
546 function fetchData($user_id) {
547 $res = db_query_params ('SELECT * FROM users WHERE user_id=$1',
549 if (!$res || db_numrows($res) < 1) {
550 $this->setError('GFUser::fetchData()::'.db_error());
553 $this->data_array =& db_fetch_array($res);
558 * getID - Simply return the user_id for this object.
560 * @return int This user's user_id number.
563 return $this->data_array['user_id'];
567 * getStatus - get the status of this user.
569 * Statuses include (A)ctive, (P)ending, (S)uspended ,(D)eleted.
571 * @return char This user's status flag.
573 function getStatus() {
574 return $this->data_array['status'];
578 * setStatus - set this user's status.
580 * @param string Status - P, A, S, or D.
581 * @return boolean success.
583 function setStatus($status) {
585 if ($status != 'P' && $status != 'A'
586 && $status != 'S' && $status != 'D') {
587 $this->setError('ERROR: Invalid status value');
592 $res = db_query_params ('UPDATE users SET status=$1 WHERE user_id=$2',
597 $this->setError('ERROR - Could Not Update User Status: '.db_error());
601 $this->data_array['status']=$status;
602 if ($status == 'D') {
603 // Remove this user from all groups
604 $res = db_query_params ('DELETE FROM user_group WHERE user_id=$1',
605 array ($this->getID())) ;
607 $this->setError('ERROR - Could Not Propogate Deleted Status: '.db_error());
612 $hook_params = array ();
613 $hook_params['user'] = $this;
614 $hook_params['user_id'] = $this->getID();
615 $hook_params['status'] = $status;
616 plugin_hook ("user_setstatus", $hook_params);
620 //plugin webcalendar, create cal_user
621 plugin_hook('add_cal_user',$this->getID());
628 * isActive - whether this user is confirmed and active.
630 * Database field status of 'A' returns true.
631 * @return boolean is_active.
633 function isActive() {
634 if ($this->getStatus()=='A') {
642 * getUnixStatus - Status of activation of unix account.
644 * @return char (N)one, (A)ctive, (S)uspended or (D)eleted
646 function getUnixStatus() {
647 return $this->data_array['unix_status'];
651 * setUnixStatus - Sets status of activation of unix account.
653 * @param string The unix status.
659 * @return boolean success.
661 function setUnixStatus($status) {
664 $res = db_query_params ('UPDATE users SET unix_status=$1 WHERE user_id=$2',
669 $this->setError('ERROR - Could Not Update User Unix Status: '.db_error());
673 if ($status == 'A') {
674 if (!$SYS->sysCheckCreateUser($this->getID())) {
675 $this->setError($SYS->getErrorMessage());
680 if ($SYS->sysCheckUser($this->getID())) {
681 if (!$SYS->sysRemoveUser($this->getID())) {
682 $this->setError($SYS->getErrorMessage());
689 $this->data_array['unix_status']=$status;
696 * getUnixName - the user's unix_name.
698 * @return string This user's unix/login name.
700 function getUnixName() {
701 return strtolower($this->data_array['user_name']);
705 * getUnixPasswd - get the user's password.
707 * @return string This user's unix crypted passwd.
709 function getUnixPasswd() {
710 return $this->data_array['unix_pw'];
714 * getUnixBox - the hostname of the unix box this user has an account on.
716 * @return string This user's shell login machine.
718 function getUnixBox() {
719 return $this->data_array['unix_box'];
723 * getMD5Passwd - the password.
725 * @return string This user's MD5-crypted passwd.
727 function getMD5Passwd() {
728 return $this->data_array['user_pw'];
732 * getConfirmHash - the confirm hash in the db.
734 * @return string This user's confirmation hash.
736 function getConfirmHash() {
737 return $this->data_array['confirm_hash'];
741 * getEmail - the user's email address.
743 * @return string This user's email address.
745 function getEmail() {
746 return $this->data_array['email'];
750 * getNewEmail - while changing an email address, it is stored here until confirmation.
752 * getNewEmail is a private operation for email change.
754 * @return string This user's new (not yet confirmed) email address.
757 function getNewEmail() {
758 return $this->data_array['email_new'];
762 * setEmail - set a new email address, which must be confirmed.
764 * @param string The email address.
765 * @return boolean success.
767 function setEmail($email) {
769 if (!strcasecmp($this->getEmail(), $email)) {
773 if (!$email || !validate_email($email)) {
774 $this->setError('ERROR: Invalid Email');
778 if ($GLOBALS['sys_require_unique_email']) {
779 if (db_numrows(db_query("SELECT user_id FROM users WHERE email ILIKE '$email' OR email_new ILIKE '$email'")) > 0) {
780 $this->setError(_('User with this email already exists.'));
786 $res = db_query_params ('UPDATE users SET email=$1 WHERE user_id=$2',
791 $this->setError('ERROR - Could Not Update User Email: '.db_error());
795 $hook_params = array ();
796 $hook_params['user'] = $this;
797 $hook_params['user_id'] = $this->getID();
798 $hook_params['user_email'] = $email;
799 plugin_hook ("user_setemail", $hook_params);
801 $this->data_array['email'] = $email;
808 * setNewEmailAndHash - setNewEmailAndHash is a private operation for email change.
810 * @param string The email address.
811 * @param string The email hash.
812 * @return boolean success.
814 function setNewEmailAndHash($email, $hash='') {
817 $hash = substr(md5(strval(time()) . strval(mt_rand())), 0, 16);
820 if (!$email || !validate_email($email)) {
821 $this->setError('ERROR - Invalid Email');
825 if ($GLOBALS['sys_require_unique_email']) {
826 if (db_numrows(db_query_params ('SELECT user_id FROM users WHERE email ILIKE $1 OR email_new ILIKE $1',
827 array ($email))) > 0) {
828 $this->setError(_('User with this email already exists.'));
832 $res = db_query_params ('UPDATE users SET confirm_hash=$1, email_new=$2 WHERE user_id=$3',
837 $this->setError('ERROR - Could Not Update User Email And Hash: '.db_error());
840 $this->data_array['email_new'] = $email;
841 $this->data_array['confirm_hash'] = $hash;
847 * getRealName - get the user's real name.
849 * @return string This user's real name.
851 function getRealName() {
852 return $this->getFirstName(). ' ' .$this->getLastName();
856 * getFirstName - get the user's first name.
858 * @return string This user's first name.
860 function getFirstName() {
861 return $this->data_array['firstname'];
865 * getLastName - get the user's last name.
867 * @return string This user's last name.
869 function getLastName() {
870 return $this->data_array['lastname'];
874 * getAddDate - this user's unix time when account was opened.
876 * @return int This user's unix time when account was opened.
878 function getAddDate() {
879 return $this->data_array['add_date'];
883 * getTimeZone - this user's timezone setting.
885 * @return string This user's timezone setting.
887 function getTimeZone() {
888 return $this->data_array['timezone'];
892 * getCountryCode - this user's ccode setting.
894 * @return string This user's ccode setting.
896 function getCountryCode() {
897 return $this->data_array['ccode'];
901 * getShell - this user's preferred shell.
903 * @return string This user's preferred shell.
905 function getShell() {
906 return $this->data_array['shell'];
910 * setShell - sets user's preferred shell.
912 * @param string The users preferred shell.
913 * @return boolean success.
915 function setShell($shell) {
917 $shells = file('/etc/shells');
918 $shells[count($shells)] = "/bin/cvssh";
919 $out_shells = array();
920 foreach ($shells as $s) {
921 if (substr($s, 0, 1) == '#') {
924 $out_shells[] = chop($s);
926 if (!in_array($shell, $out_shells)) {
927 $this->setError('ERROR: Invalid Shell');
932 $res = db_query_params ('UPDATE users SET shell=$1 WHERE user_id=$2',
936 $this->setError('ERROR - Could Not Update User Unix Shell: '.db_error());
940 // Now change LDAP attribute, but only if corresponding
941 // entry exists (i.e. if user have shell access)
942 if ($SYS->sysCheckUser($this->getID()))
944 if (!$SYS->sysUserSetAttribute($this->getID(),"loginShell",$shell)) {
945 $this->setError($SYS->getErrorMessage());
950 $this->data_array['shell']=$shell;
957 * getUnixUID() - Get the unix UID of the user
959 * @return int This user's UID.
961 function getUnixUID() {
962 return $this->data_array['unix_uid'];
966 * getUnixGID() - Get the unix GID of the user
968 * @return int This user's GID.
970 function getUnixGID() {
971 return $this->data_array['unix_gid'];
975 * getLanguage - this user's language_id from supported_languages table.
977 * @return int This user's language_id.
979 function getLanguage() {
980 return $this->data_array['language'];
984 * getJabberAddress - this user's optional jabber address.
986 * @return string This user's jabber address.
988 function getJabberAddress() {
989 return $this->data_array['jabber_address'];
993 * getJabberOnly - whether this person wants updates sent ONLY to jabber.
995 * @return boolean This user's jabber preference.
997 function getJabberOnly() {
998 return $this->data_array['jabber_only'];
1002 * getAddress - get this user's address.
1004 * @return text This user's address.
1006 function getAddress() {
1007 return $this->data_array['address'];
1011 * getAddress2 - get this user's address2.
1013 * @return text This user's address2.
1015 function getAddress2() {
1016 return $this->data_array['address2'];
1020 * getPhone - get this person's phone number.
1022 * @return text This user's phone number.
1024 function getPhone() {
1025 return $this->data_array['phone'];
1029 * getFax - get this person's fax number.
1031 * @return text This user's fax.
1034 return $this->data_array['fax'];
1038 * getTitle - get this person's title.
1040 * @return text This user's title.
1042 function getTitle() {
1043 return $this->data_array['title'];
1047 * getGroups - get an array of groups this user is a member of.
1049 * @return array Array of groups.
1051 function &getGroups() {
1052 $res = db_query_params ('SELECT group_id FROM user_group WHERE user_id=$1',
1053 array ($this->getID())) ;
1054 $arr =& util_result_column_to_array($res,0);
1055 return group_get_objects($arr);
1059 * getAuthorizedKeys - the SSH authorized keys set by the user.
1061 * @return string This user's SSH authorized (public) keys.
1063 function getAuthorizedKeys() {
1064 return ereg_replace("###", "\n", $this->data_array['authorized_keys']);
1068 * setAuthorizedKeys - set the SSH authorized keys for the user.
1070 * @param string The users public keys.
1071 * @return boolean success.
1073 function setAuthorizedKeys($keys) {
1074 $keys = trim($keys);
1075 $keys = ereg_replace("\r\n", "\n", $keys); // Convert to Unix EOL
1076 $keys = ereg_replace("\n+", "\n", $keys); // Remove empty lines
1077 $keys = ereg_replace("\n", "###", $keys); // Convert EOL to marker
1079 $res = db_query_params ('UPDATE users SET authorized_keys=$1 WHERE user_id=$2',
1083 $this->setError('ERROR - Could Not Update User SSH Keys');
1086 $this->data_array['authorized_keys'] = $keys;
1092 * setLoggedIn($val) - Really only used by session code.
1094 * @param boolean The session value.
1096 function setLoggedIn($val=true) {
1097 $this->is_logged_in=$val;
1099 //if this is the logged in user, see if they are a super user
1100 $result = db_query_params ('SELECT count(*) AS count FROM user_group WHERE user_id=$1 AND group_id=1 AND admin_flags=$2',
1101 array ($this->getID(),
1104 $this->is_super_user=false;
1107 $row_count = db_fetch_array($result);
1108 $this->is_super_user = ($row_count['count'] > 0);
1113 * isLoggedIn - only used by session code.
1115 * @return boolean is_logged_in.
1117 function isLoggedIn() {
1118 return $this->is_logged_in;
1122 * deletePreference - delete a preference for this user.
1124 * @param string The unique field name for this preference.
1125 * @return boolean success.
1127 function deletePreference($preference_name) {
1128 $preference_name=strtolower(trim($preference_name));
1129 unset($this->user_pref["$preference_name"]);
1130 $res = db_query_params ('DELETE FROM user_preferences WHERE user_id=$1 AND preference_name=$2',
1131 array ($this->getID(),
1132 $preference_name)) ;
1137 * setPreference - set a new preference for this user.
1139 * @param string The unique field name for this preference.
1140 * @param string The value you are setting this preference to.
1141 * @return boolean success.
1143 function setPreference($preference_name,$value) {
1144 $preference_name=strtolower(trim($preference_name));
1145 //delete pref if not value passed in
1146 unset($this->user_pref);
1147 if (!isset($value)) {
1148 $result = db_query_params ('DELETE FROM user_preferences WHERE user_id=$1 AND preference_name=$2',
1149 array ($this->getID(),
1150 $preference_name)) ;
1152 $result = db_query_params ('UPDATE user_preferences SET preference_value=$1,set_date=$2 WHERE user_id=$3 AND preference_name=$4',
1156 $preference_name)) ;
1157 if (db_affected_rows($result) < 1) {
1159 $result = db_query_params ('INSERT INTO user_preferences (user_id,preference_name,preference_value,set_date) VALUES ($1,$2,$3,$4)',
1160 array ($this->getID(),
1170 * getPreference - get a specific preference.
1172 * @param string The unique field name for this preference.
1173 * @return the preference string or false on failure.
1175 function getPreference($preference_name) {
1176 $preference_name=strtolower(trim($preference_name));
1178 First check to see if we have already fetched the preferences
1180 if (isset($this->user_pref)) {
1181 //echo "\n\nPrefs were fetched already";
1182 if (isset($this->user_pref["$preference_name"])) {
1183 //we have fetched prefs - return part of array
1184 return $this->user_pref["$preference_name"];
1186 //we have fetched prefs, but this pref hasn't been set
1190 //we haven't returned prefs - go to the db
1191 $result = db_query_params ('SELECT preference_name,preference_value FROM user_preferences WHERE user_id=$1',
1192 array ($this->getID())) ;
1193 if (db_numrows($result) < 1) {
1194 //echo "\n\nNo Prefs Found";
1198 //iterate and put the results into an array
1199 for ($i=0; $i<db_numrows($result); $i++) {
1200 $pref["".db_result($result,$i,'preference_name').""]=db_result($result,$i,'preference_value');
1202 $this->user_pref = $pref;
1204 if (array_key_exists($preference_name,$this->user_pref)) {
1205 //we have fetched prefs - return part of array
1206 return $this->user_pref["$preference_name"];
1208 //we have fetched prefs, but this pref hasn't been set
1216 * setPasswd - Changes user's password.
1218 * @param string The plaintext password.
1219 * @return boolean success.
1221 function setPasswd($passwd) {
1223 if (!account_pwvalid($passwd)) {
1224 $this->setError('Error: '.$GLOBALS['register_error']);
1229 $md5_pw = md5 ($passwd) ;
1230 $unix_pw = account_genunixpw ($passwd) ;
1232 $res = db_query_params ('UPDATE users SET user_pw=$1, unix_pw=$2 WHERE user_id=$3',
1237 if (!$res || db_affected_rows($res) < 1) {
1238 $this->setError('ERROR - Could Not Change User Password: '.db_error());
1242 // Now change LDAP password, but only if corresponding
1243 // entry exists (i.e. if user have shell access)
1244 if ($SYS->sysCheckUser($this->getID())) {
1245 if (!$SYS->sysUserSetAttribute($this->getID(),"userPassword",'{crypt}'.$unix_pw)) {
1246 $this->setError($SYS->getErrorMessage());
1252 $hook_params = array ();
1253 $hook_params['user'] = $this;
1254 $hook_params['user_id'] = $this->getID();
1255 $hook_params['user_password'] = $passwd;
1256 plugin_hook ("user_setpasswd", $hook_params);
1262 * usesRatings - whether user participates in rating system.
1264 * @return boolean success.
1266 function usesRatings() {
1267 return !$this->data_array['block_ratings'];
1271 * getPlugins - get a list of all available user plugins
1273 * @return array array containing plugin_id => plugin_name
1275 function getPlugins() {
1276 if (!isset($this->plugins_data)) {
1277 $this->plugins_data = array () ;
1279 $res = db_query_params ('SELECT user_plugin.plugin_id, plugins.plugin_name
1280 FROM user_plugin, plugins
1281 WHERE user_plugin.user_id=$1
1282 AND user_plugin.plugin_id=plugins.plugin_id',
1283 array ($this->getID())) ;
1284 $rows = db_numrows($res);
1286 for ($i=0; $i<$rows; $i++) {
1287 $plugin_id = db_result($res,$i,'plugin_id');
1288 $this->plugins_data[$plugin_id] = db_result($res,$i,'plugin_name');
1291 return $this->plugins_data ;
1295 * usesPlugin - returns true if the user uses a particular plugin
1297 * @param string name of the plugin
1298 * @return boolean whether plugin is being used or not
1300 function usesPlugin($pluginname) {
1301 $plugins_data = $this->getPlugins() ;
1302 foreach ($plugins_data as $p_name) {
1303 if ($p_name == $pluginname) {
1311 * setPluginUse - enables/disables plugins for the user
1313 * @param string name of the plugin
1314 * @param boolean the new state
1315 * @return string database result
1317 function setPluginUse($pluginname, $val=true) {
1318 if ($val == $this->usesPlugin($pluginname)) {
1319 // State is already good, returning
1322 $res = db_query_params ('SELECT plugin_id FROM plugins WHERE plugin_name=$1',
1323 array ($pluginname)) ;
1324 $rows = db_numrows($res);
1326 // Error: no plugin by that name
1329 $plugin_id = db_result($res,0,'plugin_id');
1331 unset ($this->plugins_data) ;
1333 return db_query_params ('INSERT INTO user_plugin (user_id,plugin_id) VALUES ($1,$2)',
1334 array ($this->getID(),
1337 return db_query_params ('DELETE FROM user_plugin WHERE user_id=$1 AND plugin_id=$2',
1338 array ($this->getID(),
1344 * getMailingsPrefs - Get activity status for one of the site mailings.
1346 * @param string The id of mailing ('mail_va' for community mailings, 'mail_siteupdates' for site mailings)
1347 * @return boolean success.
1349 function getMailingsPrefs($mailing_id) {
1350 if ($mailing_id=='va') {
1351 return $this->data_array['mail_va'];
1352 } else if ($mailing_id=='site') {
1353 return $this->data_array['mail_siteupdates'];
1360 * unsubscribeFromMailings - Disable email notifications for user.
1362 * @param boolean If false, disable general site mailings, else - all.
1363 * @return boolean success.
1365 function unsubscribeFromMailings($all=false) {
1366 $res1 = $res2 = $res3 = true;
1367 $res1 = db_query_params ('UPDATE users SET mail_siteupdates=0, mail_va=0 WHERE user_id=$1',
1368 array ($this->getID())) ;
1370 $res2 = db_query_params ('DELETE FROM forum_monitored_forums WHERE user_id=$1',
1371 array ($this->getID())) ;
1372 $res3 = db_query_params ('DELETE FROM filemodule_monitor WHERE user_id=$1',
1373 array ($this->getID())) ;
1376 return $res1 && $res2 && $res3;
1380 * getThemeID - get the theme_id for this user.
1382 * @return int The theme_id.
1384 function getThemeID() {
1385 return $this->data_array['theme_id'];
1389 * getThemeID - get the theme_id for this user from the theme_prefs table.
1391 * @return int The theme_id.
1393 function setUpTheme() {
1395 // An optimization in session_getdata lets us pre-fetch this in most cases.....
1397 if (!$this->data_array['dirname']) {
1398 $res = db_query_params ('SELECT dirname FROM themes WHERE theme_id=$1',
1399 array ($this->getThemeID())) ;
1400 $this->theme=db_result($res,0,'dirname');
1402 $this->theme=$this->data_array['dirname'];
1404 if (is_file($GLOBALS['sys_themeroot'].$this->theme.'/Theme.class.php')) {
1405 $GLOBALS['sys_theme']=$this->theme;
1407 $this->theme=$GLOBALS['sys_theme'];
1409 return $this->theme;
1418 EVERYTHING BELOW HERE IS DEPRECATED
1421 DO NOT USE FOR ANY NEW CODE
1430 * user_ismember() - DEPRECATED; DO NOT USE!
1432 * @param int The Group ID
1433 * @param int The Type
1437 function user_ismember($group_id,$type=0) {
1438 if (!session_loggedin()) {
1442 $project =& group_get_object($group_id);
1444 if (!$project || !is_object($project)) {
1448 $perm =& $project->getPermission( session_get_user() );
1449 if (!$perm || !is_object($perm) || !$perm->isMember()) {
1453 $type=strtoupper($type);
1458 return $perm->isPMAdmin();
1463 return $perm->isForumAdmin();
1467 //just in this group
1468 return $perm->isMember();
1472 //admin for this group
1473 return $perm->isAdmin();
1478 return $perm->isDocEditor();
1490 * user_getname() - DEPRECATED; DO NOT USE!
1492 * @param int The User ID
1496 function user_getname($user_id = false) {
1497 // use current user if one is not passed in
1499 if (session_loggedin()) {
1500 $user=&user_get_object(user_getid());
1502 return $user->getUnixName();
1504 return 'Error getting user';
1507 return 'No User Id';
1510 $user=&user_get_object($user_id);
1512 return $user->getUnixName();
1514 return 'Invalid User';
1521 // c-file-style: "bsd"