Give names to debug flags and add a new flag for smarty debugging:
[platal.git] / classes / corelogger.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
22 class CoreLogger
23 {
24 /** user id */
25 public $uid;
26 /** id of the session */
27 private $session;
28 /** list of available actions */
29 private $actions;
30
31 /** The constructor, creates a new entry in the sessions table
32 *
33 * @param $uid the id of the logged user
34 * @param $suid the id of the administrator who has just su'd to the user
35 * @return VOID
36 */
37 public function __construct($uid, $suid = 0)
38 {
39 // write the session entry
40 $this->uid = $uid;
41 $this->session = $this->writeSession($uid, $suid);
42
43 // retrieve available actions
44 $res = XDB::iterRow("SELECT id, text FROM logger.actions");
45
46 while (list($action_id, $action_text) = $res->next()) {
47 $this->actions[$action_text] = $action_id;
48 }
49 }
50
51 /** Creates a new session entry in database and return its ID.
52 *
53 * @param $uid the id of the logged user
54 * @param $suid the id of the administrator who has just su'd to the user
55 * @return session the session id
56 */
57 private function writeSession($uid, $suid = 0)
58 {
59 $ip = $_SERVER['REMOTE_ADDR'];
60 $host = strtolower(gethostbyaddr($_SERVER['REMOTE_ADDR']));
61 $browser = (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '');
62
63 @list($forward_ip,) = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
64 $forward_host = $forward_ip;
65 if ($forward_host) {
66 $forward_host = strtolower(gethostbyaddr($forward_host));
67 }
68 $proxy = '';
69 if ($forward_ip || @$_SERVER['HTTP_VIA']) {
70 $proxy = 'proxy';
71 }
72
73 XDB::execute("INSERT INTO logger.sessions
74 SET uid={?}, host={?}, ip={?}, forward_ip={?}, forward_host={?}, browser={?}, suid={?}, flags={?}",
75 $uid, $host, $ip, $forward_ip, $forward_host, $browser, $suid, $proxy);
76
77 return XDB::insertId();
78 }
79
80
81 /** Logs an action and its related data.
82 *
83 * @param $action le type d'action
84 * @param $data les données (id de liste, etc.)
85 * @return VOID
86 */
87 public function log($action, $data = null)
88 {
89 if (isset($this->actions[$action])) {
90 XDB::execute("INSERT INTO logger.events
91 SET session={?}, action={?}, data={?}",
92 $this->session, $this->actions[$action], $data);
93 } else {
94 trigger_error("CoreLogger: unknown action, $action", E_USER_WARNING);
95 }
96 }
97 }
98
99 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
100 ?>