]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WhoIsOnline.php
Fix case to be consistent
[SourceForge/phpwiki.git] / lib / plugin / WhoIsOnline.php
1 <?php // -*-php-*-
2 // $Id$
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         return _("WhoIsOnline");
39     }
40
41     function getDescription () {
42         return _("Show summary information of the current user sessions.");
43     }
44
45     function getDefaultArguments() {
46         // two modes: summary and detail, page links to the page with the other mode
47         return array(
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,
52                      );
53     }
54
55     function run($dbi, $argstr, &$request, $basepage) {
56         global $WikiTheme;
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, 'alt' => $this->getName()));
64         else $img = '';
65         $other = array();
66         $other['ONLINE_ICON'] = $img;
67         return new Template('online', $request, array_merge($args, $stats, $other));
68     }
69
70     // box is used to display a fixed-width, narrow version with common header
71     // just the number of online users.
72     function box($args=false, $request=false, $basepage=false) {
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         // check the current user sessions and what they are doing
83         ;
84     }
85
86     // check the current sessions
87     function getStats($dbi, &$request, $mode='summary') {
88         $num_pages = 0; $num_users = 0;
89         $page_iter = $dbi->getAllPages();
90         while ($page = $page_iter->next()) {
91             if ($page->isUserPage()) $num_users++;
92             $num_pages++;
93         }
94         //get session data from database
95         $num_online = 0; $num_guests = 0; $num_registered = 0;
96         $registered = array(); $guests = array();
97         $admins = array(); $uniquenames = array();
98         $sess_time = ini_get('session.gc_maxlifetime'); // in seconds
99         if (!$sess_time) $sess_time = 24*60;
100         if (isset($request->_dbsession)) { // only ADODB and SQL backends
101             $dbsession =& $request->_dbsession;
102             if (method_exists($dbsession->_backend, "gc"))
103                 $dbsession->_backend->gc($sess_time);
104             $sessions = $dbsession->currentSessions();
105             //$num_online = count($sessions);
106             $guestname = _("Guest");
107             foreach ($sessions as $row) {
108                 $data = $row['wiki_user'];
109                 $date = $row['date'];
110                 //Todo: Security issue: Expose REMOTE_ADDR?
111                 //      Probably only to WikiAdmin
112                 $ip   = $row['ip'];
113                 if (empty($date)) continue;
114                 $num_online++;
115                 $user = @unserialize($data);
116                 if (!empty($user) and !isa($user, "__PHP_incomplete_Class")) {
117                     // if "__PHP_incomplete_Class" try to avoid notice
118                     $userid = @$user->_userid;
119                     $level = @$user->_level;
120                     if ($mode == 'summary' and in_array($userid, $uniquenames)) continue;
121                     $uniquenames[] = $userid;
122                     $page = _("<unknown>");  // where is he?
123                     $action = 'browse';
124                     $objvars = array_keys(get_object_vars($user));
125                     if (in_array('action',$objvars))
126                         $action = @$user->action;
127                     if (in_array('page',$objvars))
128                         $page = @$user->page;
129                     if ($level and $userid) { // registered or guest or what?
130                         //FIXME: htmlentitities name may not be called here. but where then?
131                         $num_registered++;
132                         $registered[] = array('name'  => $userid,
133                                               'date'  => $date,
134                                               'action'=> $action,
135                                               'page'  => $page,
136                                               'level' => $level,
137                                               'ip'    => $ip,
138                                               'x'     => 'x');
139                         if ($user->_level == WIKIAUTH_ADMIN)
140                            $admins[] = $registered[count($registered)-1];
141                     } else {
142                         $num_guests++;
143                         $guests[] = array('name'  => $guestname,
144                                           'date'  => $date,
145                                           'action'=> $action,
146                                           'page'  => $page,
147                                           'level' => $level,
148                                           'ip'    => $ip,
149                                           'x'     => 'x');
150                     }
151                 } else {
152                     $num_guests++;
153                     $guests[] = array('name'  => $guestname,
154                                       'date'  => $date,
155                                       'action'=> '',
156                                       'page'  => '',
157                                       'level' => 0,
158                                       'ip'    => $ip,
159                                       'x'     => 'x');
160                 }
161             }
162         }
163         $num_users = $num_guests + $num_registered;
164
165         //TODO: get and sets max stats in global_data
166         //$page = $dbi->getPage($request->getArg('pagename'));
167         $stats = array(); $stats['max_online_num'] = 0;
168         if ($stats = $dbi->get('stats')) {
169             if ($num_users > $stats['max_online_num']) {
170                 $stats['max_online_num'] = $num_users;
171                 $stats['max_online_time'] = time();
172                 $dbi->set('stats',$stats);
173             }
174         } else {
175             $stats['max_online_num'] = $num_users;
176             $stats['max_online_time'] = time();
177             $dbi->set('stats',$stats);
178         }
179         return array('SESSDATA_BOOL'    => !empty($dbsession),
180                      'NUM_PAGES'         => $num_pages,
181                      'NUM_USERS'          => $num_users,
182                      'NUM_ONLINE'         => $num_online,
183                      'NUM_REGISTERED'         => $num_registered,
184                      'NUM_GUESTS'        => $num_guests,
185                      'NEWEST_USER'         => '', // todo
186                      'MAX_ONLINE_NUM'         => $stats['max_online_num'],
187                      'MAX_ONLINE_TIME'         => $stats['max_online_time'],
188                      'REGISTERED'         => $registered,
189                      'ADMINS'                 => $admins,
190                      'GUESTS'           => $guests,
191                      'SESSION_TIME'         => sprintf(_("%d minutes"),$sess_time / 60),
192                      );
193     }
194 };
195
196 // Local Variables:
197 // mode: php
198 // tab-width: 8
199 // c-basic-offset: 4
200 // c-hanging-comment-ender-p: nil
201 // indent-tabs-mode: nil
202 // End:
203 ?>