simplifications
[platal.git] / classes / LoggerView.php
1 <?php
2 /*
3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
5 *
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.
10 *
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.
15 *
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
19 */
20
21 class LoggerView {
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.
25 *
26 * @param integer year
27 * @param integer month
28 * @return array days in that month we have log entries covering.
29 * @private
30 */
31 function _getDays($year, $month)
32 {
33 // give a 'no filter' option
34 $months[0] = "----";
35
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();
45
46 if (($year < $ymin) || ($year == $ymin && $month < $mmin)) {
47 return array();
48 }
49
50 if (($year > $ymax) || ($year == $ymax && $month > $mmax)) {
51 return array();
52 }
53
54 $min = ($year==$ymin && $month==$mmin) ? intval($dmin) : 1;
55 $max = ($year==$ymax && $month==$mmax) ? intval($dmax) : $day_max[$month];
56
57 for($i = $min; $i<=$max; $i++) {
58 $days[$i] = $i;
59 }
60 }
61 return $days;
62 }
63
64
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.
68 *
69 * @param integer year
70 * @return array List of month numbers we have log info for.
71 * @private
72 */
73 function _getMonths($year)
74 {
75 // give a 'no filter' option
76 $months[0] = "----";
77
78 if ($year) {
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();
83
84 if (($year < $ymin) || ($year > $ymax)) {
85 return array();
86 }
87
88 $min = $year == $ymin ? intval($mmin) : 1;
89 $max = $year == $ymax ? intval($mmax) : 12;
90
91 for($i = $min; $i<=$max; $i++) {
92 $months[$i] = $i;
93 }
94 }
95 return $months;
96 }
97
98
99 /** Retrieves the username for a given authentication method and user id.
100 * This function caches the results of the lookups to avoid uncecessary
101 * database requests.
102 *
103 * @return the matching username.
104 * @private
105 */
106 function _getUsername($uid) {
107 static $cache;
108
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();
113 }
114
115 return $cache[$uid];
116 }
117
118
119 /** Retrieves the available years.
120 * Obtains a list of years that we have log entries covering.
121 *
122 * @return array years we have log entries for.
123 * @private
124 */
125 function _getYears()
126 {
127 // give a 'no filter' option
128 $years[0] = "----";
129
130 // retrieve available years
131 $res = XDB::query("select YEAR(MAX(start)), YEAR(MIN(start)) FROM logger.sessions");
132 list($max, $min) = $res->fetchOneRow();
133
134 for($i = intval($min); $i<=$max; $i++) {
135 $years[$i] = $i;
136 }
137 return $years;
138 }
139
140
141 /** Make a where clause to get a user's sessions.
142 * Prepare the where clause request that will retrieve the sessions.
143 *
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.
148 *
149 * @return STRING the WHERE clause of a query, including the 'WHERE' keyword
150 * @private
151 */
152 function _makeWhere($year, $month, $day, $uid)
153 {
154 // start constructing the "where" clause
155 $where = array();
156
157 if ($uid)
158 array_push($where, "uid='$uid'");
159
160 // we were given at least a year
161 if ($year) {
162 if ($day) {
163 $dmin = mktime(0, 0, 0, $month, $day, $year);
164 $dmax = mktime(0, 0, 0, $month, $day+1, $year);
165 } elseif ($month) {
166 $dmin = mktime(0, 0, 0, $month, 1, $year);
167 $dmax = mktime(0, 0, 0, $month+1, 1, $year);
168 } else {
169 $dmin = mktime(0, 0, 0, 1, 1, $year);
170 $dmax = mktime(0, 0, 0, 1, 1, $year+1);
171 }
172 $where[] = "start >= " . date("Ymd000000", $dmin);
173 $where[] = "start < " . date("Ymd000000", $dmax);
174 }
175
176 if (!empty($where)) {
177 return ' WHERE ' . implode($where, " AND ");
178 } else {
179 return '';
180 }
181 // WE know it's totally reversed, so better use array_reverse than a SORT BY start DESC
182 }
183
184
185 /** Run the log viewer and fill out the Smarty variables for display.
186 *
187 * @param page the page that will display the viewer's data
188 */
189 function run(&$page)
190 {
191 if (isset($_REQUEST['logsess'])) {
192
193 // we are viewing a session
194 $res = XDB::query("SELECT host, ip, browser, uid, suid
195 FROM logger.sessions
196 WHERE id =".$_REQUEST['logsess']);
197
198 $sarr = $res->fetchOneAssoc();
199
200 $sarr['username'] = $this->_getUsername($sarr['uid']);
201 if ($sarr['suid']) {
202 $sarr['suer'] = $this->_getUsername($sarr['suid']);
203 }
204 $page->assign('session', $sarr);
205
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);
212 }
213
214 } else {
215
216 // we are browsing the available sessions
217 $loguser = isset($_REQUEST['loguser']) ? $_REQUEST['loguser'] : '';
218
219 $res = XDB::query('SELECT id FROM aliases WHERE alias={?}',
220 Env::v('loguser'));
221 $loguid = $res->fetchOneCell();
222
223 if ($loguid) {
224 $year = isset($_REQUEST['year']) ? $_REQUEST['year'] : 0;
225 $month = isset($_REQUEST['month']) ? $_REQUEST['month'] : 0;
226 $day = isset($_REQUEST['day']) ? $_REQUEST['day'] : 0;
227 } else {
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");
231 }
232
233 if (!$year) $month = 0;
234 if (!$month) $day = 0;
235
236 // smarty assignments
237 // retrieve available years
238 $page->assign('years', $this->_getYears());
239 $page->assign('year', $year);
240
241 // retrieve available months for the current year
242 $page->assign('months', $this->_getMonths($year));
243 $page->assign('month', $month);
244
245 // retrieve available days for the current year and month
246 $page->assign('days', $this->_getDays($year, $month));
247 $page->assign('day', $day);
248
249 $page->assign('loguser', $loguser);
250 // smarty assignments
251
252 if ($loguid || $year) {
253
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
258 $where
259 ORDER BY start DESC";
260 $res = XDB::iterator($select);
261
262 $sessions = array();
263 while ($mysess = $res->next()) {
264 $mysess['username'] = $this->_getUsername($mysess['uid']);
265 $mysess['events'] = array();
266 $sessions[$mysess['id']] = $mysess;
267 }
268 array_reverse($sessions);
269
270 // attach events
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)
275 $where";
276
277 $res = XDB::iterator($sql);
278 while ($event = $res->next()) {
279 array_push($sessions[$event['id']]['events'], $event['text']);
280 }
281 $page->assign_by_ref('sessions', $sessions);
282 } else {
283 $page->assign('msg_nofilters', "Sélectionner une annuée et/ou un utilisateur");
284 }
285 }
286
287 $page->changeTpl('logger-view.tpl');
288 }
289
290 }
291
292 ?>