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 getVersion() {
46 return preg_replace("/[Revision: $]/", '',
50 function getDefaultArguments() {
51 // two modes: summary and detail, page links to the page with the other mode
53 'mode' => 'summary', // or "detail"
54 'pagename' => '[pagename]', // refer to the page with the other mode
55 'allow_detail' => false, // if false, page is ignored
56 'dispose_admin' => false,
60 function run($dbi, $argstr, &$request, $basepage) {
62 $request->setArg('nocache',1);
63 $args = $this->getArgs($argstr, $request);
64 // use the "online.tmpl" template
65 // todo: check which arguments are really needed in the template.
66 $stats = $this->getStats($dbi,$request,$args['mode']);
67 if ($src = $WikiTheme->getImageURL("whosonline"))
68 $img = HTML::img(array('src' => $src,
69 'alt' => $this->getName(),
73 $other['ONLINE_ICON'] = $img;
74 return new Template('online', $request, array_merge($args, $stats, $other));
77 // box is used to display a fixed-width, narrow version with common header
78 // just the number of online users.
79 function box($args=false, $request=false, $basepage=false) {
80 if (!$request) $request =& $GLOBALS['request'];
81 $stats = $this->getStats($request->_dbi,$request,'summary');
82 return $this->makeBox(_("Who is online"),
83 HTML(HTML::Raw('· '),
84 WikiLink(_("WhoIsOnline"),'auto',
85 fmt("%d online users", $stats['NUM_USERS']))));
88 function getSessions($dbi, &$request) {
89 // check the current user sessions and what they are doing
93 // check the current sessions
94 function getStats($dbi, &$request, $mode='summary') {
95 $num_pages = 0; $num_users = 0;
96 $page_iter = $dbi->getAllPages();
97 while ($page = $page_iter->next()) {
98 if ($page->isUserPage()) $num_users++;
101 //get session data from database
102 $num_online = 0; $num_guests = 0; $num_registered = 0;
103 $registered = array(); $guests = array();
104 $admins = array(); $uniquenames = array();
105 $sess_time = ini_get('session.gc_maxlifetime'); // in seconds
106 if (!$sess_time) $sess_time = 24*60;
107 if (isset($request->_dbsession)) { // only ADODB and SQL backends
108 $dbsession =& $request->_dbsession;
109 if (method_exists($dbsession->_backend, "gc"))
110 $dbsession->_backend->gc($sess_time);
111 $sessions = $dbsession->currentSessions();
112 //$num_online = count($sessions);
113 $guestname = _("Guest");
114 foreach ($sessions as $row) {
115 $data = $row['wiki_user'];
116 $date = $row['date'];
117 //Todo: Security issue: Expose REMOTE_ADDR?
118 // Probably only to WikiAdmin
120 if (empty($date)) continue;
122 $user = @unserialize($data);
123 if (!empty($user) and !isa($user, "__PHP_incomplete_Class")) {
124 // if "__PHP_incomplete_Class" try to avoid notice
125 $userid = @$user->_userid;
126 $level = @$user->_level;
127 if ($mode == 'summary' and in_array($userid, $uniquenames)) continue;
128 $uniquenames[] = $userid;
129 $page = _("<unknown>"); // where is he?
131 $objvars = array_keys(get_object_vars($user));
132 if (in_array('action',$objvars))
133 $action = @$user->action;
134 if (in_array('page',$objvars))
135 $page = @$user->page;
136 if ($level and $userid) { // registered or guest or what?
137 //FIXME: htmlentitities name may not be called here. but where then?
139 $registered[] = array('name' => $userid,
146 if ($user->_level == WIKIAUTH_ADMIN)
147 $admins[] = $registered[count($registered)-1];
150 $guests[] = array('name' => $guestname,
160 $guests[] = array('name' => $guestname,
170 $num_users = $num_guests + $num_registered;
172 //TODO: get and sets max stats in global_data
173 //$page = $dbi->getPage($request->getArg('pagename'));
174 $stats = array(); $stats['max_online_num'] = 0;
175 if ($stats = $dbi->get('stats')) {
176 if ($num_users > $stats['max_online_num']) {
177 $stats['max_online_num'] = $num_users;
178 $stats['max_online_time'] = time();
179 $dbi->set('stats',$stats);
182 $stats['max_online_num'] = $num_users;
183 $stats['max_online_time'] = time();
184 $dbi->set('stats',$stats);
186 return array('SESSDATA_BOOL' => !empty($dbsession),
187 'NUM_PAGES' => $num_pages,
188 'NUM_USERS' => $num_users,
189 'NUM_ONLINE' => $num_online,
190 'NUM_REGISTERED' => $num_registered,
191 'NUM_GUESTS' => $num_guests,
192 'NEWEST_USER' => '', // todo
193 'MAX_ONLINE_NUM' => $stats['max_online_num'],
194 'MAX_ONLINE_TIME' => $stats['max_online_time'],
195 'REGISTERED' => $registered,
198 'SESSION_TIME' => sprintf(_("%d minutes"),$sess_time / 60),
208 // c-hanging-comment-ender-p: nil
209 // indent-tabs-mode: nil