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