Store IP as uint instead of strings.
[platal.git] / classes / corelogger.php
CommitLineData
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
9ffe0e77 22class CoreLogger
23{
c4271d38 24 /** user id */
670b0ac0 25 public $uid;
c4271d38 26 /** id of the session */
9ffe0e77 27 private $session;
c4271d38 28 /** list of available actions */
9ffe0e77 29 private $actions;
c4271d38 30
20b71450 31 public $ip;
32 public $host;
33 public $proxy_ip;
34 public $proxy_host;
35
c4271d38 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
c4271d38 40 * @return VOID
41 */
155819d9 42 public function __construct($uid, $suid = 0)
9ffe0e77 43 {
c4271d38 44 // write the session entry
45 $this->uid = $uid;
441e9e98 46 $this->session = $this->writeSession($uid, $suid);
c4271d38 47
48 // retrieve available actions
ef83dab9 49 $res = XDB::iterRow("SELECT id, text FROM logger.actions");
c4271d38 50
ef83dab9 51 while (list($action_id, $action_text) = $res->next()) {
52 $this->actions[$action_text] = $action_id;
53 }
54 }
c4271d38 55
56 /** Creates a new session entry in database and return its ID.
eaf30d86 57 *
c4271d38 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
c4271d38 60 * @return session the session id
61 */
155819d9 62 private function writeSession($uid, $suid = 0)
e76421d8 63 {
c4271d38 64 $ip = $_SERVER['REMOTE_ADDR'];
65 $host = strtolower(gethostbyaddr($_SERVER['REMOTE_ADDR']));
66 $browser = (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '');
c4271d38 67
e76421d8 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
ef83dab9 78 XDB::execute("INSERT INTO logger.sessions
9ffe0e77 79 SET uid={?}, host={?}, ip={?}, forward_ip={?}, forward_host={?}, browser={?}, suid={?}, flags={?}",
9797734d 80 $uid, $host, ip_to_uint($ip), ip_to_uint($forward_ip), $forward_host, $browser, $suid, $proxy);
20b71450 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 }
c4271d38 90
91 return XDB::insertId();
92 }
93
94
c4271d38 95 /** Logs an action and its related data.
96 *
97 * @param $action le type d'action
a7de4ef7 98 * @param $data les données (id de liste, etc.)
c4271d38 99 * @return VOID
100 */
9ffe0e77 101 public function log($action, $data = null)
102 {
c4271d38 103 if (isset($this->actions[$action])) {
ef83dab9 104 XDB::execute("INSERT INTO logger.events
9ffe0e77 105 SET session={?}, action={?}, data={?}",
c4271d38 106 $this->session, $this->actions[$action], $data);
107 } else {
9ffe0e77 108 trigger_error("CoreLogger: unknown action, $action", E_USER_WARNING);
c4271d38 109 }
110 }
111}
112
a7de4ef7 113// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
c4271d38 114?>