source: branches/1.0/content/plugins/whos_online/whos_online.php @ 981

Revision 981, 8.0 KB checked in by nick_ramsay, 3 years ago (diff)

[Branch] Fix for moving widgets into other widget blocks and switched RSS icon from left to right of RSS Show widget header.

Line 
1<?php
2/**
3 * name: Who's Online
4 * description: Show who's online
5 * version: 0.1
6 * folder: whos_online
7 * class: WhosOnline
8 * requires: widgets 0.6, users 1.1
9 * hooks: install_plugin, header_include, admin_plugin_settings, admin_sidebar_plugin_settings, userauth_checkcookie_success,
10 *
11 * PHP version 5
12 *
13 * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation, either version 3 of
16 * the License, or (at your option) any later version.
17 *
18 * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 * FITNESS FOR A PARTICULAR PURPOSE.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
24 *
25 * @category  Content Management System
26 * @package   HotaruCMS
27 * @author    Nick Ramsay <admin@hotarucms.org>
28 * @copyright Copyright (c) 2009, Hotaru CMS
29 * @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
30 * @link      http://www.hotarucms.org/
31 */
32
33class WhosOnline
34{
35     /**
36     * *********************************************************************
37     * ********************* FUNCTIONS FOR POST CLASS **********************
38     * *********************************************************************
39     * ****************************************************************** */
40     
41    /**
42     * Install widget settings
43     */
44    public function install_plugin($h)
45    {
46        $whos_online_settings = $h->getSerializedSettings();
47       
48        if (!isset($whos_online_settings['online_num'])) { $whos_online_settings['online_num'] = 10; }
49        if (!isset($whos_online_settings['online_list'])) { $whos_online_settings['online_list'] = ''; }
50        if (!isset($whos_online_settings['online_avatars'])) { $whos_online_settings['online_avatars'] = ''; }
51        if (!isset($whos_online_settings['online_avatar_size'])) { $whos_online_settings['online_avatar_size'] = '16'; }
52        if (!isset($whos_online_settings['online_names'])) { $whos_online_settings['online_names'] = 'checked'; }
53        if (!isset($whos_online_settings['online_widget_title'])) { $whos_online_settings['online_widget_title'] = 'checked'; }
54       
55        $h->updateSetting('whos_online_settings', serialize($whos_online_settings));
56
57        // widget
58        $h->addWidget('whos_online', 'whos_online', '');  // plugin name, function name, optional arguments
59    }
60   
61   
62    /**
63     * Update last activity
64     * This only keeps last activity records for the last 5 minutes and only one per user.
65     *
66     * @return bool
67     */
68    public function userauth_checkcookie_success($h)
69    {
70        if ($h->currentUser->id != 0) {
71            // delete all user activity older than 5 minutes and any activity by this user
72            $time = date('Y-m-d H:i:s', strtotime("-5 minutes"));
73            $sql = "DELETE FROM " . TABLE_USERMETA . " WHERE (usermeta_key = %s AND usermeta_value < %s) OR (usermeta_userid = %d AND usermeta_key = %s)";
74            $h->db->query($h->db->prepare($sql, 'last_activity', $time, $h->currentUser->id, 'last_activity'));
75           
76            // insert current time for this user
77            $sql = "INSERT INTO " . TABLE_USERMETA . " SET usermeta_userid = %d, usermeta_key = %s, usermeta_value = CURRENT_TIMESTAMP";
78            $h->db->query($h->db->prepare($sql, $h->currentUser->id, 'last_activity'));
79            return true;
80        } else {
81            return false;
82        }
83    }
84
85
86    /**
87     * Widget Who's Online
88     */
89    public function widget_whos_online($h)
90    {
91        $whos_online_settings = $h->getSerializedSettings('whos_online');
92        $limit = $whos_online_settings['online_num'];
93        $list = $whos_online_settings['online_list'];
94        $avatars = $whos_online_settings['online_avatars'];
95        $avatar_size = $whos_online_settings['online_avatar_size'];
96        $names = $whos_online_settings['online_names'];
97        $show_title = $whos_online_settings['online_widget_title'];
98       
99        // build the who's online:
100        $members = $this->getOnlineMembers($h, $limit);
101        $guests = $this->getOnlineGuests($h);
102       
103        if ($show_title) {
104            echo "<h2 class='widget_head widget_whos_online_title'>";
105            echo $h->lang["whos_online_widget_title"];
106            echo "</h2>\n";
107        }
108       
109        echo "<div class='widget_body widget_whos_online'>";
110       
111        echo "<div id='whos_online_counts'>" . $h->lang["whos_online_currently"] . count($members) . " " . $h->lang['whos_online_members'] . " " . $guests . " " . $h->lang['whos_online_guests'] . "</div>";
112       
113        $need_cache = false;
114        $label = 'whos_online';
115       
116        // check for a cached version and use it if no recent update:
117        $output = $h->smartCache('html', 'usermeta', 10, '', $label);
118       
119        if ($output) {
120            echo $output;
121            echo "</div>"; return true;
122        } else {
123            $output = "";
124            $need_cache = true;
125        }
126       
127        if ($list) { $output .="<ul class='whos_online_list'>\n"; }
128       
129        if ($members) {
130        foreach ($members as $member)
131            {
132                $userid = $member->usermeta_userid;
133                $username = $h->getUserNameFromId($userid);
134               
135                if ($list) {
136                    $output .="<li class='whos_online_item'>";
137                }
138               
139                if ($avatars) {
140                    $h->setAvatar($userid, $avatar_size);
141                    $output .= $h->linkAvatar();
142                }
143               
144                if ($names) {
145                    $output .="<a href='" . $h->url(array('user' => $username)) . "'>" . $username . "</a>\n";
146                }
147               
148                if ($list) { $output .="</li>"; } else { $output .="&nbsp;"; }
149            }
150        }
151       
152        if ($list) { $output .="</ul>"; }
153       
154        if ($need_cache) {
155            $h->smartCache('html', 'users', 10, $output, $label); // make or rewrite the cache file
156        }
157       
158        echo $output;
159       
160        echo "</div>\n";
161    }
162   
163   
164    /**
165     * Get Online Members
166     * Returns active members within the last 5 minutes (i.e. online now)
167     * Anything older than 5 minutes would have already been deleted in userauth_checkcookie_success()
168     *
169     * @param int $limit number of users to show
170     * @return array
171     */
172    public function getOnlineMembers($h, $limit)
173    {
174        $sql = "SELECT usermeta_userid FROM " . TABLE_USERMETA . " WHERE usermeta_key = %s ORDER BY usermeta_value DESC LIMIT " . $limit;
175        $members = $h->db->get_results($h->db->prepare($sql, 'last_activity'));
176       
177        if ($members) { return $members; } else {return false; }
178    }
179   
180   
181    /**
182     * Count online guests
183     *
184     * @link http://www.devarticles.com/c/a/PHP/The-Quickest-Way-To-Count-Users-Online-With-PHP/1/
185     * @return array
186     */
187    public function getOnlineGuests($h)
188    {
189        /* Define how long the maximum amount of time the session can be inactive. */
190        define("MAX_IDLE_TIME", 20);
191       
192        if ( $directory_handle = opendir( session_save_path() ) ) {
193            $count = 0;
194           
195            while ( false !== ( $file = readdir( $directory_handle ) ) ) {
196                if($file != '.' && $file != '..') {
197                    // Comment the 'if(...){' and '}' lines if you get a significant amount of traffic
198                    // if(time()- fileatime(session_save_path() . '\\' . $file) < MAX_IDLE_TIME * 60) {
199                        $count++;
200                    // }
201                }
202            }
203            closedir($directory_handle);
204        }
205       
206        return $count;
207    }
208
209}
210?>
Note: See TracBrowser for help on using the repository browser.