]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WhoIsOnline.php
getName should not translate
[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 getDescription()
38     {
39         return _("Show summary information of the current user sessions.");
40     }
41
42     function getDefaultArguments()
43     {
44         // two modes: summary and detail, page links to the page with the other mode
45         return array(
46             'mode' => 'summary', // or "detail"
47             'pagename' => '[pagename]', // refer to the page with the other mode
48             'allow_detail' => false, // if false, page is ignored
49             'dispose_admin' => false,
50         );
51     }
52
53     function run($dbi, $argstr, &$request, $basepage)
54     {
55         global $WikiTheme;
56         $request->setArg('nocache', 1);
57         $args = $this->getArgs($argstr, $request);
58         // use the "online.tmpl" template
59         // todo: check which arguments are really needed in the template.
60         $stats = $this->getStats($dbi, $request, $args['mode']);
61         if ($src = $WikiTheme->getImageURL("whosonline"))
62             $img = HTML::img(array('src' => $src, 'alt' => $this->getName()));
63         else $img = '';
64         $other = array();
65         $other['ONLINE_ICON'] = $img;
66         return new Template('online', $request, array_merge($args, $stats, $other));
67     }
68
69     // box is used to display a fixed-width, narrow version with common header
70     // just the number of online users.
71     function box($args = false, $request = false, $basepage = false)
72     {
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('&middot; '),
77                 WikiLink(_("WhoIsOnline"), 'auto',
78                     fmt("%d online users", $stats['NUM_USERS']))));
79     }
80
81     function getSessions($dbi, &$request)
82     {
83         // check the current user sessions and what they are doing
84         ;
85     }
86
87     // check the current sessions
88     function getStats($dbi, &$request, $mode = 'summary')
89     {
90         $num_pages = 0;
91         $num_users = 0;
92         $page_iter = $dbi->getAllPages();
93         while ($page = $page_iter->next()) {
94             if ($page->isUserPage()) $num_users++;
95             $num_pages++;
96         }
97         //get session data from database
98         $num_online = 0;
99         $num_guests = 0;
100         $num_registered = 0;
101         $registered = array();
102         $guests = array();
103         $admins = array();
104         $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
119                 $ip = $row['ip'];
120                 if (empty($date)) continue;
121                 $num_online++;
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?
130                     $action = 'browse';
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?
138                         $num_registered++;
139                         $registered[] = array('name' => $userid,
140                             'date' => $date,
141                             'action' => $action,
142                             'page' => $page,
143                             'level' => $level,
144                             'ip' => $ip,
145                             'x' => 'x');
146                         if ($user->_level == WIKIAUTH_ADMIN)
147                             $admins[] = $registered[count($registered) - 1];
148                     } else {
149                         $num_guests++;
150                         $guests[] = array('name' => $guestname,
151                             'date' => $date,
152                             'action' => $action,
153                             'page' => $page,
154                             'level' => $level,
155                             'ip' => $ip,
156                             'x' => 'x');
157                     }
158                 } else {
159                     $num_guests++;
160                     $guests[] = array('name' => $guestname,
161                         'date' => $date,
162                         'action' => '',
163                         'page' => '',
164                         'level' => 0,
165                         'ip' => $ip,
166                         'x' => 'x');
167                 }
168             }
169         }
170         $num_users = $num_guests + $num_registered;
171
172         //TODO: get and sets max stats in global_data
173         //$page = $dbi->getPage($request->getArg('pagename'));
174         $stats = array();
175         $stats['max_online_num'] = 0;
176         if ($stats = $dbi->get('stats')) {
177             if ($num_users > $stats['max_online_num']) {
178                 $stats['max_online_num'] = $num_users;
179                 $stats['max_online_time'] = time();
180                 $dbi->set('stats', $stats);
181             }
182         } else {
183             $stats['max_online_num'] = $num_users;
184             $stats['max_online_time'] = time();
185             $dbi->set('stats', $stats);
186         }
187         return array('SESSDATA_BOOL' => !empty($dbsession),
188             'NUM_PAGES' => $num_pages,
189             'NUM_USERS' => $num_users,
190             'NUM_ONLINE' => $num_online,
191             'NUM_REGISTERED' => $num_registered,
192             'NUM_GUESTS' => $num_guests,
193             'NEWEST_USER' => '', // todo
194             'MAX_ONLINE_NUM' => $stats['max_online_num'],
195             'MAX_ONLINE_TIME' => $stats['max_online_time'],
196             'REGISTERED' => $registered,
197             'ADMINS' => $admins,
198             'GUESTS' => $guests,
199             'SESSION_TIME' => sprintf(_("%d minutes"), $sess_time / 60),
200         );
201     }
202 }
203
204 // Local Variables:
205 // mode: php
206 // tab-width: 8
207 // c-basic-offset: 4
208 // c-hanging-comment-ender-p: nil
209 // indent-tabs-mode: nil
210 // End: