-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.php
387 lines (366 loc) · 11.2 KB
/
error.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/* Eregansu: Fatal errors
*
* Copyright 2009-2011 Mo McRoberts.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @year 2009-2011
* @include uses('error');
* @since Available in Eregansu 1.0 and later.
*/
if(!defined('TEMPLATES_PATH')) define('TEMPLATES_PATH', 'templates');
class TerminalErrorException extends Exception implements IRequestProcessor
{
public $processed = false;
public function process(Request $req)
{
if($this->processed)
{
return false;
}
$this->processed = true;
return true;
}
}
class Error extends TerminalErrorException
{
const BAD_REQUEST = 400;
const AUTHORIZATION_REQUIRED = 401;
const PAYMENT_REQUIRED = 402;
const FORBIDDEN = 403;
const OBJECT_NOT_FOUND = 404;
const ROUTE_NOT_MATCHED = '404.1';
const NO_OBJECT = '404.2';
const METHOD_NOT_ALLOWED = 405;
const TYPE_NOT_SUPPORTED = 406;
const PROXY_AUTHENTICATION_REQUIRED = 407;
const REQUEST_TIMEOUT = 408;
const CONFLICT = 409;
const GONE = 410;
const LENGTH_REQUIRED = 411;
const PRECONDITION_FAILED = 412;
const REQUEST_TOO_LARGE = 413;
const URI_TOO_LONG = 414;
const UNSUPPORTED_MEDIA_TYPE = 415;
const RANGE_NOT_SATISFIABLE = 416;
const EXPECTATION_FAILED = 417;
const INTERNAL = 500;
const NOT_IMPLEMENTED = 501;
const METHOD_NOT_IMPLEMENTED = '501.1';
const BAD_GATEWAY = 502;
const SERVICE_UNAVAILABLE = 503;
const ROUTE_NOT_PROCESSOR = '503.1';
const GATEWAY_TIMEOUT = 504;
const HTTP_VERSION_NOT_SUPPORTED = 505;
const VARIANT_ALSO_NEGOTIATES = 506;
const INSUFFICIENT_STORAGE = 507;
const BANDWIDTH_LIMIT_EXCEEDED = 508;
const NOT_EXTENDED = 509;
public static $throw = false;
protected $backtrace = null;
protected static $titles = array(
self::BAD_REQUEST => 'Bad request',
self::AUTHORIZATION_REQUIRED => 'Authorization required',
self::PAYMENT_REQUIRED => 'Payment required',
self::FORBIDDEN => 'Forbidden',
self::OBJECT_NOT_FOUND => 'Object not found',
self::ROUTE_NOT_MATCHED => 'Route not matched',
self::NO_OBJECT => 'No object specified',
self::METHOD_NOT_ALLOWED => 'Method not allowed',
self::TYPE_NOT_SUPPORTED => 'Type not supported',
self::PROXY_AUTHENTICATION_REQUIRED => 'Proxy authentication required',
self::REQUEST_TIMEOUT => 'Request timed out',
self::CONFLICT => 'Conflict',
self::GONE => 'Gone',
self::LENGTH_REQUIRED => 'Length required',
self::PRECONDITION_FAILED => 'Precondition failed',
self::REQUEST_TOO_LARGE => 'Request too large',
self::URI_TOO_LONG => 'URI too long',
self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported media type',
self::RANGE_NOT_SATISFIABLE => 'Range not satisfiable',
self::EXPECTATION_FAILED => 'Expectation failed',
self::INTERNAL => 'Internal error',
self::NOT_IMPLEMENTED => 'Not implemented',
self::METHOD_NOT_IMPLEMENTED => 'Method not implemented',
self::BAD_GATEWAY => 'Bad gateway',
self::SERVICE_UNAVAILABLE => 'Service unavailable',
self::GATEWAY_TIMEOUT => 'Gateway timed out',
self::HTTP_VERSION_NOT_SUPPORTED => 'HTTP version not supported',
self::VARIANT_ALSO_NEGOTIATES => 'Variant also negotiates',
self::INSUFFICIENT_STORAGE => 'Insufficient storage',
self::BANDWIDTH_LIMIT_EXCEEDED => 'Bandwidth limit exceeded',
self::NOT_EXTENDED => 'Not extended',
);
/* %1$s is the requested object (with trailing space)
* %2$s is the request method
* %3$s is the request content type
*/
protected static $descriptions = array(
self::BAD_REQUEST => 'Your request could not be processed because it was malformed.',
self::AUTHORIZATION_REQUIRED => 'The requested object is not available without authentication.',
self::PAYMENT_REQUIRED => 'Payment is required to access the requested object.',
self::FORBIDDEN => 'Access to the object %1$swas denied.',
self::OBJECT_NOT_FOUND => 'The requested object %1$scould not be found.',
self::NO_OBJECT => 'Your request could not be processed because an object is required, but none was specified.',
self::METHOD_NOT_ALLOWED => 'Your request could not be processed because the method %2$s is not supported by this object.',
self::TYPE_NOT_SUPPORTED => 'Your request could not be processed because the requested type is not supported by the object %1$s',
self::UNSUPPORTED_MEDIA_TYPE => 'Your request could not be processed because the submitted type %3$s is not supported by this object',
);
public $status = self::INTERNAL;
public $detail = null;
public $object = null;
public $headers = array();
public static function backtrace($skip = 1, $backtrace = null)
{
if($backtrace === null)
{
$backtrace = debug_backtrace();
}
$list = array();
foreach($backtrace as $bt)
{
if(isset($bt['function']))
{
$func = $bt['function'];
if(isset($bt['class']))
{
$func = $bt['class'] . $bt['type'] . $func . '()';
}
}
else
{
$func = '(None)';
}
if(isset($bt['file']))
{
$location = $bt['file'] . ':' . $bt['line'];
}
if(isset($bt['object']))
{
$ctx = get_class($bt['object']);
}
else
{
$ctx = '';
}
$list[] = array($func, $ctx, $location);
}
if($skip)
{
return array_slice($list, $skip);
}
return $list;
}
public function __construct($aStatus = null, $object = null, $detail = null)
{
$this->backtrace = debug_backtrace();
if($aStatus && !is_object($aStatus))
{
$this->status = $aStatus;
$this->code = $this->status;
$this->message = $this->statusTitle($this->status);
}
$this->object = $object;
$this->detail = $detail;
}
public function process(Request $req)
{
if(!parent::process($req))
{
return;
}
$title = $this->statusTitle($this->status);
$desc = $this->statusDescription($this->status, $req);
error_log($this->status . ' [' . $title . '] ' . $req->method . ' ' . $req->uri . ' ' . $this->detail);
@header('HTTP/1.0 ' . floor($this->status) . ' ' . $title);
foreach($this->headers as $name => $value)
{
if(is_numeric($name))
{
@header($value);
}
else
{
@header($name . ': ' . $value);
}
}
$this->message = $title;
$this->code = $this->status;
if(!isset($req->types) || (!isset($req->types['text/html']) && !isset($req->types['*'])))
{
@header('Content-type: text/plain');
echo $title . " (" . $this->status . ")\n\n";
/* On the command-line, we don't have to worry quite so much about who
* we the detailed information to.
*/
if($req instanceof CLIRequest)
{
if($this->detail)
{
echo $this->detail . "\n";
}
else
{
echo $desc . "\n";
}
}
else
{
echo $desc . "\n\n";
if(defined('EREGANSU_DEBUG') && EREGANSU_DEBUG)
{
echo $this->detail . "\n";
}
}
if(self::$throw) throw $this;
if($req) $req->abort();
exit(1);
}
if(isset($req->data['errorSkin']) && isset($req->siteRoot) && file_exists($req->siteRoot . 'templates/' . $req->data['errorSkin'] . '/error.php'))
{
$this->errorTemplate($req, $req->data['errorSkin'], $title, $desc);
}
else if(defined('DEFAULT_ERROR_SKIN') && isset($req->siteRoot))
{
$path = $req->siteRoot . TEMPLATES_PATH . '/' . DEFAULT_ERROR_SKIN . '/error.php';
if(file_exists($path))
{
$this->errorTemplate($req, DEFAULT_ERROR_SKIN, $title, $desc);
}
}
if($req)
{
@$req->header('Content-type', 'text/html;charset=UTF-8');
}
echo '<!DOCTYPE html>' . "\n";
echo '<html>' . "\n";
echo "\t" . '<head>' . "\n";
echo "\t\t" . '<meta charset="UTF-8">' . "\n";
echo "\t\t" . '<title>' . _e($title) . '</title>' . "\n";
echo "\t" . '</head>' . "\n";
echo "\t" . '<body>' . "\n";
echo "\t\t" . '<h1>' . _e($title) . '</h1>' . "\n";
echo "\t\t" . '<p>' . _e($desc) . '</p>' . "\n";
echo $this->debugInfo();
echo "\t" . '</body>' . "\n";
echo '</html>' . "\n";
if($req) $req->abort();
exit(1);
}
protected function errorTemplate(Request $request, $skin, $title, $desc)
{
error_reporting(0);
$templates_iri = $request->root . 'templates/';
$skin_iri = $template_root . $skin . '/';
$templates_path = $request->siteRoot . TEMPLATES_PATH . '/';
$skin_path = $templates_Path . $skin . '/';
$detail = $this->detail;
$debug = $this->debugInfo();
require($request->siteRoot . TEMPLATES_PATH . '/' . $skin . '/error.php');
$request->abort();
exit(1);
}
protected function debugInfo()
{
ob_start();
if(defined('EREGANSU_DEBUG') && EREGANSU_DEBUG)
{
if(strlen($this->detail))
{
echo '<pre>' . _e($this->detail) . '</pre>';
}
echo "\t\t" . '<hr />' . "\n";
echo "\t\t" . '<h2>Backtrace:</h2>' . "\n";
echo "\t\t" . '<table>' . "\n";
echo "\t\t\t" . '<thead>' . "\n";
echo "\t\t\t\t" . '<tr>' . "\n";
echo "\t\t\t\t\t" . '<th scope="col">Function</th>' . "\n";
echo "\t\t\t\t\t" . '<th scope="col">Context</th>' . "\n";
echo "\t\t\t\t\t" . '<th scope="col">Called at</th>' . "\n";
echo "\t\t\t\t" . '</tr>' . "\n";
echo "\t\t\t" . '</thead>' . "\n";
echo "\t\t\t" . '<tbody>' . "\n";
foreach($this->backtrace as $bt)
{
if(isset($bt['function']))
{
$func = $bt['function'];
if(isset($bt['class']))
{
$func = $bt['class'] . $bt['type'] . $func . '()';
}
}
else
{
$func = '(None)';
}
if(isset($bt['file']))
{
$location = $bt['file'] . ':' . $bt['line'];
}
if(isset($bt['object']))
{
$ctx = get_class($bt['object']);
}
else
{
$ctx = '';
}
echo "\t\t\t\t<tr>\n";
echo "\t\t\t\t\t<td>" . _e($func) . '</td>' . "\n";
echo "\t\t\t\t\t<td>" . _e($ctx) . '</td>' . "\n";
echo "\t\t\t\t\t<td>" . _e($location) . '</td>' . "\n";
echo "\t\t\t\t</tr>\n";
}
echo "\t\t\t" . '</tbody>' . "\n";
echo "\t\t" . '</table>' . "\n";
echo "\t\t" . '<hr />' . "\n";
echo "\t\t" . '<p>Error generated at ' . strftime('%Y-%m-%d %H:%M:%S') . ' UTC</p>';
}
return ob_get_clean();
}
protected function statusTitle($code)
{
if(isset(self::$titles[$code])) return self::$titles[$code];
$code = floor($code);
if(isset(self::$titles[$code])) return self::$titles[$code];
return 'Error';
}
protected function statusDescription($code, Request $req)
{
$method = $req->method;
if(isset($req->postData['__method'])) $method = $req->postData['__method'];
if($this->object)
{
$object = $this->object;
}
else
{
$object = $req->uri;
}
if(strlen($object))
{
$object = $object . ' ';
}
if(isset(self::$descriptions[$code])) return sprintf(self::$descriptions[$code], $object, $method, $req->contentType);
$code = floor($code);
if(isset(self::$descriptions[$code])) return sprintf(self::$descriptions[$code], $object, $method, $req->contentType);
if(strlen($object))
{
return 'An error occurred while attempting to process a ' . $method . ' request on ' . $object;
}
return 'An error occurred while attempting to process a ' . $method . ' request';
}
}