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