source: branches/1.3/libs/UserInfo.php @ 1771

Revision 1771, 14.0 KB checked in by nick_ramsay, 3 years ago (diff)

[Branch 1.3] Code cleanup - converted spaced indents to tabs, etc.

Line 
1<?php
2/**
3 * Functions for retrieving information about users
4 *
5 * PHP version 5
6 *
7 * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
18 *
19 * @category  Content Management System
20 * @package   HotaruCMS
21 * @author    Nick Ramsay <admin@hotarucms.org>
22 * @copyright Copyright (c) 2010, Hotaru CMS
23 * @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
24 * @link      http://www.hotarucms.org/
25 */
26class UserInfo extends UserBase
27{
28        /**
29         * Get the username for a given user id
30         *
31         * @param int $id user id
32         * @return string|false
33         */
34        public function getUserNameFromId($h, $id = 0)
35        {
36                $sql = "SELECT user_username FROM " . TABLE_USERS . " WHERE user_id = %d LIMIT 1";
37               
38                $username = $h->db->get_var($h->db->prepare($sql, $id));
39                if ($username) { return $username; } else { return false; }
40        }
41       
42       
43        /**
44         * Get the user id for a given username
45         *
46         * @param string $username
47         * @return int|false
48         */
49        public function getUserIdFromName($h, $username = '')
50        {
51                $sql = "SELECT user_id FROM " . TABLE_USERS . " WHERE user_username = %s AND user_siteid = %d  LIMIT 1";
52               
53                $userid = $h->db->get_var($h->db->prepare($sql, $username, SITEID));
54                if ($userid) { return $userid; } else { return false; }
55        }
56       
57       
58        /**
59         * Get the email from user id
60         *
61         * @param int $userid
62         * @return string|false
63         */
64        public function getEmailFromId($h, $userid = 0)
65        {
66                $sql = "SELECT user_email FROM " . TABLE_USERS . " WHERE user_id = %d  LIMIT 1";
67               
68                $email = $h->db->get_var($h->db->prepare($sql, $userid));
69                if ($email) { return $email; } else { return false; }
70        }
71       
72       
73        /**
74         * Get the user id from email
75         *
76         * @param string $email
77         * @return string|false
78         */
79        public function getUserIdFromEmail($h, $email = '')
80        {
81                $sql = "SELECT user_id FROM " . TABLE_USERS . " WHERE user_email = %s  LIMIT 1";
82               
83                $userid = $h->db->get_var($h->db->prepare($sql, $email));
84                if ($userid) { return $userid; } else { return false; }
85        }
86       
87       
88         /**
89         * Checks if the user has an 'admin' role
90         *
91         * @return bool
92         */
93        public function isAdmin($db, $username)
94        {
95                $sql = "SELECT * FROM " . TABLE_USERS . " WHERE user_username = %s AND user_siteid = %d AND user_role = %s  LIMIT 1";
96                $role = $db->get_row($db->prepare($sql, $username, SITEID, 'admin'));
97               
98                if ($role) { return true; } else { return false; }
99        }
100       
101       
102        /**
103         * Check if a user exists
104         *
105         * @param int $userid
106         * @param string $username
107         * @return int
108         *
109         * Notes: Returns 'no' if a user doesn't exist, else field under which found
110         */
111        public function userExists($db, $id = 0, $username = '', $email = '')
112        {
113                // id found
114                if ($id != 0) {
115                        if ($db->get_var($db->prepare("SELECT user_id FROM " . TABLE_USERS . " WHERE user_id = %d  LIMIT 1", $id))) {
116                                return 'id'; // id exists
117                        }
118                }
119               
120                // name found
121                if ($username != '') {
122                        if ($db->get_var($db->prepare("SELECT user_id FROM " . TABLE_USERS . " WHERE user_username = %s  LIMIT 1", $username))) {
123                                return 'name'; // username exists
124                        }
125                }
126               
127                // email found
128                if ($email != '') {
129                        if ($db->get_var($db->prepare("SELECT user_id FROM " . TABLE_USERS . " WHERE user_email = %s  LIMIT 1", $email))) {
130                                return 'email'; // email exists
131                        }
132                }
133               
134                // Error - no arguments provided
135                if (($id == 0) && ($username == '') && ($email == '')) {
136                        return 'error'; // no arguments provided
137                }
138               
139                return 'no'; // User doesn't exist
140        }
141       
142       
143        /**
144         * Check if an username exists in the database (used in forgotten password)
145         *
146         * @param string $username user username
147         * @param string $role user role (optional)
148         * @param int $exclude - exclude a user
149         * @return string|false
150         */
151        public function nameExists($h, $username = '', $role = '', $exclude = 0)
152        {
153                if (!$username) {  return false; }
154               
155                if (!$exclude) {
156                        if ($role) {
157                                $sql = "SELECT user_username FROM " . TABLE_USERS . " WHERE user_username = %s AND user_role = %s  LIMIT 1";
158                                $valid_username = $h->db->get_var($h->db->prepare($sql, $username, $role));
159                        } else {
160                                $sql = "SELECT user_username FROM " . TABLE_USERS . " WHERE user_username = %s  LIMIT 1";
161                                $valid_username = $h->db->get_var($h->db->prepare($sql, $username));
162                        }
163                } else {
164                        if ($role) {
165                                $sql = "SELECT user_username FROM " . TABLE_USERS . " WHERE user_username = %s AND user_role = %s AND user_id != %d  LIMIT 1";
166                                $valid_username = $h->db->get_var($h->db->prepare($sql, $username, $role, $exclude));
167                        } else {
168                                $sql = "SELECT user_username FROM " . TABLE_USERS . " WHERE user_username = %s AND user_id != %d  LIMIT 1";
169                                $valid_username = $h->db->get_var($h->db->prepare($sql, $username, $exclude));
170                        }
171                }
172       
173                if ($valid_username) { return $valid_username; } else { return false; }
174        }
175       
176       
177        /**
178         * Check if an email exists in the database (used in forgotten password)
179         *
180         * @param string $email user email
181         * @param string $role user role (optional)
182         * @param int $exclude - exclude a user
183         * @return string|false
184         */
185        public function emailExists($h, $email = '', $role = '', $exclude = 0)
186        {
187                if (!$email) {  return false; }
188               
189                if (!$exclude) {
190                        if ($role) {
191                                $sql = "SELECT user_email FROM " . TABLE_USERS . " WHERE user_email = %s AND user_role = %s  LIMIT 1";
192                                $valid_email = $h->db->get_var($h->db->prepare($sql, $email, $role));
193                        } else {
194                                $sql = "SELECT user_email FROM " . TABLE_USERS . " WHERE user_email = %s  LIMIT 1";
195                                $valid_email = $h->db->get_var($h->db->prepare($sql, $email));
196                        }
197                } else {
198                        if ($role) {
199                                $sql = "SELECT user_email FROM " . TABLE_USERS . " WHERE user_email = %s AND user_role = %s AND user_id != %d  LIMIT 1";
200                                $valid_email = $h->db->get_var($h->db->prepare($sql, $email, $role, $exclude));
201                        } else {
202                                $sql = "SELECT user_email FROM " . TABLE_USERS . " WHERE user_email = %s AND user_id != %d  LIMIT 1";
203                                $valid_email = $h->db->get_var($h->db->prepare($sql, $email, $exclude));
204                        }
205                }
206               
207                if ($valid_email) { return $valid_email; } else { return false; }
208        }
209       
210       
211        /**
212         * Get all users with permission to access admin
213         */
214        public function getMods($h, $permission = 'can_access_admin', $value = 'yes')
215        {
216                $sql = "SELECT user_id FROM " . TABLE_USERS . " WHERE (user_role = %s) || (user_role = %s) || (user_role = %s)";
217                $users = $h->db->get_results($h->db->prepare($sql, 'admin', 'supermod', 'moderator'));
218       
219                if (!$users) { return false; }
220               
221                $mods = array();
222               
223                foreach ($users as $user) {
224                        $details = new UserBase();
225                        $details->getUser($h, $user->user_id);
226                        if ($details->getPermission($permission) == $value) {
227                                $mods[$details->id]['id'] = $details->id;
228                                $mods[$details->id]['role'] = $details->role;
229                                $mods[$details->id]['name'] = $details->name;
230                                $mods[$details->id]['email'] = $details->email;
231                        }
232                }
233                return $mods;
234        }
235       
236       
237        /**
238         * Get the ids and names of all users or those with a specified role, sorted alphabetically
239         *
240         * @param string $role - optional user role to filter to
241         * @return array
242         */
243        public function userIdNameList($h, $role = '')
244        {
245                if ($role) {
246                        $sql = "SELECT user_id, user_username FROM " . TABLE_USERS . " WHERE user_siteid = %d AND user_role = %s ORDER BY user_username ASC";
247                        $results = $h->db->get_results($h->db->prepare($sql, SITEID, $role));
248                } else {
249                        $sql = "SELECT user_id, user_username FROM " . TABLE_USERS . " WHERE user_siteid = %d ORDER BY user_username ASC";
250                        $results = $h->db->get_results($h->db->prepare($sql, SITEID));
251                }
252               
253                return $results;
254        }
255       
256       
257        /**
258         * Get settings for all users
259         *
260         * @return array
261         */
262        public function userSettingsList($h, $userid = 0)
263        {
264                if ($userid) {
265                        $settings = $h->getProfileSettingsData($type = 'user_settings', $userid);
266                        return $settings;
267                } else {
268                        $sql = "SELECT usermeta_userid, usermeta_value FROM " . DB_PREFIX . "usermeta WHERE usermeta_key = %s";
269                        $results = $h->db->get_results($h->db->prepare($sql, 'user_settings'));
270                }
271               
272                return $results;
273        }
274       
275       
276        /**
277         * Get full details of all users or batches of users, sorted alphabetically
278         *
279         * @param array $id_array - optional array of user ids
280         * @param int $start - LIMIT $start $range (optional)
281         * @param int $range - LIMIT $start $range (optional)
282         * @return array
283         */
284        public function userListFull($h, $id_array = array(), $start = 0, $range = 0)
285        {
286                if (!$id_array) {
287                        // get all users
288                        $sql = "SELECT * FROM " . TABLE_USERS . " WHERE user_siteid = %d ORDER BY user_username ASC";
289                        $query = $h->db->prepare($sql, SITEID);
290                        $results = $h->db->get_results($query);
291                } else {
292                        // for grabbing
293                        if ($range) { $limit = " LIMIT " . $start . ", " . $range; }
294                        $sql = "SELECT * FROM " . TABLE_USERS . " WHERE (user_siteid = %d) AND (";
295                        for ($i=0; $i < count($id_array); $i++) {
296                                $sql .= "user_id = %d OR ";
297                        }
298                        $sql = rstrtrim($sql, "OR "); // strip trailing OR
299                        $sql .= ") ORDER BY user_username ASC" . $limit;
300               
301//                      $prepare_array[0] = $sql;
302//                      $prepare_array = array_merge($prepare_array, $id_array);
303                        $results = $h->db->get_results($h->db->prepare($sql, SITEID, $id_array));
304                }
305                return $results;
306        }
307       
308       
309        /**
310         * Stats for Admin homepage
311         *
312         * @param string $stat_type
313         * @return int
314         */
315        public function stats($h, $stat_type = '')
316        {
317                switch ($stat_type) {
318                        case 'admins':
319                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_siteid = %d AND user_role = %s";
320                                $query = $h->db->prepare($sql, SITEID, 'admin');
321                                $h->smartCache('on', 'users', 60, $query); // start using cache
322                                $users = $h->db->get_var($query);
323                                break;
324                        case 'supermods':
325                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_siteid = %d AND user_role = %s";
326                                $query = $h->db->prepare($sql, SITEID, 'supermod');
327                                $h->smartCache('on', 'users', 60, $query); // start using cache
328                                $users = $h->db->get_var($query);
329                                break;
330                        case 'moderators':
331                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_siteid = %d AND user_role = %s";
332                                $query = $h->db->prepare($sql, SITEID, 'moderator');
333                                $h->smartCache('on', 'users', 60, $query); // start using cache
334                                $users = $h->db->get_var($query);
335                                break;
336                        case 'members':
337                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_siteid = %d AND user_role = %s";
338                                $query = $h->db->prepare($sql, SITEID, 'member');
339                                $h->smartCache('on', 'users', 60, $query); // start using cache
340                                $users = $h->db->get_var($query);
341                                break;
342                        case 'total_users':
343                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_siteid = %d";
344                                $query = $h->db->prepare($sql, SITEID);
345                                $h->smartCache('on', 'users', 60, $query); // start using cache
346                                $users = $h->db->get_var($query);
347                                break;
348                        case 'approved_users':
349                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE (user_siteid = %d) AND user_role = %s OR user_role = %s OR user_role = %s OR  user_role = %s";
350                                $query = $h->db->prepare($sql, SITEID, 'admin', 'supermod', 'moderator', 'member');
351                                $h->smartCache('on', 'users', 60, $query); // start using cache
352                                $users = $h->db->get_var($query);
353                                break;
354                        case 'pending_users':
355                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_siteid = %d AND user_role = %s";
356                                $query = $h->db->prepare($sql, SITEID, 'pending');
357                                $h->smartCache('on', 'users', 60, $query); // start using cache
358                                $users = $h->db->get_var($query);
359                                break;
360                        case 'undermod_users':
361                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_siteid = %d AND user_role = %s";
362                                $query = $h->db->prepare($sql, SITEID, 'undermod');
363                                $h->smartCache('on', 'users', 60, $query); // start using cache
364                                $users = $h->db->get_var($query);
365                                break;
366                        case 'banned_users':
367                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_siteid = %d AND user_role = %s";
368                                $query = $h->db->prepare($sql, SITEID, 'banned');
369                                $h->smartCache('on', 'users', 60, $query); // start using cache
370                                $users = $h->db->get_var($query);
371                                break;
372                        case 'killspammed_users':
373                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_siteid = %d AND user_role = %s";
374                                $query = $h->db->prepare($sql, SITEID, 'killspammed');
375                                $h->smartCache('on', 'users', 60, $query); // start using cache
376                                $users = $h->db->get_var($query);
377                                break;
378                        default:
379                                $users = 0;
380                }
381                $h->smartCache('off'); // stop using cache
382               
383                return $users;
384        }
385       
386       
387        /**
388         * Get Unique Roles
389         *
390         * @return array|false
391         */
392        public function getUniqueRoles($h)
393        {
394                /* This function pulls all the different user roles from the database,
395                or adds some defaults if not present.*/
396               
397                $unique_roles = array();
398               
399                // Some essentials:
400                array_push($unique_roles, 'admin');
401                array_push($unique_roles, 'supermod');
402                array_push($unique_roles, 'moderator');
403                array_push($unique_roles, 'member');
404                array_push($unique_roles, 'undermod');
405                array_push($unique_roles, 'pending');
406                array_push($unique_roles, 'suspended');
407                array_push($unique_roles, 'banned');
408                array_push($unique_roles, 'killspammed');
409               
410                // Add any other roles already in use:
411                $sql = "SELECT DISTINCT user_role FROM " . TABLE_USERS;
412                $roles = $h->db->get_results($h->db->prepare($sql));
413                if ($roles) {
414                        foreach ($roles as $role) {
415                                if (!in_array($role->user_role, $unique_roles)) {
416                                        array_push($unique_roles, $role->user_role);
417                                }
418                        }
419                }
420               
421                if ($unique_roles) { return $unique_roles; } else { return false; }
422        }
423}
Note: See TracBrowser for help on using the repository browser.