root/releases/0.672/lib/userlib.php

Revision 667, 10.2 kB (checked in by sven, 2 years ago)

feeds page shows viewed user's feeds instead of viewing user.
feeds system relies less on assumptions about $page_owner, more on explicit parameters.

Line 
1 <?php
2
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  */
9
10  
11 // INITIALISATION //////////////////////////////////////////////////////////////
12
13     // TODO: These need somewhere else to live. They're to do with
14     // authentication and session management, not user management.
15
16     // Session variable name
17     define('user_session_name', 'elgguser');
18     
19     // Persistent login cookie DEFs
20     define('AUTH_COOKIE', 'elggperm');
21     define('AUTH_COOKIE_LENGTH', 31556926); // 1YR in seconds
22     
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."));
27
28 // USER INFORMATION RETRIEVAL //////////////////////////////////////////////////
29
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) {
33         
34         // Name table
35         static $id_to_name_table;
36
37         // Returns field from a given ID
38
39         $user_id = (int) $user_id;
40         
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);
44                 
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);
48             }
49             if (isset($id_to_name_table[$user_id][$fieldname])) {
50                 return $id_to_name_table[$user_id][$fieldname];
51             }
52         }
53         
54         // If we've got here, the user didn't exist in the database
55         return false;
56         
57     }
58     
59     // Given a username, returns the specified field
60     // Returns false if the user doesn't exist.
61     function user_info_username($fieldname, $username) {
62         
63         // Name table
64         static $name_to_id_table;
65
66         // Returns user's ID from a given name
67         
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);
72             }
73             if (isset($name_to_id_table[$username][$fieldname])){
74                 return $name_to_id_table[$username][$fieldname];
75             }
76         }
77         
78         // If we've got here, the user didn't exist in the database
79         return false;
80         
81     }
82     
83     // Gets the type of a particular user
84     function user_type($user_id) {
85         
86         return user_info('user_type', $user_id);
87         
88     }
89     
90 // USER FLAGS //////////////////////////////////////////////////////////////////
91
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;
96         }
97         return false;
98     }
99     
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);
103     }
104     
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);
111             
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);
118         }
119     }
120     
121 // ACCESS RESTRICTIONS /////////////////////////////////////////////////////////
122
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;
128
129         // For now, there are three access levels: 0 (logged out), 1 (logged in) and 1000 (me)
130         if (logged_on == 1) {
131             $currentaccess++;
132         }
133             
134         if ($_SESSION['userid'] == $owner) {
135             $currentaccess += 1000;
136         }
137             
138         return $currentaccess;
139     }
140     
141     // Protect users to a certain access level
142     function protect($level, $owner = -1) {
143         if (accesslevel($owner) < $level) {
144             run("access_denied");
145             exit();
146         }
147     }
148
149 // NOTIFICATIONS AND MESSAGING /////////////////////////////////////////////////
150
151     // Send a message to a user
152     
153     function message_user($to, $from, $title, $message) {
154         
155        global $messages, $CFG;
156         
157         if (isset($to->ident)) {
158             $to = $to->ident;
159         }
160         
161         $notifications = user_flag_get("emailnotifications",$to);
162         if ($notifications) {
163             $email_from = new StdClass;
164             $email_from->email = $CFG->noreplyaddress;
165             $email_from->name = $CFG->sitename;
166             
167             if ($email_to = get_record_sql("select * from ".$CFG->prefix."users where ident = " . $to)) {
168             
169                 if (!email_to_user($email_to,$email_from,$title,$message . "\n\n\n" . __gettext("You cannot reply to this message by email."))) {
170                     $messages[] = __gettext("Failed to send email. An unknown error occurred.");
171                 }
172             }
173         }
174         
175         $m = new StdClass;
176         $m->title = $title;
177         $m->body = $message;
178         $m->from_id = $from;
179         $m->to_id = $to;
180         $m->posted = time();
181         $m->status = 'unread';
182         
183         if (!insert_record('messages',$m)) {
184             $messages[] = __gettext("Failed to send message. An unknown error occurred.");
185         }
186         
187     }
188     
189     // Get user $user_id's messages; optionally limit the number or the timeframe
190     
191     function get_messages($user_id, $number = null, $timeframe = null) {
192         
193         global $CFG;
194         
195         $where = "";
196         $limit = "";
197         if ($number != null) {
198             $limit = "limit $number";
199         }
200         if ($timeframe != null) {
201             $where = " and posted > ". (time() - $timeframe);
202         }
203         
204         return get_records_sql("select * from ".$CFG->prefix."messages where to_id = $user_id $where order by posted desc $limit");
205         
206     }
207     
208     // Return the basic HTML for a message (given its database row), where the
209     // title is a heading 2 and the body is in a paragraph.
210     
211     function display_message($message) {
212         
213         global $CFG;
214         
215         if ($message->from_id == -1) {
216             $from->name = __gettext("System");
217         } else {
218             $from = get_record_sql("select * from ".$CFG->prefix."users where ident = " . $message->from_id);
219         }
220         
221         $title = "[Message from ";
222         if ($message->from_id != -1) {
223             $title .= "<a href=\"" . $CFG->wwwroot . user_info("username",$message->from_id) . "/\">";
224         }
225         $title .= $from->name;
226         if ($message->from_id != -1) {
227             $title .= "</a>";
228         }
229         $title .= "] " . $message->title;
230         $body = "<p>" . nl2br(str_replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;",activate_urls($message->body))) . "</p>";
231         
232         $body = templates_draw(array(
233                                         'context' => 'databox1',
234                                         'name' => $title,
235                                         'column1' => $body
236                                       )
237                                 );
238         
239         return $body;
240         
241     }
242
243     // Send a notification to a user, both using the notifications table and
244     // - potentially - email, depending on a user's preferences
245     
246     function notify_user($user_id, $title, $message) {
247         
248         message_user($user_id, -1, $title, $message);
249         
250     }
251     
252     // Mark a user's messages as read
253     
254     function messages_read($user_id) {
255         
256         global $CFG;
257         //execute_sql("update ".$CFG->prefix."messages set status = 'read' where to_id = $user_id",false);
258         set_field('messages', 'status', 'read', 'to_id', $user_id);
259         
260     }
261     
262     // Cleanup messages - this should be relatively temporary
263     
264     function cleanup_messages($older_than) {
265     
266         global $CFG, $messages;
267         execute_sql("delete from ".$CFG->prefix."messages where posted < " . $older_than,false);
268         
269           
270     }
271     
272 // STATISTICS //////////////////////////////////////////////////////////////////
273
274     // Count number of users
275     // Optional: the user_type (eg 'person') and the minimum last time they
276     // performed an action
277     
278     function count_users($type = '', $last_action = 0) {
279         
280         global $CFG;
281         
282         $where = "1 = 1";
283         if (!empty($type)) {
284             $where .= " AND user_type = '$type'";
285         }
286         if ($last_action > 0) {
287             $where .= " AND last_action > " . $last_action;
288         }
289         if ($users = get_records_sql('SELECT user_type, count(ident) AS numusers
290                                   FROM '.$CFG->prefix.'users
291                                   WHERE '.$where.'
292                                   GROUP BY user_type')) {
293             if (sizeof($users) > 1) {
294                 return $users;
295             }
296             foreach($users as $user) {
297                 return $user->numusers;
298             }
299         }
300         
301         return false;
302     }
303
304 ?>
Note: See TracBrowser for help on using the browser.