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 . 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)) {
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:
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.)
370 $this->getUnixName(),
371 util_make_url ('/account/verify.php?confirm_hash=_'.$this->getConfirmHash()),
372 $GLOBALS['sys_name']));
375 sprintf(_('%1$s Account Registration'), $GLOBALS['sys_name']),
381 * delete() - remove the User from all his groups.
383 * Remove the User from all his groups and set his status to D.
385 * @param boolean Confirmation of deletion.
386 * @return true or false
388 function delete($sure) {
392 $groups = &$this->getGroups();
393 if (is_array($groups)) {
394 foreach ($groups as $group) {
395 $group->removeUser($this->getID());
400 $res = db_query("DELETE FROM artifact_monitor WHERE user_id='".$this->getID()."' ");
402 $this->setError('ERROR - Could Not Delete From artifact_monitor: '.db_error());
406 $res = db_query("DELETE FROM artifact_type_monitor WHERE user_id='".$this->getID()."' ");
408 $this->setError('ERROR - Could Not Delete From artifact_type_monitor: '.db_error());
412 $res = db_query("DELETE FROM forum_monitored_forums WHERE user_id='".$this->getID()."' ");
414 $this->setError('ERROR - Could Not Delete From forum_monitored_forums: '.db_error());
418 $res = db_query("DELETE FROM filemodule_monitor WHERE user_id='".$this->getID()."' ");
420 $this->setError('ERROR - Could Not Delete From filemodule_monitor: '.db_error());
424 $this->setStatus('D');
431 * update() - update *common* properties of GFUser object.
433 * Use specific setter to change other properties.
435 * @param string The users first name.
436 * @param string The users last name.
437 * @param int The ID of the users language preference.
438 * @param string The useres timezone preference.
439 * @param string The users preference for receiving site updates by email.
440 * @param string The users preference for receiving community updates by email.
441 * @param string The users preference for being participating in "peer ratings".
442 * @param string The users Jabber account address.
443 * @param int The users Jabber preference.
444 * @param int The users theme_id preference.
445 * @param string The users address.
446 * @param string The users address2.
447 * @param string The users phone.
448 * @param string The users fax.
449 * @param string The users title.
450 * @param string The users ccode.
452 function update($firstname,$lastname,$language_id,$timezone,$mail_site,$mail_va,$use_ratings,
453 $jabber_address,$jabber_only,$theme_id,$address,$address2,$phone,$fax,$title,$ccode) {
454 $mail_site = $mail_site ? 1 : 0;
455 $mail_va = $mail_va ? 1 : 0;
456 $block_ratings = $use_ratings ? 0 : 1;
458 if ($jabber_address && !validate_email($jabber_address)) {
459 $this->setError(_('Invalid Jabber Address'));
473 realname='".htmlspecialchars($firstname . ' ' .$lastname)."',
474 firstname='".htmlspecialchars($firstname)."',
475 lastname='".htmlspecialchars($lastname)."',
476 language='$language_id',
477 timezone='$timezone',
478 mail_siteupdates=$mail_site,
480 block_ratings='$block_ratings',
481 jabber_address='$jabber_address',
482 jabber_only='$jabber_only',
483 address='". htmlspecialchars($address) ."',
484 address2='". htmlspecialchars($address2) ."',
485 phone='". htmlspecialchars($phone) ."',
486 fax='". htmlspecialchars($fax) ."',
487 title='". htmlspecialchars($title) ."',
490 WHERE user_id='".$this->getID()."'
494 $this->setError('ERROR - Could Not Update User Object: '.db_error());
498 // If there's a transaction from using to not
499 // using ratings, remove all rating made by the
500 // user (ratings by others should not be removed,
501 // as it opens possibility to abuse rate system)
502 if (!$use_ratings && $this->usesRatings()) {
503 vote_remove_all_ratings_by($this->getID());
505 if (!$this->fetchData($this->getID())) {
515 * fetchData - May need to refresh database fields.
517 * If an update occurred and you need to access the updated info.
519 * @return boolean success;
521 function fetchData($user_id) {
522 $res=db_query("SELECT * FROM users WHERE user_id='$user_id'");
523 if (!$res || db_numrows($res) < 1) {
524 $this->setError('GFUser::fetchData()::'.db_error());
527 $this->data_array =& db_fetch_array($res);
532 * getID - Simply return the user_id for this object.
534 * @return int This user's user_id number.
537 return $this->data_array['user_id'];
541 * getStatus - get the status of this user.
543 * Statuses include (A)ctive, (P)ending, (S)uspended ,(D)eleted.
545 * @return char This user's status flag.
547 function getStatus() {
548 return $this->data_array['status'];
552 * setStatus - set this user's status.
554 * @param string Status - P, A, S, or D.
555 * @return boolean success.
557 function setStatus($status) {
559 if ($status != 'P' && $status != 'A'
560 && $status != 'S' && $status != 'D') {
561 $this->setError('ERROR: Invalid status value');
566 $res=db_query("UPDATE users
568 WHERE user_id='". $this->getID()."'");
571 $this->setError('ERROR - Could Not Update User Status: '.db_error());
575 $this->data_array['status']=$status;
576 if ($status == 'D') {
577 // Remove this user from all groups
578 $res = db_query(" DELETE FROM user_group WHERE user_id='".$this->getID()."' ");
580 $this->setError('ERROR - Could Not Propogate Deleted Status: '.db_error());
587 //plugin webcalendar, create cal_user
588 plugin_hook('add_cal_user',$this->getID());
595 * isActive - whether this user is confirmed and active.
597 * Database field status of 'A' returns true.
598 * @return boolean is_active.
600 function isActive() {
601 if ($this->getStatus()=='A') {
609 * getUnixStatus - Status of activation of unix account.
611 * @return char (N)one, (A)ctive, (S)uspended or (D)eleted
613 function getUnixStatus() {
614 return $this->data_array['unix_status'];
618 * setUnixStatus - Sets status of activation of unix account.
620 * @param string The unix status.
626 * @return boolean success.
628 function setUnixStatus($status) {
633 SET unix_status='$status'
634 WHERE user_id='". $this->getID()."'
638 $this->setError('ERROR - Could Not Update User Unix Status: '.db_error());
642 if ($status == 'A') {
643 if (!$SYS->sysCheckCreateUser($this->getID())) {
644 $this->setError($SYS->getErrorMessage());
649 if ($SYS->sysCheckUser($this->getID())) {
650 if (!$SYS->sysRemoveUser($this->getID())) {
651 $this->setError($SYS->getErrorMessage());
658 $this->data_array['unix_status']=$status;
665 * getUnixName - the user's unix_name.
667 * @return string This user's unix/login name.
669 function getUnixName() {
670 return strtolower($this->data_array['user_name']);
674 * getUnixPasswd - get the user's password.
676 * @return string This user's unix crypted passwd.
678 function getUnixPasswd() {
679 return $this->data_array['unix_pw'];
683 * getUnixBox - the hostname of the unix box this user has an account on.
685 * @return string This user's shell login machine.
687 function getUnixBox() {
688 return $this->data_array['unix_box'];
692 * getMD5Passwd - the password.
694 * @return string This user's MD5-crypted passwd.
696 function getMD5Passwd() {
697 return $this->data_array['user_pw'];
701 * getConfirmHash - the confirm hash in the db.
703 * @return string This user's confirmation hash.
705 function getConfirmHash() {
706 return $this->data_array['confirm_hash'];
710 * getEmail - the user's email address.
712 * @return string This user's email address.
714 function getEmail() {
715 return $this->data_array['email'];
719 * getNewEmail - while changing an email address, it is stored here until confirmation.
721 * getNewEmail is a private operation for email change.
723 * @return string This user's new (not yet confirmed) email address.
726 function getNewEmail() {
727 return $this->data_array['email_new'];
731 * setEmail - set a new email address, which must be confirmed.
733 * @param string The email address.
734 * @return boolean success.
736 function setEmail($email) {
737 if (!$email || !validate_email($email)) {
738 $this->setError('ERROR: Invalid Email');
744 WHERE user_id='". $this->getID()."'
748 $this->setError('ERROR - Could Not Update User Email: '.db_error());
751 $this->data_array['email'] = $email;
757 * setNewEmailAndHash - setNewEmailAndHash is a private operation for email change.
759 * @param string The email address.
760 * @param string The email hash.
761 * @return boolean success.
763 function setNewEmailAndHash($email, $hash='') {
766 $hash = substr(md5(strval(time()) . strval(mt_rand())), 0, 16);
769 if (!$email || !validate_email($email)) {
770 $this->setError('ERROR - Invalid Email');
776 SET confirm_hash='$hash',
778 WHERE user_id='".$this->getID()."'
782 $this->setError('ERROR - Could Not Update User Email And Hash: '.db_error());
785 $this->data_array['email_new'] = $email;
786 $this->data_array['confirm_hash'] = $hash;
792 * getRealName - get the user's real name.
794 * @return string This user's real name.
796 function getRealName() {
797 return $this->getFirstName(). ' ' .$this->getLastName();
801 * getFirstName - get the user's first name.
803 * @return string This user's first name.
805 function getFirstName() {
806 return $this->data_array['firstname'];
810 * getLastName - get the user's last name.
812 * @return string This user's last name.
814 function getLastName() {
815 return $this->data_array['lastname'];
819 * getAddDate - this user's unix time when account was opened.
821 * @return int This user's unix time when account was opened.
823 function getAddDate() {
824 return $this->data_array['add_date'];
828 * getTimeZone - this user's timezone setting.
830 * @return string This user's timezone setting.
832 function getTimeZone() {
833 return $this->data_array['timezone'];
837 * getCountryCode - this user's ccode setting.
839 * @return string This user's ccode setting.
841 function getCountryCode() {
842 return $this->data_array['ccode'];
846 * getShell - this user's preferred shell.
848 * @return string This user's preferred shell.
850 function getShell() {
851 return $this->data_array['shell'];
855 * setShell - sets user's preferred shell.
857 * @param string The users preferred shell.
858 * @return boolean success.
860 function setShell($shell) {
862 $shells = file('/etc/shells');
863 $shells[count($shells)] = "/bin/cvssh";
864 $out_shells = array();
865 foreach ($shells as $s) {
866 if (substr($s, 0, 1) == '#') {
869 $out_shells[] = chop($s);
871 if (!in_array($shell, $out_shells)) {
872 $this->setError('ERROR: Invalid Shell');
880 WHERE user_id='". $this->getID()."'
884 $this->setError('ERROR - Could Not Update User Unix Shell: '.db_error());
888 // Now change LDAP attribute, but only if corresponding
889 // entry exists (i.e. if user have shell access)
890 if ($SYS->sysCheckUser($this->getID()))
892 if (!$SYS->sysUserSetAttribute($this->getID(),"loginShell",$shell)) {
893 $this->setError($SYS->getErrorMessage());
898 $this->data_array['shell']=$shell;
905 * getUnixUID() - Get the unix UID of the user
907 * @return int This user's UID.
909 function getUnixUID() {
910 return $this->data_array['unix_uid'];
914 * getUnixGID() - Get the unix GID of the user
916 * @return int This user's GID.
918 function getUnixGID() {
919 return $this->data_array['unix_gid'];
923 * getLanguage - this user's language_id from supported_languages table.
925 * @return int This user's language_id.
927 function getLanguage() {
928 return $this->data_array['language'];
932 * getJabberAddress - this user's optional jabber address.
934 * @return string This user's jabber address.
936 function getJabberAddress() {
937 return $this->data_array['jabber_address'];
941 * getJabberOnly - whether this person wants updates sent ONLY to jabber.
943 * @return boolean This user's jabber preference.
945 function getJabberOnly() {
946 return $this->data_array['jabber_only'];
950 * getAddress - get this user's address.
952 * @return text This user's address.
954 function getAddress() {
955 return $this->data_array['address'];
959 * getAddress2 - get this user's address2.
961 * @return text This user's address2.
963 function getAddress2() {
964 return $this->data_array['address2'];
968 * getPhone - get this person's phone number.
970 * @return text This user's phone number.
972 function getPhone() {
973 return $this->data_array['phone'];
977 * getFax - get this person's fax number.
979 * @return text This user's fax.
982 return $this->data_array['fax'];
986 * getTitle - get this person's title.
988 * @return text This user's title.
990 function getTitle() {
991 return $this->data_array['title'];
995 * getGroups - get an array of groups this user is a member of.
997 * @return array Array of groups.
999 function &getGroups() {
1000 $sql="SELECT group_id
1002 WHERE user_id='". $this->getID() ."'";
1003 $res=db_query($sql);
1004 $arr =& util_result_column_to_array($res,0);
1005 return group_get_objects($arr);
1009 * getAuthorizedKeys - the SSH authorized keys set by the user.
1011 * @return string This user's SSH authorized (public) keys.
1013 function getAuthorizedKeys() {
1014 return ereg_replace("###", "\n", $this->data_array['authorized_keys']);
1018 * setAuthorizedKeys - set the SSH authorized keys for the user.
1020 * @param string The users public keys.
1021 * @return boolean success.
1023 function setAuthorizedKeys($keys) {
1024 $keys = trim($keys);
1025 $keys = ereg_replace("\r\n", "\n", $keys); // Convert to Unix EOL
1026 $keys = ereg_replace("\n+", "\n", $keys); // Remove empty lines
1027 $keys = ereg_replace("\n", "###", $keys); // Convert EOL to marker
1031 SET authorized_keys='$keys'
1032 WHERE user_id='".$this->getID()."'
1036 $this->setError('ERROR - Could Not Update User SSH Keys');
1039 $this->data_array['authorized_keys'] = $keys;
1045 * setLoggedIn($val) - Really only used by session code.
1047 * @param boolean The session value.
1049 function setLoggedIn($val=true) {
1050 $this->is_logged_in=$val;
1052 //if this is the logged in user, see if they are a super user
1053 $sql="SELECT count(*) AS count FROM user_group WHERE user_id='". $this->getID() ."' AND group_id='1' AND admin_flags='A'";
1054 $result=db_query($sql);
1056 $this->is_super_user=false;
1059 $row_count = db_fetch_array($result);
1060 $this->is_super_user = ($row_count['count'] > 0);
1065 * isLoggedIn - only used by session code.
1067 * @return boolean is_logged_in.
1069 function isLoggedIn() {
1070 return $this->is_logged_in;
1074 * deletePreference - delete a preference for this user.
1076 * @param string The unique field name for this preference.
1077 * @return boolean success.
1079 function deletePreference($preference_name) {
1080 $preference_name=strtolower(trim($preference_name));
1081 unset($this->user_pref["$preference_name"]);
1082 $res= db_query("DELETE FROM user_preferences
1083 WHERE user_id='". $this->getID() ."'
1084 AND preference_name='$preference_name'");
1089 * setPreference - set a new preference for this user.
1091 * @param string The unique field name for this preference.
1092 * @param string The value you are setting this preference to.
1093 * @return boolean success.
1095 function setPreference($preference_name,$value) {
1096 $preference_name=strtolower(trim($preference_name));
1097 //delete pref if not value passed in
1098 unset($this->user_pref);
1099 if (!isset($value)) {
1100 $result=db_query("DELETE FROM user_preferences WHERE
1101 user_id='". $this->getID() ."' AND preference_name='$preference_name'");
1103 $result=db_query("UPDATE user_preferences SET preference_value='$value',set_date='". time() ."' ".
1104 "WHERE user_id='". $this->getID() ."' ".
1105 "AND preference_name='$preference_name'");
1106 if (db_affected_rows($result) < 1) {
1108 $result=db_query("INSERT INTO user_preferences (user_id,preference_name,preference_value,set_date) ".
1109 "VALUES ('". $this->getID() ."','$preference_name','$value','". time() ."')");
1116 * getPreference - get a specific preference.
1118 * @param string The unique field name for this preference.
1119 * @return the preference string or false on failure.
1121 function getPreference($preference_name) {
1122 $preference_name=strtolower(trim($preference_name));
1124 First check to see if we have already fetched the preferences
1126 if ($this->user_pref) {
1127 //echo "\n\nPrefs were fetched already";
1128 if ($this->user_pref["$preference_name"]) {
1129 //we have fetched prefs - return part of array
1130 return $this->user_pref["$preference_name"];
1132 //we have fetched prefs, but this pref hasn't been set
1136 //we haven't returned prefs - go to the db
1137 $result=db_query("SELECT preference_name,preference_value FROM user_preferences ".
1138 "WHERE user_id='". $this->getID() ."'");
1139 if (db_numrows($result) < 1) {
1140 //echo "\n\nNo Prefs Found";
1144 //iterate and put the results into an array
1145 for ($i=0; $i<db_numrows($result); $i++) {
1146 $pref["".db_result($result,$i,'preference_name').""]=db_result($result,$i,'preference_value');
1148 $this->user_pref = $pref;
1150 if (array_key_exists($preference_name,$this->user_pref)) {
1151 //we have fetched prefs - return part of array
1152 return $this->user_pref["$preference_name"];
1154 //we have fetched prefs, but this pref hasn't been set
1162 * setPasswd - Changes user's password.
1164 * @param string The plaintext password.
1165 * @return boolean success.
1167 function setPasswd($passwd) {
1169 if (!account_pwvalid($passwd)) {
1170 $this->setError('Error: '.$GLOBALS['register_error']);
1175 $unix_pw = account_genunixpw($passwd);
1179 SET user_pw='" . md5($passwd) . "',
1181 WHERE user_id='".$this->getID()."'
1184 if (!$res || db_affected_rows($res) < 1) {
1185 $this->setError('ERROR - Could Not Change User Password: '.db_error());
1189 // Now change LDAP password, but only if corresponding
1190 // entry exists (i.e. if user have shell access)
1191 if ($SYS->sysCheckUser($this->getID())) {
1192 if (!$SYS->sysUserSetAttribute($this->getID(),"userPassword",'{crypt}'.$unix_pw)) {
1193 $this->setError($SYS->getErrorMessage());
1204 * usesRatings - whether user participates in rating system.
1206 * @return boolean success.
1208 function usesRatings() {
1209 return !$this->data_array['block_ratings'];
1213 * getPlugins - get a list of all available user plugins
1215 * @return array array containing plugin_id => plugin_name
1217 function getPlugins() {
1218 if (!isset($this->plugins_data)) {
1219 $this->plugins_data = array () ;
1220 $sql="SELECT user_plugin.plugin_id, plugins.plugin_name
1221 FROM user_plugin, plugins
1222 WHERE user_plugin.user_id=".$this->getID()."
1223 AND user_plugin.plugin_id = plugins.plugin_id" ;
1224 $res=db_query($sql);
1225 $rows = db_numrows($res);
1227 for ($i=0; $i<$rows; $i++) {
1228 $plugin_id = db_result($res,$i,'plugin_id');
1229 $this->plugins_data[$plugin_id] = db_result($res,$i,'plugin_name');
1232 return $this->plugins_data ;
1236 * usesPlugin - returns true if the user uses a particular plugin
1238 * @param string name of the plugin
1239 * @return boolean whether plugin is being used or not
1241 function usesPlugin($pluginname) {
1242 $plugins_data = $this->getPlugins() ;
1243 foreach ($plugins_data as $p_name) {
1244 if ($p_name == $pluginname) {
1252 * setPluginUse - enables/disables plugins for the user
1254 * @param string name of the plugin
1255 * @param boolean the new state
1256 * @return string database result
1258 function setPluginUse($pluginname, $val=true) {
1259 if ($val == $this->usesPlugin($pluginname)) {
1260 // State is already good, returning
1263 $sql="SELECT plugin_id
1265 WHERE plugin_name = '" . $pluginname . "'" ;
1266 $res=db_query($sql);
1267 $rows = db_numrows($res);
1269 // Error: no plugin by that name
1272 $plugin_id = db_result($res,0,'plugin_id');
1274 unset ($this->plugins_data) ;
1276 $sql="INSERT INTO user_plugin (user_id, plugin_id)
1277 VALUES (". $this->getID() . ", ". $plugin_id .")" ;
1278 $res=db_query($sql);
1281 $sql="DELETE FROM user_plugin
1282 WHERE user_id = ". $this->getID() . "
1283 AND plugin_id = ". $plugin_id ;
1284 $res=db_query($sql);
1290 * getMailingsPrefs - Get activity status for one of the site mailings.
1292 * @param string The id of mailing ('mail_va' for community mailings, 'mail_siteupdates' for site mailings)
1293 * @return boolean success.
1295 function getMailingsPrefs($mailing_id) {
1296 if ($mailing_id=='va') {
1297 return $this->data_array['mail_va'];
1298 } else if ($mailing_id=='site') {
1299 return $this->data_array['mail_siteupdates'];
1306 * unsubscribeFromMailings - Disable email notifications for user.
1308 * @param boolean If false, disable general site mailings, else - all.
1309 * @return boolean success.
1311 function unsubscribeFromMailings($all=false) {
1312 $res1 = $res2 = $res3 = true;
1315 SET mail_siteupdates=0,
1317 WHERE user_id='".$this->getID()."'
1321 DELETE FROM forum_monitored_forums
1322 WHERE user_id='".$this->getID()."'
1325 DELETE FROM filemodule_monitor
1326 WHERE user_id='".$this->getID()."'
1330 return $res1 && $res2 && $res3;
1334 * getThemeID - get the theme_id for this user.
1336 * @return int The theme_id.
1338 function getThemeID() {
1339 return $this->data_array['theme_id'];
1343 * getThemeID - get the theme_id for this user from the theme_prefs table.
1345 * @return int The theme_id.
1347 function setUpTheme() {
1349 // An optimization in session_getdata lets us pre-fetch this in most cases.....
1351 if (!$this->data_array['dirname']) {
1352 $res=db_query("SELECT dirname FROM themes WHERE theme_id='".$this->getThemeID()."'");
1353 $this->theme=db_result($res,0,'dirname');
1355 $this->theme=$this->data_array['dirname'];
1357 if (is_file($GLOBALS['sys_themeroot'].$this->theme.'/Theme.class.php')) {
1358 $GLOBALS['sys_theme']=$this->theme;
1360 $this->theme=$GLOBALS['sys_theme'];
1362 return $this->theme;
1371 EVERYTHING BELOW HERE IS DEPRECATED
1374 DO NOT USE FOR ANY NEW CODE
1383 * user_ismember() - DEPRECATED; DO NOT USE!
1385 * @param int The Group ID
1386 * @param int The Type
1390 function user_ismember($group_id,$type=0) {
1391 if (!session_loggedin()) {
1395 $project =& group_get_object($group_id);
1397 if (!$project || !is_object($project)) {
1401 $perm =& $project->getPermission( session_get_user() );
1402 if (!$perm || !is_object($perm) || !$perm->isMember()) {
1406 $type=strtoupper($type);
1411 return $perm->isPMAdmin();
1416 return $perm->isForumAdmin();
1420 //just in this group
1421 return $perm->isMember();
1425 //admin for this group
1426 return $perm->isAdmin();
1431 return $perm->isDocEditor();
1443 * user_getname() - DEPRECATED; DO NOT USE!
1445 * @param int The User ID
1449 function user_getname($user_id = false) {
1450 // use current user if one is not passed in
1452 if (session_loggedin()) {
1453 $user=&user_get_object(user_getid());
1455 return $user->getUnixName();
1457 return 'Error getting user';
1460 return 'No User Id';
1463 $user=&user_get_object($user_id);
1465 return $user->getUnixName();
1467 return 'Invalid User';
1474 // c-file-style: "bsd"