1: <?php
2: namespace Define\Utilities;
3: use Define\Traits\SingletonTrait;
4:
5: if(!defined('DIRECT_ACCESS')) {
6: die("Direct access is forbidden.");
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: class ErrorExceptionHandler implements IUtilities {
36:
37:
38: use SingletonTrait;
39:
40: 41: 42: 43:
44: private $MAXLENGTH = 64;
45:
46: 47: 48: 49:
50: private $errorType = array (
51: E_ERROR => 'ERROR',
52: E_WARNING => 'WARNING',
53: E_PARSE => 'PARSING ERROR',
54: E_NOTICE => 'NOTICE',
55: E_CORE_ERROR => 'CORE ERROR',
56: E_CORE_WARNING => 'CORE WARNING',
57: E_COMPILE_ERROR => 'COMPILE ERROR',
58: E_COMPILE_WARNING => 'COMPILE WARNING',
59: E_USER_ERROR => 'USER ERROR',
60: E_USER_WARNING => 'USER WARNING',
61: E_USER_NOTICE => 'USER NOTICE',
62: E_STRICT => 'STRICT',
63: E_RECOVERABLE_ERROR => 'RECOVERABLE ERROR',
64: E_DEPRECATED => 'DEPRECATED',
65: E_USER_DEPRECATED => 'USER_DEPRECATED'
66: );
67:
68: 69: 70: 71:
72: private $exceptionType = array (
73: 0 => 'EXCEPTION OCCURRED',
74: 1001 => 'INCORRECT FORMAT',
75: 1002 => 'INVALID FILE',
76: 1003 => 'TYPE MISMATCH',
77: 1004 => 'FILE NOT FOUND',
78: 1005 => 'NO FILE'
79: );
80:
81: 82: 83: 84: 85: 86:
87: private function __construct() {
88:
89:
90: $this->enableHandler();
91: }
92:
93: 94: 95: 96: 97: 98:
99: public function enableHandler() {
100: \error_reporting(1);
101: \set_exception_handler(array($this, 'exceptionHandler'));
102: \set_error_handler(array($this,'errorHandler'), APP_ERROR);
103: \register_shutdown_function(array($this, 'fatalError'));
104: }
105:
106: 107: 108: 109: 110: 111: 112: 113: 114:
115: public function errorHandler($errNo, $errStr, $errFile, $errLine) {
116:
117: if(error_reporting() == 0) {
118: return;
119: }
120: $backTrace = $this->debugBacktrace(2);
121:
122: $logMessage = $this->_toStringForLogging($errNo, $errStr, $errFile, $errLine, $backTrace, 'Error', $this->errorType[$errNo]);
123: $webMessage = $this->_toStringForWeb($errNo, $errStr, $errFile, $errLine, $backTrace, 'Error', $this->errorType[$errNo]);
124:
125: $this->debug($logMessage, $webMessage);
126: }
127:
128: 129: 130: 131: 132: 133:
134: public function exceptionHandler(\Exception $exception) {
135: while ($e = $exception->getPrevious()) {
136: $exception = $e;
137: }
138:
139: $logMessage = $this->_toStringForLogging($exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), $exception->getTraceAsString(), 'Exception', $this->exceptionType[$exception->getCode()]);
140: $webMessage = $this->_toStringForWeb($exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), $exception->getTraceAsString(), 'Exception', $this->exceptionType[$exception->getCode()]);
141:
142: $this->debug($logMessage, $webMessage);
143: }
144:
145: 146: 147: 148: 149: 150: 151:
152: private function debug($logMessage, $webMessage) {
153: SEND_ERROR_MAIL === TRUE ? error_log($logMessage, 1, ADMIN_ERROR_MAIL, "From: ".SEND_ERROR_FROM."\r\nTo: ".ADMIN_ERROR_MAIL) : "";
154: ERROR_LOGGING === TRUE ? error_log($logMessage, 3, LOG_FILE_PATH) : "";
155: echo DEBUGGING === TRUE ? $webMessage : SITE_GENERIC_ERROR_MSG;
156: MODE == 'DEVELOPMENT' ? exit() : "";
157: }
158:
159: 160: 161: 162: 163: 164:
165: private function debugBacktrace($entriesMade) {
166:
167: $traceArray = debug_backtrace();
168: $argsDefine = array();
169:
170: $traceMessage = '';
171:
172: for($i=0;$i<$entriesMade;$i++) {
173: array_shift($traceArray);
174: }
175:
176: foreach($traceArray as $newArray) {
177: if(isset($newArray['class'])) {
178: $traceMessage .= $newArray['class'].'.';
179: }
180: if(!empty($newArray['args'])) {
181:
182: foreach($newArray['args'] as $newValue) {
183: if(is_null($newValue)) {
184: $argsDefine[] = NULL;
185: } elseif(is_array($newValue)) {
186: $argsDefine[] = 'Array['.sizeof($newValue).']';
187: } elseif(is_object($newValue)) {
188: $argsDefine[] = 'Object: '.get_class($newValue);
189: } elseif(is_bool($newValue)) {
190: $argsDefine[] = $newValue ? 'TRUE' : 'FALSE';
191: } else {
192: $newValue = (string)@$newValue;
193: $stringValue = htmlspecialchars(substr($newValue, 0, $this->MAXLENGTH));
194: if(strlen($newValue)>$this->MAXLENGTH) {
195: $stringValue = '...';
196: }
197: $argsDefine[] = "\"".$stringValue."\"";
198: }
199: }
200: }
201: $traceMessage .= $newArray['function'].'('.implode(',', $argsDefine).')';
202: $lineNumber = (isset($newArray['line']) ? $newArray['line']:"unknown");
203: $fileName = (isset($newArray['file']) ? $newArray['file']:"unknown");
204:
205: $traceMessage .= sprintf(" # line %4d. file: %s", $lineNumber, $fileName, $fileName);
206: $traceMessage .= "\n";
207: }
208: return $traceMessage;
209: }
210:
211: 212: 213: 214: 215: 216:
217: public function fatalError() {
218: $lastError = error_get_last();
219: if($lastError['type'] == 1 || $lastError['type'] == 4 || $lastError['type'] == 16 || $lastError['type'] == 64 || $lastError['type'] == 256 || $lastError['type'] == 4096) {
220: $this->errorHandler($lastError['type'], $lastError['message'], $lastError['file'], $lastError['line']);
221: }
222: }
223:
224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234:
235: private function _toStringForWeb($errNo, $errStr, $errFile, $errLine, $backTrace, $category, $type) {
236: $css = <<<EOT
237: <style>
238: .errormessage {
239: margin:0px;padding:0px;
240: width:100%;
241: }
242: .errormessage table{
243: border-collapse: collapse;
244: border-spacing: 0;
245: width:100%;
246: margin:0px;padding:0px;
247: }
248: .errormessage tr:last-child td:last-child {
249: -moz-border-radius-bottomright:0px;
250: -webkit-border-bottom-right-radius:0px;
251: border-bottom-right-radius:0px;
252: }
253: .errormessage table tr:first-child td:first-child {
254: -moz-border-radius-topleft:0px;
255: -webkit-border-top-left-radius:0px;
256: border-top-left-radius:0px;
257: }
258: .errormessage table tr:first-child td:last-child {
259: -moz-border-radius-topright:0px;
260: -webkit-border-top-right-radius:0px;
261: border-top-right-radius:0px;
262: }
263: .errormessage tr:last-child td:first-child{
264: -moz-border-radius-bottomleft:0px;
265: -webkit-border-bottom-left-radius:0px;
266: border-bottom-left-radius:0px;
267: }
268: .errormessage tr:hover td{
269:
270: }
271: .errormessage tr:nth-child(odd){
272: background-color:#e5e5e5;
273: }
274: .errormessage tr:nth-child(even) {
275: background-color:#ffffff;
276: }.errormessage td {
277: vertical-align:middle;
278: border:1px solid #000000;
279: border-width:0px 1px 1px 0px;
280: text-align:left;
281: padding:5px;
282: font-size:12px;
283: font-family:Arial;
284: font-weight:normal;
285: color:#000000;
286: }.errormessage tr:last-child td{
287: border-width:0px 1px 0px 0px;
288: }.errormessage tr td:last-child{
289: border-width:0px 0px 1px 0px;
290: }.errormessage tr:last-child td:last-child{
291: border-width:0px 0px 0px 0px;
292: }
293: .errorhead {
294: font: 20px Arial;
295: margin: 5px 0 10px 0;
296: font-weight: bold;
297: }
298: </style>
299: EOT;
300:
301: $errorMessage = "<title>Website Generic Error and Exception Application - {$type}</title><div class='errorhead'>Website Generic Error and Exception Application</div><div class='errormessage'><table border=1>";
302: $errorMessage .= "<tr><td><b>CATEGORY : </b></td><td><font color='red'>{$category}</font></td></tr>";
303: $errorMessage .= "<tr><td><b>ERROR NO : </b></td><td><font color='red'>{$errNo}</font></td></tr>";
304: $errorMessage .= "<tr><td><b>ERROR TYPE : </b></td><td><i><b><font color='red'>{$type}</font></b></i></td></tr>";
305: $errorMessage .= "<tr><td><b>TEXT : </b></td><td><font color='red'>{$errStr}</font></td></tr>";
306: $errorMessage .= "<tr><td><b>LOCATION : </b></td><td><font color='red'>{$errFile}</font>, <b>line</b> {$errLine}, at ".date("F j, Y, g:i a")."</td></tr>";
307: $errorMessage .= "<tr><td width='120px'><b>Showing Backtrace : </b></td><td>{$backTrace} </td></tr></table></div>";
308: $webMessage = str_replace("#", "<br />", $errorMessage);
309: $webMessage = $css.$webMessage;
310: return $webMessage;
311: }
312:
313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323:
324: private function _toStringForLogging($errNo, $errStr, $errFile, $errLine, $backTrace, $category, $type) {
325: $logMessage = "=====================================================================================================================================\n";
326: $logMessage .= "Website Generic Error!\n";
327: $logMessage .= "=====================================================================================================================================\n";
328: $logMessage .= "CATEGORY : {$category}\n";
329: $logMessage .= "=====================================================================================================================================\n";
330: $logMessage .= "ERROR NO : {$errNo}\n";
331: $logMessage .= "=====================================================================================================================================\n";
332: $logMessage .= "ERROR TYPE : {$type}\n";
333: $logMessage .= "=====================================================================================================================================\n";
334: $logMessage .= "TEXT : {$errStr}\n";
335: $logMessage .= "=====================================================================================================================================\n";
336: $logMessage .= "LOCATION : {$errFile}, line {$errLine}, at ".date("F j, Y, g:i a")."\n";
337: $logMessage .= "=====================================================================================================================================\n";
338: $logMessage .= "Showing Backtrace : \n{$backTrace} \n";
339: $logMessage .= "=====================================================================================================================================\n\n";
340: return $logMessage;
341: }
342: }