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
19 * along with PhpWiki; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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,
64 'alt' => $this->getName(),
68 $other['ONLINE_ICON'] = $img;
69 return new Template('online', $request, array_merge($args, $stats, $other));
72 // box is used to display a fixed-width, narrow version with common header
73 // just the number of online users.
74 function box($args=false, $request=false, $basepage=false) {
75 if (!$request) $request =& $GLOBALS['request'];
76 $stats = $this->getStats($request->_dbi,$request,'summary');
77 return $this->makeBox(_("Who is online"),
78 HTML(HTML::Raw('· '),
79 WikiLink(_("WhoIsOnline"),'auto',
80 fmt("%d online users", $stats['NUM_USERS']))));
83 function getSessions($dbi, &$request) {
84 // check the current user sessions and what they are doing
88 // check the current sessions
89 function getStats($dbi, &$request, $mode='summary') {
90 $num_pages = 0; $num_users = 0;
91 $page_iter = $dbi->getAllPages();
92 while ($page = $page_iter->next()) {
93 if ($page->isUserPage()) $num_users++;
96 //get session data from database
97 $num_online = 0; $num_guests = 0; $num_registered = 0;
98 $registered = array(); $guests = array();
99 $admins = array(); $uniquenames = array();
100 $sess_time = ini_get('session.gc_maxlifetime'); // in seconds
101 if (!$sess_time) $sess_time = 24*60;
102 if (isset($request->_dbsession)) { // only ADODB and SQL backends
103 $dbsession =& $request->_dbsession;
104 if (method_exists($dbsession->_backend, "gc"))
105 $dbsession->_backend->gc($sess_time);
106 $sessions = $dbsession->currentSessions();
107 //$num_online = count($sessions);
108 $guestname = _("Guest");
109 foreach ($sessions as $row) {
110 $data = $row['wiki_user'];
111 $date = $row['date'];
112 //Todo: Security issue: Expose REMOTE_ADDR?
113 // Probably only to WikiAdmin
115 if (empty($date)) continue;
117 $user = @unserialize($data);
118 if (!empty($user) and !isa($user, "__PHP_incomplete_Class")) {
119 // if "__PHP_incomplete_Class" try to avoid notice
120 $userid = @$user->_userid;
121 $level = @$user->_level;
122 if ($mode == 'summary' and in_array($userid, $uniquenames)) continue;
123 $uniquenames[] = $userid;
124 $page = _("<unknown>"); // where is he?
126 $objvars = array_keys(get_object_vars($user));
127 if (in_array('action',$objvars))
128 $action = @$user->action;
129 if (in_array('page',$objvars))
130 $page = @$user->page;
131 if ($level and $userid) { // registered or guest or what?
132 //FIXME: htmlentitities name may not be called here. but where then?
134 $registered[] = array('name' => $userid,
141 if ($user->_level == WIKIAUTH_ADMIN)
142 $admins[] = $registered[count($registered)-1];
145 $guests[] = array('name' => $guestname,
155 $guests[] = array('name' => $guestname,
165 $num_users = $num_guests + $num_registered;
167 //TODO: get and sets max stats in global_data
168 //$page = $dbi->getPage($request->getArg('pagename'));
169 $stats = array(); $stats['max_online_num'] = 0;
170 if ($stats = $dbi->get('stats')) {
171 if ($num_users > $stats['max_online_num']) {
172 $stats['max_online_num'] = $num_users;
173 $stats['max_online_time'] = time();
174 $dbi->set('stats',$stats);
177 $stats['max_online_num'] = $num_users;
178 $stats['max_online_time'] = time();
179 $dbi->set('stats',$stats);
181 return array('SESSDATA_BOOL' => !empty($dbsession),
182 'NUM_PAGES' => $num_pages,
183 'NUM_USERS' => $num_users,
184 'NUM_ONLINE' => $num_online,
185 'NUM_REGISTERED' => $num_registered,
186 'NUM_GUESTS' => $num_guests,
187 'NEWEST_USER' => '', // todo
188 'MAX_ONLINE_NUM' => $stats['max_online_num'],
189 'MAX_ONLINE_TIME' => $stats['max_online_time'],
190 'REGISTERED' => $registered,
193 'SESSION_TIME' => sprintf(_("%d minutes"),$sess_time / 60),
203 // c-hanging-comment-ender-p: nil
204 // indent-tabs-mode: nil