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('www/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 . time()),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)) {
339 setup_gettext_from_lang_id($language_id);
340 $this->sendRegistrationEmail();
341 setup_gettext_from_browser() ;
350 * sendRegistrationEmail() - Send email for registration verification
352 * @return true or false
354 function sendRegistrationEmail() {
355 $message=stripcslashes(sprintf(_('Thank you for registering on the %4$s web site. You have
356 account with username %1$s created for you. In order
357 to complete your registration, visit the following url:
359 <http://%2$s/account/verify.php?confirm_hash=_%3$s>
361 (If you don\'t see any URL above, it is likely due to a bug in your mail client.
362 Use one below, but make sure it is entered as the single line.)
364 http://%2$s/account/verify.php?confirm_hash=_%3$s
369 '), $this->getUnixName(), $GLOBALS['sys_default_domain'], $this->getConfirmHash(), $GLOBALS['sys_name']));
372 sprintf(_('%1$s Account Registration'), $GLOBALS['sys_name']),
378 * delete() - remove the User from all his groups.
380 * Remove the User from all his groups and set his status to D.
382 * @param boolean Confirmation of deletion.
383 * @return true or false
385 function delete($sure) {
389 $groups = &$this->getGroups();
390 if (is_array($groups)) {
391 foreach ($groups as $group) {
392 $group->removeUser($this->getID());
397 $res = db_query("DELETE FROM artifact_monitor WHERE user_id='".$this->getID()."' ");
399 $this->setError('ERROR - Could Not Delete From artifact_monitor: '.db_error());
403 $res = db_query("DELETE FROM artifact_type_monitor WHERE user_id='".$this->getID()."' ");
405 $this->setError('ERROR - Could Not Delete From artifact_type_monitor: '.db_error());
409 $res = db_query("DELETE FROM forum_monitored_forums WHERE user_id='".$this->getID()."' ");
411 $this->setError('ERROR - Could Not Delete From forum_monitored_forums: '.db_error());
415 $res = db_query("DELETE FROM filemodule_monitor WHERE user_id='".$this->getID()."' ");
417 $this->setError('ERROR - Could Not Delete From filemodule_monitor: '.db_error());
421 $this->setStatus('D');
428 * update() - update *common* properties of GFUser object.
430 * Use specific setter to change other properties.
432 * @param string The users first name.
433 * @param string The users last name.
434 * @param int The ID of the users language preference.
435 * @param string The useres timezone preference.
436 * @param string The users preference for receiving site updates by email.
437 * @param string The users preference for receiving community updates by email.
438 * @param string The users preference for being participating in "peer ratings".
439 * @param string The users Jabber account address.
440 * @param int The users Jabber preference.
441 * @param int The users theme_id preference.
442 * @param string The users address.
443 * @param string The users address2.
444 * @param string The users phone.
445 * @param string The users fax.
446 * @param string The users title.
447 * @param string The users ccode.
449 function update($firstname,$lastname,$language_id,$timezone,$mail_site,$mail_va,$use_ratings,
450 $jabber_address,$jabber_only,$theme_id,$address,$address2,$phone,$fax,$title,$ccode) {
451 $mail_site = $mail_site ? 1 : 0;
452 $mail_va = $mail_va ? 1 : 0;
453 $block_ratings = $use_ratings ? 0 : 1;
455 if ($jabber_address && !validate_email($jabber_address)) {
456 $this->setError(_('Invalid Jabber Address'));
470 realname='".htmlspecialchars($firstname . ' ' .$lastname)."',
471 firstname='".htmlspecialchars($firstname)."',
472 lastname='".htmlspecialchars($lastname)."',
473 language='$language_id',
474 timezone='$timezone',
475 mail_siteupdates=$mail_site,
477 block_ratings='$block_ratings',
478 jabber_address='$jabber_address',
479 jabber_only='$jabber_only',
480 address='". htmlspecialchars($address) ."',
481 address2='". htmlspecialchars($address2) ."',
482 phone='". htmlspecialchars($phone) ."',
483 fax='". htmlspecialchars($fax) ."',
484 title='". htmlspecialchars($title) ."',
487 WHERE user_id='".$this->getID()."'
491 $this->setError('ERROR - Could Not Update User Object: '.db_error());
495 // If there's a transaction from using to not
496 // using ratings, remove all rating made by the
497 // user (ratings by others should not be removed,
498 // as it opens possibility to abuse rate system)
499 if (!$use_ratings && $this->usesRatings()) {
500 vote_remove_all_ratings_by($this->getID());
502 if (!$this->fetchData($this->getID())) {
512 * fetchData - May need to refresh database fields.
514 * If an update occurred and you need to access the updated info.
516 * @return boolean success;
518 function fetchData($user_id) {
519 $res=db_query("SELECT * FROM users WHERE user_id='$user_id'");
520 if (!$res || db_numrows($res) < 1) {
521 $this->setError('GFUser::fetchData()::'.db_error());
524 $this->data_array =& db_fetch_array($res);
529 * getID - Simply return the user_id for this object.
531 * @return int This user's user_id number.
534 return $this->data_array['user_id'];
538 * getStatus - get the status of this user.
540 * Statuses include (A)ctive, (P)ending, (S)uspended ,(D)eleted.
542 * @return char This user's status flag.
544 function getStatus() {
545 return $this->data_array['status'];
549 * setStatus - set this user's status.
551 * @param string Status - P, A, S, or D.
552 * @return boolean success.
554 function setStatus($status) {
556 if ($status != 'P' && $status != 'A'
557 && $status != 'S' && $status != 'D') {
558 $this->setError('ERROR: Invalid status value');
563 $res=db_query("UPDATE users
565 WHERE user_id='". $this->getID()."'");
568 $this->setError('ERROR - Could Not Update User Status: '.db_error());
572 $this->data_array['status']=$status;
573 if ($status == 'D') {
574 // Remove this user from all groups
575 $res = db_query(" DELETE FROM user_group WHERE user_id='".$this->getID()."' ");
577 $this->setError('ERROR - Could Not Propogate Deleted Status: '.db_error());
584 //plugin webcalendar, create cal_user
585 plugin_hook('add_cal_user',$this->getID());
592 * isActive - whether this user is confirmed and active.
594 * Database field status of 'A' returns true.
595 * @return boolean is_active.
597 function isActive() {
598 if ($this->getStatus()=='A') {
606 * getUnixStatus - Status of activation of unix account.
608 * @return char (N)one, (A)ctive, (S)uspended or (D)eleted
610 function getUnixStatus() {
611 return $this->data_array['unix_status'];
615 * setUnixStatus - Sets status of activation of unix account.
617 * @param string The unix status.
623 * @return boolean success.
625 function setUnixStatus($status) {
630 SET unix_status='$status'
631 WHERE user_id='". $this->getID()."'
635 $this->setError('ERROR - Could Not Update User Unix Status: '.db_error());
639 if ($status == 'A') {
640 if (!$SYS->sysCheckCreateUser($this->getID())) {
641 $this->setError($SYS->getErrorMessage());
646 if ($SYS->sysCheckUser($this->getID())) {
647 if (!$SYS->sysRemoveUser($this->getID())) {
648 $this->setError($SYS->getErrorMessage());
655 $this->data_array['unix_status']=$status;
662 * getUnixName - the user's unix_name.
664 * @return string This user's unix/login name.
666 function getUnixName() {
667 return strtolower($this->data_array['user_name']);
671 * getUnixPasswd - get the user's password.
673 * @return string This user's unix crypted passwd.
675 function getUnixPasswd() {
676 return $this->data_array['unix_pw'];
680 * getUnixBox - the hostname of the unix box this user has an account on.
682 * @return string This user's shell login machine.
684 function getUnixBox() {
685 return $this->data_array['unix_box'];
689 * getMD5Passwd - the password.
691 * @return string This user's MD5-crypted passwd.
693 function getMD5Passwd() {
694 return $this->data_array['user_pw'];
698 * getConfirmHash - the confirm hash in the db.
700 * @return string This user's confirmation hash.
702 function getConfirmHash() {
703 return $this->data_array['confirm_hash'];
707 * getEmail - the user's email address.
709 * @return string This user's email address.
711 function getEmail() {
712 return $this->data_array['email'];
716 * getNewEmail - while changing an email address, it is stored here until confirmation.
718 * getNewEmail is a private operation for email change.
720 * @return string This user's new (not yet confirmed) email address.
723 function getNewEmail() {
724 return $this->data_array['email_new'];
728 * setEmail - set a new email address, which must be confirmed.
730 * @param string The email address.
731 * @return boolean success.
733 function setEmail($email) {
734 if (!$email || !validate_email($email)) {
735 $this->setError('ERROR: Invalid Email');
741 WHERE user_id='". $this->getID()."'
745 $this->setError('ERROR - Could Not Update User Email: '.db_error());
748 $this->data_array['email'] = $email;
754 * setNewEmailAndHash - setNewEmailAndHash is a private operation for email change.
756 * @param string The email address.
757 * @param string The email hash.
758 * @return boolean success.
760 function setNewEmailAndHash($email, $hash='') {
763 $hash = substr(md5(strval(time()) . strval(mt_rand())), 0, 16);
766 if (!$email || !validate_email($email)) {
767 $this->setError('ERROR - Invalid Email');
773 SET confirm_hash='$hash',
775 WHERE user_id='".$this->getID()."'
779 $this->setError('ERROR - Could Not Update User Email And Hash: '.db_error());
782 $this->data_array['email_new'] = $email;
783 $this->data_array['confirm_hash'] = $hash;
789 * getRealName - get the user's real name.
791 * @return string This user's real name.
793 function getRealName() {
794 return $this->getFirstName(). ' ' .$this->getLastName();
798 * getFirstName - get the user's first name.
800 * @return string This user's first name.
802 function getFirstName() {
803 return $this->data_array['firstname'];
807 * getLastName - get the user's last name.
809 * @return string This user's last name.
811 function getLastName() {
812 return $this->data_array['lastname'];
816 * getAddDate - this user's unix time when account was opened.
818 * @return int This user's unix time when account was opened.
820 function getAddDate() {
821 return $this->data_array['add_date'];
825 * getTimeZone - this user's timezone setting.
827 * @return string This user's timezone setting.
829 function getTimeZone() {
830 return $this->data_array['timezone'];
834 * getCountryCode - this user's ccode setting.
836 * @return string This user's ccode setting.
838 function getCountryCode() {
839 return $this->data_array['ccode'];
843 * getShell - this user's preferred shell.
845 * @return string This user's preferred shell.
847 function getShell() {
848 return $this->data_array['shell'];
852 * setShell - sets user's preferred shell.
854 * @param string The users preferred shell.
855 * @return boolean success.
857 function setShell($shell) {
859 $shells = file('/etc/shells');
860 $shells[count($shells)] = "/bin/cvssh";
861 $out_shells = array();
862 foreach ($shells as $s) {
863 if (substr($s, 0, 1) == '#') {
866 $out_shells[] = chop($s);
868 if (!in_array($shell, $out_shells)) {
869 $this->setError('ERROR: Invalid Shell');
877 WHERE user_id='". $this->getID()."'
881 $this->setError('ERROR - Could Not Update User Unix Shell: '.db_error());
885 // Now change LDAP attribute, but only if corresponding
886 // entry exists (i.e. if user have shell access)
887 if ($SYS->sysCheckUser($this->getID()))
889 if (!$SYS->sysUserSetAttribute($this->getID(),"loginShell",$shell)) {
890 $this->setError($SYS->getErrorMessage());
895 $this->data_array['shell']=$shell;
902 * getUnixUID() - Get the unix UID of the user
904 * @return int This user's UID.
906 function getUnixUID() {
907 return $this->data_array['unix_uid'];
911 * getUnixGID() - Get the unix GID of the user
913 * @return int This user's GID.
915 function getUnixGID() {
916 return $this->data_array['unix_gid'];
920 * getLanguage - this user's language_id from supported_languages table.
922 * @return int This user's language_id.
924 function getLanguage() {
925 return $this->data_array['language'];
929 * getJabberAddress - this user's optional jabber address.
931 * @return string This user's jabber address.
933 function getJabberAddress() {
934 return $this->data_array['jabber_address'];
938 * getJabberOnly - whether this person wants updates sent ONLY to jabber.
940 * @return boolean This user's jabber preference.
942 function getJabberOnly() {
943 return $this->data_array['jabber_only'];
947 * getAddress - get this user's address.
949 * @return text This user's address.
951 function getAddress() {
952 return $this->data_array['address'];
956 * getAddress2 - get this user's address2.
958 * @return text This user's address2.
960 function getAddress2() {
961 return $this->data_array['address2'];
965 * getPhone - get this person's phone number.
967 * @return text This user's phone number.
969 function getPhone() {
970 return $this->data_array['phone'];
974 * getFax - get this person's fax number.
976 * @return text This user's fax.
979 return $this->data_array['fax'];
983 * getTitle - get this person's title.
985 * @return text This user's title.
987 function getTitle() {
988 return $this->data_array['title'];
992 * getGroups - get an array of groups this user is a member of.
994 * @return array Array of groups.
996 function &getGroups() {
997 $sql="SELECT group_id
999 WHERE user_id='". $this->getID() ."'";
1000 $res=db_query($sql);
1001 $arr =& util_result_column_to_array($res,0);
1002 return group_get_objects($arr);
1006 * getAuthorizedKeys - the SSH authorized keys set by the user.
1008 * @return string This user's SSH authorized (public) keys.
1010 function getAuthorizedKeys() {
1011 return ereg_replace("###", "\n", $this->data_array['authorized_keys']);
1015 * setAuthorizedKeys - set the SSH authorized keys for the user.
1017 * @param string The users public keys.
1018 * @return boolean success.
1020 function setAuthorizedKeys($keys) {
1021 $keys = trim($keys);
1022 $keys = ereg_replace("\r\n", "\n", $keys); // Convert to Unix EOL
1023 $keys = ereg_replace("\n+", "\n", $keys); // Remove empty lines
1024 $keys = ereg_replace("\n", "###", $keys); // Convert EOL to marker
1028 SET authorized_keys='$keys'
1029 WHERE user_id='".$this->getID()."'
1033 $this->setError('ERROR - Could Not Update User SSH Keys');
1036 $this->data_array['authorized_keys'] = $keys;
1042 * setLoggedIn($val) - Really only used by session code.
1044 * @param boolean The session value.
1046 function setLoggedIn($val=true) {
1047 $this->is_logged_in=$val;
1049 //if this is the logged in user, see if they are a super user
1050 $sql="SELECT count(*) AS count FROM user_group WHERE user_id='". $this->getID() ."' AND group_id='1' AND admin_flags='A'";
1051 $result=db_query($sql);
1053 $this->is_super_user=false;
1056 $row_count = db_fetch_array($result);
1057 $this->is_super_user = ($row_count['count'] > 0);
1062 * isLoggedIn - only used by session code.
1064 * @return boolean is_logged_in.
1066 function isLoggedIn() {
1067 return $this->is_logged_in;
1071 * deletePreference - delete a preference for this user.
1073 * @param string The unique field name for this preference.
1074 * @return boolean success.
1076 function deletePreference($preference_name) {
1077 $preference_name=strtolower(trim($preference_name));
1078 unset($this->user_pref["$preference_name"]);
1079 $res= db_query("DELETE FROM user_preferences
1080 WHERE user_id='". $this->getID() ."'
1081 AND preference_name='$preference_name'");
1086 * setPreference - set a new preference for this user.
1088 * @param string The unique field name for this preference.
1089 * @param string The value you are setting this preference to.
1090 * @return boolean success.
1092 function setPreference($preference_name,$value) {
1093 $preference_name=strtolower(trim($preference_name));
1094 //delete pref if not value passed in
1095 unset($this->user_pref);
1096 if (!isset($value)) {
1097 $result=db_query("DELETE FROM user_preferences WHERE
1098 user_id='". $this->getID() ."' AND preference_name='$preference_name'");
1100 $result=db_query("UPDATE user_preferences SET preference_value='$value',set_date='". time() ."' ".
1101 "WHERE user_id='". $this->getID() ."' ".
1102 "AND preference_name='$preference_name'");
1103 if (db_affected_rows($result) < 1) {
1105 $result=db_query("INSERT INTO user_preferences (user_id,preference_name,preference_value,set_date) ".
1106 "VALUES ('". $this->getID() ."','$preference_name','$value','". time() ."')");
1113 * getPreference - get a specific preference.
1115 * @param string The unique field name for this preference.
1116 * @return the preference string or false on failure.
1118 function getPreference($preference_name) {
1119 $preference_name=strtolower(trim($preference_name));
1121 First check to see if we have already fetched the preferences
1123 if ($this->user_pref) {
1124 //echo "\n\nPrefs were fetched already";
1125 if ($this->user_pref["$preference_name"]) {
1126 //we have fetched prefs - return part of array
1127 return $this->user_pref["$preference_name"];
1129 //we have fetched prefs, but this pref hasn't been set
1133 //we haven't returned prefs - go to the db
1134 $result=db_query("SELECT preference_name,preference_value FROM user_preferences ".
1135 "WHERE user_id='". $this->getID() ."'");
1136 if (db_numrows($result) < 1) {
1137 //echo "\n\nNo Prefs Found";
1141 //iterate and put the results into an array
1142 for ($i=0; $i<db_numrows($result); $i++) {
1143 $pref["".db_result($result,$i,'preference_name').""]=db_result($result,$i,'preference_value');
1145 $this->user_pref = $pref;
1147 if (array_key_exists($preference_name,$this->user_pref)) {
1148 //we have fetched prefs - return part of array
1149 return $this->user_pref["$preference_name"];
1151 //we have fetched prefs, but this pref hasn't been set
1159 * setPasswd - Changes user's password.
1161 * @param string The plaintext password.
1162 * @return boolean success.
1164 function setPasswd($passwd) {
1166 if (!account_pwvalid($passwd)) {
1167 $this->setError('Error: '.$GLOBALS['register_error']);
1172 $unix_pw = account_genunixpw($passwd);
1176 SET user_pw='" . md5($passwd) . "',
1178 WHERE user_id='".$this->getID()."'
1181 if (!$res || db_affected_rows($res) < 1) {
1182 $this->setError('ERROR - Could Not Change User Password: '.db_error());
1186 // Now change LDAP password, but only if corresponding
1187 // entry exists (i.e. if user have shell access)
1188 if ($SYS->sysCheckUser($this->getID())) {
1189 if (!$SYS->sysUserSetAttribute($this->getID(),"userPassword",'{crypt}'.$unix_pw)) {
1190 $this->setError($SYS->getErrorMessage());
1201 * usesRatings - whether user participates in rating system.
1203 * @return boolean success.
1205 function usesRatings() {
1206 return !$this->data_array['block_ratings'];
1210 * getPlugins - get a list of all available user plugins
1212 * @return array array containing plugin_id => plugin_name
1214 function getPlugins() {
1215 if (!isset($this->plugins_data)) {
1216 $this->plugins_data = array () ;
1217 $sql="SELECT user_plugin.plugin_id, plugins.plugin_name
1218 FROM user_plugin, plugins
1219 WHERE user_plugin.user_id=".$this->getID()."
1220 AND user_plugin.plugin_id = plugins.plugin_id" ;
1221 $res=db_query($sql);
1222 $rows = db_numrows($res);
1224 for ($i=0; $i<$rows; $i++) {
1225 $plugin_id = db_result($res,$i,'plugin_id');
1226 $this->plugins_data[$plugin_id] = db_result($res,$i,'plugin_name');
1229 return $this->plugins_data ;
1233 * usesPlugin - returns true if the user uses a particular plugin
1235 * @param string name of the plugin
1236 * @return boolean whether plugin is being used or not
1238 function usesPlugin($pluginname) {
1239 $plugins_data = $this->getPlugins() ;
1240 foreach ($plugins_data as $p_name) {
1241 if ($p_name == $pluginname) {
1249 * setPluginUse - enables/disables plugins for the user
1251 * @param string name of the plugin
1252 * @param boolean the new state
1253 * @return string database result
1255 function setPluginUse($pluginname, $val=true) {
1256 if ($val == $this->usesPlugin($pluginname)) {
1257 // State is already good, returning
1260 $sql="SELECT plugin_id
1262 WHERE plugin_name = '" . $pluginname . "'" ;
1263 $res=db_query($sql);
1264 $rows = db_numrows($res);
1266 // Error: no plugin by that name
1269 $plugin_id = db_result($res,0,'plugin_id');
1271 unset ($this->plugins_data) ;
1273 $sql="INSERT INTO user_plugin (user_id, plugin_id)
1274 VALUES (". $this->getID() . ", ". $plugin_id .")" ;
1275 $res=db_query($sql);
1278 $sql="DELETE FROM user_plugin
1279 WHERE user_id = ". $this->getID() . "
1280 AND plugin_id = ". $plugin_id ;
1281 $res=db_query($sql);
1287 * getMailingsPrefs - Get activity status for one of the site mailings.
1289 * @param string The id of mailing ('mail_va' for community mailings, 'mail_siteupdates' for site mailings)
1290 * @return boolean success.
1292 function getMailingsPrefs($mailing_id) {
1293 if ($mailing_id=='va') {
1294 return $this->data_array['mail_va'];
1295 } else if ($mailing_id=='site') {
1296 return $this->data_array['mail_siteupdates'];
1303 * unsubscribeFromMailings - Disable email notifications for user.
1305 * @param boolean If false, disable general site mailings, else - all.
1306 * @return boolean success.
1308 function unsubscribeFromMailings($all=false) {
1309 $res1 = $res2 = $res3 = true;
1312 SET mail_siteupdates=0,
1314 WHERE user_id='".$this->getID()."'
1318 DELETE FROM forum_monitored_forums
1319 WHERE user_id='".$this->getID()."'
1322 DELETE FROM filemodule_monitor
1323 WHERE user_id='".$this->getID()."'
1327 return $res1 && $res2 && $res3;
1331 * getThemeID - get the theme_id for this user.
1333 * @return int The theme_id.
1335 function getThemeID() {
1336 return $this->data_array['theme_id'];
1340 * getThemeID - get the theme_id for this user from the theme_prefs table.
1342 * @return int The theme_id.
1344 function setUpTheme() {
1346 // An optimization in session_getdata lets us pre-fetch this in most cases.....
1348 if (!$this->data_array['dirname']) {
1349 $res=db_query("SELECT dirname FROM themes WHERE theme_id='".$this->getThemeID()."'");
1350 $this->theme=db_result($res,0,'dirname');
1352 $this->theme=$this->data_array['dirname'];
1354 if (is_file($GLOBALS['sys_themeroot'].$this->theme.'/Theme.class.php')) {
1355 $GLOBALS['sys_theme']=$this->theme;
1357 $this->theme=$GLOBALS['sys_theme'];
1359 return $this->theme;
1368 EVERYTHING BELOW HERE IS DEPRECATED
1371 DO NOT USE FOR ANY NEW CODE
1380 * user_ismember() - DEPRECATED; DO NOT USE!
1382 * @param int The Group ID
1383 * @param int The Type
1387 function user_ismember($group_id,$type=0) {
1388 if (!session_loggedin()) {
1392 $project =& group_get_object($group_id);
1394 if (!$project || !is_object($project)) {
1398 $perm =& $project->getPermission( session_get_user() );
1399 if (!$perm || !is_object($perm) || !$perm->isMember()) {
1403 $type=strtoupper($type);
1408 return $perm->isPMAdmin();
1413 return $perm->isForumAdmin();
1417 //just in this group
1418 return $perm->isMember();
1422 //admin for this group
1423 return $perm->isAdmin();
1428 return $perm->isDocEditor();
1440 * user_getname() - DEPRECATED; DO NOT USE!
1442 * @param int The User ID
1446 function user_getname($user_id = false) {
1447 // use current user if one is not passed in
1449 if (session_loggedin()) {
1450 $user=&user_get_object(user_getid());
1452 return $user->getUnixName();
1454 return 'Error getting user';
1457 return 'No User Id';
1460 $user=&user_get_object($user_id);
1462 return $user->getUnixName();
1464 return 'Invalid User';
1471 // c-file-style: "bsd"