]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WhoIsOnline.php
new finally reliable way to detect if /index.php is called directly
[SourceForge/phpwiki.git] / lib / plugin / WhoIsOnline.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WhoIsOnline.php,v 1.6 2004-05-02 15:10:08 rurban Exp $');
3 /*
4  Copyright 2004 $ThePhpWikiProgrammingTeam
5  
6  This file is (not yet) 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 DB_Sessions.
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.6 $");
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 $Theme;
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 = $Theme->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(WikiLink(_("WhoIsOnline"),'',_("Who is online")),
83                               fmt("%d online users",$stats['NUM_USERS']));
84     }
85
86     function getSessions($dbi, &$request) {
87         // check the current user sessions and what they are doing
88         ;
89     }
90
91     // check the current sessions
92     function getStats($dbi, &$request, $mode='summary') {
93         $num_pages = 0; $num_users = 0;
94         $page_iter = $dbi->getAllPages();
95         while ($page = $page_iter->next()) {
96             if ($page->isUserPage()) $num_users++;
97             $num_pages++;
98         }
99         //get session data from database
100         $num_online = 0; $num_guests = 0; $num_registered = 0;
101         $registered = array(); $guests = array();
102         $admins = array(); $uniquenames = array();
103         if (isset($request->_dbsession)) { // only ADODB and SQL backends
104             $dbsession = &$request->_dbsession;
105             $sessions = $dbsession->currentSessions();
106             //$num_online = count($sessions);
107             $guestname = _("Guest");
108             foreach ($sessions as $row) {
109                 $data = $row['wiki_user'];
110                 $date = $row['date'];
111                 //Todo: Security issue: Expose REMOTE_ADDR?
112                 //      Probably only to WikiAdmin
113                 $ip   = $row['ip'];  
114                 if (empty($date)) continue;
115                 $num_online++;
116                 $user = @unserialize($data);
117                 if (!empty($user)) {
118                     if ($mode == 'summary' and in_array($user->UserName(),$uniquenames)) continue;
119                     $uniquenames[] = $user->UserName();
120                     $page = _("<unknown>");  // where is he?
121                     $action = 'browse';
122                     $objvars = array_keys(get_object_vars($user));
123                     if (in_array('action',$objvars))
124                         $action = $user->action;
125                     if (in_array('page',$objvars))
126                         $page = $user->page;
127                     if ($user->_level and $user->UserName()) { // registered or guest or what?
128                         //FIXME: htmlentitities name may not be called here. but where then?
129                         $num_registered++;
130                         $registered[] = array('name'  => $user->UserName(),
131                                               'date'  => $date,
132                                               'action'=> $action,
133                                               'page'  => $page,
134                                               'level' => $user->_level,
135                                               'ip'    => $ip,
136                                               'x'     => 'x');
137                         if ($user->_level == WIKIAUTH_ADMIN)
138                            $admins[] = $registered[count($registered)-1];
139                     } else {
140                         $num_guests++;
141                         $guests[] = array('name'  => $guestname,
142                                           'date'  => $date,
143                                           'action'=> $action,
144                                           'page'  => $page,
145                                           'level' => $user->_level,
146                                           'ip'    => $ip,
147                                           'x'     => 'x');
148                     }
149                 } else {
150                     $num_guests++;
151                     $guests[] = array('name'  => $guestname,
152                                       'date'  => $date,
153                                       'action'=> '',
154                                       'page'  => '',
155                                       'level' => 0,
156                                       'ip'    => $ip,
157                                       'x'     => 'x');
158                 }
159             }
160         }
161         $num_users = $num_guests + $num_registered;
162
163         $sess_time = ini_get('session.gc_maxlifetime'); // in seconds
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 // $Log: not supported by cvs2svn $
197 // Revision 1.5  2004/04/06 20:27:05  rurban
198 // fixed guests (no wiki_user session)
199 // added ip (to help in ip-throttling)
200 //
201 // Revision 1.4  2004/03/30 02:14:04  rurban
202 // fixed yet another Prefs bug
203 // added generic PearDb_iter
204 // $request->appendValidators no so strict as before
205 // added some box plugin methods
206 // PageList commalist for condensed output
207 //
208 // Revision 1.3  2004/03/12 15:48:08  rurban
209 // fixed explodePageList: wrong sortby argument order in UnfoldSubpages
210 // simplified lib/stdlib.php:explodePageList
211 //
212 // Revision 1.2  2004/03/10 15:38:49  rurban
213 // store current user->page and ->action in session for WhoIsOnline
214 // better WhoIsOnline icon
215 // fixed WhoIsOnline warnings
216 //
217 // Revision 1.1  2004/02/26 19:15:37  rurban
218 // new WhoIsOnline plugin: session explorer (postnuke style)
219 //
220 //
221
222 // For emacs users
223 // Local Variables:
224 // mode: php
225 // tab-width: 8
226 // c-basic-offset: 4
227 // c-hanging-comment-ender-p: nil
228 // indent-tabs-mode: nil
229 // End:
230 ?>