|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Error Notifier |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh <[email protected]> |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\error_notifier; |
|
11
|
|
|
|
|
12
|
|
|
use gplcart\core\Module, |
|
13
|
|
|
gplcart\core\Logger, |
|
14
|
|
|
gplcart\core\Container; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Main class for Error Notifier module |
|
18
|
|
|
*/ |
|
19
|
|
|
class Main |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Logger class instance |
|
24
|
|
|
* @var \gplcart\core\Logger $logger |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $logger; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Module class instance |
|
30
|
|
|
* @var \gplcart\core\Module $module |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $module; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param Logger $logger |
|
36
|
|
|
* @param Module $module |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(Logger $logger, Module $module) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->logger = $logger; |
|
41
|
|
|
$this->module = $module; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Implements hook "route.list" |
|
46
|
|
|
* @param array $routes |
|
47
|
|
|
*/ |
|
48
|
|
|
public function hookRouteList(array &$routes) |
|
49
|
|
|
{ |
|
50
|
|
|
$routes['admin/module/settings/error_notifier'] = array( |
|
51
|
|
|
'access' => 'module_edit', |
|
52
|
|
|
'handlers' => array( |
|
53
|
|
|
'controller' => array('gplcart\\modules\\error_notifier\\controllers\\Settings', 'editSettings') |
|
54
|
|
|
) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Implements hook "cron" |
|
60
|
|
|
* @param \gplcart\core\Controller $controller |
|
61
|
|
|
*/ |
|
62
|
|
|
public function hookCron($controller) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->setEmailReport($controller); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Implements hook "template.output" |
|
69
|
|
|
* @param string $template |
|
70
|
|
|
* @param array $data |
|
71
|
|
|
* @param \gplcart\core\Controller $controller |
|
72
|
|
|
*/ |
|
73
|
|
|
public function hookTemplate($template, array &$data, $controller) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->setLiveReport($template, $data, $controller); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Sends last PHP errors via Email |
|
80
|
|
|
* @param \gplcart\core\Controller $controller |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function setEmailReport($controller) |
|
84
|
|
|
{ |
|
85
|
|
|
$settings = $this->module->getSettings('error_notifier'); |
|
86
|
|
|
|
|
87
|
|
|
if (empty($settings['email']) || empty($settings['recipient'])) { |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$messages = $this->getEmailErrors($settings, $controller); |
|
92
|
|
|
|
|
93
|
|
|
if (empty($messages)) { |
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this->sendEmail($settings, $messages, $controller); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Sends an E-mail |
|
102
|
|
|
* @param array $settings |
|
103
|
|
|
* @param array $messages |
|
104
|
|
|
* @param \gplcart\core\Controller $controller |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function sendEmail(array $settings, array $messages, $controller) |
|
108
|
|
|
{ |
|
109
|
|
|
$subject = 'Last PHP errors'; |
|
110
|
|
|
$body = implode("\r\n", $messages); |
|
111
|
|
|
$from = $controller->getStore('data.email.0'); |
|
112
|
|
|
|
|
113
|
|
|
/* @var $mailer \gplcart\core\models\Mail */ |
|
114
|
|
|
$mailer = Container::get('gplcart\\core\\models\\Mail'); |
|
115
|
|
|
return (bool) $mailer->send($settings['recipient'], $subject, $body, $from); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns an array of PHP errors to send via Email |
|
120
|
|
|
* @param array $settings |
|
121
|
|
|
* @param \gplcart\core\Controller $controller |
|
122
|
|
|
* @return array |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function getEmailErrors(array $settings, $controller) |
|
125
|
|
|
{ |
|
126
|
|
|
if (empty($settings['email_limit'])) { |
|
127
|
|
|
$settings['email_limit'] = null; // Unlimited |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$errors = $this->logger->selectErrors($settings['email_limit']); |
|
131
|
|
|
return $this->getFormattedErrors($errors, $controller); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Sets live error reporting |
|
136
|
|
|
* @param string $template |
|
137
|
|
|
* @param array $data |
|
138
|
|
|
* @param \gplcart\core\Controller $controller |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function setLiveReport($template, array &$data, $controller) |
|
141
|
|
|
{ |
|
142
|
|
|
$allowed_template = 'layout/body'; |
|
143
|
|
|
if (substr($template, -strlen($allowed_template)) === $allowed_template) { |
|
144
|
|
|
foreach ($this->getLiveErrors($controller) as $message) { |
|
145
|
|
|
$data['_messages']['warning'][] = $message; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Prepare an array of messages |
|
152
|
|
|
* @param array $messages |
|
153
|
|
|
* @param array $settings |
|
154
|
|
|
* @param \gplcart\core\Controller $controller |
|
155
|
|
|
* @return array |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function prepareMessages(&$messages, $settings, $controller) |
|
158
|
|
|
{ |
|
159
|
|
|
if (empty($messages) || $controller->path('^admin/report/events$')) { |
|
160
|
|
|
return $messages = array(); // Suppress errors on admin/report/events page |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$remaining = count($messages) - $settings['live_limit']; |
|
164
|
|
|
|
|
165
|
|
|
if (!empty($settings['live_limit']) && $remaining > 0) { |
|
166
|
|
|
$messages = array_slice($messages, 0, $settings['live_limit']); |
|
167
|
|
|
$messages[] = $controller->text('...and @remaining more', array('@remaining' => $remaining)); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if ($controller->access('report_events')) { |
|
171
|
|
|
$message = $controller->text('<a href="@url">see saved errors</a>', array('@url' => $controller->url('admin/report/events', array('type' => 'php_error')))); |
|
172
|
|
|
if ($settings['live'] == 2) { |
|
173
|
|
|
$vars = array('@url' => $controller->url('admin/report/events', array('clear' => true, 'target' => $controller->path()))); |
|
174
|
|
|
$message .= ' | ' . $controller->text('<a href="@url">clear all saved errors</a>', $vars); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$messages[] = $message; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $messages; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns an array of PHP errors for live reporting |
|
185
|
|
|
* @param \gplcart\core\Controller $controller |
|
186
|
|
|
* @return array |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function getLiveErrors($controller) |
|
189
|
|
|
{ |
|
190
|
|
|
$settings = $this->module->getSettings('error_notifier'); |
|
191
|
|
|
|
|
192
|
|
|
if (empty($settings['live'])) { |
|
193
|
|
|
return array(); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
$errors = array(); |
|
197
|
|
|
if ($settings['live'] == 1) { |
|
198
|
|
|
$errors = $this->logger->getErrors(false); |
|
199
|
|
|
} else if ($settings['live'] == 2) { |
|
200
|
|
|
$errors = $this->logger->selectErrors(); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$messages = $this->getFormattedErrors($errors, $controller); |
|
204
|
|
|
$this->prepareMessages($messages, $settings, $controller); |
|
205
|
|
|
return $messages; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Returns an array of formatted error messages |
|
210
|
|
|
* @param array $errors |
|
211
|
|
|
* @param \gplcart\core\Controller $controller |
|
212
|
|
|
* @return array |
|
213
|
|
|
*/ |
|
214
|
|
|
protected function getFormattedErrors(array $errors, $controller) |
|
215
|
|
|
{ |
|
216
|
|
|
$messages = array(); |
|
217
|
|
|
foreach ($errors as $error) { |
|
218
|
|
|
|
|
219
|
|
|
$vars = array( |
|
220
|
|
|
'@line' => $error['line'], |
|
221
|
|
|
'@message' => $error['message'], |
|
222
|
|
|
'@file' => gplcart_path_relative($error['file']) |
|
223
|
|
|
); |
|
224
|
|
|
|
|
225
|
|
|
$messages[] = $controller->text('@message on line @line in @file', $vars); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $messages; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
} |
|
232
|
|
|
|