FusionDirectory
class_logging.inc
Go to the documentation of this file.
1 <?php
2 
3 /*
4  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
5  Copyright (C) 2007 Fabian Hickert
6  Copyright (C) 2011-2016 FusionDirectory
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22 
36 class logging
37 {
38  static $validActions = ['modify','create','remove','copy','snapshot','security','error'];
39 
53  static function log (string $action, string $objecttype, string $object, array $changes = [], string $result = '')
54  {
55  global $config, $ui;
56 
57  /* Create data object */
58  $entry = [
59  'timestamp' => microtime(TRUE),
60  'action' => $action,
61  'objecttype' => $objecttype,
62  'object' => $object,
63  'changes' => $changes,
64  'result' => $result
65  ];
66  if (isset($ui->dn) && !empty($ui->dn)) {
67  $entry['user'] = $ui->dn;
68  } elseif (isset($_SERVER['REMOTE_ADDR'])) {
69  $entry['user'] = $_SERVER['REMOTE_ADDR'];
70  } else {
71  $entry['user'] = 'unknown';
72  }
73 
74  /* Check if all given values are valid */
75  $msgs = static::check($entry);
76  if (count($msgs)) {
77  foreach ($msgs as $msg) {
78  trigger_error('Logging failed, reason was: '.$msg);
79  $error = new FusionDirectoryError(htmlescape(sprintf(_('Logging failed: %s'), $msg)));
80  $error->display();
81  }
82  } else {
83  if (is_object($config) && preg_match('/true/i', $config->get_cfg_value('logging', ''))) {
84  static::log_into_syslog($entry);
85  if (in_array($action, $config->get_cfg_value('auditActions', []))) {
86  static::log_into_ldap($entry);
87  }
88  }
89  }
90  }
91 
110  static function debug (int $level, int $line, string $function, string $file, $data, string $info = '')
111  {
112  global $config;
113 
114  static $first = TRUE;
115 
116  if (session::get('DEBUGLEVEL') & $level) {
117  $output = '';
118  if ($first) {
119  $output .= '<div id="debug-handling" class="notice">'.
120  '<img src="geticon.php?context=status&amp;icon=dialog-information&amp;size=22" alt="Information" style="vertical-align:middle;margin-right:.2em;"/>'.
121  'There is some debug output '.
122  '<button onClick="javascript:$$(\'div.debug_div\').each(function (a) { a.toggle(); });">Toggle</button>'.
123  '</div>';
124  $first = FALSE;
125  }
126  $logline = "DEBUG[$level] ";
127  if ($function != '') {
128  $logline .= "($file:$function():$line) - $info: ";
129  } else {
130  $logline .= "($file:$line) - $info: ";
131  }
132  $output .= '<div class="debug_div">';
133  $output .= $logline;
134  if (is_array($data)) {
135  $output .= print_a($data, TRUE);
136  $logline .= print_r($data, TRUE);
137  } else {
138  $output .= "'$data'";
139  $logline .= "'$data'";
140  }
141  $output .= "</div>\n";
142 
143  if (is_object($config) && preg_match('/true/i', $config->get_cfg_value('debugLogging', ''))) {
144  fusiondirectory_log($logline);
145  }
146 
147  if (($_SERVER['REQUEST_METHOD'] == 'POST') && preg_match('/index.php$/', $_SERVER['REQUEST_URI'])) {
148  return;
149  }
150 
151  echo $output;
152  }
153  }
154 
160  static protected function check ($entry = [])
161  {
162  $msgs = [];
163 
164  if (!isset($entry['action']) || !in_array($entry['action'], static::$validActions)) {
165  $msgs[] = sprintf(_('Invalid option "%s" specified!'), $entry['action']);
166  }
167 
168  if (!isset($entry['objecttype']) || empty($entry['objecttype'])) {
169  $msgs[] = _('Specified objectType is empty or invalid!');
170  }
171 
172  return $msgs;
173  }
174 
175  /*
176  * \brief This function is used to into the systems syslog
177  *
178  * \param Array $entry Entry to be loged
179  */
180  static protected function log_into_syslog ($entry)
181  {
182  $str = '';
183  if (empty($entry['object']) && empty($entry['changes'])) {
184  $str = '('.$entry['action'].') '.$entry['objecttype'].': '.$entry['result'];
185  } else {
186  $str = '('.$entry['action'].') '.$entry['object'].' of type '.$entry['objecttype'].' '.implode(',', $entry['changes']).': '.$entry['result'];
187  }
188  fusiondirectory_log($str);
189  }
190 
191  /*
192  * \brief This function is used to into the ldap for audit plugin
193  *
194  * \param Array $entry Entry to be logged
195  */
196  static protected function log_into_ldap ($entry)
197  {
198  global $config;
199  if ($entry['objecttype'] == 'plugin/auditEvent') {
200  /* Avoid infinite loop */
201  return;
202  }
203  if (empty($entry['object'])) {
204  $entry['object'] = 'none';
205  }
206  try {
207  $tabObject = objects::create('auditEvent');
208  $baseObject = $tabObject->getBaseObject();
209  $baseObject->fdAuditDateTime = DateTime::createFromFormat('U.u', number_format($entry['timestamp'], 6, '.', ''));
210  $baseObject->fdAuditAction = $entry['action'];
211  $baseObject->fdAuditAuthorDN = $entry['user'];
212  $baseObject->fdAuditObjectType = $entry['objecttype'];
213  $baseObject->fdAuditObject = $entry['object'];
214  $baseObject->fdAuditAttributes = $entry['changes'];
215  $baseObject->fdAuditResult = $entry['result'];
216  $baseObject->base = $config->current['BASE'];
217  $errors = $tabObject->save();
218  if (!empty($errors)) {
219  msg_dialog::displayChecks($errors);
220  }
221  } catch (FusionDirectoryException $e) {
222  $error = new FusionDirectoryError(
223  htmlescape(sprintf(
224  _('Failed to log event (%s - %s): %s'),
225  $entry['action'],
226  $entry['objecttype'],
227  $e->getMessage()
228  )),
229  0,
230  $e
231  );
232  $error->display();
233  }
234  }
235 }
htmlescape(string $str)
Escape string for HTML output.
Definition: php_setup.inc:32
static get($name)
Accessor of a session var.
static check($entry=[])
Check the options.
fusiondirectory_log($message)
Generate a system log info.
Definition: functions.inc:288
static log(string $action, string $objecttype, string $object, array $changes=[], string $result='')
logging method
This is the base class for the FusionDirectory logging functionality. All logging should lead to this...
static debug(int $level, int $line, string $function, string $file, $data, string $info='')
Debug output method.
Parent class for all exceptions thrown in FusionDirectory.
Parent class for all errors in FusionDirectory.