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