-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWhoopsErrorHandler.php
executable file
·214 lines (181 loc) · 5.78 KB
/
WhoopsErrorHandler.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
<?php
use Whoops\Handler\JsonResponseHandler;
use Whoops\Handler\PrettyPageHandler;
class WhoopsErrorHandler extends CErrorHandler {
/**
* Whoops instance.
* @var Whoops
*/
protected $whoops;
/**
* Page title in case of non-AJAX requests. Whoops already have a default "Whoops! There was an error."
* @var string
*/
public $pageTitle;
protected $defaultDisabledLogRoutes = array('YiiDebugToolbarRoute');
protected $disabledLogRoutes = array();
/**
* Weirdly {@link CErrorHandler::error} is private, so we need a new property =.=
* @var CErrorEvent|Exception
*/
protected $problem;
protected $handled = false;
public function handled() {
$this->handled = true;
}
/**
* Instantiate Whoops with the correct handlers.
*/
public function __construct() {
require 'YiiWhoopsRunner.php';
$this->whoops = new YiiWhoopsRunner;
if (Yii::app()->request->isAjaxRequest) {
$this->whoops->pushHandler(new JsonResponseHandler);
}
else {
$contentType = '';
foreach (headers_list() as $header) {
list($key, $value) = explode(':', $header);
$value = ltrim($value, ' ');
if (strtolower($key) === 'content-type') {
// Split encoding if exists
$contentType = explode(";", strtolower($value));
$contentType = current($contentType);
break;
}
}
if ($contentType && strpos($contentType, 'json')) {
$this->whoops->pushHandler(new JsonResponseHandler);
}
else {
$page_handler = new PrettyPageHandler;
if ($this->pageTitle)
$page_handler->setPageTitle($this->pageTitle);
$reordered_tables = array(
'Request information' => static::createRequestTable(),
"GET Data" => $_GET,
"POST Data" => $_POST,
"Files" => $_FILES,
"Cookies" => $_COOKIE,
"Session" => isset($_SESSION)? $_SESSION : array(),
"Environment Variables" => $_ENV,
"Server/Request Data" => $_SERVER,
);
foreach ($reordered_tables as $label => $data)
$page_handler->addDataTable($label, $data);
$this->whoops->pushHandler($page_handler);
}
}
}
protected static function createRequestTable() {
$request = array();
$header = array();
if (isset($_SERVER['SERVER_PROTOCOL'])) $header[] = $_SERVER['SERVER_PROTOCOL'];
if (isset($_SERVER['REQUEST_METHOD'])) $header[] = $_SERVER['REQUEST_METHOD'];
if (isset($_SERVER['HTTP_HOST'])) $header[] = $_SERVER['HTTP_HOST'];
$request['Request'] = implode(' ', $header);
if (isset($_SERVER['REQUEST_URI'])) $request['Resource'] = ltrim($_SERVER['REQUEST_URI'], '/');
if (isset($_SERVER['SCRIPT_FILENAME'])) $request['Entry script'] = $_SERVER['SCRIPT_FILENAME'];
$ips = array();
if (isset($_SERVER['SERVER_ADDR'])) $ips[] = 'Server: '.$_SERVER['SERVER_ADDR'];
if (isset($_SERVER['REMOTE_ADDR'])) $ips[] = 'Client: '.$_SERVER['REMOTE_ADDR'];
$request['IPs'] = implode(' || ', $ips);
if (isset($_SERVER['HTTP_USER_AGENT'])) $request['User agent'] = $_SERVER['HTTP_USER_AGENT'];
if (isset($_SERVER['REQUEST_TIME'])) $request['Request time'] = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
return $request;
}
/**
* Disables some log routes that would output stuff whenever the script finishes, trashing Whoops screen.
* @return true
*/
protected function disableLogRoutes() {
//This part verifies if the log routes to disable really exists. If none, simply returns
$disabled_routes = array_merge($this->defaultDisabledLogRoutes, $this->disabledLogRoutes);
$continue = false;
foreach ($disabled_routes as $route) {
if (class_exists($route, false)) {
$continue = true;
break;
}
}
if (!$continue) return true;
//Here we actually disable the given routes...
$total = sizeof(Yii::app()->log->routes);
for ($i = 0; $i < $total; $i++) {
foreach ($disabled_routes as $route) {
if (Yii::app()->log->routes[$i] instanceof $route) {
Yii::app()->log->routes[$i]->enabled = false;
}
}
}
return true;
}
/**
* Forwards an error to Whoops.
* @param CErrorEvent $event
*/
protected function handleError($event) {
if ($this->beforeHandling($event)) {
if($this->isAjaxRequest()) {
Yii::app()->displayError($event->code, $event->message, $event->file, $event->line);
}
elseif(!YII_DEBUG) {
parent::render('error', $event);
}
else {
try {
$this->whoops->handleError($event->code, $event->message, $event->file, $event->line);
}
catch (\Exception $e) {
$this->handleException($e);
}
}
}
}
/**
* Forwards an exception to Whoops.
* @param Exception $exception
*/
protected function handleException($exception) {
if ($this->beforeHandling($exception)) {
if(!headers_sent()) {
$code = ($exception instanceof CHttpException)? $exception->statusCode : 500;
$msg = $this->getHttpHeader($code, get_class($exception));
header("{$_SERVER['SERVER_PROTOCOL']} $code $msg");
}
if($exception instanceof CHttpException || !YII_DEBUG)
parent::render('error', $exception);
else {
if($this->isAjaxRequest()) {
Yii::app()->displayException($exception);
}
else {
if ($this->errorAction)
Yii::app()->runController($this->errorAction);
if (!$this->handled)
$this->whoops->handleException($exception);
}
}
}
}
protected function beforeHandling($problem) {
$this->problem = $problem;
$this->disableLogRoutes();
if (Yii::app() instanceof CWebApplication) {
return true;
}
else {
if ($problem instanceof \Exception)
Yii::app()->displayException($problem);
elseif ($problem instanceof CErrorEvent)
Yii::app()->displayError($problem->code, $problem->message, $problem->file, $problem->line);
return false;
}
}
/**
* @return CErrorEvent|Exception
*/
public function getError() {
return $this->problem;
}
}