]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/WhoIsOnline.php
fixed explodePageList: wrong sortby argument order in UnfoldSubpages
[SourceForge/phpwiki.git] / lib / plugin / WhoIsOnline.php
1 <?php // -*-php-*-
2 rcs_id('$Id: WhoIsOnline.php,v 1.3 2004-03-12 15:48: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  * Currently only PearDB, adodb in the works.
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.3 $");
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     function getSessions($dbi, &$request) {
78         // check the current user sessions and what they are doing
79         ;
80     }
81
82     // check the current sessions
83     function getStats($dbi, &$request, $mode='summary') {
84         $num_pages = 0; $num_users = 0;
85         $page_iter = $dbi->getAllPages();
86         while ($page = $page_iter->next()) {
87             if ($page->isUserPage()) $num_users++;
88             $num_pages++;
89         }
90         //get session data from database
91         $num_online = 0; $num_guests = 0; $num_registered = 0;
92         $registered = array(); $guests = array();
93         $admins = array(); $uniquenames = array();
94         if (isset($request->_dbsession)) { // only ADODB and SQL backends
95             $dbsession = &$request->_dbsession;
96             $sessions = $dbsession->currentSessions();
97             //$num_online = count($sessions);
98             $guestname = _("Guest");
99             foreach ($sessions as $row) {
100                 $data = $row['wiki_user'];
101                 $date = $row['date'];
102                 if (empty($data)) continue;
103                 $num_online++;
104                 $user = @unserialize($data);
105                 if (!empty($user)) {
106                     if ($mode == 'summary' and in_array($user->UserName(),$uniquenames)) continue;
107                     $uniquenames[] = $user->UserName();
108                     $page = _("<unknown>");  // where is he?
109                     $action = 'browse';
110                     $objvars = array_keys(get_object_vars($user));
111                     if (in_array('action',$objvars))
112                         $action = $user->action;
113                     if (in_array('page',$objvars))
114                         $page = $user->page;
115                     if ($user->_level and $user->UserName()) { // registered or guest or what?
116                         //Todo: expose REMOTE_ADDR?
117                         //FIXME: htmlentitities name may not be called here. but where then?
118                         $num_registered++;
119                         $registered[] = array('name'  => $user->UserName(),
120                                               'date'  => $date,
121                                               'action'=> $action,
122                                               'page'  => $page,
123                                               'level' => $user->_level,
124                                               'x'     => 'x');
125                         if ($user->_level == WIKIAUTH_ADMIN)
126                            $admins[] = $registered[count($registered)-1];
127                     } else {
128                         $num_guests++;
129                         $guests[] = array('name'  => $guestname,
130                                           'date'  => $date,
131                                           'action'=> $action,
132                                           'page'  => $page,
133                                           'level' => $user->_level,
134                                           'x'     => 'x');
135                     }
136                 }
137             }
138         }
139         $num_users = $num_guests + $num_registered;
140
141         $sess_time = ini_get('session.gc_maxlifetime'); // in seconds
142
143         //TODO: get and sets max stats
144         $page = $dbi->getPage($request->getArg('pagename'));
145         $stats = array(); $stats['max_online_num'] = 0;
146         if ($stats = $page->get('stats')) {
147             if ($num_users > $stats['max_online_num']) {
148                 $stats['max_online_num'] = $num_users;
149                 $stats['max_online_time'] = time();
150                 $page->set('stats',$stats);
151             }
152         } else {
153             $stats['max_online_num'] = $num_users;
154             $stats['max_online_time'] = time();
155             $page->set('stats',$stats);
156         }
157         return array('SESSDATA_BOOL'    => !empty($dbsession),
158                      'NUM_PAGES'        => $num_pages,
159                      'NUM_USERS'        => $num_users,
160                      'NUM_ONLINE'       => $num_online,
161                      'NUM_REGISTERED'   => $num_registered,
162                      'NUM_GUESTS'       => $num_guests,
163                      'NEWEST_USER'      => '', // todo
164                      'MAX_ONLINE_NUM'   => $stats['max_online_num'],
165                      'MAX_ONLINE_TIME'  => $stats['max_online_time'],
166                      'REGISTERED'       => $registered,
167                      'ADMINS'           => $admins,
168                      'GUESTS'           => $guests,
169                      'SESSION_TIME'     => sprintf(_("%d minutes"),$sess_time / 60),
170                      );
171     }
172 };
173
174 // $Log: not supported by cvs2svn $
175 // Revision 1.2  2004/03/10 15:38:49  rurban
176 // store current user->page and ->action in session for WhoIsOnline
177 // better WhoIsOnline icon
178 // fixed WhoIsOnline warnings
179 //
180 // Revision 1.1  2004/02/26 19:15:37  rurban
181 // new WhoIsOnline plugin: session explorer (postnuke style)
182 //
183 //
184
185 // For emacs users
186 // Local Variables:
187 // mode: php
188 // tab-width: 8
189 // c-basic-offset: 4
190 // c-hanging-comment-ender-p: nil
191 // indent-tabs-mode: nil
192 // End:
193 ?>