1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Framework\Http; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
use O2System\Gear\Trace; |
18
|
|
|
use O2System\Spl\Exceptions\Abstracts\AbstractException; |
19
|
|
|
use O2System\Spl\Exceptions\ErrorException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Output |
23
|
|
|
* @package O2System\Framework\Http |
24
|
|
|
*/ |
25
|
|
|
class Output extends \O2System\Kernel\Http\Output |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Output::shutdownHandler |
29
|
|
|
* |
30
|
|
|
* Kernel defined shutdown handler function. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
* @throws \O2System\Spl\Exceptions\ErrorException |
34
|
|
|
*/ |
35
|
|
|
public function shutdownHandler() |
36
|
|
|
{ |
37
|
|
|
parent::shutdownHandler(); |
38
|
|
|
|
39
|
|
|
// Execute Shutdown Service |
40
|
|
|
if (services()->has('shutdown')) { |
41
|
|
|
shutdown()->execute(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// ------------------------------------------------------------------------ |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Output::getFilePath |
50
|
|
|
* |
51
|
|
|
* @param string $filename |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getFilePath($filename) |
56
|
|
|
{ |
57
|
|
|
if (modules()) { |
58
|
|
|
$filePaths = modules()->getDirs('Views'); |
|
|
|
|
59
|
|
|
} else { |
60
|
|
|
$filePaths = array_reverse($this->filePaths); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($filePaths as $filePath) { |
64
|
|
|
if (is_file($filePath . $filename . '.phtml')) { |
65
|
|
|
return $filePath . $filename . '.phtml'; |
66
|
|
|
break; |
|
|
|
|
67
|
|
|
} elseif (is_file($filePath . 'errors' . DIRECTORY_SEPARATOR . $filename . '.phtml')) { |
68
|
|
|
return $filePath . 'errors' . DIRECTORY_SEPARATOR . $filename . '.phtml'; |
69
|
|
|
break; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// ------------------------------------------------------------------------ |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Output::errorHandler |
78
|
|
|
* |
79
|
|
|
* Kernel defined error handler function. |
80
|
|
|
* |
81
|
|
|
* @param int $errorSeverity The first parameter, errno, contains the level of the error raised, as an integer. |
82
|
|
|
* @param string $errorMessage The second parameter, errstr, contains the error message, as a string. |
83
|
|
|
* @param string $errorFile The third parameter is optional, errfile, which contains the filename that the error |
84
|
|
|
* was raised in, as a string. |
85
|
|
|
* @param string $errorLine The fourth parameter is optional, errline, which contains the line number the error |
86
|
|
|
* was raised at, as an integer. |
87
|
|
|
* @param array $errorContext The fifth parameter is optional, errcontext, which is an array that points to the |
88
|
|
|
* active symbol table at the point the error occurred. In other words, errcontext will |
89
|
|
|
* contain an array of every variable that existed in the scope the error was triggered |
90
|
|
|
* in. User error handler must not modify error context. |
91
|
|
|
* |
92
|
|
|
* @return bool If the function returns FALSE then the normal error handler continues. |
93
|
|
|
* @throws ErrorException |
94
|
|
|
*/ |
95
|
|
|
public function errorHandler($errorSeverity, $errorMessage, $errorFile, $errorLine, $errorContext = []) |
96
|
|
|
{ |
97
|
|
|
$isFatalError = (((E_ERROR | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR) & $errorSeverity) === $errorSeverity); |
98
|
|
|
|
99
|
|
|
if (strpos($errorFile, 'parser') !== false) { |
100
|
|
|
if (function_exists('parser')) { |
101
|
|
|
if (services()->has('presenter')) { |
102
|
|
|
$vars = presenter()->getArrayCopy(); |
103
|
|
|
extract($vars); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$errorFile = str_replace(PATH_ROOT, DIRECTORY_SEPARATOR, parser()->getSourceFilePath()); |
|
|
|
|
107
|
|
|
$error = new ErrorException($errorMessage, $errorSeverity, $errorFile, $errorLine, $errorContext); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$filePath = $this->getFilePath('error'); |
110
|
|
|
|
111
|
|
|
ob_start(); |
112
|
|
|
include $filePath; |
113
|
|
|
$htmlOutput = ob_get_contents(); |
114
|
|
|
ob_end_clean(); |
115
|
|
|
|
116
|
|
|
echo $htmlOutput; |
117
|
|
|
|
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// When the error is fatal the Kernel will throw it as an exception. |
123
|
|
|
if ($isFatalError) { |
124
|
|
|
throw new ErrorException($errorMessage, $errorSeverity, $errorLine, $errorLine, $errorContext); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Should we ignore the error? We'll get the current error_reporting |
128
|
|
|
// level and add its bits with the severity bits to find out. |
129
|
|
|
if (($errorSeverity & error_reporting()) !== $errorSeverity) { |
130
|
|
|
return false; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$error = new ErrorException($errorMessage, $errorSeverity, $errorFile, $errorLine, $errorContext); |
134
|
|
|
|
135
|
|
|
// Logged the error |
136
|
|
|
logger()->error( |
137
|
|
|
implode( |
138
|
|
|
' ', |
139
|
|
|
[ |
140
|
|
|
'[ ' . $error->getStringSeverity() . ' ] ', |
141
|
|
|
$error->getMessage(), |
142
|
|
|
$error->getFile() . ':' . $error->getLine(), |
143
|
|
|
] |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
// Should we display the error? |
148
|
|
|
if (str_ireplace(['off', 'none', 'no', 'false', 'null'], 0, ini_get('display_errors')) == 1) { |
149
|
|
|
if (is_ajax()) { |
150
|
|
|
$this->setContentType('application/json'); |
151
|
|
|
$this->statusCode = 500; |
152
|
|
|
$this->reasonPhrase = 'Internal Server Error'; |
153
|
|
|
|
154
|
|
|
$this->send(implode( |
155
|
|
|
' ', |
156
|
|
|
[ |
157
|
|
|
'[ ' . $error->getStringSeverity() . ' ] ', |
158
|
|
|
$error->getMessage(), |
159
|
|
|
$error->getFile() . ':' . $error->getLine(), |
160
|
|
|
] |
161
|
|
|
)); |
162
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (services()->has('presenter')) { |
166
|
|
|
if (presenter()->theme) { |
167
|
|
|
presenter()->theme->load(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$vars = presenter()->getArrayCopy(); |
171
|
|
|
extract($vars); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$filePath = $this->getFilePath('error'); |
175
|
|
|
|
176
|
|
|
ob_start(); |
177
|
|
|
include $filePath; |
178
|
|
|
$htmlOutput = ob_get_contents(); |
179
|
|
|
ob_end_clean(); |
180
|
|
|
|
181
|
|
|
if (services()->has('presenter')) { |
182
|
|
|
$htmlOutput = presenter()->assets->parseSourceCode($htmlOutput); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
echo $htmlOutput; |
186
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// ------------------------------------------------------------------------ |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Output::exceptionHandler |
194
|
|
|
* |
195
|
|
|
* Kernel defined exception handler function. |
196
|
|
|
* |
197
|
|
|
* @param \Exception|\Error|\O2System\Spl\Exceptions\Abstracts\AbstractException $exception Throwable exception. |
198
|
|
|
* |
199
|
|
|
* @return void |
200
|
|
|
*/ |
201
|
|
|
public function exceptionHandler($exception) |
202
|
|
|
{ |
203
|
|
|
if (is_ajax()) { |
204
|
|
|
$this->statusCode = 500; |
205
|
|
|
$this->reasonPhrase = 'Internal Server Error'; |
206
|
|
|
|
207
|
|
|
$this->send(implode( |
208
|
|
|
' ', |
209
|
|
|
[ |
210
|
|
|
($exception->getCode() != 0 ? '[ ' . $exception->getCode() . ']' : ''), |
211
|
|
|
$exception->getMessage(), |
212
|
|
|
$exception->getFile() . ':' . $exception->getLine(), |
213
|
|
|
] |
214
|
|
|
)); |
215
|
|
|
} elseif ($exception instanceof AbstractException) { |
216
|
|
|
|
217
|
|
|
if (presenter()->theme) { |
218
|
|
|
presenter()->theme->load(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$vars = presenter()->getArrayCopy(); |
222
|
|
|
extract($vars); |
223
|
|
|
|
224
|
|
|
ob_start(); |
225
|
|
|
include $this->getFilePath('exception'); |
226
|
|
|
$htmlOutput = ob_get_contents(); |
227
|
|
|
ob_end_clean(); |
228
|
|
|
|
229
|
|
|
if (services()->has('presenter')) { |
230
|
|
|
$htmlOutput = presenter()->assets->parseSourceCode($htmlOutput); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
echo $htmlOutput; |
234
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
235
|
|
|
} elseif ($exception instanceof \Exception || $exception instanceof \Error) { |
|
|
|
|
236
|
|
|
|
237
|
|
|
$exceptionClassName = get_class_name($exception); |
238
|
|
|
$header = language()->getLine('E_HEADER_' . $exceptionClassName); |
239
|
|
|
$description = language()->getLine('E_DESCRIPTION_' . $exceptionClassName); |
240
|
|
|
$trace = new Trace($exception->getTrace()); |
241
|
|
|
|
242
|
|
|
if (services()->has('presenter')) { |
243
|
|
|
if (presenter()->theme) { |
244
|
|
|
presenter()->theme->load(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$vars = presenter()->getArrayCopy(); |
248
|
|
|
extract($vars); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
ob_start(); |
252
|
|
|
include $this->getFilePath('exception-spl'); |
253
|
|
|
$htmlOutput = ob_get_contents(); |
254
|
|
|
ob_end_clean(); |
255
|
|
|
|
256
|
|
|
if (services()->has('presenter')) { |
257
|
|
|
$htmlOutput = presenter()->assets->parseSourceCode($htmlOutput); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
echo $htmlOutput; |
261
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
// ------------------------------------------------------------------------ |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Output::sendError |
269
|
|
|
* |
270
|
|
|
* @param int $code |
271
|
|
|
* @param null|array|string $vars |
272
|
|
|
* @param array $headers |
273
|
|
|
*/ |
274
|
|
|
public function sendError($code = 204, $vars = null, $headers = []) |
275
|
|
|
{ |
276
|
|
|
$languageKey = $code . '_' . error_code_string($code); |
277
|
|
|
|
278
|
|
|
$error = [ |
279
|
|
|
'code' => $code, |
280
|
|
|
'title' => language()->getLine($languageKey . '_TITLE'), |
281
|
|
|
'message' => language()->getLine($languageKey . '_MESSAGE'), |
282
|
|
|
]; |
283
|
|
|
|
284
|
|
|
$this->statusCode = $code; |
285
|
|
|
$this->reasonPhrase = $error[ 'title' ]; |
286
|
|
|
|
287
|
|
|
if (is_string($vars)) { |
288
|
|
|
$vars = ['message' => $vars]; |
289
|
|
|
} elseif (is_array($vars) and empty($vars[ 'message' ])) { |
290
|
|
|
$vars[ 'message' ] = $error[ 'message' ]; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
if (isset($vars[ 'message' ])) { |
294
|
|
|
$error[ 'message' ] = $vars[ 'message' ]; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
if (is_ajax() or $this->mimeType !== 'text/html') { |
298
|
|
|
$this->statusCode = $code; |
299
|
|
|
$this->reasonPhrase = $error[ 'title' ]; |
300
|
|
|
$this->send($vars); |
301
|
|
|
|
302
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
$this->sendHeaders($headers); |
306
|
|
|
|
307
|
|
|
if (services()->has('presenter')) { |
308
|
|
|
presenter()->initialize(); |
309
|
|
|
|
310
|
|
|
if (presenter()->theme) { |
311
|
|
|
presenter()->theme->load(); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
$vars = presenter()->getArrayCopy(); |
315
|
|
|
extract($vars); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
extract($error); |
319
|
|
|
|
320
|
|
|
ob_start(); |
321
|
|
|
include $this->getFilePath('error-code'); |
322
|
|
|
$htmlOutput = ob_get_contents(); |
323
|
|
|
ob_end_clean(); |
324
|
|
|
|
325
|
|
|
if (services()->has('presenter')) { |
326
|
|
|
$htmlOutput = presenter()->assets->parseSourceCode($htmlOutput); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
echo $htmlOutput; |
330
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
331
|
|
|
} |
332
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.