3 * FusionForge user management
5 * Copyright 1999-2001, VA Linux Systems, Inc.
7 * This file is part of FusionForge.
9 * FusionForge is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published
11 * by the Free Software Foundation; either version 2 of the License,
12 * or (at your option) any later version.
14 * FusionForge is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with FusionForge; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 require_once $gfwww.'include/vote_function.php';
29 * user_get_object_by_name() - Get User object by username.
30 * user_get_object is useful so you can pool user objects/save database queries
31 * You should always use this instead of instantiating the object directly
33 * @param string The unix username - required
34 * @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
35 * @return a user object or false on failure
38 function &user_get_object_by_name($user_name,$res=false) {
39 $user_name = strtolower($user_name);
41 $res=db_query("SELECT * FROM users WHERE user_name='$user_name'");
43 return user_get_object(db_result($res,0,'user_id'),$res);
47 * user_get_object() - Get User object by user ID.
48 * user_get_object is useful so you can pool user objects/save database queries
49 * You should always use this instead of instantiating the object directly
51 * @param int The ID of the user - required
52 * @param int The result set handle ("SELECT * FROM USERS WHERE user_id=xx")
53 * @return a user object or false on failure
56 function &user_get_object($user_id,$res=false) {
57 //create a common set of group objects
58 //saves a little wear on the database
60 //automatically checks group_type and
61 //returns appropriate object
64 if (!isset($USER_OBJ["_".$user_id."_"])) {
66 //the db result handle was passed in
68 $res=db_query("SELECT * FROM users WHERE user_id='$user_id'");
70 if (!$res || db_numrows($res) < 1) {
71 $USER_OBJ["_".$user_id."_"]=false;
73 $USER_OBJ["_".$user_id."_"]= new GFUser($user_id,$res);
76 return $USER_OBJ["_".$user_id."_"];
79 function &user_get_objects($id_arr) {
84 for ($i=0; $i<count($id_arr); $i++) {
86 // See if this ID already has been fetched in the cache
91 if (!isset($USER_OBJ["_".$id_arr[$i]."_"])) {
94 $return[] =& $USER_OBJ["_".$id_arr[$i]."_"];
97 if (count($fetch) > 0) {
98 $sql="SELECT * FROM users WHERE user_id IN ('".implode($fetch,'\',\'') ."')";
100 while ($arr =& db_fetch_array($res)) {
101 $USER_OBJ["_".$arr['user_id']."_"] = new GFUser($arr['user_id'],$arr);
102 $return[] =& $USER_OBJ["_".$arr['user_id']."_"];
108 function &user_get_objects_by_name($username_arr) {
109 $res=db_query("SELECT user_id FROM users WHERE user_name IN ('".implode($username_arr,'\',\'')."')");
110 $arr =& util_result_column_to_array($res,0);
111 return user_get_objects($arr);
114 class GFUser extends Error {
116 * Associative array of data from db.
118 * @var array $data_array.
123 * Is this person a site super-admin?
125 * @var bool $is_super_user
130 * Is this person the logged in user?
132 * @var bool $is_logged_in
137 * Array of preferences
139 * @var array $user_pref
147 * GFUser($id,$res) - CONSTRUCTOR - GENERALLY DON'T USE THIS
149 * instead use the user_get_object() function call
151 * @param int The user_id
152 * @param int The database result set OR array of data
154 function GFUser($id=false,$res=false) {
157 //setting up an empty object
158 //probably going to call create()
162 $this->fetchData($id);
164 if (is_array($res)) {
165 $this->data_array =& $res;
166 } elseif (db_numrows($res) < 1) {
167 //function in class we extended
168 $this->setError('User Not Found');
169 $this->data_array=array();
172 //set up an associative array for use by other functions
173 db_reset_result($res);
174 $this->data_array = db_fetch_array($res);
177 $this->is_super_user=false;
178 $this->is_logged_in=false;
183 * create() - Create a new user.
185 * @param string The unix username.
186 * @param string The real firstname.
187 * @param string The real lastname.
188 * @param string The first password.
189 * @param string The confirmation password.
190 * @param string The users email address.
191 * @param string The users preferred default language.
192 * @param string The users preferred default timezone.
193 * @param string The users preference for receiving site updates by email.
194 * @param string The users preference for receiving community updates by email.
195 * @param int The ID of the language preference.
196 * @param string The users preferred timezone.
197 * @param string The users Jabber address.
198 * @param int The users Jabber preference.
199 * @param int The users theme_id.
200 * @param string The users unix_box.
201 * @param string The users address.
202 * @param string The users address part 2.
203 * @param string The users phone.
204 * @param string The users fax.
205 * @param string The users title.
206 * @param char(2) The users ISO country_code.
207 * @param bool Whether to send an email or not
208 * @returns The newly created user ID
211 function create($unix_name,$firstname,$lastname,$password1,$password2,$email,
212 $mail_site,$mail_va,$language_id,$timezone,$jabber_address,$jabber_only,$theme_id,
213 $unix_box='shell',$address='',$address2='',$phone='',$fax='',$title='',$ccode='US',$send_mail=true) {
215 $this->setError(_('You must supply a theme'));
219 $this->setError(_('You must supply a username'));
223 $this->setError(_('You must supply a first name'));
227 $this->setError(_('You must supply a last name'));
231 $this->setError(_('You must supply a password'));
234 if ($password1 != $password2) {
235 $this->setError(_('Passwords do not match'));
238 if (!account_pwvalid($password1)) {
239 $this->setError(_('Invalid Password:'));
242 $unix_name=strtolower($unix_name);
243 if (!account_namevalid($unix_name)) {
244 $this->setError(_('Invalid Unix Name.'));
247 if (!validate_email($email)) {
248 $this->setError(_('Invalid Email Address'));
251 if ($jabber_address && !validate_email($jabber_address)) {
252 $this->setError(_('Invalid Jabber Address'));
260 if (db_numrows(db_query("SELECT user_id FROM users WHERE user_name LIKE '$unix_name'")) > 0) {
261 $this->setError(_('That username already exists.'));
264 if ($GLOBALS['sys_require_unique_email']) {
265 if (db_numrows(db_query("SELECT user_id FROM users WHERE email='$email' OR email_new='$email'")) > 0) {
266 $this->setError(_('User with this email already exists - use people search to recover your login.'));
270 // if we got this far, it must be good
271 $confirm_hash = substr(md5($password1 . rand() . microtime()),0,16);
273 $sql="INSERT INTO users (user_name,user_pw,unix_pw,realname,firstname,lastname,email,add_date,
274 status,confirm_hash,mail_siteupdates,mail_va,language,timezone,jabber_address,jabber_only,
275 unix_box,address,address2,phone,fax,title,ccode,theme_id)
276 VALUES ('$unix_name',
277 '". md5($password1) . "',
278 '". account_genunixpw($password1) . "',
279 '". htmlspecialchars($firstname.' '.$lastname). "',
280 '". htmlspecialchars($firstname). "',
281 '". htmlspecialchars($lastname). "',
286 '". (($mail_site)?"1":"0") . "',
287 '". (($mail_va)?"1":"0") . "',
293 '". htmlspecialchars($address) ."',
294 '". htmlspecialchars($address2) ."',
295 '". htmlspecialchars($phone) ."',
296 '". htmlspecialchars($fax) ."',
297 '". htmlspecialchars($title) ."',
302 $result=db_query($sql);
305 $this->setError(_('Insert Failed') .db_error().$sql);
310 $id = db_insertid($result,'users','user_id');
312 $this->setError('Could Not Get USERID: ' .db_error());
317 if (!$this->fetchData($id)) {
322 $hook_params = array ();
323 $hook_params['user'] = $this;
324 $hook_params['user_id'] = $this->getID();
325 $hook_params['user_name'] = $unix_name;
326 $hook_params['user_password'] = $password1;
327 plugin_hook ("user_create", $hook_params);
330 setup_gettext_from_lang_id($language_id);
331 $this->sendRegistrationEmail();
332 setup_gettext_from_context();
341 * sendRegistrationEmail() - Send email for registration verification
343 * @return true or false
345 function sendRegistrationEmail() {
346 $message=stripcslashes(sprintf(_('Thank you for registering on the %3$s web site. You have
347 account with username %1$s created for you. In order
348 to complete your registration, visit the following url:
352 (If you don\'t see any URL above, it is likely due to a bug in your mail client.
353 Use one below, but make sure it is entered as the single line.)
361 $this->getUnixName(),
362 util_make_url ('/account/verify.php?confirm_hash=_'.$this->getConfirmHash()),
363 $GLOBALS['sys_name']));
366 sprintf(_('%1$s Account Registration'), $GLOBALS['sys_name']),
372 * delete() - remove the User from all his groups.
374 * Remove the User from all his groups and set his status to D.
376 * @param boolean Confirmation of deletion.
377 * @return true or false
379 function delete($sure) {
383 $groups = &$this->getGroups();
384 if (is_array($groups)) {
385 foreach ($groups as $group) {
386 $group->removeUser($this->getID());
391 $res = db_query("DELETE FROM artifact_monitor WHERE user_id='".$this->getID()."' ");
393 $this->setError('ERROR - Could Not Delete From artifact_monitor: '.db_error());
397 $res = db_query("DELETE FROM artifact_type_monitor WHERE user_id='".$this->getID()."' ");
399 $this->setError('ERROR - Could Not Delete From artifact_type_monitor: '.db_error());
403 $res = db_query("DELETE FROM forum_monitored_forums WHERE user_id='".$this->getID()."' ");
405 $this->setError('ERROR - Could Not Delete From forum_monitored_forums: '.db_error());
409 $res = db_query("DELETE FROM filemodule_monitor WHERE user_id='".$this->getID()."' ");
411 $this->setError('ERROR - Could Not Delete From filemodule_monitor: '.db_error());
416 $hook_params = array ();
417 $hook_params['user'] = $this;
418 $hook_params['user_id'] = $this->getID();
419 plugin_hook ("user_delete", $hook_params);
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())) {
507 $hook_params = array ();
508 $hook_params['user'] = $this;
509 $hook_params['user_id'] = $this->getID();
510 plugin_hook ("user_update", $hook_params);
518 * fetchData - May need to refresh database fields.
520 * If an update occurred and you need to access the updated info.
522 * @return boolean success;
524 function fetchData($user_id) {
525 $res=db_query("SELECT * FROM users WHERE user_id='$user_id'");
526 if (!$res || db_numrows($res) < 1) {
527 $this->setError('GFUser::fetchData()::'.db_error());
530 $this->data_array =& db_fetch_array($res);
535 * getID - Simply return the user_id for this object.
537 * @return int This user's user_id number.
540 return $this->data_array['user_id'];
544 * getStatus - get the status of this user.
546 * Statuses include (A)ctive, (P)ending, (S)uspended ,(D)eleted.
548 * @return char This user's status flag.
550 function getStatus() {
551 return $this->data_array['status'];
555 * setStatus - set this user's status.
557 * @param string Status - P, A, S, or D.
558 * @return boolean success.
560 function setStatus($status) {
562 if ($status != 'P' && $status != 'A'
563 && $status != 'S' && $status != 'D') {
564 $this->setError('ERROR: Invalid status value');
569 $res=db_query("UPDATE users
571 WHERE user_id='". $this->getID()."'");
574 $this->setError('ERROR - Could Not Update User Status: '.db_error());
578 $this->data_array['status']=$status;
579 if ($status == 'D') {
580 // Remove this user from all groups
581 $res = db_query(" DELETE FROM user_group WHERE user_id='".$this->getID()."' ");
583 $this->setError('ERROR - Could Not Propogate Deleted Status: '.db_error());
588 $hook_params = array ();
589 $hook_params['user'] = $this;
590 $hook_params['user_id'] = $this->getID();
591 $hook_params['status'] = $status;
592 plugin_hook ("user_setstatus", $hook_params);
596 //plugin webcalendar, create cal_user
597 plugin_hook('add_cal_user',$this->getID());
604 * isActive - whether this user is confirmed and active.
606 * Database field status of 'A' returns true.
607 * @return boolean is_active.
609 function isActive() {
610 if ($this->getStatus()=='A') {
618 * getUnixStatus - Status of activation of unix account.
620 * @return char (N)one, (A)ctive, (S)uspended or (D)eleted
622 function getUnixStatus() {
623 return $this->data_array['unix_status'];
627 * setUnixStatus - Sets status of activation of unix account.
629 * @param string The unix status.
635 * @return boolean success.
637 function setUnixStatus($status) {
642 SET unix_status='$status'
643 WHERE user_id='". $this->getID()."'
647 $this->setError('ERROR - Could Not Update User Unix Status: '.db_error());
651 if ($status == 'A') {
652 if (!$SYS->sysCheckCreateUser($this->getID())) {
653 $this->setError($SYS->getErrorMessage());
658 if ($SYS->sysCheckUser($this->getID())) {
659 if (!$SYS->sysRemoveUser($this->getID())) {
660 $this->setError($SYS->getErrorMessage());
667 $this->data_array['unix_status']=$status;
674 * getUnixName - the user's unix_name.
676 * @return string This user's unix/login name.
678 function getUnixName() {
679 return strtolower($this->data_array['user_name']);
683 * getUnixPasswd - get the user's password.
685 * @return string This user's unix crypted passwd.
687 function getUnixPasswd() {
688 return $this->data_array['unix_pw'];
692 * getUnixBox - the hostname of the unix box this user has an account on.
694 * @return string This user's shell login machine.
696 function getUnixBox() {
697 return $this->data_array['unix_box'];
701 * getMD5Passwd - the password.
703 * @return string This user's MD5-crypted passwd.
705 function getMD5Passwd() {
706 return $this->data_array['user_pw'];
710 * getConfirmHash - the confirm hash in the db.
712 * @return string This user's confirmation hash.
714 function getConfirmHash() {
715 return $this->data_array['confirm_hash'];
719 * getEmail - the user's email address.
721 * @return string This user's email address.
723 function getEmail() {
724 return $this->data_array['email'];
728 * getNewEmail - while changing an email address, it is stored here until confirmation.
730 * getNewEmail is a private operation for email change.
732 * @return string This user's new (not yet confirmed) email address.
735 function getNewEmail() {
736 return $this->data_array['email_new'];
740 * setEmail - set a new email address, which must be confirmed.
742 * @param string The email address.
743 * @return boolean success.
745 function setEmail($email) {
746 if (!$email || !validate_email($email)) {
747 $this->setError('ERROR: Invalid Email');
754 WHERE user_id='". $this->getID()."'
758 $this->setError('ERROR - Could Not Update User Email: '.db_error());
762 $hook_params = array ();
763 $hook_params['user'] = $this;
764 $hook_params['user_id'] = $this->getID();
765 $hook_params['user_email'] = $email;
766 plugin_hook ("user_setemail", $hook_params);
768 $this->data_array['email'] = $email;
775 * setNewEmailAndHash - setNewEmailAndHash is a private operation for email change.
777 * @param string The email address.
778 * @param string The email hash.
779 * @return boolean success.
781 function setNewEmailAndHash($email, $hash='') {
784 $hash = substr(md5(strval(time()) . strval(mt_rand())), 0, 16);
787 if (!$email || !validate_email($email)) {
788 $this->setError('ERROR - Invalid Email');
792 if ($GLOBALS['sys_require_unique_email']) {
793 if (db_numrows(db_query("SELECT user_id FROM users WHERE email='$email' OR email_new='$email'")) > 0) {
794 $this->setError(_('User with this email already exists.'));
800 SET confirm_hash='$hash',
802 WHERE user_id='".$this->getID()."'
806 $this->setError('ERROR - Could Not Update User Email And Hash: '.db_error());
809 $this->data_array['email_new'] = $email;
810 $this->data_array['confirm_hash'] = $hash;
816 * getRealName - get the user's real name.
818 * @return string This user's real name.
820 function getRealName() {
821 return $this->getFirstName(). ' ' .$this->getLastName();
825 * getFirstName - get the user's first name.
827 * @return string This user's first name.
829 function getFirstName() {
830 return $this->data_array['firstname'];
834 * getLastName - get the user's last name.
836 * @return string This user's last name.
838 function getLastName() {
839 return $this->data_array['lastname'];
843 * getAddDate - this user's unix time when account was opened.
845 * @return int This user's unix time when account was opened.
847 function getAddDate() {
848 return $this->data_array['add_date'];
852 * getTimeZone - this user's timezone setting.
854 * @return string This user's timezone setting.
856 function getTimeZone() {
857 return $this->data_array['timezone'];
861 * getCountryCode - this user's ccode setting.
863 * @return string This user's ccode setting.
865 function getCountryCode() {
866 return $this->data_array['ccode'];
870 * getShell - this user's preferred shell.
872 * @return string This user's preferred shell.
874 function getShell() {
875 return $this->data_array['shell'];
879 * setShell - sets user's preferred shell.
881 * @param string The users preferred shell.
882 * @return boolean success.
884 function setShell($shell) {
886 $shells = file('/etc/shells');
887 $shells[count($shells)] = "/bin/cvssh";
888 $out_shells = array();
889 foreach ($shells as $s) {
890 if (substr($s, 0, 1) == '#') {
893 $out_shells[] = chop($s);
895 if (!in_array($shell, $out_shells)) {
896 $this->setError('ERROR: Invalid Shell');
904 WHERE user_id='". $this->getID()."'
908 $this->setError('ERROR - Could Not Update User Unix Shell: '.db_error());
912 // Now change LDAP attribute, but only if corresponding
913 // entry exists (i.e. if user have shell access)
914 if ($SYS->sysCheckUser($this->getID()))
916 if (!$SYS->sysUserSetAttribute($this->getID(),"loginShell",$shell)) {
917 $this->setError($SYS->getErrorMessage());
922 $this->data_array['shell']=$shell;
929 * getUnixUID() - Get the unix UID of the user
931 * @return int This user's UID.
933 function getUnixUID() {
934 return $this->data_array['unix_uid'];
938 * getUnixGID() - Get the unix GID of the user
940 * @return int This user's GID.
942 function getUnixGID() {
943 return $this->data_array['unix_gid'];
947 * getLanguage - this user's language_id from supported_languages table.
949 * @return int This user's language_id.
951 function getLanguage() {
952 return $this->data_array['language'];
956 * getJabberAddress - this user's optional jabber address.
958 * @return string This user's jabber address.
960 function getJabberAddress() {
961 return $this->data_array['jabber_address'];
965 * getJabberOnly - whether this person wants updates sent ONLY to jabber.
967 * @return boolean This user's jabber preference.
969 function getJabberOnly() {
970 return $this->data_array['jabber_only'];
974 * getAddress - get this user's address.
976 * @return text This user's address.
978 function getAddress() {
979 return $this->data_array['address'];
983 * getAddress2 - get this user's address2.
985 * @return text This user's address2.
987 function getAddress2() {
988 return $this->data_array['address2'];
992 * getPhone - get this person's phone number.
994 * @return text This user's phone number.
996 function getPhone() {
997 return $this->data_array['phone'];
1001 * getFax - get this person's fax number.
1003 * @return text This user's fax.
1006 return $this->data_array['fax'];
1010 * getTitle - get this person's title.
1012 * @return text This user's title.
1014 function getTitle() {
1015 return $this->data_array['title'];
1019 * getGroups - get an array of groups this user is a member of.
1021 * @return array Array of groups.
1023 function &getGroups() {
1024 $sql="SELECT group_id
1026 WHERE user_id='". $this->getID() ."'";
1027 $res=db_query($sql);
1028 $arr =& util_result_column_to_array($res,0);
1029 return group_get_objects($arr);
1033 * getAuthorizedKeys - the SSH authorized keys set by the user.
1035 * @return string This user's SSH authorized (public) keys.
1037 function getAuthorizedKeys() {
1038 return ereg_replace("###", "\n", $this->data_array['authorized_keys']);
1042 * setAuthorizedKeys - set the SSH authorized keys for the user.
1044 * @param string The users public keys.
1045 * @return boolean success.
1047 function setAuthorizedKeys($keys) {
1048 $keys = trim($keys);
1049 $keys = ereg_replace("\r\n", "\n", $keys); // Convert to Unix EOL
1050 $keys = ereg_replace("\n+", "\n", $keys); // Remove empty lines
1051 $keys = ereg_replace("\n", "###", $keys); // Convert EOL to marker
1055 SET authorized_keys='$keys'
1056 WHERE user_id='".$this->getID()."'
1060 $this->setError('ERROR - Could Not Update User SSH Keys');
1063 $this->data_array['authorized_keys'] = $keys;
1069 * setLoggedIn($val) - Really only used by session code.
1071 * @param boolean The session value.
1073 function setLoggedIn($val=true) {
1074 $this->is_logged_in=$val;
1076 //if this is the logged in user, see if they are a super user
1077 $sql="SELECT count(*) AS count FROM user_group WHERE user_id='". $this->getID() ."' AND group_id='1' AND admin_flags='A'";
1078 $result=db_query($sql);
1080 $this->is_super_user=false;
1083 $row_count = db_fetch_array($result);
1084 $this->is_super_user = ($row_count['count'] > 0);
1089 * isLoggedIn - only used by session code.
1091 * @return boolean is_logged_in.
1093 function isLoggedIn() {
1094 return $this->is_logged_in;
1098 * deletePreference - delete a preference for this user.
1100 * @param string The unique field name for this preference.
1101 * @return boolean success.
1103 function deletePreference($preference_name) {
1104 $preference_name=strtolower(trim($preference_name));
1105 unset($this->user_pref["$preference_name"]);
1106 $res= db_query("DELETE FROM user_preferences
1107 WHERE user_id='". $this->getID() ."'
1108 AND preference_name='$preference_name'");
1113 * setPreference - set a new preference for this user.
1115 * @param string The unique field name for this preference.
1116 * @param string The value you are setting this preference to.
1117 * @return boolean success.
1119 function setPreference($preference_name,$value) {
1120 $preference_name=strtolower(trim($preference_name));
1121 //delete pref if not value passed in
1122 unset($this->user_pref);
1123 if (!isset($value)) {
1124 $result=db_query("DELETE FROM user_preferences WHERE
1125 user_id='". $this->getID() ."' AND preference_name='$preference_name'");
1127 $result=db_query("UPDATE user_preferences SET preference_value='$value',set_date='". time() ."' ".
1128 "WHERE user_id='". $this->getID() ."' ".
1129 "AND preference_name='$preference_name'");
1130 if (db_affected_rows($result) < 1) {
1132 $result=db_query("INSERT INTO user_preferences (user_id,preference_name,preference_value,set_date) ".
1133 "VALUES ('". $this->getID() ."','$preference_name','$value','". time() ."')");
1140 * getPreference - get a specific preference.
1142 * @param string The unique field name for this preference.
1143 * @return the preference string or false on failure.
1145 function getPreference($preference_name) {
1146 $preference_name=strtolower(trim($preference_name));
1148 First check to see if we have already fetched the preferences
1150 if (isset($this->user_pref)) {
1151 //echo "\n\nPrefs were fetched already";
1152 if (isset($this->user_pref["$preference_name"])) {
1153 //we have fetched prefs - return part of array
1154 return $this->user_pref["$preference_name"];
1156 //we have fetched prefs, but this pref hasn't been set
1160 //we haven't returned prefs - go to the db
1161 $result=db_query("SELECT preference_name,preference_value FROM user_preferences ".
1162 "WHERE user_id='". $this->getID() ."'");
1163 if (db_numrows($result) < 1) {
1164 //echo "\n\nNo Prefs Found";
1168 //iterate and put the results into an array
1169 for ($i=0; $i<db_numrows($result); $i++) {
1170 $pref["".db_result($result,$i,'preference_name').""]=db_result($result,$i,'preference_value');
1172 $this->user_pref = $pref;
1174 if (array_key_exists($preference_name,$this->user_pref)) {
1175 //we have fetched prefs - return part of array
1176 return $this->user_pref["$preference_name"];
1178 //we have fetched prefs, but this pref hasn't been set
1186 * setPasswd - Changes user's password.
1188 * @param string The plaintext password.
1189 * @return boolean success.
1191 function setPasswd($passwd) {
1193 if (!account_pwvalid($passwd)) {
1194 $this->setError('Error: '.$GLOBALS['register_error']);
1199 $unix_pw = account_genunixpw($passwd);
1203 SET user_pw='" . md5($passwd) . "',
1205 WHERE user_id='".$this->getID()."'
1208 if (!$res || db_affected_rows($res) < 1) {
1209 $this->setError('ERROR - Could Not Change User Password: '.db_error());
1213 // Now change LDAP password, but only if corresponding
1214 // entry exists (i.e. if user have shell access)
1215 if ($SYS->sysCheckUser($this->getID())) {
1216 if (!$SYS->sysUserSetAttribute($this->getID(),"userPassword",'{crypt}'.$unix_pw)) {
1217 $this->setError($SYS->getErrorMessage());
1223 $hook_params = array ();
1224 $hook_params['user'] = $this;
1225 $hook_params['user_id'] = $this->getID();
1226 $hook_params['user_password'] = $passwd;
1227 plugin_hook ("user_setpasswd", $hook_params);
1233 * usesRatings - whether user participates in rating system.
1235 * @return boolean success.
1237 function usesRatings() {
1238 return !$this->data_array['block_ratings'];
1242 * getPlugins - get a list of all available user plugins
1244 * @return array array containing plugin_id => plugin_name
1246 function getPlugins() {
1247 if (!isset($this->plugins_data)) {
1248 $this->plugins_data = array () ;
1249 $sql="SELECT user_plugin.plugin_id, plugins.plugin_name
1250 FROM user_plugin, plugins
1251 WHERE user_plugin.user_id=".$this->getID()."
1252 AND user_plugin.plugin_id = plugins.plugin_id" ;
1253 $res=db_query($sql);
1254 $rows = db_numrows($res);
1256 for ($i=0; $i<$rows; $i++) {
1257 $plugin_id = db_result($res,$i,'plugin_id');
1258 $this->plugins_data[$plugin_id] = db_result($res,$i,'plugin_name');
1261 return $this->plugins_data ;
1265 * usesPlugin - returns true if the user uses a particular plugin
1267 * @param string name of the plugin
1268 * @return boolean whether plugin is being used or not
1270 function usesPlugin($pluginname) {
1271 $plugins_data = $this->getPlugins() ;
1272 foreach ($plugins_data as $p_name) {
1273 if ($p_name == $pluginname) {
1281 * setPluginUse - enables/disables plugins for the user
1283 * @param string name of the plugin
1284 * @param boolean the new state
1285 * @return string database result
1287 function setPluginUse($pluginname, $val=true) {
1288 if ($val == $this->usesPlugin($pluginname)) {
1289 // State is already good, returning
1292 $sql="SELECT plugin_id
1294 WHERE plugin_name = '" . $pluginname . "'" ;
1295 $res=db_query($sql);
1296 $rows = db_numrows($res);
1298 // Error: no plugin by that name
1301 $plugin_id = db_result($res,0,'plugin_id');
1303 unset ($this->plugins_data) ;
1305 $sql="INSERT INTO user_plugin (user_id, plugin_id)
1306 VALUES (". $this->getID() . ", ". $plugin_id .")" ;
1307 $res=db_query($sql);
1310 $sql="DELETE FROM user_plugin
1311 WHERE user_id = ". $this->getID() . "
1312 AND plugin_id = ". $plugin_id ;
1313 $res=db_query($sql);
1319 * getMailingsPrefs - Get activity status for one of the site mailings.
1321 * @param string The id of mailing ('mail_va' for community mailings, 'mail_siteupdates' for site mailings)
1322 * @return boolean success.
1324 function getMailingsPrefs($mailing_id) {
1325 if ($mailing_id=='va') {
1326 return $this->data_array['mail_va'];
1327 } else if ($mailing_id=='site') {
1328 return $this->data_array['mail_siteupdates'];
1335 * unsubscribeFromMailings - Disable email notifications for user.
1337 * @param boolean If false, disable general site mailings, else - all.
1338 * @return boolean success.
1340 function unsubscribeFromMailings($all=false) {
1341 $res1 = $res2 = $res3 = true;
1344 SET mail_siteupdates=0,
1346 WHERE user_id='".$this->getID()."'
1350 DELETE FROM forum_monitored_forums
1351 WHERE user_id='".$this->getID()."'
1354 DELETE FROM filemodule_monitor
1355 WHERE user_id='".$this->getID()."'
1359 return $res1 && $res2 && $res3;
1363 * getThemeID - get the theme_id for this user.
1365 * @return int The theme_id.
1367 function getThemeID() {
1368 return $this->data_array['theme_id'];
1372 * getThemeID - get the theme_id for this user from the theme_prefs table.
1374 * @return int The theme_id.
1376 function setUpTheme() {
1378 // An optimization in session_getdata lets us pre-fetch this in most cases.....
1380 if (!$this->data_array['dirname']) {
1381 $res=db_query("SELECT dirname FROM themes WHERE theme_id='".$this->getThemeID()."'");
1382 $this->theme=db_result($res,0,'dirname');
1384 $this->theme=$this->data_array['dirname'];
1386 if (is_file($GLOBALS['sys_themeroot'].$this->theme.'/Theme.class.php')) {
1387 $GLOBALS['sys_theme']=$this->theme;
1389 $this->theme=$GLOBALS['sys_theme'];
1391 return $this->theme;
1400 EVERYTHING BELOW HERE IS DEPRECATED
1403 DO NOT USE FOR ANY NEW CODE
1412 * user_ismember() - DEPRECATED; DO NOT USE!
1414 * @param int The Group ID
1415 * @param int The Type
1419 function user_ismember($group_id,$type=0) {
1420 if (!session_loggedin()) {
1424 $project =& group_get_object($group_id);
1426 if (!$project || !is_object($project)) {
1430 $perm =& $project->getPermission( session_get_user() );
1431 if (!$perm || !is_object($perm) || !$perm->isMember()) {
1435 $type=strtoupper($type);
1440 return $perm->isPMAdmin();
1445 return $perm->isForumAdmin();
1449 //just in this group
1450 return $perm->isMember();
1454 //admin for this group
1455 return $perm->isAdmin();
1460 return $perm->isDocEditor();
1472 * user_getname() - DEPRECATED; DO NOT USE!
1474 * @param int The User ID
1478 function user_getname($user_id = false) {
1479 // use current user if one is not passed in
1481 if (session_loggedin()) {
1482 $user=&user_get_object(user_getid());
1484 return $user->getUnixName();
1486 return 'Error getting user';
1489 return 'No User Id';
1492 $user=&user_get_object($user_id);
1494 return $user->getUnixName();
1496 return 'Invalid User';
1503 // c-file-style: "bsd"