/
ErrorHandler.php
183 lines (155 loc) · 5.84 KB
/
ErrorHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* Provides methods used to handle error reporting.
*
* Copyright 2012-2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL-2.1
* @package Core
*/
class Horde_ErrorHandler
{
/**
* Aborts with a fatal error, displaying debug information to the user.
*
* @param mixed $error Either a string or an object with a getMessage()
* method (e.g. PEAR_Error, Exception).
*/
static public function fatal($error)
{
global $registry;
if (is_object($error)) {
switch (get_class($error)) {
case 'Horde_Exception_AuthenticationFailure':
if ($registry->isAuthenticated(array('app' => $error->application, 'notransparent' => true)) &&
!$registry->clearAuthApp($error->application)) {
break;
}
try {
Horde::log($error, 'NOTICE');
} catch (Exception $e) {}
if (Horde_Cli::runningFromCLI()) {
$cli = new Horde_Cli();
$cli->fatal($error);
}
$params = array();
if ($registry->getAuth()) {
$params['app'] = $error->application;
}
switch ($error->getCode()) {
case Horde_Auth::REASON_MESSAGE:
$params['msg'] = $error->getMessage();
$params['reason'] = $error->getCode();
break;
}
$logout_url = $registry->getLogoutUrl($params);
/* Clear authentication here. Otherwise, there might be
* issues on the login page since we would otherwise need
* to do session token checking (which might not be
* available, so logout won't happen, etc...) */
if (array_key_exists('app', $params)) {
$registry->clearAuth();
}
$logout_url->redirect();
}
}
try {
Horde::log($error, 'EMERG');
} catch (Exception $e) {}
try {
$cli = Horde_Cli::runningFromCLI();
} catch (Exception $e) {
die($e);
}
if ($cli) {
$cli = new Horde_Cli();
$cli->fatal($error);
}
if (!headers_sent()) {
header('Content-type: text/html; charset=UTF-8');
}
echo <<< HTML
<html>
<head><title>Horde :: Fatal Error</title></head>
<body style="background:#fff; color:#000">
HTML;
ob_start();
try {
$admin = (isset($registry) && $registry->isAdmin());
echo '<h1>' . Horde_Core_Translation::t("A fatal error has occurred") . '</h1>';
if (is_object($error) && method_exists($error, 'getMessage')) {
echo '<h3>' . htmlspecialchars($error->getMessage()) . '</h3>';
} elseif (is_string($error)) {
echo '<h3>' . htmlspecialchars($error) . '</h3>';
}
if ($admin) {
$trace = ($error instanceof Exception)
? $error
: debug_backtrace();
echo '<div id="backtrace"><pre>' .
strval(new Horde_Support_Backtrace($trace)) .
'</pre></div>';
if (is_object($error)) {
echo '<h3>' . Horde_Core_Translation::t("Details") . '</h3>';
echo '<h4>' . Horde_Core_Translation::t("The full error message is logged in Horde's log file, and is shown below only to administrators. Non-administrative users will not see error details.") . '</h4>';
ob_flush();
flush();
echo '<div id="details"><pre>' . htmlspecialchars(print_r($error, true)) . '</pre></div>';
}
} else {
echo '<h3>' . Horde_Core_Translation::t("Details have been logged for the administrator.") . '</h3>';
}
} catch (Exception $e) {
die($e);
}
ob_end_flush();
echo '</body></html>';
exit(1);
}
/**
* PHP legacy error handling (non-Exceptions).
*
* @param integer $errno See set_error_handler().
* @param string $errstr See set_error_handler().
* @param string $errfile See set_error_handler().
* @param integer $errline See set_error_handler().
* @param array $errcontext See set_error_handler().
*/
static public function errorHandler($errno, $errstr, $errfile, $errline,
$errcontext)
{
$er = error_reporting();
// Calls prefixed with '@'.
if ($er == 0) {
// Must return false to populate $php_errormsg (as of PHP 5.2).
return false;
}
if (!($er & $errno) || !class_exists('Horde_Log')) {
return;
}
$options = array();
try {
switch ($errno) {
case E_WARNING:
$priority = Horde_Log::WARN;
break;
case E_NOTICE:
$priority = Horde_Log::NOTICE;
break;
case E_STRICT:
$options['notracelog'] = true;
$priority = Horde_Log::DEBUG;
break;
default:
$priority = Horde_Log::DEBUG;
break;
}
Horde::log(new ErrorException('PHP ERROR: ' . $errstr, 0, $errno, $errfile, $errline), $priority, $options);
} catch (Exception $e) {}
}
}