Code

b46a5adc90bc5d6ba861d9be3073adefdfa06fe7
[gosa.git] / 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   
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 = TRUE)
47   {
48     global $config;
49     $ui   = get_userinfo(); 
51     /* Create string out of changes */
52     $changes  ="";
53     foreach($changes_array as $str ){
54       $changes .= $str.",";
55     }
56     $changes = preg_replace("/,$/","",$changes );
57     
58     /* Create data object */
59     $entry = array(); 
60     $entry['timestamp'] = time();
61     $entry['user']  =$ui->dn;
62     $entry['action'] = $action;
63     $entry['objecttype'] = $objecttype;
64     $entry['object'] = $object;
65     $entry['changes'] = $changes;
66     $entry['result'] = $result;
67  
68     /* Start logging for configured methods */
69     if(isset($this->config->current['LOGGING']) && preg_match("/(^|,)syslog(,|$)/i",$this->config->current['LOGGING'])){
70       @log::log_into_syslog($entry);
71     }
72     if(isset($this->config->current['LOGGING']) && preg_match("/(^|,)mysql(,|$)/i",$this->config->current['LOGGING'])){
73       @log::log_into_db($entry);
74     }
75   }
77    
78   /* This function is used to into the systems syslog */
79   function log_into_syslog($entry)
80   {
81     $str = $entry['user']." ".$entry['action']." ".$entry['object']." from type ".$entry['objecttype']." ".$data." : Result was ".$entry['result'];
82     gosa_log($str);
83   }
86   /* Log into configured logging databses.*/
87   function log_into_db($entry)
88   {
89     if(isset($this->config->data['SERVERS']['LOGGING'])){
90       $servers = $this->config->data['SERVERS']['LOGGING'];
91     }else{
92       print_red(_("You have enabled the logging into mysql databse, but there are no logging servers available."));
93       return(FALSE);
94     }
96     foreach($servers as $server_name => $server){
97   
98       $con = mysql_pconnect($server['SERVER'],$server['USER'],$server['PWD']);
99       if(!$con){
100         print_red(sprintf(_("Could not connect to logging server %s."),$server['SERVER']));
101       }else{
102         $db = mysql_select_db($server['DB'],$con);
103         if(!$db){
104           print_red(sprintf(_("Could not select database %s on server %s. Server ssys :%s"),$server['DB'],$server['SERVER'],mysql_error($con)));
105         }else{
107           /* Create mysql syntax */
108           $query ="INSERT INTO gosa_log 
109                     (timestamp,user,action,objecttype,object,changes,result)
110                    VALUES 
111                     (
112                         \"".addslashes($entry['timestamp'])."\", 
113                         \"".addslashes($entry['user'])."\", 
114                         \"".addslashes($entry['action'])."\", 
115                         \"".addslashes($entry['objecttype'])."\", 
116                         \"".addslashes($entry['object'])."\", 
117                         \"".addslashes($entry['changes'])."\", 
118                         \"".addslashes($entry['result'])."\" 
119                     );
120               ";
121           $res = mysql_query($query,$con);
122           if(!$res){
123             print_red(sprintf(_("Could not query database %s on server %s. Server ssys :%s"),$server['DB'],$server['SERVER'],mysql_error($con)));
124             
125           } 
126         }
127       }
128     }
129   }
132 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
133 ?>