HotaruCMS Extensions is an open source project powered by Assembla

Assembla offers free public and private SVN/Git repositories and project hosting with bug/issue tracking and collaboration tools.

HotaruExtensions

/pluginsusers
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<?php
/**
 * name: Users
 * description: Provides profile, settings and permission pages
 * version: 2.2
 * folder: users
 * type: users
 * class: Users
 * hooks: pagehandling_getpagename, theme_index_top, header_include, bookmarking_functions_preparelist, breadcrumbs, theme_index_main, users_edit_profile_save, user_settings_save, admin_theme_main_stats, header_meta, post_rss_feed
 * author: Nick Ramsay
 * authorurl: http://hotarucms.org/member.php?1-Nick
 *
 * PHP version 5
 *
 * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU General Public License along
 * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
 *
 * @category  Content Management System
 * @package   HotaruCMS
 * @author    Nick Ramsay <admin@hotarucms.org>
 * @copyright Copyright (c) 2009, Hotaru CMS
 * @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
 * @link      http://www.hotarucms.org/
 */

class Users
{
    /**
     * Check if we're looking at a user page
     */
    public function pagehandling_getpagename($h, $query_vars)
    {
        // we already know that there's no "page" parameter, so...
        if ($h->cage->get->keyExists('user')) {
            return 'profile'; // sets $h->pageName to "profile"
        }
    }


    /**
     * Determine what page we're looking at
     */
    public function theme_index_top($h)
    {
        $user = $h->cage->get->testUsername('user');
        if ($user) {
            $h->subPage = 'user';
        }

        switch ($h->pageName)
        {
            case 'profile':
                $h->pageTitle = $h->lang["users_profile"] . '[delimiter]' . $user;
                $h->pageType = 'user';
                break;
            case 'account':
                $h->pageTitle = $h->lang["users_account"] . '[delimiter]' . $user;
                $h->pageType = 'user';
                break;
            case 'edit-profile':
                $h->pageTitle = $h->lang["users_profile_edit"] . '[delimiter]' . $user;
                $h->pageType = 'user';
                break;
            case 'user-settings':
                $h->pageTitle = $h->lang["users_settings"] . '[delimiter]' . $user;
                $h->pageType = 'user';
                break;
            case 'permissions':
                if (!$user) { // when the permissions form is submitted
                    $userid = $h->cage->post->testInt('userid');
                    $user = $h->getUserNameFromId($userid);
                }
                $h->pageTitle = $h->lang["users_permissions"] . '[delimiter]' . $user;
                $h->pageType = 'user';
                break;
            case 'popular':
                if ($h->subPage == 'user') { $h->pageTitle = $h->lang["bookmarking_top"] . '[delimiter]' . $user . '[delimiter]' . SITE_NAME; }
                break;
            case 'latest':
                if ($h->subPage == 'user') { $h->pageTitle = $h->lang["bookmarking_latest"] . '[delimiter]' . $user; }
                break;
            case 'upcoming':
                if ($h->subPage == 'user') { $h->pageTitle = $h->lang["bookmarking_upcoming"] . '[delimiter]' . $user; }
                break;
            case 'all':
                if ($h->subPage == 'user') { $h->pageTitle = $h->lang["bookmarking_all"] . '[delimiter]' . $user; }
                break;
            case 'sort':
                if ($h->subPage == 'user') {
                    $sort = $h->cage->get->testPage('sort');
                    $sort_lang = 'bookmarking_' . str_replace('-', '_', $sort);
                    $h->pageTitle = $h->lang[$sort_lang] . '[delimiter]' . $user;
                }
                break;
        }

        // read this user into the global hotaru object for later use on this page
        if ($h->pageType != 'user' && $h->subPage != 'user') { return false; }

        $h->vars['user'] = new UserAuth();
        if ($user) {
            $result = $h->vars['user']->getUser($h, 0, $user);
        } else {
            // when the account page has been submitted (get id in case username has changed)
            $userid = $h->cage->post->testInt('userid');
            if ($userid) {
                $result = $h->vars['user']->getUser($h, $userid);
            } else {
                $result = $h->vars['user']->getUser($h, $h->currentUser->id); // default to self
            }
        }

        if (isset($result)) {
            $h->vars['profile'] = $h->vars['user']->getProfileSettingsData($h, 'user_profile');
            $h->vars['settings'] = $h->vars['user']->getProfileSettingsData($h, 'user_settings');
        } else {
            $h->pageTitle = $h->lang["main_theme_page_not_found"];
            $h->pageType = '';
            $h->vars['user'] = false;
        }

        /* check for account updates */
        if ($h->pageName == 'account') {
            $h->vars['checks'] = $h->vars['user']->updateAccount($h);
            $h->vars['user']->name = $h->vars['checks']['username_check'];
            $h->pageTitle = $h->lang["users_account"] . '[delimiter]' . $h->vars['user']->name;
            $h->pageType = 'user';
        }
    }


    /**
     * Match meta tags when browsing results for individual users
     */
    public function header_meta($h)
    {
        if ($h->pageName == 'profile') {
            if (isset($h->vars['profile']['bio']) && ($h->vars['profile']['bio'] != $h->lang['users_profile_default_bio'])) {
                echo '<meta name="description" content="' . $h->vars['profile']['bio'] . '" />' . "\n";
            } else {
                echo '<meta name="description" content="' . $h->lang['users_default_meta_description_before'] . $h->vars['user']->name . $h->lang['users_default_meta_description_after'] . '" />' . "\n";  // default profile meta description (see language file)
            }

            echo '<meta name="keywords" content="' . $h->vars['user']->name . $h->lang['users_profile_meta_keywords_more'] . '" />' . "\n";  // default profile meta keywords (see language file)

            return true;
        }


        if ($h->subPage == 'user' && ($h->pageName != 'profile'))
        {
            $user = $h->cage->get->testUsername('user');
            if ($user) {
                $first_word = $h->pageName;
                if ($first_word == 'sort') { $first_word = $h->cage->get->testPage('sort'); }
                if ($first_word == 'index') { $first_word = $h->lang['users_meta_description_popular']; }
                $first_word = ucfirst(strtolower(make_name($first_word, '-')));
                echo '<meta name="description" content="' . $h->lang['users_meta_description_results_before'] . $first_word . $h->lang['users_meta_description_results_middle'] . $user . $h->lang['users_meta_description_results_after'] . '" />' . "\n";
                echo '<meta name="keywords" content="' . $user . $h->lang['users_profile_meta_keywords_more'] . '" />' . "\n";  // default profile meta keywords (see language file)
                return true;
            }
        }
    }



    /**
     * Filter posts to this user
     */
    public function bookmarking_functions_preparelist($h)
    {
        $username = $h->cage->get->testUsername('user');
        if ($username) {
            $h->vars['filter']['post_author = %d'] = $h->getUserIdFromName($username);
            unset($h->vars['filter']['post_archived = %s']);
        }
    }


    /**
     * Replace the default breadcrumbs in specific circumstances
     */
    public function breadcrumbs($h)
    {
        if (isset($h->vars['user']) && $h->vars['user']->name) {
            $userlink = "<a href='" . $h->url(array('user'=>$h->vars['user']->name)) . "'>";
            $userlink .= $h->vars['user']->name . "</a>";
        } else {
            return false;
        }

        // This is for user pages, e.g. account, edit profile, etc:
        switch ($h->pageName)
        {
            case 'profile':
                $crumbs = $userlink . ' &raquo; ' . $h->lang["users_profile"];
                return $crumbs;
                break;
            case 'account':
                $crumbs = $userlink . ' &raquo; ' . $h->lang["users_account"];
                return $crumbs;
                break;
            case 'edit-profile':
                $crumbs = $userlink . ' &raquo; ' . $h->lang["users_profile_edit"];
                return $crumbs;
                break;
            case 'user-settings':
                $crumbs = $userlink . ' &raquo; ' . $h->lang["users_settings"];
                return $crumbs;
                break;
            case 'permissions':
                $crumbs = $userlink . ' &raquo; ' . $h->lang["users_permissions"];
                return $crumbs;
                break;
        }

        // This is used for filtered story pages, e.g. popular, latest, etc:
        if ($h->subPage == 'user' && $h->pageType == 'list') {
            switch ($h->pageName) {
                case 'index':
                    $title = $h->lang["bookmarking_top"];
                    break;
                case 'latest':
                    $title = $h->lang["bookmarking_latest"];
                    break;
                case 'upcoming':
                    $title = $h->lang["bookmarking_upcoming"];
                    break;
                case 'all':
                    $title = $h->lang["bookmarking_all"];
                    break;
                case 'sort':
                    $sort = $h->cage->get->testPage('sort');
                    $sort_lang = 'bookmarking_' . str_replace('-', '_', $sort);
                    $title = $h->lang[$sort_lang];
                    break;
                default:
                    $title = $h->lang['users_posts'];
                    break;
            }

            $user = $h->cage->get->testUsername('user');
            $crumbs = "<a href='" . $h->url(array('user'=>$user)) . "'>\n";
            $crumbs .= $user . "</a>\n ";
            $crumbs .= " &raquo; " . $title . "&nbsp;";

            return $crumbs . $h->rssBreadcrumbsLink('', array('user'=>$user));
        }
    }


    /**
     * Display the right page
     */
    public function theme_index_main($h)
    {
        if ($h->pageType != 'user') { return false; }

        // if user doesn't exist
        if (!$h->vars['user']->name) { return false; }
        if ($h->userExists(0, $h->vars['user']->name) == 'no') { return false; }

        // determine permissions
        $admin = false; $own = false; $denied = false;
        if ($h->currentUser->getPermission('can_access_admin') == 'yes') { $admin = true; }
        if ($h->currentUser->id == $h->vars['user']->id) { $own = true; }

        $h->displayTemplate('users_navigation');

        switch($h->pageName) {
            case 'profile':
                $h->displayTemplate('users_profile');
                return true;
                break;
            case 'account':
                if (!$admin && !$own) { $denied = true; break; }
                $h->displayTemplate('users_account');
                return true;
                break;
            case 'edit-profile':
                if (!$admin && !$own) { $denied = true; break; }
                $h->displayTemplate('users_edit_profile');
                return true;
                break;
            case 'user-settings':
                if (!$admin && !$own) { $denied = true; break; }
                $h->displayTemplate('users_settings');
                return true;
                break;
            case 'permissions':
                if (!$admin) { $denied = true; break; }
                $this->editPermissions($h);
                $h->displayTemplate('users_permissions');
                return true;
                break;
        }

        if ($denied) {
            $h->messages[$h->lang["main_access_denied"]] = 'red';
            $h->showMessages();
        }
    }


    /**
     * Save profile data (from hook in edit_profile.php)
     */
    public function users_edit_profile_save($h, $vars)
    {
        $username = $vars[0];
        $profile = $vars[1];

        // check CSRF key
        if (!$h->csrf()) {
            $h->message = $h->lang['error_csrf'];
            $h->messageType = "red";
            return false;
        }

        $h->vars['user']->saveProfileSettingsData($h, $profile, 'user_profile', $h->vars['user']->id);

        /*  Problem! The previous profile data is cached and we don't want to disable caching for profiles,
            nor do we want to clear the entire db_cache, so instead, we'll delete the cache file that holds
            the previous profile for this user. */
        $sql = "SELECT usermeta_value FROM " . DB_PREFIX . "usermeta WHERE usermeta_userid = %d AND usermeta_key = %s";
        $query = $h->db->prepare($sql, $h->vars['user']->id, 'user_profile');
        $cache_file = CACHE . 'db_cache/' . md5($query) . '.php';
        if (file_exists($cache_file)) {
            unlink($cache_file); // delete cache file.
        }

        $h->message = $h->lang["users_profile_edit_saved"] . "<br />\n";
        $h->message .= "<a href='" . $h->url(array('user'=>$h->vars['user']->name)) . "'>";
        $h->message .= $h->lang["users_profile_edit_view_profile"] . "</a>\n";
        $h->messageType = "green";
    }


    /**
     * Save settings data (from hook in user_settings.php)
     */
    public function user_settings_save($h, $vars)
    {
        $username = $vars[0];
        $settings = $vars[1];

        // check CSRF key
        if (!$h->csrf()) {
            $h->message = $h->lang['error_csrf'];
            $h->messageType = "red";
            return false;
        }

        $h->vars['user']->saveProfileSettingsData($h, $settings, 'user_settings', $h->vars['user']->id);

        /*  Problem! The previous settings data is cached and we don't want to disable caching for settings,
            nor do we want to clear the entire db_cache, so instead, we'll delete the cache file that holds
            the previous settings for this user. */
        $sql = "SELECT usermeta_value FROM " . DB_PREFIX . "usermeta WHERE usermeta_userid = %d AND usermeta_key = %s";
        $query = $h->db->prepare($sql, $h->vars['user']->id, 'user_settings');
        $cache_file = CACHE . 'db_cache/' . md5($query) . '.php';
        if (file_exists($cache_file)) {
            unlink($cache_file); // delete cache file.
        }

        $h->message = $h->lang["users_settings_saved"] . "<br />\n";
        $h->messageType = "green";
    }


    /**
     * Enable admins to edit a user
     */
    public function editPermissions($h)
    {
        // prevent non-admin user viewing permissions of admin user
        if (($h->vars['user']->role) == 'admin' && ($h->currentUser->role != 'admin')) {
            $h->messages[$h->lang["users_account_admin_admin"]] = 'red';
            $h->showMessages();
            return true;
        }

        $perm_options = $h->getDefaultPermissions('', 'site', true);
        $perms = $h->vars['user']->getAllPermissions();

        // If the form has been submitted...
        if ($h->cage->post->keyExists('permissions')) {

            // check CSRF key
            if (!$h->csrf()) {
                $h->messages[$h->lang['error_csrf']] = 'red';
                return false;
            }

           foreach ($perm_options as $key => $options) {
                if ($value = $h->cage->post->testAlnumLines($key)) {
                    $h->vars['user']->setPermission($key, $value);
                }
            }

            $h->vars['user']->updatePermissions($h);   // physically store changes in the database

            // get the newly updated latest permissions:
            $perm_options = $h->getDefaultPermissions('', 'site', true);
            $perms = $h->vars['user']->getAllPermissions();
            $h->messages[$h->lang['users_permissions_updated']] = 'green';
        }

        $h->vars['perm_options'] = '';
        foreach ($perm_options as $key => $options) {
            $h->vars['perm_options'] .= "<tr><td>" . make_name($key) . ": </td>\n";
            foreach($options as $value) {
                if (isset($perms[$key]) && ($perms[$key] == $value)) { $checked = 'checked'; } else { $checked = ''; }
                if ($key == 'can_access_admin' && $h->vars['user']->role == 'admin') { $disabled = 'disabled'; } else { $disabled = ''; }
                $h->vars['perm_options'] .= "<td><input type='radio' name='" . $key . "' value='" . $value . "' " . $checked . " " . $disabled . "> " . $value . " &nbsp;</td>\n";
            }
            $h->vars['perm_options'] .= "</tr>";
        }
    }


    /**
     * Show stats on Admin home page
     */
    public function admin_theme_main_stats($h, $vars)
    {
        $ui = new UserInfo();
        $stats = $ui->stats($h);

        //var_dump($stats);

        echo "<li>&nbsp;</li>";
        if ($stats) {
            foreach ($stats as $stat) {
            //var_dump($stat);
            $users[$stat[0]] = $stat[1];
            }
        }

        if (isset($vars) && (!empty($vars))) {
            foreach ($vars as $key => $value) {
                $key_lang = 'users_admin_stats_' . $key;
                echo "<li class='title'>" . $h->lang[$key_lang] . "</li>";
                foreach ($value as $stat_type) {
                    if (isset($value) && !empty($value)) {

                        switch ($stat_type) {
                            case 'all':
                            $user_count = array_sum($users);
                            break;
                            default:
                            if (isset($users[$stat_type])) { $user_count = $users[$stat_type]; } else { $user_count = 0; }
                            break;
                        }

                        if (!defined('SITEURL')) { define('SITEURL', BASEURL); }

                        $link = "";
                        $dontlink = array('');
                        if ($h->isActive('user_manager')) {
                            if (!in_array($stat_type, $dontlink)) {
                            $link = SITEURL . "admin_index.php?user_filter=$stat_type&plugin=user_manager&page=plugin_settings&type=filter&csrf=" . $h->csrfToken;
                            }
                        }

                        $lang_name = 'users_admin_stats_' . $stat_type;
                        echo "<li>";
                        if ($link) { echo "<a href='" . $link . "'>"; }
                        echo $h->lang[$lang_name] . ": " . $user_count;
                        if ($link) { echo "</a>"; }
                        echo "</li>";
                    }
                }
            }
        }
    }


    /**
     * If a user feed, set it up
     */
    public function post_rss_feed($h)
    {
        $user = $h->cage->get->testUsername('user');
        if (!$user) { return false; }

        $user_id = $h->getUserIdFromName($user);
        if ($user_id) { $h->vars['postRssFilter']['post_author = %d'] = $user_id; }
        $h->vars['postRssFeed']['description'] = $h->lang["post_rss_from_user"] . " " . $user;
    }
}

?>
Ajax-loader Loading, please wait...