3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /** Retrieves the available days for a given year and month.
23 * Obtain a list of days of the given month in the given year
24 * that are within the range of dates that we have log entries for.
27 * @param integer month
28 * @return array days in that month we have log entries covering.
31 function _getDays($year, $month)
33 // give a 'no filter' option
36 if ($year && $month) {
37 $day_max = Array(-1, 31, checkdate(2, 29, $year) ?
29 : 28 , 31,
38 30, 31, 30, 31, 31, 30, 31, 30, 31);
39 $res = XDB
::query("SELECT YEAR (MAX(start)), YEAR (MIN(start)),
40 MONTH(MAX(start)), MONTH(MIN(start)),
41 DAYOFMONTH(MAX(start)),
42 DAYOFMONTH(MIN(start))
43 FROM logger.sessions");
44 list($ymax, $ymin, $mmax, $mmin, $dmax, $dmin) = $res->fetchOneRow();
46 if (($year < $ymin) ||
($year == $ymin && $month < $mmin)) {
50 if (($year > $ymax) ||
($year == $ymax && $month > $mmax)) {
54 $min = ($year==$ymin && $month==$mmin) ?
intval($dmin) : 1;
55 $max = ($year==$ymax && $month==$mmax) ?
intval($dmax) : $day_max[$month];
57 for($i = $min; $i<=$max; $i++
) {
65 /** Retrieves the available months for a given year.
66 * Obtains a list of month numbers that are within the timeframe that
67 * we have log entries for.
70 * @return array List of month numbers we have log info for.
73 function _getMonths($year)
75 // give a 'no filter' option
79 $res = XDB
::query("SELECT YEAR (MAX(start)), YEAR (MIN(start)),
80 MONTH(MAX(start)), MONTH(MIN(start))
81 FROM logger.sessions");
82 list($ymax, $ymin, $mmax, $mmin) = $res->fetchOneRow();
84 if (($year < $ymin) ||
($year > $ymax)) {
88 $min = $year == $ymin ?
intval($mmin) : 1;
89 $max = $year == $ymax ?
intval($mmax) : 12;
91 for($i = $min; $i<=$max; $i++
) {
99 /** Retrieves the username for a given authentication method and user id.
100 * This function caches the results of the lookups to avoid uncecessary
103 * @return the matching username.
106 function _getUsername($uid) {
109 if (!isset($cache[$uid])) {
110 $res = XDB
::query('SELECT alias FROM aliases
111 WHERE id = {?} AND type="a_vie"', $uid);
112 $cache[$uid] = $res->fetchOneCell();
119 /** Retrieves the available years.
120 * Obtains a list of years that we have log entries covering.
122 * @return array years we have log entries for.
127 // give a 'no filter' option
130 // retrieve available years
131 $res = XDB
::query("select YEAR(MAX(start)), YEAR(MIN(start)) FROM logger.sessions");
132 list($max, $min) = $res->fetchOneRow();
134 for($i = intval($min); $i<=$max; $i++
) {
141 /** Make a where clause to get a user's sessions.
142 * Prepare the where clause request that will retrieve the sessions.
144 * @param $year INTEGER Only get log entries made during the given year.
145 * @param $month INTEGER Only get log entries made during the given month.
146 * @param $day INTEGER Only get log entries made during the given day.
147 * @param $uid INTEGER Only get log entries referring to the given user ID.
149 * @return STRING the WHERE clause of a query, including the 'WHERE' keyword
152 function _makeWhere($year, $month, $day, $uid)
154 // start constructing the "where" clause
158 array_push($where, "uid='$uid'");
160 // we were given at least a year
163 $dmin = mktime(0, 0, 0, $month, $day, $year);
164 $dmax = mktime(0, 0, 0, $month, $day+
1, $year);
166 $dmin = mktime(0, 0, 0, $month, 1, $year);
167 $dmax = mktime(0, 0, 0, $month+
1, 1, $year);
169 $dmin = mktime(0, 0, 0, 1, 1, $year);
170 $dmax = mktime(0, 0, 0, 1, 1, $year+
1);
172 $where[] = "start >= " . date("Ymd000000", $dmin);
173 $where[] = "start < " . date("Ymd000000", $dmax);
176 if (!empty($where)) {
177 return ' WHERE ' . implode($where, " AND ");
181 // WE know it's totally reversed, so better use array_reverse than a SORT BY start DESC
185 /** Run the log viewer and fill out the Smarty variables for display.
187 * @param page the page that will display the viewer's data
191 if (isset($_REQUEST['logsess'])) {
193 // we are viewing a session
194 $res = XDB
::query("SELECT host, ip, browser, uid, suid
196 WHERE id =".$_REQUEST['logsess']);
198 $sarr = $res->fetchOneAssoc();
200 $sarr['username'] = $this->_getUsername($sarr['uid']);
202 $sarr['suer'] = $this->_getUsername($sarr['suid']);
204 $page->assign('session', $sarr);
206 $res = XDB
::iterator("SELECT a.text, e.data, UNIX_TIMESTAMP(e.stamp) AS stamp
207 FROM logger.events AS e
208 LEFT JOIN logger.actions AS a ON e.action=a.id
209 WHERE e.session='{$_REQUEST['logsess']}'");
210 while ($myarr = $res->next()) {
211 $page->append('events', $myarr);
216 // we are browsing the available sessions
217 $loguser = isset($_REQUEST['loguser']) ?
$_REQUEST['loguser'] : '';
219 $res = XDB
::query('SELECT id FROM aliases WHERE alias={?}',
221 $loguid = $res->fetchOneCell();
224 $year = isset($_REQUEST['year']) ?
$_REQUEST['year'] : 0;
225 $month = isset($_REQUEST['month']) ?
$_REQUEST['month'] : 0;
226 $day = isset($_REQUEST['day']) ?
$_REQUEST['day'] : 0;
228 $year = isset($_REQUEST['year']) ?
$_REQUEST['year'] : date("Y");
229 $month = isset($_REQUEST['month']) ?
$_REQUEST['month'] : date("m");
230 $day = isset($_REQUEST['day']) ?
$_REQUEST['day'] : date("d");
233 if (!$year) $month = 0;
234 if (!$month) $day = 0;
236 // smarty assignments
237 // retrieve available years
238 $page->assign('years', $this->_getYears());
239 $page->assign('year', $year);
241 // retrieve available months for the current year
242 $page->assign('months', $this->_getMonths($year));
243 $page->assign('month', $month);
245 // retrieve available days for the current year and month
246 $page->assign('days', $this->_getDays($year, $month));
247 $page->assign('day', $day);
249 $page->assign('loguser', $loguser);
250 // smarty assignments
252 if ($loguid ||
$year) {
254 // get the requested sessions
255 $where = $this->_makeWhere($year, $month, $day, $loguid);
256 $select = "SELECT id, UNIX_TIMESTAMP(start) as start, uid
257 FROM logger.sessions AS s
259 ORDER BY start DESC";
260 $res = XDB
::iterator($select);
263 while ($mysess = $res->next()) {
264 $mysess['username'] = $this->_getUsername($mysess['uid']);
265 $mysess['events'] = array();
266 $sessions[$mysess['id']] = $mysess;
268 array_reverse($sessions);
271 $sql = "SELECT s.id, a.text
272 FROM logger.sessions AS s
273 LEFT JOIN logger.events AS e ON(e.session=s.id)
274 INNER JOIN logger.actions AS a ON(a.id=e.action)
277 $res = XDB
::iterator($sql);
278 while ($event = $res->next()) {
279 array_push($sessions[$event['id']]['events'], $event['text']);
281 $page->assign_by_ref('sessions', $sessions);
283 $page->assign('msg_nofilters', "Sélectionner une annuée et/ou un utilisateur");
287 $page->changeTpl('logger-view.tpl');