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
39 return _("WhoIsOnline");
42 function getDescription()
44 return _("Show summary information of the current user sessions.");
47 function getDefaultArguments()
49 // two modes: summary and detail, page links to the page with the other mode
51 'mode' => 'summary', // or "detail"
52 'pagename' => '[pagename]', // refer to the page with the other mode
53 'allow_detail' => false, // if false, page is ignored
54 'dispose_admin' => false,
58 function run($dbi, $argstr, &$request, $basepage)
61 $request->setArg('nocache', 1);
62 $args = $this->getArgs($argstr, $request);
63 // use the "online.tmpl" template
64 // todo: check which arguments are really needed in the template.
65 $stats = $this->getStats($dbi, $request, $args['mode']);
66 if ($src = $WikiTheme->getImageURL("whosonline"))
67 $img = HTML::img(array('src' => $src, 'alt' => $this->getName()));
70 $other['ONLINE_ICON'] = $img;
71 return new Template('online', $request, array_merge($args, $stats, $other));
74 // box is used to display a fixed-width, narrow version with common header
75 // just the number of online users.
76 function box($args = false, $request = false, $basepage = false)
78 if (!$request) $request =& $GLOBALS['request'];
79 $stats = $this->getStats($request->_dbi, $request, 'summary');
80 return $this->makeBox(_("Who is Online"),
81 HTML(HTML::Raw('· '),
82 WikiLink(_("WhoIsOnline"), 'auto',
83 fmt("%d online users", $stats['NUM_USERS']))));
86 function getSessions($dbi, &$request)
88 // check the current user sessions and what they are doing
92 // check the current sessions
93 function getStats($dbi, &$request, $mode = 'summary')
97 $page_iter = $dbi->getAllPages();
98 while ($page = $page_iter->next()) {
99 if ($page->isUserPage()) $num_users++;
102 //get session data from database
106 $registered = array();
109 $uniquenames = array();
110 $sess_time = ini_get('session.gc_maxlifetime'); // in seconds
111 if (!$sess_time) $sess_time = 24 * 60;
112 if (isset($request->_dbsession)) { // only ADODB and SQL backends
113 $dbsession =& $request->_dbsession;
114 if (method_exists($dbsession->_backend, "gc"))
115 $dbsession->_backend->gc($sess_time);
116 $sessions = $dbsession->currentSessions();
117 //$num_online = count($sessions);
118 $guestname = _("Guest");
119 foreach ($sessions as $row) {
120 $data = $row['wiki_user'];
121 $date = $row['date'];
122 //Todo: Security issue: Expose REMOTE_ADDR?
123 // Probably only to WikiAdmin
125 if (empty($date)) continue;
127 $user = @unserialize($data);
128 if (!empty($user) and !isa($user, "__PHP_incomplete_Class")) {
129 // if "__PHP_incomplete_Class" try to avoid notice
130 $userid = @$user->_userid;
131 $level = @$user->_level;
132 if ($mode == 'summary' and in_array($userid, $uniquenames)) continue;
133 $uniquenames[] = $userid;
134 $page = _("<unknown>"); // where is he?
136 $objvars = array_keys(get_object_vars($user));
137 if (in_array('action', $objvars))
138 $action = @$user->action;
139 if (in_array('page', $objvars))
140 $page = @$user->page;
141 if ($level and $userid) { // registered or guest or what?
142 //FIXME: htmlentitities name may not be called here. but where then?
144 $registered[] = array('name' => $userid,
151 if ($user->_level == WIKIAUTH_ADMIN)
152 $admins[] = $registered[count($registered) - 1];
155 $guests[] = array('name' => $guestname,
165 $guests[] = array('name' => $guestname,
175 $num_users = $num_guests + $num_registered;
177 //TODO: get and sets max stats in global_data
178 //$page = $dbi->getPage($request->getArg('pagename'));
180 $stats['max_online_num'] = 0;
181 if ($stats = $dbi->get('stats')) {
182 if ($num_users > $stats['max_online_num']) {
183 $stats['max_online_num'] = $num_users;
184 $stats['max_online_time'] = time();
185 $dbi->set('stats', $stats);
188 $stats['max_online_num'] = $num_users;
189 $stats['max_online_time'] = time();
190 $dbi->set('stats', $stats);
192 return array('SESSDATA_BOOL' => !empty($dbsession),
193 'NUM_PAGES' => $num_pages,
194 'NUM_USERS' => $num_users,
195 'NUM_ONLINE' => $num_online,
196 'NUM_REGISTERED' => $num_registered,
197 'NUM_GUESTS' => $num_guests,
198 'NEWEST_USER' => '', // todo
199 'MAX_ONLINE_NUM' => $stats['max_online_num'],
200 'MAX_ONLINE_TIME' => $stats['max_online_time'],
201 'REGISTERED' => $registered,
204 'SESSION_TIME' => sprintf(_("%d minutes"), $sess_time / 60),
215 // c-hanging-comment-ender-p: nil
216 // indent-tabs-mode: nil