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 $config;
35 /*! \brief logging constructor
37 \param action One of these values (modify|create|remove|snapshot|copy)
38 \param objecttype represents the current edited objecttype, like users/user
39 \param object represents the current edited object dn
40 \param changes_array An array containing names of all touched attributes
41 \param result A status message, containing errors or success messages
43 \sa log()
44 */
45 function log($action,$objecttype,$object,$changes_array = array(),$result = "")
46 {
47 if(!is_array($changes_array)){
48 trigger_error("log(string,string,string,array(),bool). Forth parameter must be an array.");
49 $changes_array = array();
50 }
52 $entry = array();
53 if(!session::global_is_set('config')){
54 $entry['user']= "unkown";
55 }else{
57 $this->config = session::global_get('config');
58 $ui = get_userinfo();
59 $entry['user']= @$ui->dn;
60 }
62 /* Create string out of changes */
63 $changes ="";
64 foreach($changes_array as $str ){
65 $changes .= $str.",";
66 }
67 $changes = preg_replace("/,$/","",$changes );
69 /* Create data object */
70 $entry['timestamp'] = time();
71 $entry['action'] = $action;
72 $entry['objecttype']= $objecttype;
73 $entry['object'] = $object;
74 $entry['changes'] = $changes;
75 $entry['result'] = $result;
77 if(!isset($this->config) && empty($entry['user'])){
78 $entry['user'] = "unknown";
79 }
81 /* Check if all given values are valid */
82 global $config;
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 }else{
90 if(is_object($config) && preg_match("/true/i",$config->get_cfg_value("logging",""))){
91 $this->log_into_syslog($entry);
92 }
93 }
94 }
97 function check($entry = array())
98 {
99 $msgs = array();
101 if(!isset($entry['action']) || !in_array($entry['action'],array("modify","create","remove","copy","snapshot","view","security","debug"))){
102 $msgs[] = sprintf(_("Invalid option '%s' specified!"), $entry['action']);
103 }
105 if(!isset($entry['objecttype']) || empty($entry['objecttype'])){
106 $msgs[] = _("Specified objectType is empty or invalid!");
107 }
109 return($msgs);
110 }
113 /* This function is used to into the systems syslog */
114 function log_into_syslog($entry)
115 {
116 $str= "";
117 if (empty($entry['object']) && empty($entry['changes'])) {
118 $str = "(".$entry['action'].") ".$entry['objecttype'].": ".$entry['result'];
119 } else {
120 $str = "(".$entry['action'].") ".$entry['object']." of type ".$entry['objecttype']." ".$entry['changes'].": ".$entry['result'];
121 }
122 gosa_log($str);
123 }
125 }
127 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
128 ?>