ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
class.ilErrorHandling.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Services/Environment/classes/class.ilRuntime.php';
5
16include_once 'PEAR.php';
17
18class ilErrorHandling extends PEAR
19{
26
32 var $FATAL;
33
40
47
52 function ilErrorHandling()
53 {
54 $this->PEAR();
55
56 // init vars
57 $this->DEBUG_ENV = true;
58 $this->FATAL = 1;
59 $this->WARNING = 2;
60 $this->MESSAGE = 3;
61
62 $this->error_obj = false;
63
64 // Runtime errors currently only handled for HHVM
65 if(ilRuntime::getInstance()->isHHVM())
66 {
67 set_error_handler(
68 array($this, 'handleRuntimeErrors'),
69 ilRuntime::getInstance()->getReportedErrorLevels()
70 );
71 }
72 set_exception_handler(array($this, 'handleUncaughtException'));
73 }
74
75 function getLastError()
76 {
77 return $this->error_obj;
78 }
79
85 function errorHandler($a_error_obj)
86 {
87 global $log;
88
89 // see bug 18499 (some calls to raiseError do not pass a code, which leads to security issues, if these calls
90 // are done due to permission checks)
91 if ($a_error_obj->getCode() == null)
92 {
93 $a_error_obj->code = $this->WARNING;
94 }
95
96 $this->error_obj =& $a_error_obj;
97//echo "-".$_SESSION["referer"]."-";
98 if ($_SESSION["failure"] && substr($a_error_obj->getMessage(), 0, 22) != "Cannot find this block")
99 {
100 $m = "Fatal Error: Called raise error two times.<br>".
101 "First error: ".$_SESSION["failure"].'<br>'.
102 "Last Error:". $a_error_obj->getMessage();
103 //return;
104 $log->logError($a_error_obj->getCode(), $m);
105 unset($_SESSION["failure"]);
106 die ($m);
107 }
108
109 if (substr($a_error_obj->getMessage(), 0, 22) == "Cannot find this block")
110 {
111 if (DEVMODE == 1)
112 {
113 echo "<b>DEVMODE</b><br><br>";
114 echo "<b>Template Block not found.</b><br>";
115 echo "You used a template block in your code that is not available.<br>";
116 echo "Native Messge: <b>".$a_error_obj->getMessage()."</b><br>";
117 if (is_array($a_error_obj->backtrace))
118 {
119 echo "Backtrace:<br>";
120 foreach ($a_error_obj->backtrace as $b)
121 {
122 if ($b["function"] == "setCurrentBlock" &&
123 basename($b["file"]) != "class.ilTemplate.php")
124 {
125 echo "<b>";
126 }
127 echo "File: ".$b["file"].", ";
128 echo "Line: ".$b["line"].", ";
129 echo $b["function"]."()<br>";
130 if ($b["function"] == "setCurrentBlock" &&
131 basename($b["file"]) != "class.ilTemplate.php")
132 {
133 echo "</b>";
134 }
135 }
136 }
137 exit;
138 }
139 return;
140 }
141
142 if (is_object($log) and $log->enabled == true)
143 {
144 $log->logError($a_error_obj->getCode(),$a_error_obj->getMessage());
145 }
146
147//echo $a_error_obj->getCode().":"; exit;
148 if ($a_error_obj->getCode() == $this->FATAL)
149 {
150 trigger_error(stripslashes($a_error_obj->getMessage()), E_USER_ERROR);
151 exit();
152 }
153
154 if ($a_error_obj->getCode() == $this->WARNING)
155 {
156 if ($this->DEBUG_ENV)
157 {
158 $message = $a_error_obj->getMessage();
159 }
160 else
161 {
162 $message = "Under Construction";
163 }
164
165 $_SESSION["failure"] = $message;
166
167 if (!defined("ILIAS_MODULE"))
168 {
169 ilUtil::redirect("error.php");
170 }
171 else
172 {
173 ilUtil::redirect("../error.php");
174 }
175 }
176
177 if ($a_error_obj->getCode() == $this->MESSAGE)
178 {
179 $_SESSION["failure"] = $a_error_obj->getMessage();
180 // save post vars to session in case of error
181 $_SESSION["error_post_vars"] = $_POST;
182
183 if (empty($_SESSION["referer"]))
184 {
185 $dirname = dirname($_SERVER["PHP_SELF"]);
186 $ilurl = parse_url(ILIAS_HTTP_PATH);
187 $subdir = substr(strstr($dirname,$ilurl["path"]),strlen($ilurl["path"]));
188 $updir = "";
189
190 if ($subdir)
191 {
192 $num_subdirs = substr_count($subdir,"/");
193
194 for ($i=1;$i<=$num_subdirs;$i++)
195 {
196 $updir .= "../";
197 }
198 }
199 ilUtil::redirect($updir."index.php");
200 }
201
202 /* #12104
203 check if already GET-Parameters exists in Referer-URI
204 if (substr($_SESSION["referer"],-4) == ".php")
205 {
206 $glue = "?";
207 }
208 else
209 {
210 // this did break permanent links (".html&")
211 $glue = "&";
212 }
213 */
214 ilUtil::redirect($_SESSION["referer"]);
215 }
216 }
217
218 function getMessage()
219 {
220 return $this->message;
221 }
222 function setMessage($a_message)
223 {
224 $this->message = $a_message;
225 }
226 function appendMessage($a_message)
227 {
228 if($this->getMessage())
229 {
230 $this->message .= "<br /> ";
231 }
232 $this->message .= $a_message;
233 }
234
244 public static function _ilErrorWriter($errno, $errstr, $errfile, $errline)
245 {
246 global $ilLog;
247
248 switch($errno)
249 {
250 case E_USER_ERROR:
251 $ilLog->write('PHP errror: '.$errstr.'. FATAL error on line '.$errline.' in file '.$errfile);
252 unset($ilLog);
253 exit(1);
254
255 case E_USER_WARNING:
256 $ilLog->write('PHP warning: ['.$errno.'] '.$errstr.' on line '.$errline.' in file '.$errfile);
257 break;
258
259 }
260 return true;
261 }
262
268 {
269 $error = $e->getMessage();
270 if (DEVMODE)
271 {
272 $error.= '<br /><br />';
273 $error.= nl2br($e->getTraceAsString());
274 }
275 $this->raiseError($error,$this->WARNING);
276 }
277
289 public function handleRuntimeErrors($a_error_code, $a_error_message, $a_error_file, $a_error_line)
290 {
291 // #15641 - the silence operator should suppress the error completely
292 if(error_reporting() === 0)
293 {
294 return;
295 }
296
297 $backtrace_array = $this->formatBacktraceArray(debug_backtrace());
298 $error_code = $this->translateErrorCode($a_error_code);
299
300 if(ilRuntime::getInstance()->shouldLogErrors())
301 {
302 error_log($error_code . ': ' . $a_error_message . ' in '.$a_error_file . ' on line ' . $a_error_line . PHP_EOL . implode(PHP_EOL, $backtrace_array));
303 }
304
305 if(ilRuntime::getInstance()->shouldDisplayErrors())
306 {
307 print '<br /><b>' . $error_code . '</b>: ' . $a_error_message . ' in <b>'.$a_error_file . '</b> on line <b>' . $a_error_line . '</b><br/>' . implode('<br />', $backtrace_array);
308 }
309
310 return true;
311 }
312
317 protected function formatBacktraceArray(array $a_backtrace)
318 {
319 $stack = array();
320 $i = 1;
321
322 unset($a_backtrace[0]); // remove first call from stack trace
323 foreach($a_backtrace as $item)
324 {
325 $stack_line = "#$i " . $item['file'] . "(" . $item['line'] . "): ";
326 if(isset($item['class']))
327 {
328 $stack_line .= $item['class'] . "->";
329 }
330 $stack_line .= $item['function'] . "()";
331 array_push($stack, $stack_line);
332 $i++;
333 }
334
335 return $stack;
336 }
337
343 protected function translateErrorCode($error_code)
344 {
345 $constants = get_defined_constants(true);
346 foreach($constants['Core'] as $constant => $value)
347 {
348 if(substr($constant, 0, 2) == 'E_' && $value == $error_code)
349 {
350 return $constant;
351 }
352 }
353
354 return 'E_UNKNOWN';
355 }
356} // END class.ilErrorHandling
357?>
PEAR($error_class=null)
Constructor.
Definition: PEAR.php:170
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object's de...
Definition: PEAR.php:524
Error Handling & global info handling uses PEAR error class.
ilErrorHandling()
Constructor @access public.
errorHandler($a_error_obj)
defines what has to happen in case of error @access private
handleRuntimeErrors($a_error_code, $a_error_message, $a_error_file, $a_error_line)
We should enhance the error reporting in future releases (funding required).
translateErrorCode($error_code)
Translates an integer error code to the corresponding error string.
static _ilErrorWriter($errno, $errstr, $errfile, $errline)
This is used in Soap calls to write PHP error in ILIAS Logfile Not used yet!!!
handleUncaughtException(Exception $e)
Called for each uncaught exception.
formatBacktraceArray(array $a_backtrace)
static getInstance()
static redirect($a_script)
http redirect to other script
$_POST['username']
Definition: cron.php:12
< a tabindex="-1" style="border-style: none;" href="#" title="Refresh Image" onclick="document.getElementById('siimage').src = './securimage_show.php?sid=' + Math.random(); this.blur(); return false">< img src="./images/refresh.png" alt="Reload Image" height="32" width="32" onclick="this.blur()" align="bottom" border="0"/></a >< br/>< strong > Enter Code *if($_SERVER['REQUEST_METHOD']=='POST' &&@ $_POST['do']=='contact') $_SESSION['ctform']['success']
if(! $in) print
exit
Definition: login.php:54