1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace System; |
4
|
|
|
|
5
|
|
|
use Whoops\Run as Whoops; |
6
|
|
|
use Whoops\Util\Misc as WhoopsMisc; |
7
|
|
|
use Whoops\Handler\JsonResponseHandler; |
8
|
|
|
use Whoops\Handler\PrettyPageHandler; |
9
|
|
|
use DateTime; |
10
|
|
|
use DateTimeZone; |
11
|
|
|
|
12
|
|
|
class Error |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Application Object |
16
|
|
|
* |
17
|
|
|
* @var \System\Application |
18
|
|
|
*/ |
19
|
|
|
private $app; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor |
23
|
|
|
* |
24
|
|
|
* @param \System\Application $app |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Application $app) |
27
|
|
|
{ |
28
|
|
|
$this->app = $app; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Check if the errors should be displayed |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public static function allowDisplayingError() |
37
|
|
|
{ |
38
|
|
|
return (bool) ($_ENV['APP_ENV'] == 'local' && $_ENV['APP_DEBUG'] == 'true'); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Show error |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
private function showError() |
47
|
|
|
{ |
48
|
|
|
error_reporting(E_ALL); |
49
|
|
|
|
50
|
|
|
ini_set("display_errors", 1); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Hide error |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
private function hideError() |
59
|
|
|
{ |
60
|
|
|
error_reporting(0); |
61
|
|
|
|
62
|
|
|
ini_set('display_errors', 0); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Show or hide errors depend on the condition |
67
|
|
|
*/ |
68
|
|
|
public function toggleError() |
69
|
|
|
{ |
70
|
|
|
if (Error::allowDisplayingError()) { |
|
|
|
|
71
|
|
|
|
72
|
|
|
$this->showError(); |
73
|
|
|
|
74
|
|
|
$this->whoops(); |
75
|
|
|
|
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->hideError(); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Send Email to the admin contain the Error |
84
|
|
|
* and the date |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
private function sendErrorToAdmin($error) |
89
|
|
|
{ |
90
|
|
|
$date = new DateTime('now', new DateTimeZone('Europe/Berlin')); |
91
|
|
|
$date = 'Error: ' . $date->format('d.m.Y H:i:s'); |
92
|
|
|
|
93
|
|
|
$this->app->email->recipients(['admin' => $_ENV['EMAIL_ADMIN']])->content(true, $date, $error, $error)->send(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Run error handling of Whoops |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
private function whoops() |
102
|
|
|
{ |
103
|
|
|
$run = new Whoops(); |
104
|
|
|
|
105
|
|
|
$run->prependHandler(new PrettyPageHandler()); |
106
|
|
|
|
107
|
|
|
if (WhoopsMisc::isAjaxRequest()) { |
108
|
|
|
|
109
|
|
|
$jsonHandler = new JsonResponseHandler(); |
110
|
|
|
|
111
|
|
|
$jsonHandler->setJsonApi(true); |
112
|
|
|
|
113
|
|
|
$run->prependHandler($jsonHandler); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$run->register(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Display friendly error to the users |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
private function displayFriendlyMessage() |
125
|
|
|
{ |
126
|
|
|
echo $this->app->view->render('website/pages/error', []); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Check for last errors |
131
|
|
|
* if there are errors than send continue to report the admin |
132
|
|
|
* and display a friendly error to the users |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
public function handleErrors() |
137
|
|
|
{ |
138
|
|
|
$error = error_get_last(); |
139
|
|
|
|
140
|
|
|
if (!$error) return; |
|
|
|
|
141
|
|
|
|
142
|
|
|
$type = $error['type']; |
143
|
|
|
$message = $error['message']; |
144
|
|
|
$file = $error['file']; |
145
|
|
|
$line = $error['line']; |
146
|
|
|
|
147
|
|
|
$error = "There is an Error type: {$type}. says: $message. in file: $file. on line: $line."; |
148
|
|
|
|
149
|
|
|
$this->sendErrorToAdmin($error); |
150
|
|
|
|
151
|
|
|
$this->displayFriendlyMessage(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|