FusionDirectory
 All Data Structures Files Functions Variables
php_setup.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) 2003-2010 Cajus Pollmeier
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 
27 require_once ("variables.inc");
28 
29 function html_trace($errstr = "")
30 {
31  static $hideArgs = array(
32  'ldap_init' => array(3),
33  'ldap_login_user' => array(1),
34  'change_password' => array(1),
35  'cred_decrypt' => array(0,1),
36  'LDAP/__construct' => array(1),
37  );
38  if (!function_exists('debug_backtrace')) {
39  return array ('', '');
40  }
41  $trace = array_slice(debug_backtrace(), 1);
42 
43  $loc = '';
44  if (isset($trace[0]['file'])) {
45  $loc = ' - '._('File').': '.$trace[0]['file'];
46  if (isset($trace[0]['line'])) {
47  $loc .= ' ('._('Line').' '.$trace[0]['line'].')';
48  }
49  }
50 
51  $return_html = '<table width="100%" style="background-color:#402005;color:white;border:2px solid red;border-spacing:0;border-collapse:collapse;">'.
52  '<tr><td colspan="3">'.
53  '<h1 style="color:white">'._('PHP error').' "'.$errstr.'"'.$loc.'</h1>'.
54  '</td></tr>';
55  $return_mailto = rawurlencode('=== Trace ===');
56  /* Generate trace history */
57  for ($index = 1, $c = count($trace); $index < $c; $index++) {
58  $ct = $trace[$index];
59  $loc = '';
60  $func = '';
61  if (isset($ct['class'])) {
62  $loc .= _("class")." ".$ct['class'];
63  $func .= $ct['class'];
64  if (isset($ct['function'])) {
65  $loc .= ' / ';
66  $func .= '/';
67  }
68  }
69  if (isset($ct['function'])) {
70  $loc .= _("function")." ".$ct['function'];
71  $func .= $ct['function'];
72  }
73  if (isset($ct['type'])) {
74  switch ($ct['type']) {
75  case "::":
76  $type = _("static");
77  break;
78 
79  case "->":
80  $type = _("method");
81  break;
82  }
83  } else {
84  $type = "-";
85  }
86  $args = "";
87  if (isset($ct['args'])) {
88  if (isset($hideArgs[$func])) {
89  $hideArgsIndexes = $hideArgs[$func];
90  } else {
91  $hideArgsIndexes = array();
92  }
93  $f = function ($index, $arg) use(&$f, $hideArgsIndexes) {
94  static $i = 0;
95  if (($i == 0) && in_array($index, $hideArgsIndexes)) {
96  return '***';
97  }
98  if ($i > 4) {
99  return '…';
100  }
101  if (is_object($arg)) {
102  return "CLASS:&nbsp;".get_class($arg);
103  } elseif (is_array($arg)) { /* Avoid converting array to string errors */
104  $i++;
105  $ret = "array(".implode(',', array_map($f, array_keys($arg), $arg)).")";
106  $i--;
107  return $ret;
108  } else {
109  if (strlen("$arg") > 512) {
110  $arg = substr("$arg", 0, 512)."…";
111  }
112  return '"'.htmlentities("$arg", ENT_COMPAT, 'UTF-8').'"';
113  }
114  };
115  $args = implode(',', array_map($f, array_keys($ct['args']), $ct['args']));
116  }
117  if (empty($args)) {
118  $args = "-";
119  }
120  if (isset($ct['file'])) {
121  $file = $ct['file'];
122  } else {
123  $file = "";
124  }
125  if (isset($ct['line'])) {
126  $line = $ct['line'];
127  } else {
128  $line = "";
129  }
130  $color = ($index & 1)?'#404040':'606060';
131  $return_html .= "<tr style='background-color:$color'><td style='padding-left:20px' width=\"30%\">"._("Trace")."[$index]: $loc</td>";
132  $return_html .= "<td>"._("File").": $file ("._('Line')." $line)</td><td width=\"10%\">"._("Type").": $type</td></tr>";
133  $return_html .= "<tr style='background-color:$color'><td colspan=3 style='padding-left:20px;'>"._("Arguments").": $args</td></tr>";
134 
135  /* Add trace part to mailto body */
136  $return_mailto .= rawurlencode(
137  "\nTrace[".$index."]:".$loc.
138  "\nFile : ".$file.
139  "\nLine : ".$line.
140  "\nType : ".$type.
141  "\n ".$args.
142  "\n");
143  }
144 
145  $return_html .= "</table>";
146  $return_mailto .= rawurlencode("=== /Trace ===");
147 
148  return array($return_html, $return_mailto);
149 }
150 
162 function gosaRaiseError($errno, $errstr, $errfile, $errline)
163 {
164  global $error_collector,$config, $error_collector_mailto;
165 
166  // To avoid recursion - restore original error handler.
167  restore_error_handler();
168 
169  /* Return if error reporting is set to zero */
170  if (error_reporting() == 0) {
171  set_error_handler('gosaRaiseError', E_WARNING | E_NOTICE | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE | E_STRICT);
172  return FALSE;
173  }
174 
175  /* Workaround for buggy imap_open error outputs */
176  if (preg_match('/imap_open/', $errstr)) {
177  set_error_handler('gosaRaiseError', E_WARNING | E_NOTICE | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE | E_STRICT);
178  return;
179  }
180 
181  /* Hide ldap size limit messages */
182  if (preg_match('/ldap_error/', $errstr)) {
183  if (preg_match('/sizelimit/', $errstr)) {
184  set_error_handler('gosaRaiseError', E_WARNING | E_NOTICE | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE | E_STRICT);
185  return;
186  }
187  }
188 
189  /* Error messages are hidden in FusionDirectory, so we only send them to the logging class and abort here */
190  if (isset($config->data) && $config->get_cfg_value('displayerrors') != 'TRUE') {
191 
192  /* Write to syslog */
193  if (class_exists('logging') && !preg_match('/No such object/', $errstr)) {
194  logging::log('view', 'error', '', array(), "PHP error: $errstr ($errfile, line $errline)");
195  }
196 
197  set_error_handler('gosaRaiseError', E_WARNING | E_NOTICE | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE | E_STRICT);
198  return;
199  }
200 
201  /* Send all errors to logging class, except "Ldap : No such object" messages*/
202  if (class_exists('logging') && !preg_match('/No such object/', $errstr)) {
203  logging::log('debug', 'all', $errfile, array(), 'Type:'.$errno.', Message:'.$errstr.', File:'.$errfile.', Line: '.$errline);
204  }
205 
206  /* Create header as needed */
207  if ($error_collector == "") {
208 
209  /* Mailto body header */
210  $version = FD_VERSION;
211 
212  $error_collector_mailto .= rawurlencode(
213  "Oups. Seems like you've catched some kind of bug inside FusionDirectory/PHP. You may want to help ".
214  "us to improve the software stability. If so, please provide some more information below.".
215  "\n\n".
216  "*** FusionDirectory bug report ***".
217  "\nFusionDirectory Version: $version".
218  "\n\n".
219  "Please describe what you did to produce this error as detailed as possible. Can you ".
220  "reproduce this bug using the demo on http://demo.FusionDirectory.org ?".
221  "\n\n".
222  "*** PHP error information ***\n\n");
223 
224  $error_collector = '
225  <div class="error">
226  <table width="100%">
227  <tr>
228  <td>
229  <img src="geticon.php?context=status&amp;icon=dialog-warning&amp;size=16" alt="" class="center"/>&nbsp;
230  <strong style="font-size:14px">'.
231  _("Generating this page caused the PHP interpreter to raise some errors!").'
232  </strong>
233  </td>
234  <td align=right>
235  <a href="mailto:bugs@fusiondirectory.org?subject=FusionDirectory%20bugreport&amp;body=%BUGBODY%">
236  <img src="geticon.php?context=applications&amp;icon=internet-mail&amp;size=16" title="'._("Send bug report to the FusionDirectory Team").
237  '" class="center" alt="'.("Mail icon").'">&nbsp;'._("Send bugreport").'
238  </a>
239  </td>
240  <td align="right">
241  <button onClick="$(\'errorbox\').toggle();">'.
242  _("Toggle information").'
243  </button>
244  </td>
245  </tr>
246  </table>
247  </div>
248  <div id="errorbox" style="position:absolute; z-index:150; display: none;">';
249  }
250 
251  /* Create error header */
252  $error_collector_mailto .= rawurlencode("=== Error === \n");
253  $error_collector_mailto .= rawurlencode("PHP error: $errstr ($errfile, line $errline)\n");
254  $error_collector_mailto .= rawurlencode("=== /Error === \n\n");
255 
256  list($html_trace, $mailto_trace) = html_trace($errstr);
257  $error_collector .= $html_trace;
258  $error_collector_mailto .= $mailto_trace;
259 
260  /* Flush in case of fatal errors */
261  if (preg_match('/^fatal/i', $errstr) || (PHP_ERROR_FATAL == 'TRUE')) {
263  if (PHP_ERROR_FATAL == 'TRUE') {
264  $error_collector = str_replace('display: none;', '', $error_collector);
265  }
266  echo $error_collector."</div>";
267  flush();
268  exit;
269  }
270 
271  set_error_handler('gosaRaiseError', E_WARNING | E_NOTICE | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE | E_STRICT);
272 }
273 
278 {
279 }
280 
283 function sslOn()
284 {
285  if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
286  return (strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') == 0);
287  }
288  if (isset($_SERVER['HTTPS'])) {
289  return (strcasecmp($_SERVER['HTTPS'], 'on') == 0);
290  }
291  return FALSE;
292 }
293 
296 function sslUrl()
297 {
298  $ssl = 'https://';
299  if (empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
300  $ssl .= $_SERVER['HTTP_HOST'];
301  } else {
302  $ssl .= $_SERVER['HTTP_X_FORWARDED_HOST'];
303  }
304  if (empty($_SERVER['REQUEST_URI'])) {
305  $ssl .= $_SERVER['PATH_INFO'];
306  } else {
307  $ssl .= $_SERVER['REQUEST_URI'];
308  }
309  return $ssl;
310 }
311 
312 /* Bail out for incompatible/old PHP versions */
313 if (!version_compare(phpversion(), PHP_MIN_VERSION, ">=")) {
314  echo "PHP version needs to be ".PHP_MIN_VERSION." or above to run FusionDirectory. Aborted.";
315  exit();
316 }
317 
318 /* Set timezone */
319 date_default_timezone_set("GMT");
320 
321 /* Get base dir for reference */
322 $BASE_DIR = dirname(dirname(__FILE__));
323 $ROOT_DIR = $BASE_DIR."/html";
324 error_reporting (E_ALL | E_STRICT);
325 
326 /* Register error handler */
327 $error_collector = "";
328 $error_collector_mailto = "";
329 
330 set_error_handler('gosaRaiseError', E_WARNING | E_NOTICE | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE | E_STRICT);
331 
332 $variables_order = "ES";
333 ini_set("track_vars", 1);
334 ini_set("display_errors", 1);
335 ini_set("report_memleaks", 1);
336 ini_set("include_path", ".:$BASE_DIR/include:".PHP_DIR.":".PEAR_DIR);
337 
338 /* Do smarty setup */
339 require(SMARTY);
340 
341 $smarty = new Smarty;
342 
343 $smarty->template_dir = $BASE_DIR.'/ihtml/';
344 $smarty->caching = FALSE;
345 $smarty->assign('css_files', array());
346 $smarty->assign('js_files', array());
347 
348 $smarty->php_handling = Smarty::PHP_REMOVE;
349 
350 /* Check for SSL connection */
351 $ssl = '';
352 if (!sslOn()) {
353  $ssl = sslUrl();
354 }
355 ?>
const FD_VERSION
FusionDirectory Version.
const SMARTY(!defined("CONFIG_FILE"))
Allow setting the config file in the apache configuration e.g. SetEnv CONFIG_FILE fusiondirectory...
Definition: variables.inc:45
const PEAR_DIR
php pear path
const PHP_DIR(isset($_SERVER['CONFIG_FILE']))
php library path
gosaRaiseError($errno, $errstr, $errfile, $errline)
Raise an error.
Definition: php_setup.inc:162
sslOn()
Returns TRUE if SSL was used to contact FD, whether directly or through a proxy.
Definition: php_setup.inc:283
static destroy()
Destroy a session.
const PHP_MIN_VERSION
Minimum PHP version.
sslUrl()
Returns SSL URL to redirect to.
Definition: php_setup.inc:296
static log($action, $objecttype, $object, array $changes_array=array(), $result= '')
logging method
dummy_error_handler()
Dummy error handler.
Definition: php_setup.inc:277
const PHP_ERROR_FATAL
Toggle crashing on PHP error, used for test suites.