Code

fdebaaf80d0177c8cac64496de4c3c728b9b8fa7
[gosa.git] / gosa-core / include / class_log.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
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.
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.
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  */
22 /*! \brief   The logging base class
23   \author  Fabian Hickert <hickert@gonicus.de>
24   \version 2.6
25   \date    11.04.2007
27   This is the base class for the GOsa logging functionality.
28   All logging should lead to this class. 
29  */
30 class log {
32   var $LOG_TO_MYSQL = FALSE;
33   var $LOG_TO_SYSLOG= FALSE;
34   var $config;
36  /*! \brief logging constructor
38     \param action         One of these values (modify|create|remove|snapshot|copy)
39     \param objecttype     represents the current edited objecttype, like users/user
40     \param object         represents the current edited object dn
41     \param changes_array  An array containing names of all touched attributes
42     \param result         A status message, containing errors or success messages 
44     \sa log()
45    */
46   function log($action,$objecttype,$object,$changes_array = array(),$result = "")
47   {
48     if(!is_array($changes_array)){
49       trigger_error("log(string,string,string,array(),bool). Forth parameter must be an array.");
50       $changes_array = array();
51     }
53     $entry = array(); 
54     if(!session::is_set('config')){
55       $entry['user']= "unkown";
56     }else{
58       $this->config = session::get('config');
59       $ui           = get_userinfo(); 
60       $entry['user']= @$ui->dn;
61     }
63     /* Create string out of changes */
64     $changes  ="";
65     foreach($changes_array as $str ){
66       $changes .= $str.",";
67     }
68     $changes = preg_replace("/,$/","",$changes );
69     
70     /* Create data object */
71     $entry['timestamp'] = time();
72     $entry['action']    = $action;
73     $entry['objecttype']= $objecttype;
74     $entry['object']    = $object;
75     $entry['changes']   = $changes;
76     $entry['result']    = $result;
78     if(!isset($this->config->current['LOGGING']) && empty($entry['user'])){
79       $entry['user']  = "unknown";
80     }
81  
82     /* Check if all given values are valid */
83     $msgs = @log::check($entry);
84     if(count($msgs)){
85       foreach($msgs as $msg){
86         trigger_error("Logging failed, reason was: ".$msg);
87         msg_dialog::display(_("Internal error"), sprintf(_("Logging failed: %s"), $msg), ERROR_DIALOG);
88       }
89       
90     }else{
92       if(!isset($this->config->current['LOGGING'])){
93         $this->log_into_syslog($entry);
94       }else{
96         /* Start logging for configured methods */
97         if(isset($this->config->current['LOGGING']) && preg_match("/(^|,)syslog(,|$)/i",$this->config->current['LOGGING'])){
98           $this->log_into_syslog($entry);
99         }
100         if(isset($this->config->current['LOGGING']) && preg_match("/(^|,)mysql(,|$)/i",$this->config->current['LOGGING'])){
101           $res = $this->log_into_db($entry);
103           if(!$res){
104             global $config;
105             $config->current['LOGGING'] = preg_replace("/(^|,)mysql(,|$)/",",",$config->current['LOGGING']);
106             msg_dialog::display(_("Logging to MySQL disabled"),_("The logging to a MySQL database is now disabled for this session of GOsa, due to communication errors with the specified logging database."), INFO_DIALOG);
107           }
108         }
109       }
110     }
111   }
114   function check($entry = array())
115   {
116     $msgs = array();
118     if(!isset($entry['action']) || !in_array($entry['action'],array("modify","create","remove","copy","snapshot","view","security","debug"))){
119       $msgs[] = sprintf(_("Invalid option '%s' specified."), $entry['action']);
120     }
122     if(!isset($entry['objecttype']) || empty($entry['objecttype'])){
123       $msgs[] = _("Specified objectType is empty or invalid");
124     }
125   
126     return($msgs);
127   }
129    
130   /* This function is used to into the systems syslog */
131   function log_into_syslog($entry)
132   {
133     $str = $entry['user']." ".$entry['action']." ".$entry['object']." from type ".$entry['objecttype']." ".$entry['changes']." : Result was ".$entry['result'];
134     gosa_log($str);
135   }
138   /* Log into configured logging databses.*/
139   function log_into_db($entry)
140   {
141     if(isset($this->config->data['SERVERS']['LOGGING'])){
142       $servers = $this->config->data['SERVERS']['LOGGING'];
143     }else{
144       msg_dialog::display(_("Error"), _("You have enabled the logging into mysql database, but there are no logging servers available."), ERROR_DIALOG);
145       return(FALSE);
146     }
148     /* Log into each configured server 
149      */
150     foreach($servers as $server_name => $server){
151  
152       /* Connect to the database 
153        */
154       $con = @mysql_pconnect($server_name,$server['USER'],$server['PWD']);
155       if(!$con){
156         msg_dialog::display(_("Error"), sprintf(_("Cannot connect to logging server '%s'."),$server_name), ERROR_DIALOG);
157         return(FALSE);
158       }else{
160         /* Check if the database is available 
161          */
162         $db = mysql_select_db($server['DB'],$con);
163         if(!$db){
164           msg_dialog::display(_("Error"), sprintf(_("Cannot select database '%s' on server '%s': %s"),$server['DB'],$server['SERVER'], mysql_error($con)), ERROR_DIALOG);
165           return(FALSE);
166         }else{
168           /* Check for required tables 
169           */
170           $query = "SHOW TABLES;";
171           $res    = @mysql_query($query,$con);
172           $tables = array();
173           while($attrs  = mysql_fetch_row($res)){
174             $tables[] = $attrs[0];
175           }
176           $error = FALSE;
177           foreach(array("gosa_log","gosa_locations") as $required){
178             if(!in_array($required,$tables)){
179               msg_dialog::display(_("Error"), 
180               sprintf(_("Missing logging table (%s.%s) update your GOsa logging database schema."),
181                 $server['DB'],$required), ERROR_DIALOG);
182               $error = TRUE;
183             }
184             if($error) return(FALSE);
185           }
186  
187           /* Check if our current location is already registerd 
188               in this case get its id.
189              If it wasn't registered yet, create it. 
190            */
191           $base = mysql_escape_string($this->config->current['BASE']);
192           $query= "SELECT id FROM gosa_locations WHERE location=\"".$base."\";";
193           $res  = mysql_query($query);  
194           $location_id = -1;
195           while($attrs = mysql_fetch_assoc($res)){
196             $location_id = $attrs['id'];
197             break;
198           }
199   
200           /* No location found that matches our location.
201              Create it.
202            */
203           if($location_id == -1){
204             $query = "INSERT INTO gosa_locations (location) VALUES ('".$base."');";
205             mysql_query($query,$con);
206  
207             /* Try to detect the location again 
208              */ 
209             $query= "SELECT id FROM gosa_locations WHERE location=\"".$base."\";";
210             $res  = mysql_query($query);
211             $location_id = -1;
212             while($attrs = mysql_fetch_assoc($res)){
213               $location_id = $attrs['id'];
214               break;
215             }
216             if($location_id == -1){
217               msg_dialog::display(_("Error"), sprintf(_("Couldn't add your location to the logging database, the error was: %s."),
218                 mysql_error($con)), ERROR_DIALOG);
219               return(FALSE);
220             }
221           }  
223           /* Create mysql syntax */
224           $query ="INSERT INTO gosa_log 
225             (timestamp,user,action,objecttype,object,changes,result,location_id)
226             VALUES 
227             (
228              \"".mysql_escape_string($entry['timestamp'])."\", 
229              \"".mysql_escape_string($entry['user'])."\", 
230              \"".mysql_escape_string($entry['action'])."\", 
231              \"".mysql_escape_string($entry['objecttype'])."\", 
232              \"".mysql_escape_string($entry['object'])."\", 
233              \"".mysql_escape_string($entry['changes'])."\", 
234              \"".mysql_escape_string($entry['result'])."\", 
235              \"".mysql_escape_string($location_id)."\" 
236             );
237           ";
238           $res = mysql_query($query,$con);
239           if(!$res){
240             msg_dialog::display(_("Error"), sprintf(_("Cannot query database '%s' on server '%s': %s"),
241                   $server['DB'],$server['SERVER'], mysql_error($con)), ERROR_DIALOG);
242             return(FALSE);
243           } 
244         }
245         mysql_close($con);
246       }
247     }
248     return(TRUE);
249   }
252 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
253 ?>