5 * Sets up database results and preferences for a user and abstracts this info
7 * You can now optionally pass in a db result
8 * handle. If you do, it re-uses that query
9 * to instantiate the objects
11 * IMPORTANT! That db result must contain all fields
12 * from users table or you will have problems
14 * GENERALLY YOU SHOULD NEVER INSTANTIATE THIS OBJECT DIRECTLY
15 * USE user_get_object() to instantiate properly - this will pool the objects
16 * and increase efficiency
18 * Copyright 1999-2001 (c) VA Linux Systems
21 * @author Tim Perdue tperdue@valinux.com
24 * This file is part of GForge.
26 * GForge is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
31 * GForge is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with GForge; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 require_once $gfwww.'include/vote_function.php';
45 * user_get_object_by_name() - Get User object by username.
46 * user_get_object is useful so you can pool user objects/save database queries
47 * You should always use this instead of instantiating the object directly
49 * @param string The unix username - required
50 * @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
51 * @return a user object or false on failure
54 function &user_get_object_by_name($user_name,$res=false) {
55 $user_name = strtolower($user_name);
57 $res=db_query("SELECT * FROM users WHERE user_name='$user_name'");
59 return user_get_object(db_result($res,0,'user_id'),$res);
63 * user_get_object() - Get User object by user ID.
64 * user_get_object is useful so you can pool user objects/save database queries
65 * You should always use this instead of instantiating the object directly
67 * @param int The ID of the user - required
68 * @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
69 * @return a user object or false on failure
72 function &user_get_object($user_id,$res=false) {
73 //create a common set of group objects
74 //saves a little wear on the database
76 //automatically checks group_type and
77 //returns appropriate object
80 if (!isset($USER_OBJ["_".$user_id."_"])) {
82 //the db result handle was passed in
84 $res=db_query("SELECT * FROM users WHERE user_id='$user_id'");
86 if (!$res || db_numrows($res) < 1) {
87 $USER_OBJ["_".$user_id."_"]=false;
89 $USER_OBJ["_".$user_id."_"]= new GFUser($user_id,$res);
92 return $USER_OBJ["_".$user_id."_"];
95 function &user_get_objects($id_arr) {
100 for ($i=0; $i<count($id_arr); $i++) {
102 // See if this ID already has been fetched in the cache
107 if (!isset($USER_OBJ["_".$id_arr[$i]."_"])) {
108 $fetch[]=$id_arr[$i];
110 $return[] =& $USER_OBJ["_".$id_arr[$i]."_"];
113 if (count($fetch) > 0) {
114 $sql="SELECT * FROM users WHERE user_id IN ('".implode($fetch,'\',\'') ."')";
116 while ($arr =& db_fetch_array($res)) {
117 $USER_OBJ["_".$arr['user_id']."_"] = new GFUser($arr['user_id'],$arr);
118 $return[] =& $USER_OBJ["_".$arr['user_id']."_"];
124 function &user_get_objects_by_name($username_arr) {
125 $res=db_query("SELECT user_id FROM users WHERE user_name IN ('".implode($username_arr,'\',\'')."')");
126 $arr =& util_result_column_to_array($res,0);
127 return user_get_objects($arr);
130 class GFUser extends Error {
132 * Associative array of data from db.
134 * @var array $data_array.
139 * Is this person a site super-admin?
141 * @var bool $is_super_user
146 * Is this person the logged in user?
148 * @var bool $is_logged_in
153 * Array of preferences
155 * @var array $user_pref
163 * GFUser($id,$res) - CONSTRUCTOR - GENERALLY DON'T USE THIS
165 * instead use the user_get_object() function call
167 * @param int The user_id
168 * @param int The database result set OR array of data
170 function GFUser($id=false,$res=false) {
173 //setting up an empty object
174 //probably going to call create()
178 $this->fetchData($id);
180 if (is_array($res)) {
181 $this->data_array =& $res;
182 } elseif (db_numrows($res) < 1) {
183 //function in class we extended
184 $this->setError('User Not Found');
185 $this->data_array=array();
188 //set up an associative array for use by other functions
189 db_reset_result($res);
190 $this->data_array = db_fetch_array($res);
193 $this->is_super_user=false;
194 $this->is_logged_in=false;
199 * create() - Create a new user.
201 * @param string The unix username.
202 * @param string The real firstname.
203 * @param string The real lastname.
204 * @param string The first password.
205 * @param string The confirmation password.
206 * @param string The users email address.
207 * @param string The users preferred default language.
208 * @param string The users preferred default timezone.
209 * @param string The users preference for receiving site updates by email.
210 * @param string The users preference for receiving community updates by email.
211 * @param int The ID of the language preference.
212 * @param string The users preferred timezone.
213 * @param string The users Jabber address.
214 * @param int The users Jabber preference.
215 * @param int The users theme_id.
216 * @param string The users unix_box.
217 * @param string The users address.
218 * @param string The users address part 2.
219 * @param string The users phone.
220 * @param string The users fax.
221 * @param string The users title.
222 * @param char(2) The users ISO country_code.
223 * @param bool Whether to send an email or not
224 * @returns The newly created user ID
227 function create($unix_name,$firstname,$lastname,$password1,$password2,$email,
228 $mail_site,$mail_va,$language_id,$timezone,$jabber_address,$jabber_only,$theme_id,
229 $unix_box='shell',$address='',$address2='',$phone='',$fax='',$title='',$ccode='US',$send_mail=true) {
231 $this->setError(_('You must supply a theme'));
235 $this->setError(_('You must supply a username'));
239 $this->setError(_('You must supply a first name'));
243 $this->setError(_('You must supply a last name'));
247 $this->setError(_('You must supply a password'));
250 if ($password1 != $password2) {
251 $this->setError(_('Passwords do not match'));
254 if (!account_pwvalid($password1)) {
255 $this->setError(_('Invalid Password:'));
258 $unix_name=strtolower($unix_name);
259 if (!account_namevalid($unix_name)) {
260 $this->setError(_('Invalid Unix Name.'));
263 if (!validate_email($email)) {
264 $this->setError(_('Invalid Email Address'));
267 if ($jabber_address && !validate_email($jabber_address)) {
268 $this->setError(_('Invalid Jabber Address'));
276 if (db_numrows(db_query("SELECT user_id FROM users WHERE user_name LIKE '$unix_name'")) > 0) {
277 $this->setError(_('That username already exists.'));
280 if ($GLOBALS['sys_require_unique_email']) {
281 if (db_numrows(db_query("SELECT user_id FROM users WHERE email='$email'")) > 0) {
282 $this->setError(_('User with this email already exists - use people search to recover your login.'));
286 // if we got this far, it must be good
287 $confirm_hash = substr(md5($password1 . rand() . microtime()),0,16);
289 $sql="INSERT INTO users (user_name,user_pw,unix_pw,realname,firstname,lastname,email,add_date,
290 status,confirm_hash,mail_siteupdates,mail_va,language,timezone,jabber_address,jabber_only,
291 unix_box,address,address2,phone,fax,title,ccode,theme_id)
292 VALUES ('$unix_name',
293 '". md5($password1) . "',
294 '". account_genunixpw($password1) . "',
295 '". htmlspecialchars($firstname.' '.$lastname). "',
296 '". htmlspecialchars($firstname). "',
297 '". htmlspecialchars($lastname). "',
302 '". (($mail_site)?"1":"0") . "',
303 '". (($mail_va)?"1":"0") . "',
309 '". htmlspecialchars($address) ."',
310 '". htmlspecialchars($address2) ."',
311 '". htmlspecialchars($phone) ."',
312 '". htmlspecialchars($fax) ."',
313 '". htmlspecialchars($title) ."',
318 $result=db_query($sql);
321 $this->setError(_('Insert Failed') .db_error().$sql);
326 $id = db_insertid($result,'users','user_id');
328 $this->setError('Could Not Get USERID: ' .db_error());
333 if (!$this->fetchData($id)) {
338 $hook_params = array ();
339 $hook_params['user'] = $this;
340 $hook_params['user_id'] = $this->getID();
341 $hook_params['user_name'] = $unix_name;
342 $hook_params['user_password'] = $password1;
343 plugin_hook ("user_create", $hook_params);
346 setup_gettext_from_lang_id($language_id);
347 $this->sendRegistrationEmail();
348 setup_gettext_from_browser() ;
357 * sendRegistrationEmail() - Send email for registration verification
359 * @return true or false
361 function sendRegistrationEmail() {
362 $message=stripcslashes(sprintf(_('Thank you for registering on the %3$s web site. You have
363 account with username %1$s created for you. In order
364 to complete your registration, visit the following url:
368 (If you don\'t see any URL above, it is likely due to a bug in your mail client.
369 Use one below, but make sure it is entered as the single line.)
377 $this->getUnixName(),
378 util_make_url ('/account/verify.php?confirm_hash=_'.$this->getConfirmHash()),
379 $GLOBALS['sys_name']));
382 sprintf(_('%1$s Account Registration'), $GLOBALS['sys_name']),
388 * delete() - remove the User from all his groups.
390 * Remove the User from all his groups and set his status to D.
392 * @param boolean Confirmation of deletion.
393 * @return true or false
395 function delete($sure) {
399 $groups = &$this->getGroups();
400 if (is_array($groups)) {
401 foreach ($groups as $group) {
402 $group->removeUser($this->getID());
407 $res = db_query("DELETE FROM artifact_monitor WHERE user_id='".$this->getID()."' ");
409 $this->setError('ERROR - Could Not Delete From artifact_monitor: '.db_error());
413 $res = db_query("DELETE FROM artifact_type_monitor WHERE user_id='".$this->getID()."' ");
415 $this->setError('ERROR - Could Not Delete From artifact_type_monitor: '.db_error());
419 $res = db_query("DELETE FROM forum_monitored_forums WHERE user_id='".$this->getID()."' ");
421 $this->setError('ERROR - Could Not Delete From forum_monitored_forums: '.db_error());
425 $res = db_query("DELETE FROM filemodule_monitor WHERE user_id='".$this->getID()."' ");
427 $this->setError('ERROR - Could Not Delete From filemodule_monitor: '.db_error());
432 $hook_params = array ();
433 $hook_params['user'] = $this;
434 $hook_params['user_id'] = $this->getID();
435 plugin_hook ("user_delete", $hook_params);
437 $this->setStatus('D');
444 * update() - update *common* properties of GFUser object.
446 * Use specific setter to change other properties.
448 * @param string The users first name.
449 * @param string The users last name.
450 * @param int The ID of the users language preference.
451 * @param string The useres timezone preference.
452 * @param string The users preference for receiving site updates by email.
453 * @param string The users preference for receiving community updates by email.
454 * @param string The users preference for being participating in "peer ratings".
455 * @param string The users Jabber account address.
456 * @param int The users Jabber preference.
457 * @param int The users theme_id preference.
458 * @param string The users address.
459 * @param string The users address2.
460 * @param string The users phone.
461 * @param string The users fax.
462 * @param string The users title.
463 * @param string The users ccode.
465 function update($firstname,$lastname,$language_id,$timezone,$mail_site,$mail_va,$use_ratings,
466 $jabber_address,$jabber_only,$theme_id,$address,$address2,$phone,$fax,$title,$ccode) {
467 $mail_site = $mail_site ? 1 : 0;
468 $mail_va = $mail_va ? 1 : 0;
469 $block_ratings = $use_ratings ? 0 : 1;
471 if ($jabber_address && !validate_email($jabber_address)) {
472 $this->setError(_('Invalid Jabber Address'));
486 realname='".htmlspecialchars($firstname . ' ' .$lastname)."',
487 firstname='".htmlspecialchars($firstname)."',
488 lastname='".htmlspecialchars($lastname)."',
489 language='$language_id',
490 timezone='$timezone',
491 mail_siteupdates=$mail_site,
493 block_ratings='$block_ratings',
494 jabber_address='$jabber_address',
495 jabber_only='$jabber_only',
496 address='". htmlspecialchars($address) ."',
497 address2='". htmlspecialchars($address2) ."',
498 phone='". htmlspecialchars($phone) ."',
499 fax='". htmlspecialchars($fax) ."',
500 title='". htmlspecialchars($title) ."',
503 WHERE user_id='".$this->getID()."'
507 $this->setError('ERROR - Could Not Update User Object: '.db_error());
511 // If there's a transaction from using to not
512 // using ratings, remove all rating made by the
513 // user (ratings by others should not be removed,
514 // as it opens possibility to abuse rate system)
515 if (!$use_ratings && $this->usesRatings()) {
516 vote_remove_all_ratings_by($this->getID());
518 if (!$this->fetchData($this->getID())) {
523 $hook_params = array ();
524 $hook_params['user'] = $this;
525 $hook_params['user_id'] = $this->getID();
526 plugin_hook ("user_update", $hook_params);
534 * fetchData - May need to refresh database fields.
536 * If an update occurred and you need to access the updated info.
538 * @return boolean success;
540 function fetchData($user_id) {
541 $res=db_query("SELECT * FROM users WHERE user_id='$user_id'");
542 if (!$res || db_numrows($res) < 1) {
543 $this->setError('GFUser::fetchData()::'.db_error());
546 $this->data_array =& db_fetch_array($res);
551 * getID - Simply return the user_id for this object.
553 * @return int This user's user_id number.
556 return $this->data_array['user_id'];
560 * getStatus - get the status of this user.
562 * Statuses include (A)ctive, (P)ending, (S)uspended ,(D)eleted.
564 * @return char This user's status flag.
566 function getStatus() {
567 return $this->data_array['status'];
571 * setStatus - set this user's status.
573 * @param string Status - P, A, S, or D.
574 * @return boolean success.
576 function setStatus($status) {
578 if ($status != 'P' && $status != 'A'
579 && $status != 'S' && $status != 'D') {
580 $this->setError('ERROR: Invalid status value');
585 $res=db_query("UPDATE users
587 WHERE user_id='". $this->getID()."'");
590 $this->setError('ERROR - Could Not Update User Status: '.db_error());
594 $this->data_array['status']=$status;
595 if ($status == 'D') {
596 // Remove this user from all groups
597 $res = db_query(" DELETE FROM user_group WHERE user_id='".$this->getID()."' ");
599 $this->setError('ERROR - Could Not Propogate Deleted Status: '.db_error());
604 $hook_params = array ();
605 $hook_params['user'] = $this;
606 $hook_params['user_id'] = $this->getID();
607 $hook_params['status'] = $status;
608 plugin_hook ("user_setstatus", $hook_params);
612 //plugin webcalendar, create cal_user
613 plugin_hook('add_cal_user',$this->getID());
620 * isActive - whether this user is confirmed and active.
622 * Database field status of 'A' returns true.
623 * @return boolean is_active.
625 function isActive() {
626 if ($this->getStatus()=='A') {
634 * getUnixStatus - Status of activation of unix account.
636 * @return char (N)one, (A)ctive, (S)uspended or (D)eleted
638 function getUnixStatus() {
639 return $this->data_array['unix_status'];
643 * setUnixStatus - Sets status of activation of unix account.
645 * @param string The unix status.
651 * @return boolean success.
653 function setUnixStatus($status) {
658 SET unix_status='$status'
659 WHERE user_id='". $this->getID()."'
663 $this->setError('ERROR - Could Not Update User Unix Status: '.db_error());
667 if ($status == 'A') {
668 if (!$SYS->sysCheckCreateUser($this->getID())) {
669 $this->setError($SYS->getErrorMessage());
674 if ($SYS->sysCheckUser($this->getID())) {
675 if (!$SYS->sysRemoveUser($this->getID())) {
676 $this->setError($SYS->getErrorMessage());
683 $this->data_array['unix_status']=$status;
690 * getUnixName - the user's unix_name.
692 * @return string This user's unix/login name.
694 function getUnixName() {
695 return strtolower($this->data_array['user_name']);
699 * getUnixPasswd - get the user's password.
701 * @return string This user's unix crypted passwd.
703 function getUnixPasswd() {
704 return $this->data_array['unix_pw'];
708 * getUnixBox - the hostname of the unix box this user has an account on.
710 * @return string This user's shell login machine.
712 function getUnixBox() {
713 return $this->data_array['unix_box'];
717 * getMD5Passwd - the password.
719 * @return string This user's MD5-crypted passwd.
721 function getMD5Passwd() {
722 return $this->data_array['user_pw'];
726 * getConfirmHash - the confirm hash in the db.
728 * @return string This user's confirmation hash.
730 function getConfirmHash() {
731 return $this->data_array['confirm_hash'];
735 * getEmail - the user's email address.
737 * @return string This user's email address.
739 function getEmail() {
740 return $this->data_array['email'];
744 * getNewEmail - while changing an email address, it is stored here until confirmation.
746 * getNewEmail is a private operation for email change.
748 * @return string This user's new (not yet confirmed) email address.
751 function getNewEmail() {
752 return $this->data_array['email_new'];
756 * setEmail - set a new email address, which must be confirmed.
758 * @param string The email address.
759 * @return boolean success.
761 function setEmail($email) {
762 if (!$email || !validate_email($email)) {
763 $this->setError('ERROR: Invalid Email');
770 WHERE user_id='". $this->getID()."'
774 $this->setError('ERROR - Could Not Update User Email: '.db_error());
778 $hook_params = array ();
779 $hook_params['user'] = $this;
780 $hook_params['user_id'] = $this->getID();
781 $hook_params['user_email'] = $email;
782 plugin_hook ("user_setemail", $hook_params);
784 $this->data_array['email'] = $email;
791 * setNewEmailAndHash - setNewEmailAndHash is a private operation for email change.
793 * @param string The email address.
794 * @param string The email hash.
795 * @return boolean success.
797 function setNewEmailAndHash($email, $hash='') {
800 $hash = substr(md5(strval(time()) . strval(mt_rand())), 0, 16);
803 if (!$email || !validate_email($email)) {
804 $this->setError('ERROR - Invalid Email');
810 SET confirm_hash='$hash',
812 WHERE user_id='".$this->getID()."'
816 $this->setError('ERROR - Could Not Update User Email And Hash: '.db_error());
819 $this->data_array['email_new'] = $email;
820 $this->data_array['confirm_hash'] = $hash;
826 * getRealName - get the user's real name.
828 * @return string This user's real name.
830 function getRealName() {
831 return $this->getFirstName(). ' ' .$this->getLastName();
835 * getFirstName - get the user's first name.
837 * @return string This user's first name.
839 function getFirstName() {
840 return $this->data_array['firstname'];
844 * getLastName - get the user's last name.
846 * @return string This user's last name.
848 function getLastName() {
849 return $this->data_array['lastname'];
853 * getAddDate - this user's unix time when account was opened.
855 * @return int This user's unix time when account was opened.
857 function getAddDate() {
858 return $this->data_array['add_date'];
862 * getTimeZone - this user's timezone setting.
864 * @return string This user's timezone setting.
866 function getTimeZone() {
867 return $this->data_array['timezone'];
871 * getCountryCode - this user's ccode setting.
873 * @return string This user's ccode setting.
875 function getCountryCode() {
876 return $this->data_array['ccode'];
880 * getShell - this user's preferred shell.
882 * @return string This user's preferred shell.
884 function getShell() {
885 return $this->data_array['shell'];
889 * setShell - sets user's preferred shell.
891 * @param string The users preferred shell.
892 * @return boolean success.
894 function setShell($shell) {
896 $shells = file('/etc/shells');
897 $shells[count($shells)] = "/bin/cvssh";
898 $out_shells = array();
899 foreach ($shells as $s) {
900 if (substr($s, 0, 1) == '#') {
903 $out_shells[] = chop($s);
905 if (!in_array($shell, $out_shells)) {
906 $this->setError('ERROR: Invalid Shell');
914 WHERE user_id='". $this->getID()."'
918 $this->setError('ERROR - Could Not Update User Unix Shell: '.db_error());
922 // Now change LDAP attribute, but only if corresponding
923 // entry exists (i.e. if user have shell access)
924 if ($SYS->sysCheckUser($this->getID()))
926 if (!$SYS->sysUserSetAttribute($this->getID(),"loginShell",$shell)) {
927 $this->setError($SYS->getErrorMessage());
932 $this->data_array['shell']=$shell;
939 * getUnixUID() - Get the unix UID of the user
941 * @return int This user's UID.
943 function getUnixUID() {
944 return $this->data_array['unix_uid'];
948 * getUnixGID() - Get the unix GID of the user
950 * @return int This user's GID.
952 function getUnixGID() {
953 return $this->data_array['unix_gid'];
957 * getLanguage - this user's language_id from supported_languages table.
959 * @return int This user's language_id.
961 function getLanguage() {
962 return $this->data_array['language'];
966 * getJabberAddress - this user's optional jabber address.
968 * @return string This user's jabber address.
970 function getJabberAddress() {
971 return $this->data_array['jabber_address'];
975 * getJabberOnly - whether this person wants updates sent ONLY to jabber.
977 * @return boolean This user's jabber preference.
979 function getJabberOnly() {
980 return $this->data_array['jabber_only'];
984 * getAddress - get this user's address.
986 * @return text This user's address.
988 function getAddress() {
989 return $this->data_array['address'];
993 * getAddress2 - get this user's address2.
995 * @return text This user's address2.
997 function getAddress2() {
998 return $this->data_array['address2'];
1002 * getPhone - get this person's phone number.
1004 * @return text This user's phone number.
1006 function getPhone() {
1007 return $this->data_array['phone'];
1011 * getFax - get this person's fax number.
1013 * @return text This user's fax.
1016 return $this->data_array['fax'];
1020 * getTitle - get this person's title.
1022 * @return text This user's title.
1024 function getTitle() {
1025 return $this->data_array['title'];
1029 * getGroups - get an array of groups this user is a member of.
1031 * @return array Array of groups.
1033 function &getGroups() {
1034 $sql="SELECT group_id
1036 WHERE user_id='". $this->getID() ."'";
1037 $res=db_query($sql);
1038 $arr =& util_result_column_to_array($res,0);
1039 return group_get_objects($arr);
1043 * getAuthorizedKeys - the SSH authorized keys set by the user.
1045 * @return string This user's SSH authorized (public) keys.
1047 function getAuthorizedKeys() {
1048 return ereg_replace("###", "\n", $this->data_array['authorized_keys']);
1052 * setAuthorizedKeys - set the SSH authorized keys for the user.
1054 * @param string The users public keys.
1055 * @return boolean success.
1057 function setAuthorizedKeys($keys) {
1058 $keys = trim($keys);
1059 $keys = ereg_replace("\r\n", "\n", $keys); // Convert to Unix EOL
1060 $keys = ereg_replace("\n+", "\n", $keys); // Remove empty lines
1061 $keys = ereg_replace("\n", "###", $keys); // Convert EOL to marker
1065 SET authorized_keys='$keys'
1066 WHERE user_id='".$this->getID()."'
1070 $this->setError('ERROR - Could Not Update User SSH Keys');
1073 $this->data_array['authorized_keys'] = $keys;
1079 * setLoggedIn($val) - Really only used by session code.
1081 * @param boolean The session value.
1083 function setLoggedIn($val=true) {
1084 $this->is_logged_in=$val;
1086 //if this is the logged in user, see if they are a super user
1087 $sql="SELECT count(*) AS count FROM user_group WHERE user_id='". $this->getID() ."' AND group_id='1' AND admin_flags='A'";
1088 $result=db_query($sql);
1090 $this->is_super_user=false;
1093 $row_count = db_fetch_array($result);
1094 $this->is_super_user = ($row_count['count'] > 0);
1099 * isLoggedIn - only used by session code.
1101 * @return boolean is_logged_in.
1103 function isLoggedIn() {
1104 return $this->is_logged_in;
1108 * deletePreference - delete a preference for this user.
1110 * @param string The unique field name for this preference.
1111 * @return boolean success.
1113 function deletePreference($preference_name) {
1114 $preference_name=strtolower(trim($preference_name));
1115 unset($this->user_pref["$preference_name"]);
1116 $res= db_query("DELETE FROM user_preferences
1117 WHERE user_id='". $this->getID() ."'
1118 AND preference_name='$preference_name'");
1123 * setPreference - set a new preference for this user.
1125 * @param string The unique field name for this preference.
1126 * @param string The value you are setting this preference to.
1127 * @return boolean success.
1129 function setPreference($preference_name,$value) {
1130 $preference_name=strtolower(trim($preference_name));
1131 //delete pref if not value passed in
1132 unset($this->user_pref);
1133 if (!isset($value)) {
1134 $result=db_query("DELETE FROM user_preferences WHERE
1135 user_id='". $this->getID() ."' AND preference_name='$preference_name'");
1137 $result=db_query("UPDATE user_preferences SET preference_value='$value',set_date='". time() ."' ".
1138 "WHERE user_id='". $this->getID() ."' ".
1139 "AND preference_name='$preference_name'");
1140 if (db_affected_rows($result) < 1) {
1142 $result=db_query("INSERT INTO user_preferences (user_id,preference_name,preference_value,set_date) ".
1143 "VALUES ('". $this->getID() ."','$preference_name','$value','". time() ."')");
1150 * getPreference - get a specific preference.
1152 * @param string The unique field name for this preference.
1153 * @return the preference string or false on failure.
1155 function getPreference($preference_name) {
1156 $preference_name=strtolower(trim($preference_name));
1158 First check to see if we have already fetched the preferences
1160 if (isset($this->user_pref)) {
1161 //echo "\n\nPrefs were fetched already";
1162 if (isset($this->user_pref["$preference_name"])) {
1163 //we have fetched prefs - return part of array
1164 return $this->user_pref["$preference_name"];
1166 //we have fetched prefs, but this pref hasn't been set
1170 //we haven't returned prefs - go to the db
1171 $result=db_query("SELECT preference_name,preference_value FROM user_preferences ".
1172 "WHERE user_id='". $this->getID() ."'");
1173 if (db_numrows($result) < 1) {
1174 //echo "\n\nNo Prefs Found";
1178 //iterate and put the results into an array
1179 for ($i=0; $i<db_numrows($result); $i++) {
1180 $pref["".db_result($result,$i,'preference_name').""]=db_result($result,$i,'preference_value');
1182 $this->user_pref = $pref;
1184 if (array_key_exists($preference_name,$this->user_pref)) {
1185 //we have fetched prefs - return part of array
1186 return $this->user_pref["$preference_name"];
1188 //we have fetched prefs, but this pref hasn't been set
1196 * setPasswd - Changes user's password.
1198 * @param string The plaintext password.
1199 * @return boolean success.
1201 function setPasswd($passwd) {
1203 if (!account_pwvalid($passwd)) {
1204 $this->setError('Error: '.$GLOBALS['register_error']);
1209 $unix_pw = account_genunixpw($passwd);
1213 SET user_pw='" . md5($passwd) . "',
1215 WHERE user_id='".$this->getID()."'
1218 if (!$res || db_affected_rows($res) < 1) {
1219 $this->setError('ERROR - Could Not Change User Password: '.db_error());
1223 // Now change LDAP password, but only if corresponding
1224 // entry exists (i.e. if user have shell access)
1225 if ($SYS->sysCheckUser($this->getID())) {
1226 if (!$SYS->sysUserSetAttribute($this->getID(),"userPassword",'{crypt}'.$unix_pw)) {
1227 $this->setError($SYS->getErrorMessage());
1233 $hook_params = array ();
1234 $hook_params['user'] = $this;
1235 $hook_params['user_id'] = $this->getID();
1236 $hook_params['user_password'] = $passwd;
1237 plugin_hook ("user_setpasswd", $hook_params);
1243 * usesRatings - whether user participates in rating system.
1245 * @return boolean success.
1247 function usesRatings() {
1248 return !$this->data_array['block_ratings'];
1252 * getPlugins - get a list of all available user plugins
1254 * @return array array containing plugin_id => plugin_name
1256 function getPlugins() {
1257 if (!isset($this->plugins_data)) {
1258 $this->plugins_data = array () ;
1259 $sql="SELECT user_plugin.plugin_id, plugins.plugin_name
1260 FROM user_plugin, plugins
1261 WHERE user_plugin.user_id=".$this->getID()."
1262 AND user_plugin.plugin_id = plugins.plugin_id" ;
1263 $res=db_query($sql);
1264 $rows = db_numrows($res);
1266 for ($i=0; $i<$rows; $i++) {
1267 $plugin_id = db_result($res,$i,'plugin_id');
1268 $this->plugins_data[$plugin_id] = db_result($res,$i,'plugin_name');
1271 return $this->plugins_data ;
1275 * usesPlugin - returns true if the user uses a particular plugin
1277 * @param string name of the plugin
1278 * @return boolean whether plugin is being used or not
1280 function usesPlugin($pluginname) {
1281 $plugins_data = $this->getPlugins() ;
1282 foreach ($plugins_data as $p_name) {
1283 if ($p_name == $pluginname) {
1291 * setPluginUse - enables/disables plugins for the user
1293 * @param string name of the plugin
1294 * @param boolean the new state
1295 * @return string database result
1297 function setPluginUse($pluginname, $val=true) {
1298 if ($val == $this->usesPlugin($pluginname)) {
1299 // State is already good, returning
1302 $sql="SELECT plugin_id
1304 WHERE plugin_name = '" . $pluginname . "'" ;
1305 $res=db_query($sql);
1306 $rows = db_numrows($res);
1308 // Error: no plugin by that name
1311 $plugin_id = db_result($res,0,'plugin_id');
1313 unset ($this->plugins_data) ;
1315 $sql="INSERT INTO user_plugin (user_id, plugin_id)
1316 VALUES (". $this->getID() . ", ". $plugin_id .")" ;
1317 $res=db_query($sql);
1320 $sql="DELETE FROM user_plugin
1321 WHERE user_id = ". $this->getID() . "
1322 AND plugin_id = ". $plugin_id ;
1323 $res=db_query($sql);
1329 * getMailingsPrefs - Get activity status for one of the site mailings.
1331 * @param string The id of mailing ('mail_va' for community mailings, 'mail_siteupdates' for site mailings)
1332 * @return boolean success.
1334 function getMailingsPrefs($mailing_id) {
1335 if ($mailing_id=='va') {
1336 return $this->data_array['mail_va'];
1337 } else if ($mailing_id=='site') {
1338 return $this->data_array['mail_siteupdates'];
1345 * unsubscribeFromMailings - Disable email notifications for user.
1347 * @param boolean If false, disable general site mailings, else - all.
1348 * @return boolean success.
1350 function unsubscribeFromMailings($all=false) {
1351 $res1 = $res2 = $res3 = true;
1354 SET mail_siteupdates=0,
1356 WHERE user_id='".$this->getID()."'
1360 DELETE FROM forum_monitored_forums
1361 WHERE user_id='".$this->getID()."'
1364 DELETE FROM filemodule_monitor
1365 WHERE user_id='".$this->getID()."'
1369 return $res1 && $res2 && $res3;
1373 * getThemeID - get the theme_id for this user.
1375 * @return int The theme_id.
1377 function getThemeID() {
1378 return $this->data_array['theme_id'];
1382 * getThemeID - get the theme_id for this user from the theme_prefs table.
1384 * @return int The theme_id.
1386 function setUpTheme() {
1388 // An optimization in session_getdata lets us pre-fetch this in most cases.....
1390 if (!$this->data_array['dirname']) {
1391 $res=db_query("SELECT dirname FROM themes WHERE theme_id='".$this->getThemeID()."'");
1392 $this->theme=db_result($res,0,'dirname');
1394 $this->theme=$this->data_array['dirname'];
1396 if (is_file($GLOBALS['sys_themeroot'].$this->theme.'/Theme.class.php')) {
1397 $GLOBALS['sys_theme']=$this->theme;
1399 $this->theme=$GLOBALS['sys_theme'];
1401 return $this->theme;
1410 EVERYTHING BELOW HERE IS DEPRECATED
1413 DO NOT USE FOR ANY NEW CODE
1422 * user_ismember() - DEPRECATED; DO NOT USE!
1424 * @param int The Group ID
1425 * @param int The Type
1429 function user_ismember($group_id,$type=0) {
1430 if (!session_loggedin()) {
1434 $project =& group_get_object($group_id);
1436 if (!$project || !is_object($project)) {
1440 $perm =& $project->getPermission( session_get_user() );
1441 if (!$perm || !is_object($perm) || !$perm->isMember()) {
1445 $type=strtoupper($type);
1450 return $perm->isPMAdmin();
1455 return $perm->isForumAdmin();
1459 //just in this group
1460 return $perm->isMember();
1464 //admin for this group
1465 return $perm->isAdmin();
1470 return $perm->isDocEditor();
1482 * user_getname() - DEPRECATED; DO NOT USE!
1484 * @param int The User ID
1488 function user_getname($user_id = false) {
1489 // use current user if one is not passed in
1491 if (session_loggedin()) {
1492 $user=&user_get_object(user_getid());
1494 return $user->getUnixName();
1496 return 'Error getting user';
1499 return 'No User Id';
1502 $user=&user_get_object($user_id);
1504 return $user->getUnixName();
1506 return 'Invalid User';
1513 // c-file-style: "bsd"