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

Revision 1805, 13.5 KB checked in by shibuya246, 3 years ago (diff)

[branch 1.3] remove siteid ref

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  LIMIT 1";
52               
53                $userid = $h->db->get_var($h->db->prepare($sql, $username));
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_role = %s  LIMIT 1";
96                $role = $db->get_row($db->prepare($sql, $username, '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_role = %s ORDER BY user_username ASC";
247                        $results = $h->db->get_results($h->db->prepare($sql, $role));
248                } else {
249                        $sql = "SELECT user_id, user_username FROM " . TABLE_USERS . " ORDER BY user_username ASC";
250                        $results = $h->db->get_results($sql);
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 . " ORDER BY user_username ASC";
289                        $results = $h->db->get_results($sql);
290                } else {
291                        // for grabbing
292                        if ($range) { $limit = " LIMIT " . $start . ", " . $range; }
293                        $sql = "SELECT * FROM " . TABLE_USERS . " WHERE ";
294                        for ($i=0; $i < count($id_array); $i++) {
295                                $sql .= "user_id = %d OR ";
296                        }
297                        $sql = rstrtrim($sql, "OR "); // strip trailing OR
298                        $sql .= " ORDER BY user_username ASC" . $limit;
299               
300                        $prepare_array[0] = $sql;
301                        $prepare_array = array_merge($prepare_array, $id_array);
302                        $results = $h->db->get_results($h->db->prepare($prepare_array));
303                }
304                return $results;
305        }
306       
307       
308        /**
309         * Stats for Admin homepage
310         *
311         * @param string $stat_type
312         * @return int
313         */
314        public function stats($h, $stat_type = '')
315        {
316                switch ($stat_type) {
317                        case 'admins':
318                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_role = %s";
319                                $query = $h->db->prepare($sql, 'admin');
320                                $h->smartCache('on', 'users', 60, $query); // start using cache
321                                $users = $h->db->get_var($query);
322                                break;
323                        case 'supermods':
324                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_role = %s";
325                                $query = $h->db->prepare($sql, 'supermod');
326                                $h->smartCache('on', 'users', 60, $query); // start using cache
327                                $users = $h->db->get_var($query);
328                                break;
329                        case 'moderators':
330                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_role = %s";
331                                $query = $h->db->prepare($sql, 'moderator');
332                                $h->smartCache('on', 'users', 60, $query); // start using cache
333                                $users = $h->db->get_var($query);
334                                break;
335                        case 'members':
336                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_role = %s";
337                                $query = $h->db->prepare($sql, 'member');
338                                $h->smartCache('on', 'users', 60, $query); // start using cache
339                                $users = $h->db->get_var($query);
340                                break;
341                        case 'total_users':
342                                $query = "SELECT count(user_id) FROM " . TABLE_USERS;
343                                $h->smartCache('on', 'users', 60, $query); // start using cache
344                                $users = $h->db->get_var($query);
345                                break;
346                        case 'approved_users':
347                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_role = %s OR user_role = %s OR user_role = %s OR  user_role = %s";
348                                $query = $h->db->prepare($sql, 'admin', 'supermod', 'moderator', 'member');
349                                $h->smartCache('on', 'users', 60, $query); // start using cache
350                                $users = $h->db->get_var($query);
351                                break;
352                        case 'pending_users':
353                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_role = %s";
354                                $query = $h->db->prepare($sql, 'pending');
355                                $h->smartCache('on', 'users', 60, $query); // start using cache
356                                $users = $h->db->get_var($query);
357                                break;
358                        case 'undermod_users':
359                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_role = %s";
360                                $query = $h->db->prepare($sql, 'undermod');
361                                $h->smartCache('on', 'users', 60, $query); // start using cache
362                                $users = $h->db->get_var($query);
363                                break;
364                        case 'banned_users':
365                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_role = %s";
366                                $query = $h->db->prepare($sql, 'banned');
367                                $h->smartCache('on', 'users', 60, $query); // start using cache
368                                $users = $h->db->get_var($query);
369                                break;
370                        case 'killspammed_users':
371                                $sql = "SELECT count(user_id) FROM " . TABLE_USERS . " WHERE user_role = %s";
372                                $query = $h->db->prepare($sql, 'killspammed');
373                                $h->smartCache('on', 'users', 60, $query); // start using cache
374                                $users = $h->db->get_var($query);
375                                break;
376                        default:
377                                $users = 0;
378                }
379                $h->smartCache('off'); // stop using cache
380               
381                return $users;
382        }
383       
384       
385        /**
386         * Get Unique Roles
387         *
388         * @return array|false
389         */
390        public function getUniqueRoles($h)
391        {
392                /* This function pulls all the different user roles from the database,
393                or adds some defaults if not present.*/
394               
395                $unique_roles = array();
396               
397                // Some essentials:
398                array_push($unique_roles, 'admin');
399                array_push($unique_roles, 'supermod');
400                array_push($unique_roles, 'moderator');
401                array_push($unique_roles, 'member');
402                array_push($unique_roles, 'undermod');
403                array_push($unique_roles, 'pending');
404                array_push($unique_roles, 'suspended');
405                array_push($unique_roles, 'banned');
406                array_push($unique_roles, 'killspammed');
407               
408                // Add any other roles already in use:
409                $sql = "SELECT DISTINCT user_role FROM " . TABLE_USERS;
410                $roles = $h->db->get_results($h->db->prepare($sql));
411                if ($roles) {
412                        foreach ($roles as $role) {
413                                if (!in_array($role->user_role, $unique_roles)) {
414                                        array_push($unique_roles, $role->user_role);
415                                }
416                        }
417                }
418               
419                if ($unique_roles) { return $unique_roles; } else { return false; }
420        }
421}
Note: See TracBrowser for help on using the repository browser.