8021676a75fa4f51a2b5b24cf817d6a1deffb516
[diogenes.git] / include / diogenes / diogenes.core.logger.inc.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 DiogenesCoreLogger {
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 DiogenesCoreLogger($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 global $globals;
75
76 $ip = $_SERVER['REMOTE_ADDR'];
77 $host = strtolower(gethostbyaddr($_SERVER['REMOTE_ADDR']));
78 $browser = (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '');
79 $sql = "insert into {$this->table_sessions} set uid='$uid',host='$host',ip='$ip',browser='$browser'";
80 // optional parameters
81 if ($suid)
82 $sql .= ",suid='$suid'";
83 if ($auth)
84 $sql .= ",auth='$auth'";
85 if ($sauth)
86 $sql .= ",sauth='$sauth'";
87
88 $globals->db->query($sql);
89
90 return $globals->db->insert_id();
91 }
92
93
94 /** Reads available actions from database.
95 *
96 * @return actions the available actions
97 */
98 function readActions() {
99 global $globals;
100
101 $res=$globals->db->query("select id,text from {$this->table_actions}");
102 while(list($action_id,$action_text)=mysql_fetch_row($res))
103 $actions[$action_text] = $action_id;
104
105 mysql_free_result($res);
106
107 return $actions;
108 }
109
110
111 /** Logs an action and its related data.
112 *
113 * @param $action le type d'action
114 * @param $data les données (id de liste, etc.)
115 * @return VOID
116 */
117 function log($action,$data="") {
118 global $globals;
119
120 if (isset($this->actions[$action]))
121 $globals->db->query("insert into {$this->table_events} set session='{$this->session}',action='{$this->actions[$action]}',data='{$data}'");
122 else
123 echo "unknown action : $action<br />";
124 }
125
126 /** Print out the id for the current session
127 * @return VOID
128 */
129 function debug() {
130 echo "session=".$this->session."<br />";
131 echo "uid=".$this->uid."<br />";
132 print_r($this->actions);
133 echo "<br />";
134 }
135 }
136
137 ?>