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 = TRUE)
47 {
48 global $config;
49 $this->config = $config;
50 $ui = get_userinfo();
52 /* Create string out of changes */
53 $changes ="";
54 foreach($changes_array as $str ){
55 $changes .= $str.",";
56 }
57 $changes = preg_replace("/,$/","",$changes );
59 /* Create data object */
60 $entry = array();
61 $entry['timestamp'] = time();
62 $entry['user'] = @$ui->dn;
63 $entry['action'] = $action;
64 $entry['objecttype']= $objecttype;
65 $entry['object'] = $object;
66 $entry['changes'] = $changes;
67 $entry['result'] = $result;
69 if(!isset($this->config->current['LOGGING']) && empty($entry['user'])){
70 $entry['user'] = "unknown";
71 }
73 /* Check if all given values are valid */
74 $msgs = @log::check($entry);
75 if(count($msgs)){
76 foreach($msgs as $msg){
77 trigger_error("Logging failed, reason was: ".$msg);
78 print_red("Logging failed, reason was: ".$msg);
79 }
81 }else{
83 if(!isset($this->config->current['LOGGING'])){
84 @log::log_into_syslog($entry);
85 }else{
87 /* Start logging for configured methods */
88 if(isset($this->config->current['LOGGING']) && preg_match("/(^|,)syslog(,|$)/i",$this->config->current['LOGGING'])){
89 @log::log_into_syslog($entry);
90 }
91 if(isset($this->config->current['LOGGING']) && preg_match("/(^|,)mysql(,|$)/i",$this->config->current['LOGGING'])){
92 @log::log_into_db($entry);
93 }
94 }
95 }
96 }
99 function check($entry = array())
100 {
101 $msgs = array();
102 # if(!isset($entry['user']) || empty($entry['user'])){
103 # $msgs[] = "Currently active user is empty.";
104 # }
106 if(!isset($entry['action']) || !in_array($entry['action'],array("modify","create","remove","copy","snapshot","view","security"))){
107 $msgs[] = "Invalid option specified '".$entry['action']."'";
108 }
110 if(!isset($entry['objecttype']) || empty($entry['objecttype'])){
111 $msgs[] = "Specified objectType is empty or invalid.";
112 }
114 return($msgs);
115 }
118 /* This function is used to into the systems syslog */
119 function log_into_syslog($entry)
120 {
121 $str = $entry['user']." ".$entry['action']." ".$entry['object']." from type ".$entry['objecttype']." ".$data." : Result was ".$entry['result'];
122 gosa_log($str);
123 }
126 /* Log into configured logging databses.*/
127 function log_into_db($entry)
128 {
129 if(isset($this->config->data['SERVERS']['LOGGING'])){
130 $servers = $this->config->data['SERVERS']['LOGGING'];
131 }else{
132 print_red(_("You have enabled the logging into mysql databse, but there are no logging servers available."));
133 return(FALSE);
134 }
136 foreach($servers as $server_name => $server){
138 $con = mysql_pconnect($server['SERVER'],$server['USER'],$server['PWD']);
139 if(!$con){
140 print_red(sprintf(_("Could not connect to logging server %s."),$server['SERVER']));
141 }else{
142 $db = mysql_select_db($server['DB'],$con);
143 if(!$db){
144 print_red(sprintf(_("Could not select database %s on server %s. Server ssys :%s"),$server['DB'],$server['SERVER'],mysql_error($con)));
145 }else{
147 /* Create mysql syntax */
148 $query ="INSERT INTO gosa_log
149 (timestamp,user,action,objecttype,object,changes,result)
150 VALUES
151 (
152 \"".addslashes($entry['timestamp'])."\",
153 \"".addslashes($entry['user'])."\",
154 \"".addslashes($entry['action'])."\",
155 \"".addslashes($entry['objecttype'])."\",
156 \"".addslashes($entry['object'])."\",
157 \"".addslashes($entry['changes'])."\",
158 \"".addslashes($entry['result'])."\"
159 );
160 ";
161 $res = mysql_query($query,$con);
162 if(!$res){
163 print_red(sprintf(_("Could not query database %s on server %s. Server ssys :%s"),$server['DB'],$server['SERVER'],mysql_error($con)));
165 }
166 }
167 }
168 }
169 }
170 }
172 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
173 ?>