simpler database and session.
[platal.git] / classes / CoreLogger.php
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
22 /** class for logging user activity
23 *
24 */
25 class CoreLogger {
26 /** user id */
27 var $uid;
28 /** id of the session */
29 var $session;
30 /** list of available actions */
31 var $actions;
32
33 /** db table holding the list of actions */
34 var $table_actions;
35 /** db table holding the list of actions */
36 var $table_events;
37 /** db table holding the list of actions */
38 var $table_sessions;
39
40 /** The constructor, creates a new entry in the sessions table
41 *
42 * @param $uid the id of the logged user
43 * @param $suid the id of the administrator who has just su'd to the user
44 * @param $auth authentication method for the logged user
45 * @param $sauth authentication method for the su'er
46 * @return VOID
47 */
48 function CoreLogger($uid, $suid='', $auth='', $sauth='') {
49 global $globals;
50
51 // read database table names from globals
52 $this->table_actions = $globals->table_log_actions;
53 $this->table_events = $globals->table_log_events;
54 $this->table_sessions = $globals->table_log_sessions;
55
56 // write the session entry
57 $this->uid = $uid;
58 $this->session = $this->writeSession($uid, $suid, $auth, $sauth);
59
60 // retrieve available actions
61 $this->actions = $this->readActions();
62 }
63
64
65 /** Creates a new session entry in database and return its ID.
66 *
67 * @param $uid the id of the logged user
68 * @param $suid the id of the administrator who has just su'd to the user
69 * @param $auth authentication method for the logged user
70 * @param $sauth authentication method for the su'er
71 * @return session the session id
72 */
73 function writeSession($uid, $suid, $auth, $sauth) {
74 $ip = $_SERVER['REMOTE_ADDR'];
75 $host = strtolower(gethostbyaddr($_SERVER['REMOTE_ADDR']));
76 $browser = (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '');
77 $sql = "insert into {$this->table_sessions} set uid='$uid', host='$host', ip='$ip', browser='$browser'";
78 // optional parameters
79 if ($suid)
80 $sql .= ", suid='$suid'";
81 if ($auth)
82 $sql .= ", auth='$auth'";
83 if ($sauth)
84 $sql .= ", sauth='$sauth'";
85
86 XDB::execute($sql);
87
88 return XDB::insertId();
89 }
90
91
92 /** Reads available actions from database.
93 *
94 * @return actions the available actions
95 */
96 function readActions() {
97 $res = XDB::iterRow("select id, text from {$this->table_actions}");
98
99 while (list($action_id, $action_text) = $res->next()) {
100 $actions[$action_text] = $action_id;
101 }
102
103 return $actions;
104 }
105
106
107 /** Logs an action and its related data.
108 *
109 * @param $action le type d'action
110 * @param $data les données (id de liste, etc.)
111 * @return VOID
112 */
113 function log($action, $data="") {
114 if (isset($this->actions[$action])) {
115 XDB::execute("insert into {$this->table_events}
116 set session={?}, action={?}, data={?}",
117 $this->session, $this->actions[$action], $data);
118 } else {
119 echo "unknown action : $action<br />";
120 }
121 }
122 }
123
124 ?>