-
Notifications
You must be signed in to change notification settings - Fork 23
/
QErrorHandler.class.php
306 lines (262 loc) · 10.6 KB
/
QErrorHandler.class.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
/**
* Qcodo Error Handler
*
* If we are in this class, we must assume that the application is in an unstable state.
*
* Thus, we cannot depend on any other qcodo or application-based classes or objects
* to help with the error processing.
*
* Therefore, all classes and functionality for error handling must be defined in this class
* in order to minimize any dependency on the rest of the framework.
*/
class QErrorHandler {
// Static Properties that should always be set on any error
public static $Type;
public static $Message;
public static $ObjectType;
public static $Filename;
public static $LineNumber;
public static $StackTrace;
// Properties that are calculated based on the error information above
public static $FileLinesArray;
public static $MessageBody;
// Static Properties that can be optionally set
public static $RenderedPage;
public static $ErrorAttributeArray = array();
public static $AdditionalMessage;
// Other Properties
public static $DateTimeOfError;
public static $FileNameOfError;
public static $IsoDateTimeOfError;
public static $CliMode;
public static $CliReportWidth = 138;
protected static function Run() {
// Get the RenderedPage (if applicable)
if (ob_get_length()) {
QErrorHandler::$RenderedPage = ob_get_contents();
ob_clean();
}
// Setup the FileLinesArray
if (is_file(QErrorHandler::$Filename))
QErrorHandler::$FileLinesArray = file(QErrorHandler::$Filename);
else if (strpos(QErrorHandler::$Filename, 'eval()') !== false)
QErrorHandler::$FileLinesArray = array('File listing unavailable; eval()\'d code');
else
QErrorHandler::$FileLinesArray = array('File Not Found: ' . QErrorHandler::$Filename);
// Set up the MessageBody
if (QErrorHandler::$AdditionalMessage)
QErrorHandler::$MessageBody = htmlentities(QErrorHandler::$AdditionalMessage) . '<br/>' . htmlentities(QErrorHandler::$Message);
else
QErrorHandler::$MessageBody = htmlentities(QErrorHandler::$Message);
QErrorHandler::$MessageBody = str_replace(" ", " ", str_replace("\n", "<br/>\n", QErrorHandler::$MessageBody));
QErrorHandler::$MessageBody = str_replace(": ", ": ", QErrorHandler::$MessageBody);
// Figure Out DateTime (and if we are logging, the filename of the error log)
$strMicrotime = microtime();
$strParts = explode(' ', $strMicrotime);
$strMicrotime = substr($strParts[0], 2);
$intTimestamp = $strParts[1];
QErrorHandler::$DateTimeOfError = date('l, F j Y, g:i:s.' . $strMicrotime . ' A T', $intTimestamp);
QErrorHandler::$IsoDateTimeOfError = date('Y-m-d H:i:s T', $intTimestamp);
if (defined('__ERROR_LOG__') && __ERROR_LOG__ && defined('ERROR_LOG_FLAG') && ERROR_LOG_FLAG)
QErrorHandler::$FileNameOfError = sprintf('qcodo_error_%s_%s.html', date('Y-m-d_His', $intTimestamp), $strMicrotime);
// Cleanup
unset($strMicrotime);
unset($strParts);
unset($strMicrotime);
unset($intTimestamp);
// Generate the Error Dump
if (!ob_get_level()) ob_start();
if (self::$CliMode)
require(__QCODO_CORE__ . '/assets/error_dump_cli.inc.php');
else
require(__QCODO_CORE__ . '/assets/error_dump.inc.php');
// Do We Log???
if (defined('__ERROR_LOG__') && __ERROR_LOG__ && defined('ERROR_LOG_FLAG') && ERROR_LOG_FLAG) {
// Log to File in __ERROR_LOG__
$strContents = ob_get_contents();
QApplication::MakeDirectory(__ERROR_LOG__, 0777);
$strFileName = sprintf('%s/%s', __ERROR_LOG__, QErrorHandler::$FileNameOfError);
file_put_contents($strFileName, $strContents);
@chmod($strFileName, 0666);
}
if (QApplication::$RequestMode == QRequestMode::Ajax) {
if (defined('ERROR_FRIENDLY_AJAX_MESSAGE') && ERROR_FRIENDLY_AJAX_MESSAGE) {
// Reset the Buffer
while(ob_get_level()) ob_end_clean();
// Setup the Friendly Response
header('Content-Type: text/xml');
$strToReturn = '<controls/><commands><command>alert("' . str_replace('"', '\\"', ERROR_FRIENDLY_AJAX_MESSAGE) . '");</command></commands>';
if (QApplication::$EncodingType)
printf("<?xml version=\"1.0\" encoding=\"%s\"?><response>%s</response>\r\n", QApplication::$EncodingType, $strToReturn);
else
printf("<?xml version=\"1.0\"?><response>%s</response>\r\n", $strToReturn);
return false;
}
} else {
if (defined('ERROR_FRIENDLY_PAGE_PATH') && ERROR_FRIENDLY_PAGE_PATH) {
// Reset the Buffer
while(ob_get_level()) ob_end_clean();
header("HTTP/1.1 500 Internal Server Error");
require(ERROR_FRIENDLY_PAGE_PATH);
}
}
exit();
}
public static function PrepDataForScript($strData) {
$strData = str_replace("\\", "\\\\", $strData);
$strData = str_replace("\n", "\\n", $strData);
$strData = str_replace("\r", "\\r", $strData);
$strData = str_replace("\"", """, $strData);
$strData = str_replace("</script>", "</script>", $strData);
$strData = str_replace("</Script>", "</script>", $strData);
$strData = str_replace("</SCRIPT>", "</script>", $strData);
return $strData;
}
public static function HandleException(Exception $objException) {
// If we still have access to QApplicationBase, set the error flag on the Application
if (class_exists('QApplicationBase'))
QApplicationBase::$ErrorFlag = true;
// If we are currently dealing with reporting an error, don't go on
if (QErrorHandler::$Type)
return;
// Setup the QErrorHandler Object
QErrorHandler::$Type = 'Exception';
$objReflection = new ReflectionObject($objException);
QErrorHandler::$Message = $objException->getMessage();
QErrorHandler::$ObjectType = $objReflection->getName();
QErrorHandler::$Filename = $objException->getFile();
QErrorHandler::$LineNumber = $objException->getLine();
QErrorHandler::$StackTrace = trim($objException->getTraceAsString());
// Special Setup for Database Exceptions
if ($objException instanceof QDatabaseExceptionBase) {
QErrorHandler::$ErrorAttributeArray[] = new QErrorAttribute('Database Error Number', $objException->ErrorNumber, false);
if ($objException->Query) {
QErrorHandler::$ErrorAttributeArray[] = new QErrorAttribute('Query', $objException->Query, true);
}
}
// Sepcial Setup for DataBind Exceptions
if ($objException instanceof QDataBindException) {
if ($objException->Query) {
QErrorHandler::$ErrorAttributeArray[] = new QErrorAttribute('Query', $objException->Query, true);
}
}
QErrorHandler::Run();
}
public static function HandleError($intErrorNumber, $strErrorString, $strErrorFile, $intErrorLine) {
// If a command is called with "@", then we should return
if (error_reporting() == 0)
return;
// If we still have access to QApplicationBase, set the error flag on the Application
if (class_exists('QApplicationBase'))
QApplicationBase::$ErrorFlag = true;
// If we are currently dealing with reporting an error, don't go on
if (QErrorHandler::$Type)
return;
// Setup the QErrorHandler Object
QErrorHandler::$Type = 'Error';
QErrorHandler::$Message = $strErrorString;
QErrorHandler::$Filename = $strErrorFile;
QErrorHandler::$LineNumber = $intErrorLine;
switch ($intErrorNumber) {
case E_ERROR:
QErrorHandler::$ObjectType = 'E_ERROR';
break;
case E_WARNING:
QErrorHandler::$ObjectType = 'E_WARNING';
break;
case E_PARSE:
QErrorHandler::$ObjectType = 'E_PARSE';
break;
case E_NOTICE:
QErrorHandler::$ObjectType = 'E_NOTICE';
break;
case E_STRICT:
QErrorHandler::$ObjectType = 'E_STRICT';
break;
case E_CORE_ERROR:
QErrorHandler::$ObjectType = 'E_CORE_ERROR';
break;
case E_CORE_WARNING:
QErrorHandler::$ObjectType = 'E_CORE_WARNING';
break;
case E_COMPILE_ERROR:
QErrorHandler::$ObjectType = 'E_COMPILE_ERROR';
break;
case E_COMPILE_WARNING:
QErrorHandler::$ObjectType = 'E_COMPILE_WARNING';
break;
case E_USER_ERROR:
QErrorHandler::$ObjectType = 'E_USER_ERROR';
break;
case E_USER_WARNING:
QErrorHandler::$ObjectType = 'E_USER_WARNING';
break;
case E_USER_NOTICE:
QErrorHandler::$ObjectType = 'E_USER_NOTICE';
break;
case E_DEPRECATED:
QErrorHandler::$ObjectType = 'E_DEPRECATED';
break;
case E_USER_DEPRECATED:
QErrorHandler::$ObjectType = 'E_USER_DEPRECATED';
break;
case E_RECOVERABLE_ERROR:
QErrorHandler::$ObjectType = 'E_RECOVERABLE_ERROR';
break;
default:
QErrorHandler::$ObjectType = 'Unknown';
break;
}
// Setup the Stack Trace
QErrorHandler::$StackTrace = "";
$objBackTrace = debug_backtrace();
for ($intIndex = 0; $intIndex < count($objBackTrace); $intIndex++) {
$objItem = $objBackTrace[$intIndex];
$strKeyFile = (array_key_exists('file', $objItem)) ? $objItem['file'] : '';
$strKeyLine = (array_key_exists('line', $objItem)) ? $objItem['line'] : '';
$strKeyClass = (array_key_exists('class', $objItem)) ? $objItem['class'] : '';
$strKeyType = (array_key_exists('type', $objItem)) ? $objItem['type'] : '';
$strKeyFunction = (array_key_exists('function', $objItem)) ? $objItem['function'] : '';
QErrorHandler::$StackTrace .= sprintf("#%s %s(%s): %s%s%s()\n",
$intIndex,
$strKeyFile,
$strKeyLine,
$strKeyClass,
$strKeyType,
$strKeyFunction);
}
QErrorHandler::Run();
}
/**
* A modified version of var_export to use var_dump via the output buffer, which
* can better handle recursive structures.
* @param mixed $mixData
* @param boolean $blnHtmlEntities
* @return string
*/
public static function VarExport($mixData, $blnHtmlEntities = true) {
if (($mixData instanceof QForm) || ($mixData instanceof QControl))
$mixData->PrepForVarExport();
ob_start();
var_dump($mixData);
$strToReturn = ob_get_clean();
if ($blnHtmlEntities) {
if (!extension_loaded('xdebug')) $strToReturn = htmlentities($strToReturn);
} else {
if (extension_loaded('xdebug')) $strToReturn = strip_tags(html_entity_decode($strToReturn));
}
return $strToReturn;
}
}
class QErrorAttribute {
public $Label;
public $Contents;
public $MultiLine;
public function __construct($strLabel, $strContents, $blnMultiLine) {
$this->Label = $strLabel;
$this->Contents = $strContents;
$this->MultiLine = $blnMultiLine;
}
}
?>