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