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