]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WhoIsOnline.php
better box layout
[SourceForge/phpwiki.git] / lib / plugin / WhoIsOnline.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WhoIsOnline.php,v 1.11 2005-02-02 19:39:42 rurban Exp $');
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 getVersion() {
46         return preg_replace("/[Revision: $]/", '',
47                             "\$Revision: 1.11 $");
48     }
49
50     function getDefaultArguments() {
51         // two modes: summary and detail, page links to the page with the other mode
52         return array(
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,
57                      );
58     }
59
60     function run($dbi, $argstr, &$request, $basepage) {
61         global $WikiTheme;
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(),
70                                    'border' => 0));
71         else $img = '';
72         $other = array(); 
73         $other['ONLINE_ICON'] = $img;
74         return new Template('online', $request, array_merge($args, $stats, $other));
75     }
76
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('&middot; '),
84                                    WikiLink(_("WhoIsOnline"),'auto',
85                                             fmt("%d online users", $stats['NUM_USERS']))));
86     }
87
88     function getSessions($dbi, &$request) {
89         // check the current user sessions and what they are doing
90         ;
91     }
92
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++;
99             $num_pages++;
100         }
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         if (isset($request->_dbsession)) { // only ADODB and SQL backends
106             $dbsession = &$request->_dbsession;
107             $sessions = $dbsession->currentSessions();
108             //$num_online = count($sessions);
109             $guestname = _("Guest");
110             foreach ($sessions as $row) {
111                 $data = $row['wiki_user'];
112                 $date = $row['date'];
113                 //Todo: Security issue: Expose REMOTE_ADDR?
114                 //      Probably only to WikiAdmin
115                 $ip   = $row['ip'];  
116                 if (empty($date)) continue;
117                 $num_online++;
118                 $user = @unserialize($data);
119                 if (!empty($user) and !isa($user, "__PHP_incomplete_Class")) {
120                     // if "__PHP_incomplete_Class" try to avoid notice
121                     $userid = @$user->_userid;
122                     $level = @$user->_level;
123                     if ($mode == 'summary' and in_array($userid, $uniquenames)) continue;
124                     $uniquenames[] = $userid;
125                     $page = _("<unknown>");  // where is he?
126                     $action = 'browse';
127                     $objvars = array_keys(get_object_vars($user));
128                     if (in_array('action',$objvars))
129                         $action = @$user->action;
130                     if (in_array('page',$objvars))
131                         $page = @$user->page;
132                     if ($level and $userid) { // registered or guest or what?
133                         //FIXME: htmlentitities name may not be called here. but where then?
134                         $num_registered++;
135                         $registered[] = array('name'  => $userid,
136                                               'date'  => $date,
137                                               'action'=> $action,
138                                               'page'  => $page,
139                                               'level' => $level,
140                                               'ip'    => $ip,
141                                               'x'     => 'x');
142                         if ($user->_level == WIKIAUTH_ADMIN)
143                            $admins[] = $registered[count($registered)-1];
144                     } else {
145                         $num_guests++;
146                         $guests[] = array('name'  => $guestname,
147                                           'date'  => $date,
148                                           'action'=> $action,
149                                           'page'  => $page,
150                                           'level' => $level,
151                                           'ip'    => $ip,
152                                           'x'     => 'x');
153                     }
154                 } else {
155                     $num_guests++;
156                     $guests[] = array('name'  => $guestname,
157                                       'date'  => $date,
158                                       'action'=> '',
159                                       'page'  => '',
160                                       'level' => 0,
161                                       'ip'    => $ip,
162                                       'x'     => 'x');
163                 }
164             }
165         }
166         $num_users = $num_guests + $num_registered;
167
168         $sess_time = ini_get('session.gc_maxlifetime'); // in seconds
169
170         //TODO: get and sets max stats in global_data
171         //$page = $dbi->getPage($request->getArg('pagename'));
172         $stats = array(); $stats['max_online_num'] = 0;
173         if ($stats = $dbi->get('stats')) {
174             if ($num_users > $stats['max_online_num']) {
175                 $stats['max_online_num'] = $num_users;
176                 $stats['max_online_time'] = time();
177                 $dbi->set('stats',$stats);
178             }
179         } else {
180             $stats['max_online_num'] = $num_users;
181             $stats['max_online_time'] = time();
182             $dbi->set('stats',$stats);
183         }
184         return array('SESSDATA_BOOL'    => !empty($dbsession),
185                      'NUM_PAGES'        => $num_pages,
186                      'NUM_USERS'        => $num_users,
187                      'NUM_ONLINE'       => $num_online,
188                      'NUM_REGISTERED'   => $num_registered,
189                      'NUM_GUESTS'       => $num_guests,
190                      'NEWEST_USER'      => '', // todo
191                      'MAX_ONLINE_NUM'   => $stats['max_online_num'],
192                      'MAX_ONLINE_TIME'  => $stats['max_online_time'],
193                      'REGISTERED'       => $registered,
194                      'ADMINS'           => $admins,
195                      'GUESTS'           => $guests,
196                      'SESSION_TIME'     => sprintf(_("%d minutes"),$sess_time / 60),
197                      );
198     }
199 };
200
201 // $Log: not supported by cvs2svn $
202 // Revision 1.10  2005/02/01 16:22:58  rurban
203 // avoid __PHP_incomplete_Class notice
204 //
205 // Revision 1.9  2004/12/18 17:04:24  rurban
206 // stabilize not to call UserName() of an incomplete (not loaded) object
207 //
208 // Revision 1.8  2004/06/14 11:31:39  rurban
209 // renamed global $Theme to $WikiTheme (gforge nameclash)
210 // inherit PageList default options from PageList
211 //   default sortby=pagename
212 // use options in PageList_Selectable (limit, sortby, ...)
213 // added action revert, with button at action=diff
214 // added option regex to WikiAdminSearchReplace
215 //
216 // Revision 1.7  2004/05/27 17:49:06  rurban
217 // renamed DB_Session to DbSession (in CVS also)
218 // added WikiDB->getParam and WikiDB->getAuthParam method to get rid of globals
219 // remove leading slash in error message
220 // added force_unlock parameter to File_Passwd (no return on stale locks)
221 // fixed adodb session AffectedRows
222 // added FileFinder helpers to unify local filenames and DATA_PATH names
223 // editpage.php: new edit toolbar javascript on ENABLE_EDIT_TOOLBAR
224 //
225 // Revision 1.6  2004/05/02 15:10:08  rurban
226 // new finally reliable way to detect if /index.php is called directly
227 //   and if to include lib/main.php
228 // new global AllActionPages
229 // SetupWiki now loads all mandatory pages: HOME_PAGE, action pages, and warns if not.
230 // WikiTranslation what=buttons for Carsten to create the missing MacOSX buttons
231 // PageGroupTestOne => subpages
232 // renamed PhpWikiRss to PhpWikiRecentChanges
233 // more docs, default configs, ...
234 //
235 // Revision 1.5  2004/04/06 20:27:05  rurban
236 // fixed guests (no wiki_user session)
237 // added ip (to help in ip-throttling)
238 //
239 // Revision 1.4  2004/03/30 02:14:04  rurban
240 // fixed yet another Prefs bug
241 // added generic PearDb_iter
242 // $request->appendValidators no so strict as before
243 // added some box plugin methods
244 // PageList commalist for condensed output
245 //
246 // Revision 1.3  2004/03/12 15:48:08  rurban
247 // fixed explodePageList: wrong sortby argument order in UnfoldSubpages
248 // simplified lib/stdlib.php:explodePageList
249 //
250 // Revision 1.2  2004/03/10 15:38:49  rurban
251 // store current user->page and ->action in session for WhoIsOnline
252 // better WhoIsOnline icon
253 // fixed WhoIsOnline warnings
254 //
255 // Revision 1.1  2004/02/26 19:15:37  rurban
256 // new WhoIsOnline plugin: session explorer (postnuke style)
257 //
258 //
259
260 // For emacs users
261 // Local Variables:
262 // mode: php
263 // tab-width: 8
264 // c-basic-offset: 4
265 // c-hanging-comment-ender-p: nil
266 // indent-tabs-mode: nil
267 // End:
268 ?>