4 * Copyright 2004 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Show summary information of the current user sessions.
25 * We support two modes: summary and detail. The optional page argument
26 * links to the page with the other mode.
28 * Formatting and idea borrowed from postnuke. Requires USE_DB_SESSION.
29 * Works with PearDB, ADODB and dba DbSessions.
34 class WikiPlugin_WhoIsOnline
38 return _("WhoIsOnline");
41 function getDescription () {
42 return _("Show summary information of the current user sessions.");
45 function getDefaultArguments() {
46 // two modes: summary and detail, page links to the page with the other mode
48 'mode' => 'summary', // or "detail"
49 'pagename' => '[pagename]', // refer to the page with the other mode
50 'allow_detail' => false, // if false, page is ignored
51 'dispose_admin' => false,
55 function run($dbi, $argstr, &$request, $basepage) {
57 $request->setArg('nocache',1);
58 $args = $this->getArgs($argstr, $request);
59 // use the "online.tmpl" template
60 // todo: check which arguments are really needed in the template.
61 $stats = $this->getStats($dbi,$request,$args['mode']);
62 if ($src = $WikiTheme->getImageURL("whosonline"))
63 $img = HTML::img(array('src' => $src, 'alt' => $this->getName()));
66 $other['ONLINE_ICON'] = $img;
67 return new Template('online', $request, array_merge($args, $stats, $other));
70 // box is used to display a fixed-width, narrow version with common header
71 // just the number of online users.
72 function box($args=false, $request=false, $basepage=false) {
73 if (!$request) $request =& $GLOBALS['request'];
74 $stats = $this->getStats($request->_dbi,$request,'summary');
75 return $this->makeBox(_("Who is Online"),
76 HTML(HTML::Raw('· '),
77 WikiLink(_("WhoIsOnline"),'auto',
78 fmt("%d online users", $stats['NUM_USERS']))));
81 function getSessions($dbi, &$request) {
82 // check the current user sessions and what they are doing
86 // check the current sessions
87 function getStats($dbi, &$request, $mode='summary') {
88 $num_pages = 0; $num_users = 0;
89 $page_iter = $dbi->getAllPages();
90 while ($page = $page_iter->next()) {
91 if ($page->isUserPage()) $num_users++;
94 //get session data from database
95 $num_online = 0; $num_guests = 0; $num_registered = 0;
96 $registered = array(); $guests = array();
97 $admins = array(); $uniquenames = array();
98 $sess_time = ini_get('session.gc_maxlifetime'); // in seconds
99 if (!$sess_time) $sess_time = 24*60;
100 if (isset($request->_dbsession)) { // only ADODB and SQL backends
101 $dbsession =& $request->_dbsession;
102 if (method_exists($dbsession->_backend, "gc"))
103 $dbsession->_backend->gc($sess_time);
104 $sessions = $dbsession->currentSessions();
105 //$num_online = count($sessions);
106 $guestname = _("Guest");
107 foreach ($sessions as $row) {
108 $data = $row['wiki_user'];
109 $date = $row['date'];
110 //Todo: Security issue: Expose REMOTE_ADDR?
111 // Probably only to WikiAdmin
113 if (empty($date)) continue;
115 $user = @unserialize($data);
116 if (!empty($user) and !isa($user, "__PHP_incomplete_Class")) {
117 // if "__PHP_incomplete_Class" try to avoid notice
118 $userid = @$user->_userid;
119 $level = @$user->_level;
120 if ($mode == 'summary' and in_array($userid, $uniquenames)) continue;
121 $uniquenames[] = $userid;
122 $page = _("<unknown>"); // where is he?
124 $objvars = array_keys(get_object_vars($user));
125 if (in_array('action',$objvars))
126 $action = @$user->action;
127 if (in_array('page',$objvars))
128 $page = @$user->page;
129 if ($level and $userid) { // registered or guest or what?
130 //FIXME: htmlentitities name may not be called here. but where then?
132 $registered[] = array('name' => $userid,
139 if ($user->_level == WIKIAUTH_ADMIN)
140 $admins[] = $registered[count($registered)-1];
143 $guests[] = array('name' => $guestname,
153 $guests[] = array('name' => $guestname,
163 $num_users = $num_guests + $num_registered;
165 //TODO: get and sets max stats in global_data
166 //$page = $dbi->getPage($request->getArg('pagename'));
167 $stats = array(); $stats['max_online_num'] = 0;
168 if ($stats = $dbi->get('stats')) {
169 if ($num_users > $stats['max_online_num']) {
170 $stats['max_online_num'] = $num_users;
171 $stats['max_online_time'] = time();
172 $dbi->set('stats',$stats);
175 $stats['max_online_num'] = $num_users;
176 $stats['max_online_time'] = time();
177 $dbi->set('stats',$stats);
179 return array('SESSDATA_BOOL' => !empty($dbsession),
180 'NUM_PAGES' => $num_pages,
181 'NUM_USERS' => $num_users,
182 'NUM_ONLINE' => $num_online,
183 'NUM_REGISTERED' => $num_registered,
184 'NUM_GUESTS' => $num_guests,
185 'NEWEST_USER' => '', // todo
186 'MAX_ONLINE_NUM' => $stats['max_online_num'],
187 'MAX_ONLINE_TIME' => $stats['max_online_time'],
188 'REGISTERED' => $registered,
191 'SESSION_TIME' => sprintf(_("%d minutes"),$sess_time / 60),
200 // c-hanging-comment-ender-p: nil
201 // indent-tabs-mode: nil