]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WhoIsOnline.php
rcs_id no longer makes sense with Subversion global version number
[SourceForge/phpwiki.git] / lib / plugin / WhoIsOnline.php
1 <?php // -*-php-*-
2 // rcs_id('$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
19  * along with PhpWiki; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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,
64                                    'alt' => $this->getName(),
65                                    'border' => 0));
66         else $img = '';
67         $other = array();
68         $other['ONLINE_ICON'] = $img;
69         return new Template('online', $request, array_merge($args, $stats, $other));
70     }
71
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('&middot; '),
79                                    WikiLink(_("WhoIsOnline"),'auto',
80                                             fmt("%d online users", $stats['NUM_USERS']))));
81     }
82
83     function getSessions($dbi, &$request) {
84         // check the current user sessions and what they are doing
85         ;
86     }
87
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++;
94             $num_pages++;
95         }
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
114                 $ip   = $row['ip'];
115                 if (empty($date)) continue;
116                 $num_online++;
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?
125                     $action = 'browse';
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?
133                         $num_registered++;
134                         $registered[] = array('name'  => $userid,
135                                               'date'  => $date,
136                                               'action'=> $action,
137                                               'page'  => $page,
138                                               'level' => $level,
139                                               'ip'    => $ip,
140                                               'x'     => 'x');
141                         if ($user->_level == WIKIAUTH_ADMIN)
142                            $admins[] = $registered[count($registered)-1];
143                     } else {
144                         $num_guests++;
145                         $guests[] = array('name'  => $guestname,
146                                           'date'  => $date,
147                                           'action'=> $action,
148                                           'page'  => $page,
149                                           'level' => $level,
150                                           'ip'    => $ip,
151                                           'x'     => 'x');
152                     }
153                 } else {
154                     $num_guests++;
155                     $guests[] = array('name'  => $guestname,
156                                       'date'  => $date,
157                                       'action'=> '',
158                                       'page'  => '',
159                                       'level' => 0,
160                                       'ip'    => $ip,
161                                       'x'     => 'x');
162                 }
163             }
164         }
165         $num_users = $num_guests + $num_registered;
166
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);
175             }
176         } else {
177             $stats['max_online_num'] = $num_users;
178             $stats['max_online_time'] = time();
179             $dbi->set('stats',$stats);
180         }
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,
191                      'ADMINS'                 => $admins,
192                      'GUESTS'           => $guests,
193                      'SESSION_TIME'         => sprintf(_("%d minutes"),$sess_time / 60),
194                      );
195     }
196 };
197
198 // For emacs users
199 // Local Variables:
200 // mode: php
201 // tab-width: 8
202 // c-basic-offset: 4
203 // c-hanging-comment-ender-p: nil
204 // indent-tabs-mode: nil
205 // End:
206 ?>