]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WhoIsOnline.php
HTML::Raw --> HTML::raw
[SourceForge/phpwiki.git] / lib / plugin / WhoIsOnline.php
1 <?php
2
3 /*
4  * Copyright 2004 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 /**
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.
27  *
28  * Formatting and idea borrowed from postnuke. Requires USE_DB_SESSION.
29  * Works with PearDB, ADODB and dba DbSessions.
30  *
31  * Author: Reini Urban
32  */
33
34 class WikiPlugin_WhoIsOnline
35     extends WikiPlugin
36 {
37     function getName()
38     {
39         return _("WhoIsOnline");
40     }
41
42     function getDescription()
43     {
44         return _("Show summary information of the current user sessions.");
45     }
46
47     function getDefaultArguments()
48     {
49         // two modes: summary and detail, page links to the page with the other mode
50         return array(
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,
55         );
56     }
57
58     function run($dbi, $argstr, &$request, $basepage)
59     {
60         global $WikiTheme;
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()));
68         else $img = '';
69         $other = array();
70         $other['ONLINE_ICON'] = $img;
71         return new Template('online', $request, array_merge($args, $stats, $other));
72     }
73
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)
77     {
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('&middot; '),
82                 WikiLink(_("WhoIsOnline"), 'auto',
83                     fmt("%d online users", $stats['NUM_USERS']))));
84     }
85
86     function getSessions($dbi, &$request)
87     {
88         // check the current user sessions and what they are doing
89         ;
90     }
91
92     // check the current sessions
93     function getStats($dbi, &$request, $mode = 'summary')
94     {
95         $num_pages = 0;
96         $num_users = 0;
97         $page_iter = $dbi->getAllPages();
98         while ($page = $page_iter->next()) {
99             if ($page->isUserPage()) $num_users++;
100             $num_pages++;
101         }
102         //get session data from database
103         $num_online = 0;
104         $num_guests = 0;
105         $num_registered = 0;
106         $registered = array();
107         $guests = array();
108         $admins = 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
124                 $ip = $row['ip'];
125                 if (empty($date)) continue;
126                 $num_online++;
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?
135                     $action = 'browse';
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?
143                         $num_registered++;
144                         $registered[] = array('name' => $userid,
145                             'date' => $date,
146                             'action' => $action,
147                             'page' => $page,
148                             'level' => $level,
149                             'ip' => $ip,
150                             'x' => 'x');
151                         if ($user->_level == WIKIAUTH_ADMIN)
152                             $admins[] = $registered[count($registered) - 1];
153                     } else {
154                         $num_guests++;
155                         $guests[] = array('name' => $guestname,
156                             'date' => $date,
157                             'action' => $action,
158                             'page' => $page,
159                             'level' => $level,
160                             'ip' => $ip,
161                             'x' => 'x');
162                     }
163                 } else {
164                     $num_guests++;
165                     $guests[] = array('name' => $guestname,
166                         'date' => $date,
167                         'action' => '',
168                         'page' => '',
169                         'level' => 0,
170                         'ip' => $ip,
171                         'x' => 'x');
172                 }
173             }
174         }
175         $num_users = $num_guests + $num_registered;
176
177         //TODO: get and sets max stats in global_data
178         //$page = $dbi->getPage($request->getArg('pagename'));
179         $stats = array();
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);
186             }
187         } else {
188             $stats['max_online_num'] = $num_users;
189             $stats['max_online_time'] = time();
190             $dbi->set('stats', $stats);
191         }
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,
202             'ADMINS' => $admins,
203             'GUESTS' => $guests,
204             'SESSION_TIME' => sprintf(_("%d minutes"), $sess_time / 60),
205         );
206     }
207 }
208
209 // Local Variables:
210 // mode: php
211 // tab-width: 8
212 // c-basic-offset: 4
213 // c-hanging-comment-ender-p: nil
214 // indent-tabs-mode: nil
215 // End: