Fixing a small css issue in the user class.
[elgg.git] / lib / userlib.php
blob33407b5b9ce7134b6da9f9606cf5619b47f13684
1 <?php
3 /**
4 * Library of functions for user polling and manipulation.
5 * Largely taken from the old /units/users/
6 * Copyright (C) 2004-2006 Ben Werdmuller and David Tosh
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 */
11 // INITIALISATION //////////////////////////////////////////////////////////////
13 // TODO: These need somewhere else to live. They're to do with
14 // authentication and session management, not user management.
16 // Session variable name
17 define('user_session_name', 'elgguser');
19 // Persistent login cookie DEFs
20 define('AUTH_COOKIE', 'elggperm');
21 define('AUTH_COOKIE_LENGTH', 31556926); // 1YR in seconds
23 // Messages
24 define('AUTH_MSG_OK', __gettext("You have been logged on."));
25 define('AUTH_MSG_BADLOGIN', __gettext("Unrecognised username or password. The system could not log you on, or you may not have activated your account."));
26 define('AUTH_MSG_MISSING', __gettext("Either the username or password were not specified. The system could not log you on."));
28 // USER INFORMATION RETRIEVAL //////////////////////////////////////////////////
30 // Given a user ID number, returns the specified field
31 // Returns false if the user doesn't exist.
32 function user_info($fieldname, $user_id) {
34 // Name table
35 static $id_to_name_table;
37 // Returns field from a given ID
39 $user_id = (int) $user_id;
41 if (!empty($user_id)) {
42 if (!isset($id_to_name_table[$user_id][$fieldname])) {
43 //$id_to_name_table[$user_id][$fieldname] = get_field('users',$fieldname,'ident',$user_id);
45 // this reduces number of db queries, but uses slightly more memory
46 // due to adodb's recordset generation, it has both named and numeric array keys
47 $id_to_name_table[$user_id] = (array) get_record('users','ident',$user_id);
49 if (isset($id_to_name_table[$user_id][$fieldname])) {
50 return $id_to_name_table[$user_id][$fieldname];
54 // If we've got here, the user didn't exist in the database
55 return false;
59 // Given a username, returns the specified field
60 // Returns false if the user doesn't exist.
61 function user_info_username($fieldname, $username) {
63 // Name table
64 static $name_to_id_table;
66 // Returns user's ID from a given name
68 if (!empty($username)) {
69 if (!isset($name_to_id_table[$username][$fieldname])) {
70 //$name_to_id_table[$username][$fieldname] = get_field('users',$fieldname,'username',$username);
71 $name_to_id_table[$username] = (array) get_record('users','username',$username);
73 if (isset($name_to_id_table[$username][$fieldname])){
74 return $name_to_id_table[$username][$fieldname];
78 // If we've got here, the user didn't exist in the database
79 return false;
83 // Gets the type of a particular user
84 function user_type($user_id) {
86 return user_info('user_type', $user_id);
90 // USER FLAGS //////////////////////////////////////////////////////////////////
92 // Gets the value of a flag
93 function user_flag_get($flag_name, $user_id) {
94 if ($result = get_record('user_flags','flag',$flag_name,'user_id',$user_id)) {
95 return $result->value;
97 return false;
100 // Removes a flag
101 function user_flag_unset($flag_name, $user_id) {
102 return delete_records('user_flags','flag',$flag_name,'user_id',$user_id);
105 // Adds a flag
106 function user_flag_set($flag_name, $value, $user_id) {
107 $flag_name = trim($flag_name);
108 if ($flag_name) {
109 // Unset the flag first
110 user_flag_unset($flag_name, $user_id);
112 // Then add data
113 $flag = new StdClass;
114 $flag->flag = $flag_name;
115 $flag->user_id = $user_id;
116 $flag->value = $value;
117 return insert_record('user_flags',$flag);
121 // ACCESS RESTRICTIONS /////////////////////////////////////////////////////////
123 // Get current access level
124 // Utterly deprecated (user levels no longer work like this), but kept
125 // alive for now.
126 function accesslevel($owner = -1) {
127 $currentaccess = 0;
129 // For now, there are three access levels: 0 (logged out), 1 (logged in) and 1000 (me)
130 if (logged_on == 1) {
131 $currentaccess++;
134 if ($_SESSION['userid'] == $owner) {
135 $currentaccess += 1000;
138 return $currentaccess;
141 // Protect users to a certain access level
142 function protect($level, $owner = -1) {
143 global $CFG;
144 if (accesslevel($owner) < $level) {
145 echo '<a href="' . $CFG->wwwroot . '">' . __gettext("Access Denied") . '</a>';
146 exit();
150 // NOTIFICATIONS AND MESSAGING /////////////////////////////////////////////////
152 // Send a message to a user
154 function message_user($to, $from, $title, $message) {
156 global $messages, $CFG;
158 if (isset($to->ident)) {
159 $to = $to->ident;
162 $notifications = user_flag_get("emailnotifications",$to);
163 if ($notifications) {
164 $email_from = new StdClass;
165 $email_from->email = $CFG->noreplyaddress;
166 $email_from->name = $CFG->sitename;
168 if ($email_to = get_record_sql("select * from ".$CFG->prefix."users where ident = " . $to)) {
170 if (!email_to_user($email_to,$email_from,$title,$message . "\n\n\n" . __gettext("You cannot reply to this message by email."))) {
171 $messages[] = __gettext("Failed to send email. An unknown error occurred.");
176 $m = new StdClass;
177 $m->title = $title;
178 $m->body = $message;
179 $m->from_id = $from;
180 $m->to_id = $to;
181 $m->posted = time();
182 $m->status = 'unread';
184 if (!insert_record('messages',$m)) {
185 $messages[] = __gettext("Failed to send message. An unknown error occurred.");
190 // Get user $user_id's messages; optionally limit the number or the timeframe
192 function get_messages($user_id, $number = null, $timeframe = null) {
194 global $CFG;
196 $where = "";
197 $limit = "";
198 if ($number != null) {
199 $limit = "limit $number";
201 if ($timeframe != null) {
202 $where = " and posted > ". (time() - $timeframe);
205 return get_records_sql("select * from ".$CFG->prefix."messages where to_id = $user_id $where order by posted desc $limit");
209 // Return the basic HTML for a message (given its database row), where the
210 // title is a heading 2 and the body is in a paragraph.
212 function display_message($message) {
214 global $CFG;
216 if ($message->from_id == -1) {
217 $from->name = __gettext("System");
218 } else {
219 $from = get_record_sql("select * from ".$CFG->prefix."users where ident = " . $message->from_id);
222 $title = "[Message from ";
223 if ($message->from_id != -1) {
224 $title .= "<a href=\"" . $CFG->wwwroot . user_info("username",$message->from_id) . "/\">";
226 $title .= $from->name;
227 if ($message->from_id != -1) {
228 $title .= "</a>";
230 $title .= "] " . $message->title;
231 $body = "<p>" . nl2br(str_replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;",activate_urls($message->body))) . "</p>";
233 $body = templates_draw(array(
234 'context' => 'databox1',
235 'name' => $title,
236 'column1' => $body
240 return $body;
244 // Send a notification to a user, both using the notifications table and
245 // - potentially - email, depending on a user's preferences
247 function notify_user($user_id, $title, $message) {
249 message_user($user_id, -1, $title, $message);
253 // Mark a user's messages as read
255 function messages_read($user_id) {
257 global $CFG;
258 //execute_sql("update ".$CFG->prefix."messages set status = 'read' where to_id = $user_id",false);
259 set_field('messages', 'status', 'read', 'to_id', $user_id);
263 // Cleanup messages - this should be relatively temporary
265 function cleanup_messages($older_than) {
267 global $CFG, $messages;
268 execute_sql("delete from ".$CFG->prefix."messages where posted < " . $older_than,false);
273 // STATISTICS //////////////////////////////////////////////////////////////////
275 // Count number of users
276 // Optional: the user_type (eg 'person') and the minimum last time they
277 // performed an action
279 function count_users($type = '', $last_action = 0) {
281 global $CFG;
283 $where = "1 = 1";
284 if (!empty($type)) {
285 $where .= " AND user_type = '$type'";
287 if ($last_action > 0) {
288 $where .= " AND last_action > " . $last_action;
290 if ($users = get_records_sql('SELECT user_type, count(ident) AS numusers
291 FROM '.$CFG->prefix.'users
292 WHERE '.$where.'
293 GROUP BY user_type')) {
294 if (sizeof($users) > 1) {
295 return $users;
297 foreach($users as $user) {
298 return $user->numusers;
302 return false;