-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAmslib_Shutdown.php
executable file
·161 lines (137 loc) · 5.58 KB
/
Amslib_Shutdown.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
<?php
/*******************************************************************************
* Copyright (c) {15/03/2008} {Christopher Thomas}
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Contributors/Author:
* {Christopher Thomas} - Creator - [email protected]
*
*******************************************************************************/
/**
* class: Amslib_Shutdown
*
* group: core
*
* file: Amslib_Shutdown.php
*
*/
class Amslib_Shutdown
{
static protected $FATAL = array(E_ERROR,E_CORE_ERROR,E_COMPILE_ERROR,E_COMPILE_WARNING,E_STRICT,E_USER_ERROR);
static protected $WARNINGS = array(E_WARNING,E_NOTICE,E_CORE_WARNING,E_USER_WARNING,E_RECOVERABLE_ERROR,E_DEPRECATED);
static protected $shutdown_url = false;
static protected $callback = false;
static protected $trap_list = [];
static protected function __exec_shutdown($data)
{
if(is_callable(self::$callback)){
call_user_func(self::$callback,$data);
}
die("Amslib_Shutdown::setMode was not called with one of the following parameters [html,json,text]");
}
static public function setMode($mode="html")
{
if(in_array($mode,array("html","text","json")))
{
self::$callback = ["Amslib_Shutdown","render".ucwords($mode)];
}
}
static public function setup($url,$trap_warnings=false)
{
self::$shutdown_url = $url;
// E_PARSE: you cannot catch parse errors without a prepend file.
// NOTE: I think this has to do with being a different apache request stage
// All the errors I believe to be fatal/non-recoverable/you're fucked/your code is shit
self::$trap_list = $trap_warnings ? array_merge(self::$FATAL,self::$WARNINGS) : self::$FATAL;
set_error_handler(array("Amslib_Shutdown","__exception_error_handler"));
register_shutdown_function(array("Amslib_Shutdown","__shutdown_handler"));
set_exception_handler(array("Amslib_Shutdown","__shutdown_exception"));
}
static public function __exception_error_handler($errno, $errstr, $errfile, $errline)
{
if(in_array($errno,self::$trap_list)) {
if (strpos($errstr, "open_basedir restriction in effect") !== false) {
throw new Amslib_Exception_Openbasedir($errstr, $errno);
} else {
throw new Amslib_Exception($errstr, $errno);
}
}else{
Amslib_Debug::log("Error/Warning occurred but did not trigger exception",$errno,$errstr,$errfile,$errline);
}
}
/**
* method: __shutdown_handler
*
* todo: write documentation
*/
static public function __shutdown_handler()
{
$e = @error_get_last();
if($e && is_array($e) && array_key_exists("type",$e) && in_array($e["type"],self::$trap_list))
{
$data = array(
"code" => isset($e['type']) ? $e['type'] : 0,
"msg" => isset($e['message']) ? $e['message'] : '',
"file" => isset($e['file']) ? $e['file'] : '',
"line" => isset($e['line']) ? $e['line'] : '',
"uri" => $_SERVER["REQUEST_URI"],
"root" => isset($_SERVER["__WEBSITE_ROOT__"]) ? $_SERVER["__WEBSITE_ROOT__"] : "/"
);
self::__exec_shutdown($data);
}
}
/**
* method: __shutdown_exception
*
* todo: write documentation
*/
static public function __shutdown_exception($e)
{
$stack = Amslib_Debug::getStackTrace("exception", $e);
if (empty($stack)) $stack = array(array("file" => "__STACK_ERROR__", "line" => "__STACK_ERROR__"));
if (!array_key_exists("file", $stack[0])) $stack[0]["file"] = "__FILE_NOT_AVAILABLE__";
if (!array_key_exists("line", $stack[0])) $stack[0]["line"] = "__LINE_NOT_AVAILABLE__";
$data = array(
"error" => "Uncaught Exception",
"code" => get_class($e),
"msg" => $e->getMessage(),
"data" => is_callable(array($e, "getData")) ? $e->getData() : false,
"file" => $stack[0]["file"],
"line" => $stack[0]["line"],
"stack" => $stack,
"uri" => $_SERVER["REQUEST_URI"],
"root" => isset($_SERVER["__WEBSITE_ROOT__"]) ? $_SERVER["__WEBSITE_ROOT__"] : "/"
);
self::__exec_shutdown($data);
}
static public function renderHtml($data)
{
if(self::$shutdown_url){
header("Location: ".self::$shutdown_url."?data=".base64_encode(json_encode($data)));
die("waiting to redirect");
}
die(__METHOD__.", url was invalid, cannot execute");
}
static public function renderText($data)
{
self::renderJSON($data);
}
static public function renderJSON($data)
{
header("Cache-Control: no-cache");
header("Content-Type: application/json");
die(json_encode($data));
}
}