1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class Horde_ErrorHandler
14: {
15: 16: 17: 18: 19:
20: public static $errorTypes = array(
21: 1 => 'ERROR',
22: 2 => 'WARNING',
23: 4 => 'PARSE',
24: 8 => 'NOTICE',
25: 16 => 'CORE_ERROR',
26: 32 => 'CORE_WARNING',
27: 64 => 'COMPILE_ERROR',
28: 128 => 'COMPILE_WARNING',
29: 256 => 'USER_ERROR',
30: 512 => 'USER_WARNING',
31: 1024 => 'USER_NOTICE',
32: 2047 => 'ALL',
33: 2048 => 'STRICT',
34: 4096 => 'RECOVERABLE_ERROR',
35: );
36:
37: 38: 39: 40: 41:
42: protected static $_mask = E_ALL;
43:
44: 45: 46: 47: 48:
49: protected static $_errors = array();
50:
51: 52: 53: 54: 55:
56: protected static $_shutdownFunc;
57:
58: 59: 60: 61: 62:
63: public static function register($shutdownFunc = null)
64: {
65: set_error_handler(array(__CLASS__, 'handleError'));
66:
67: if (is_null($shutdownFunc)) {
68: $shutdownFunc = array(__CLASS__, 'dump');
69: }
70:
71: self::$_shutdownFunc = $shutdownFunc;
72: }
73:
74: 75: 76:
77: public function __destruct()
78: {
79: if (self::$_errors) {
80: call_user_func(self::$_shutdownFunc, self::$_errors);
81: }
82: }
83:
84: 85: 86: 87: 88: 89: 90: 91:
92: public static function handleError($errno, $errstr, $errfile, $errline)
93: {
94:
95: if (!error_reporting()) {
96:
97:
98: }
99:
100:
101: if ($errno & self::$_mask) {
102: self::$_errors[] = array(
103: 'no' => $errno,
104: 'str' => self::_cleanErrorString($errstr),
105: 'file' => $errfile,
106: 'line' => $errline,
107: 'trace' => self::_errorBacktrace(),
108: );
109: }
110: }
111:
112: 113: 114: 115: 116: 117: 118: 119: 120: 121:
122: public static function handleErrorWithContext($errno, $errstr, $errfile,
123: $errline, $errcontext)
124: {
125: self::$_errors[] = array(
126: 'no' => $errno,
127: 'str' => self::_cleanErrorString($errstr),
128: 'file' => $errfile,
129: 'line' => $errline,
130: 'context' => $errcontext,
131: 'trace' => self::_errorBacktrace(),
132: );
133: }
134:
135: 136: 137: 138: 139: 140: 141:
142: protected static function _cleanErrorString($errstr)
143: {
144: return preg_replace("%\s\[<a href='function\.[\d\w-_]+'>function\.[\d\w-_]+</a>\]%", '', $errstr);
145: }
146:
147: 148: 149: 150: 151: 152:
153: protected static function _errorBacktrace()
154: {
155:
156: $skip = 2;
157:
158: $backtrace = debug_backtrace();
159: $trace = array();
160: for ($i = $skip, $i_max = count($backtrace); $i < $i_max; $i++) {
161: $frame = $backtrace[$i];
162: $trace[$i - $skip] = array(
163: 'file' => isset($frame['file']) ? $frame['file'] : null,
164: 'line' => isset($frame['line']) ? $frame['line'] : null,
165: 'function' => isset($frame['function']) ? $frame['function'] : null,
166: 'class' => isset($frame['class']) ? $frame['class'] : null,
167: 'type' => isset($frame['type']) ? $frame['type'] : null,
168: 'args' => isset($frame['args']) ? $frame['args'] : null,
169: );
170: }
171:
172: return $trace;
173: }
174:
175: 176: 177: 178: 179: 180:
181: public static function dump($errors)
182: {
183: if (!$GLOBALS['registry']->isAdmin()) {
184: return;
185: }
186:
187: $dump = false;
188: foreach (headers_list() as $header) {
189: if (strpos($header, 'Content-type: text/html') !== false) {
190: $dump = true;
191: break;
192: }
193: }
194:
195: if ($dump) {
196: foreach ($errors as $error) {
197: echo '<p>' . htmlspecialchars($error['file']) . ':' . htmlspecialchars($error['line']) . ': ' . htmlspecialchars($error['str']) . '</p>';
198: }
199: }
200: }
201:
202: }
203: