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\Logger, |
13
|
|
|
gplcart\core\Container, |
14
|
|
|
gplcart\core\Module as CoreModule; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Main class for Error Notifier module |
18
|
|
|
*/ |
19
|
|
|
class Module |
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 CoreModule $module |
37
|
|
|
*/ |
38
|
|
|
public function __construct(Logger $logger, CoreModule $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
|
|
|
*/ |
106
|
|
|
protected function sendEmail(array $settings, array $messages, $controller) |
107
|
|
|
{ |
108
|
|
|
$subject = 'Last PHP errors'; |
109
|
|
|
$body = implode("\r\n", $messages); |
110
|
|
|
$from = $controller->getStore('data.email.0'); |
111
|
|
|
|
112
|
|
|
/* @var $mailer \gplcart\core\models\Mail */ |
113
|
|
|
$mailer = Container::get('gplcart\\core\\models\\Mail'); |
114
|
|
|
$mailer->send($settings['recipient'], $subject, $body, $from); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns an array of PHP errors to send via Email |
119
|
|
|
* @param array $settings |
120
|
|
|
* @param \gplcart\core\Controller $controller |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
protected function getEmailErrors(array $settings, $controller) |
124
|
|
|
{ |
125
|
|
|
if (empty($settings['email_limit'])) { |
126
|
|
|
$settings['email_limit'] = null; // Unlimited |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$errors = $this->logger->selectErrors($settings['email_limit']); |
130
|
|
|
return $this->getFormattedErrors($errors, $controller); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Sets live error reporting |
135
|
|
|
* @param string $template |
136
|
|
|
* @param array $data |
137
|
|
|
* @param \gplcart\core\Controller $controller |
138
|
|
|
*/ |
139
|
|
|
protected function setLiveReport($template, array &$data, $controller) |
140
|
|
|
{ |
141
|
|
|
$allowed_template = 'layout/body'; |
142
|
|
|
if (substr($template, -strlen($allowed_template)) === $allowed_template) { |
143
|
|
|
foreach ($this->getLiveErrors($controller) as $message) { |
144
|
|
|
$data['_messages']['warning'][] = $message; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Prepare an array of messages |
151
|
|
|
* @param array $messages |
152
|
|
|
* @param array $settings |
153
|
|
|
* @param \gplcart\core\Controller $controller |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
protected function prepareMessages(&$messages, $settings, $controller) |
157
|
|
|
{ |
158
|
|
|
if (empty($messages) || $controller->path('^admin/report/events$')) { |
159
|
|
|
return $messages = array(); // Suppress errors on admin/report/events page |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$remaining = count($messages) - $settings['live_limit']; |
163
|
|
|
|
164
|
|
|
if (!empty($settings['live_limit']) && $remaining > 0) { |
165
|
|
|
$messages = array_slice($messages, 0, $settings['live_limit']); |
166
|
|
|
$messages[] = $controller->text('...and @remaining more', array('@remaining' => $remaining)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if ($controller->access('report_events')) { |
170
|
|
|
$message = $controller->text('<a href="@url">see saved errors</a>', array('@url' => $controller->url('admin/report/events', array('type' => 'php_error')))); |
171
|
|
|
if ($settings['live'] == 2) { |
172
|
|
|
$vars = array('@url' => $controller->url('admin/report/events', array('clear' => true, 'target' => $controller->path()))); |
173
|
|
|
$message .= ' | ' . $controller->text('<a href="@url">clear all saved errors</a>', $vars); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$messages[] = $message; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $messages; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Returns an array of PHP errors for live reporting |
184
|
|
|
* @param \gplcart\core\Controller $controller |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
|
|
protected function getLiveErrors($controller) |
188
|
|
|
{ |
189
|
|
|
$settings = $this->module->getSettings('error_notifier'); |
190
|
|
|
|
191
|
|
|
if (empty($settings['live'])) { |
192
|
|
|
return array(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$errors = array(); |
196
|
|
|
if ($settings['live'] == 1) { |
197
|
|
|
$errors = $this->logger->getErrors(false); |
198
|
|
|
} else if ($settings['live'] == 2) { |
199
|
|
|
$errors = $this->logger->selectErrors(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$messages = $this->getFormattedErrors($errors, $controller); |
203
|
|
|
$this->prepareMessages($messages, $settings, $controller); |
204
|
|
|
return $messages; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Returns an array of formatted error messages |
209
|
|
|
* @param array $errors |
210
|
|
|
* @param \gplcart\core\Controller $controller |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
|
|
protected function getFormattedErrors(array $errors, $controller) |
214
|
|
|
{ |
215
|
|
|
$messages = array(); |
216
|
|
|
foreach ($errors as $error) { |
217
|
|
|
|
218
|
|
|
$vars = array( |
219
|
|
|
'@line' => $error['line'], |
220
|
|
|
'@message' => $error['message'], |
221
|
|
|
'@file' => gplcart_path_relative($error['file']) |
222
|
|
|
); |
223
|
|
|
|
224
|
|
|
$messages[] = $controller->text('@message on line @line in @file', $vars); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $messages; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
} |
231
|
|
|
|