1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 /*! \brief The logging base class
24 \author Fabian Hickert <hickert@gonicus.de>
25 \version 2.6
26 \date 11.04.2007
28 This is the base class for the GOsa logging functionality.
29 All logging should lead to this class.
30 */
31 class log {
33 var $LOG_TO_MYSQL = FALSE;
34 var $LOG_TO_SYSLOG= FALSE;
35 var $config;
37 /*! \brief logging constructor
39 \param action One of these values (modify|create|remove|snapshot|copy)
40 \param objecttype represents the current edited objecttype, like users/user
41 \param object represents the current edited object dn
42 \param changes_array An array containing names of all touched attributes
43 \param result A status message, containing errors or success messages
45 \sa log()
46 */
47 function log($action,$objecttype,$object,$changes_array = array(),$result = "")
48 {
49 if(!is_array($changes_array)){
50 trigger_error("log(string,string,string,array(),bool). Forth parameter must be an array.");
51 $changes_array = array();
52 }
54 $entry = array();
55 if(!session::is_set('config')){
56 $entry['user']= "unkown";
57 }else{
59 $this->config = session::get('config');
60 $ui = get_userinfo();
61 $entry['user']= @$ui->dn;
62 }
64 /* Create string out of changes */
65 $changes ="";
66 foreach($changes_array as $str ){
67 $changes .= $str.",";
68 }
69 $changes = preg_replace("/,$/","",$changes );
71 /* Create data object */
72 $entry['timestamp'] = time();
73 $entry['action'] = $action;
74 $entry['objecttype']= $objecttype;
75 $entry['object'] = $object;
76 $entry['changes'] = $changes;
77 $entry['result'] = $result;
79 if($this->config->get_cfg_value("logging") == "" && empty($entry['user'])){
80 $entry['user'] = "unknown";
81 }
83 /* Check if all given values are valid */
84 $msgs = @log::check($entry);
85 if(count($msgs)){
86 foreach($msgs as $msg){
87 trigger_error("Logging failed, reason was: ".$msg);
88 msg_dialog::display(_("Internal error"), sprintf(_("Logging failed: %s"), $msg), ERROR_DIALOG);
89 }
91 }else{
93 if ($this->conifg->get_cfg_value("logging") == ""){
94 $this->log_into_syslog($entry);
95 }else{
97 /* Start logging for configured methods */
98 if(preg_match("/(^|,)syslog(,|$)/i",$this->config->get_cfg_value("logging"))){
99 $this->log_into_syslog($entry);
100 }
101 if(preg_match("/(^|,)mysql(,|$)/i",$this->config->get_cfg_value("logging"))){
102 $this->log_into_db($entry);
103 }
104 }
105 }
106 }
109 function check($entry = array())
110 {
111 $msgs = array();
113 if(!isset($entry['action']) || !in_array($entry['action'],array("modify","create","remove","copy","snapshot","view","security","debug"))){
114 $msgs[] = sprintf(_("Invalid option '%s' specified!"), $entry['action']);
115 }
117 if(!isset($entry['objecttype']) || empty($entry['objecttype'])){
118 $msgs[] = _("Specified objectType is empty or invalid!");
119 }
121 return($msgs);
122 }
125 /* This function is used to into the systems syslog */
126 function log_into_syslog($entry)
127 {
128 $str = $entry['user']." ".$entry['action']." ".$entry['object']." from type ".$entry['objecttype']." ".$entry['changes']." : Result was ".$entry['result'];
129 gosa_log($str);
130 }
133 function disable_mysql_log($server,$error)
134 {
135 global $config;
136 msg_dialog::display(_("Error"), $error, ERROR_DIALOG);
137 msg_dialog::display(_("MySQL error"),sprintf(_("Logging to MySQL database will be disabled for server '%s'!"),$server), INFO_DIALOG);
138 unset($config->data['SERVERS']['LOGGING'][$server]) ;
139 $this->config = $config;
140 }
143 /* Log into configured logging databses.*/
144 function log_into_db($entry)
145 {
146 if(isset($this->config->data['SERVERS']['LOGGING'])){
147 $servers = $this->config->data['SERVERS']['LOGGING'];
148 }else{
149 return(FALSE);
150 }
152 /* Log into each configured server
153 */
154 foreach($servers as $server_name => $server){
156 $error = "";
158 /* Connect to the database
159 */
160 $con = @mysql_pconnect($server_name,$server['USER'],$server['PWD']);
161 if(!$con){
162 $error = msgPool::dbconnect(_("MySQL logging"),mysql_error());
163 $this->disable_mysql_log($server_name,$error);
164 continue;
165 }
167 /* Check if the database is available
168 */
169 $db = @mysql_select_db($server['DB'],$con);
170 if(!$db){
171 $error = msgPool::dbselect(_("MySQL logging"),mysql_error());
172 $this->disable_mysql_log($server_name,$error);
173 continue;
174 }
176 /* Check if our current location is already registerd
177 in this case get its id.
178 If it wasn't registered yet, create it.
179 */
180 $base = mysql_escape_string($this->config->current['BASE']);
181 $query= "SELECT id FROM gosa_locations WHERE location=\"".$base."\";";
182 $res = mysql_query($query);
183 if(!$res){
184 $error = msgPool::dbquery(_("MySQL logging"),mysql_error());
185 $this->disable_mysql_log($server_name,$error);
186 continue;
187 }
189 $location_id = -1;
190 while($attrs = mysql_fetch_assoc($res)){
191 $location_id = $attrs['id'];
192 break;
193 }
195 /* No location found that matches our location.
196 Create it.
197 */
198 if($location_id == -1){
199 $query = "INSERT INTO gosa_locations (location) VALUES ('".$base."');";
200 if(!mysql_query($query,$con)){
201 $error = msgPool::dbquery(_("MySQL logging"),mysql_error());
202 $this->disable_mysql_log($server_name,$error);
203 continue;
204 }
206 /* Try to detect the location again
207 */
208 $query= "SELECT id FROM gosa_locations WHERE location=\"".$base."\";";
209 $res = mysql_query($query);
210 $location_id = -1;
211 while($attrs = mysql_fetch_assoc($res)){
212 $location_id = $attrs['id'];
213 break;
214 }
215 if($location_id == -1){
216 $error = sprintf(_("Cannot add location to the database!")."<br><br>"._("Error").": %s",mysql_error($con));
217 $this->disable_mysql_log($server_name,$error);
218 continue;
219 }
220 }
222 /* Create mysql syntax */
223 $query ="INSERT INTO gosa_log
224 (timestamp,user,action,objecttype,object,changes,result,location_id)
225 VALUES
226 (
227 \"".mysql_escape_string($entry['timestamp'])."\",
228 \"".mysql_escape_string($entry['user'])."\",
229 \"".mysql_escape_string($entry['action'])."\",
230 \"".mysql_escape_string($entry['objecttype'])."\",
231 \"".mysql_escape_string($entry['object'])."\",
232 \"".mysql_escape_string($entry['changes'])."\",
233 \"".mysql_escape_string($entry['result'])."\",
234 \"".mysql_escape_string($location_id)."\"
235 );
236 ";
237 $res = mysql_query($query,$con);
238 if(!$res){
239 $error = dbquery(_("MySQL logging"), mysql_error());
240 $this->disable_mysql_log($server_name,$error);
241 continue;
242 }
243 if(is_resource($con)){
244 mysql_close($con);
245 }
246 }
247 }
248 }
250 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
251 ?>