Moving to GitHub.
[platal.git] / classes / platallogger.php
CommitLineData
27caa1c8
FB
1<?php
2/*
c441aabe 3 * Copyright (C) 2003-2014 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
21class 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 48 $this->actions = XDB::fetchAllAssoc('text', 'SELECT id, text
6586a74f 49 FROM log_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 */
becfe71d 58 private function writeSession($uid, $suid = null)
27caa1c8
FB
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
becfe71d
SJ
74 $uid = ($uid == 0) ? null : $uid;
75 $suid = ($suid == 0) ? null : $suid;
6586a74f 76 XDB::execute("INSERT INTO log_sessions
7d49650a 77 SET uid={?}, host={?}, ip={?}, forward_ip={?}, forward_host={?}, browser={?}, suid={?}, flags={?}",
27caa1c8
FB
78 $uid, $host, ip_to_uint($ip), ip_to_uint($forward_ip), $forward_host, $browser, $suid, $proxy);
79 if ($forward_ip) {
80 $this->proxy_ip = $ip;
81 $this->proxy_host = $host;
82 $this->ip = $forward_ip;
83 $this->host = $forward_host;
84 } else {
85 $this->ip = $ip;
86 $this->host = $host;
87 }
88
4ea44525
FB
89 return XDB::insertId();
90 }
91
92 public function saveLastSession() {
6586a74f 93 XDB::execute('REPLACE INTO log_last_sessions (uid, id)
4ea44525
FB
94 VALUES ({?}, {?})',
95 $this->uid, $this->session);
27caa1c8
FB
96 }
97
604dfd58
FB
98 public function isValid($uid) {
99 return $uid == $this->uid;
100 }
27caa1c8
FB
101
102 /** Logs an action and its related data.
103 *
104 * @param $action le type d'action
105 * @param $data les données (id de liste, etc.)
106 * @return VOID
107 */
108 public function log($action, $data = null)
109 {
110 if (isset($this->actions[$action])) {
6586a74f 111 XDB::execute("INSERT INTO log_events
7d49650a 112 SET session={?}, action={?}, data={?}",
27caa1c8
FB
113 $this->session, $this->actions[$action], $data);
114 } else {
115 trigger_error("PlLogger: unknown action, $action", E_USER_WARNING);
116 }
117 }
118}
119
448c8cdc 120// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
27caa1c8 121?>