Code

Added list footer
[gosa.git] / gosa-core / include / class_log.inc
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 );
70     
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(!isset($this->config->current['LOGGING']) && empty($entry['user'])){
80       $entry['user']  = "unknown";
81     }
82  
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       }
90       
91     }else{
93       if(!isset($this->config->current['LOGGING'])){
94         $this->log_into_syslog($entry);
95       }else{
97         /* Start logging for configured methods */
98         if(isset($this->config->current['LOGGING']) && preg_match("/(^|,)syslog(,|$)/i",$this->config->current['LOGGING'])){
99           $this->log_into_syslog($entry);
100         }
101         if(isset($this->config->current['LOGGING']) && preg_match("/(^|,)mysql(,|$)/i",$this->config->current['LOGGING'])){
102           $res = $this->log_into_db($entry);
104           if(!$res){
105             global $config;
106             $config->current['LOGGING'] = preg_replace("/(^|,)mysql(,|$)/",",",$config->current['LOGGING']);
107             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);
108           }
109         }
110       }
111     }
112   }
115   function check($entry = array())
116   {
117     $msgs = array();
119     if(!isset($entry['action']) || !in_array($entry['action'],array("modify","create","remove","copy","snapshot","view","security","debug"))){
120       $msgs[] = sprintf(_("Invalid option '%s' specified."), $entry['action']);
121     }
123     if(!isset($entry['objecttype']) || empty($entry['objecttype'])){
124       $msgs[] = _("Specified objectType is empty or invalid");
125     }
126   
127     return($msgs);
128   }
130    
131   /* This function is used to into the systems syslog */
132   function log_into_syslog($entry)
133   {
134     $str = $entry['user']." ".$entry['action']." ".$entry['object']." from type ".$entry['objecttype']." ".$entry['changes']." : Result was ".$entry['result'];
135     gosa_log($str);
136   }
139   /* Log into configured logging databses.*/
140   function log_into_db($entry)
141   {
142     if(isset($this->config->data['SERVERS']['LOGGING'])){
143       $servers = $this->config->data['SERVERS']['LOGGING'];
144     }else{
145       msg_dialog::display(_("Error"), _("You have enabled the logging into mysql database, but there are no logging servers available."), ERROR_DIALOG);
146       return(FALSE);
147     }
149     /* Log into each configured server 
150      */
151     foreach($servers as $server_name => $server){
152  
153       /* Connect to the database 
154        */
155       $con = @mysql_pconnect($server_name,$server['USER'],$server['PWD']);
156       if(!$con){
157         msg_dialog::display(_("Error"), sprintf(_("Cannot connect to logging server '%s'."),$server_name), ERROR_DIALOG);
158         return(FALSE);
159       }else{
161         /* Check if the database is available 
162          */
163         $db = mysql_select_db($server['DB'],$con);
164         if(!$db){
165           msg_dialog::display(_("Error"), sprintf(_("Cannot select database '%s' on server '%s': %s"),$server['DB'],$server['SERVER'], mysql_error($con)), ERROR_DIALOG);
166           return(FALSE);
167         }else{
169           /* Check for required tables 
170           */
171           $query = "SHOW TABLES;";
172           $res    = @mysql_query($query,$con);
173           $tables = array();
174           while($attrs  = mysql_fetch_row($res)){
175             $tables[] = $attrs[0];
176           }
177           $error = FALSE;
178           foreach(array("gosa_log","gosa_locations") as $required){
179             if(!in_array($required,$tables)){
180               msg_dialog::display(_("Error"), 
181               sprintf(_("Missing logging table (%s.%s) update your GOsa logging database schema."),
182                 $server['DB'],$required), ERROR_DIALOG);
183               $error = TRUE;
184             }
185             if($error) return(FALSE);
186           }
187  
188           /* Check if our current location is already registerd 
189               in this case get its id.
190              If it wasn't registered yet, create it. 
191            */
192           $base = mysql_escape_string($this->config->current['BASE']);
193           $query= "SELECT id FROM gosa_locations WHERE location=\"".$base."\";";
194           $res  = mysql_query($query);  
195           $location_id = -1;
196           while($attrs = mysql_fetch_assoc($res)){
197             $location_id = $attrs['id'];
198             break;
199           }
200   
201           /* No location found that matches our location.
202              Create it.
203            */
204           if($location_id == -1){
205             $query = "INSERT INTO gosa_locations (location) VALUES ('".$base."');";
206             mysql_query($query,$con);
207  
208             /* Try to detect the location again 
209              */ 
210             $query= "SELECT id FROM gosa_locations WHERE location=\"".$base."\";";
211             $res  = mysql_query($query);
212             $location_id = -1;
213             while($attrs = mysql_fetch_assoc($res)){
214               $location_id = $attrs['id'];
215               break;
216             }
217             if($location_id == -1){
218               msg_dialog::display(_("Error"), sprintf(_("Couldn't add your location to the logging database, the error was: %s."),
219                 mysql_error($con)), ERROR_DIALOG);
220               return(FALSE);
221             }
222           }  
224           /* Create mysql syntax */
225           $query ="INSERT INTO gosa_log 
226             (timestamp,user,action,objecttype,object,changes,result,location_id)
227             VALUES 
228             (
229              \"".mysql_escape_string($entry['timestamp'])."\", 
230              \"".mysql_escape_string($entry['user'])."\", 
231              \"".mysql_escape_string($entry['action'])."\", 
232              \"".mysql_escape_string($entry['objecttype'])."\", 
233              \"".mysql_escape_string($entry['object'])."\", 
234              \"".mysql_escape_string($entry['changes'])."\", 
235              \"".mysql_escape_string($entry['result'])."\", 
236              \"".mysql_escape_string($location_id)."\" 
237             );
238           ";
239           $res = mysql_query($query,$con);
240           if(!$res){
241             msg_dialog::display(_("Error"), sprintf(_("Cannot query database '%s' on server '%s': %s"),
242                   $server['DB'],$server['SERVER'], mysql_error($con)), ERROR_DIALOG);
243             return(FALSE);
244           } 
245         }
246         mysql_close($con);
247       }
248     }
249     return(TRUE);
250   }
253 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
254 ?>