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 | */ |
ef83dab9 |
55 | function writeSession($uid, $suid = null) { |
c4271d38 |
56 | $ip = $_SERVER['REMOTE_ADDR']; |
57 | $host = strtolower(gethostbyaddr($_SERVER['REMOTE_ADDR'])); |
58 | $browser = (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''); |
c4271d38 |
59 | |
ef83dab9 |
60 | XDB::execute("INSERT INTO logger.sessions |
61 | SET uid={?}, host={?}, ip={?}, browser={?}, suid={?}", |
62 | $uid, $Host, $ip, $browser, $suid); |
c4271d38 |
63 | |
64 | return XDB::insertId(); |
65 | } |
66 | |
67 | |
c4271d38 |
68 | /** Logs an action and its related data. |
69 | * |
70 | * @param $action le type d'action |
71 | * @param $data les données (id de liste, etc.) |
72 | * @return VOID |
73 | */ |
ef83dab9 |
74 | function log($action, $data = null) { |
c4271d38 |
75 | if (isset($this->actions[$action])) { |
ef83dab9 |
76 | XDB::execute("INSERT INTO logger.events |
77 | SET session={?}, action={?}, data={?}", |
c4271d38 |
78 | $this->session, $this->actions[$action], $data); |
79 | } else { |
80 | echo "unknown action : $action<br />"; |
81 | } |
82 | } |
83 | } |
84 | |
85 | ?> |