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
|
|
|
$limit = 'layout/body'; |
60
|
|
|
if (substr($template, -strlen($limit)) === $limit) { |
61
|
|
|
$this->setLiveReport($data, $controller); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sends last PHP errors via Email |
67
|
|
|
* @param \gplcart\core\Controller $controller |
68
|
|
|
*/ |
69
|
|
|
protected function setEmailReport($controller) |
70
|
|
|
{ |
71
|
|
|
$settings = $this->config->module('error_notifier'); |
72
|
|
|
if (!empty($settings['email']) && !empty($settings['recipient'])) { |
73
|
|
|
$messages = $this->getEmailErrorMessages($settings, $controller); |
74
|
|
|
if (!empty($messages)) { |
75
|
|
|
$this->sendEmail($settings, $messages, $controller); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Sends an E-mail |
82
|
|
|
* @param array $settings |
83
|
|
|
* @param array $messages |
84
|
|
|
* @param \gplcart\core\Controller $controller |
85
|
|
|
*/ |
86
|
|
|
protected function sendEmail(array $settings, array $messages, $controller) |
87
|
|
|
{ |
88
|
|
|
/* @var $mailer \gplcart\core\models\Mail */ |
89
|
|
|
$mailer = $this->getInstance('gplcart\\core\\models\Mail'); |
90
|
|
|
|
91
|
|
|
$subject = 'Last PHP errors'; |
92
|
|
|
$body = implode("\r\n", $messages); |
93
|
|
|
$from = $controller->getStore('data.email.0'); |
94
|
|
|
|
95
|
|
|
$mailer->send($settings['recipient'], $subject, $body, $from); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns an array of PHP errors to send via Email |
100
|
|
|
* @param array $settings |
101
|
|
|
* @param \gplcart\core\Controller $controller |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
protected function getEmailErrorMessages(array $settings, $controller) |
105
|
|
|
{ |
106
|
|
|
if (empty($settings['email_limit'])) { |
107
|
|
|
$settings['email_limit'] = null; // Unlimited |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/* @var $logger \gplcart\core\Logger */ |
111
|
|
|
$logger = $this->getInstance('gplcart\\core\\Logger'); |
112
|
|
|
$errors = $logger->selectPhpErrors($settings['email_limit']); |
113
|
|
|
|
114
|
|
|
return $this->getFormattedErrorMessages($errors, $controller); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Sets live error reporting |
119
|
|
|
* @param array $data |
120
|
|
|
* @param \gplcart\core\Controller $controller |
121
|
|
|
*/ |
122
|
|
|
protected function setLiveReport(array &$data, $controller) |
123
|
|
|
{ |
124
|
|
|
$settings = $this->config->module('error_notifier'); |
125
|
|
|
$messages = $this->getLiveErrorMessages($settings, $controller); |
126
|
|
|
|
127
|
|
|
if (empty($messages)) { |
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->prepareMessages($messages, $settings, $controller); |
132
|
|
|
|
133
|
|
|
foreach ($messages as $message) { |
134
|
|
|
$data['_messages']['warning'][] = $message; |
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
|
|
|
*/ |
144
|
|
|
protected function prepareMessages(&$messages, $settings, $controller) |
145
|
|
|
{ |
146
|
|
|
if ($controller->path('^admin/report/events$')) { |
147
|
|
|
$messages = array(); // Suppress errors on admin/report/events page |
148
|
|
|
return null; |
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
|
|
|
$message = $controller->text('<a href="@href">see saved errors</a>', array('@href' => $controller->url('admin/report/events', array('type' => 'php_error')))); |
160
|
|
|
if ($settings['live'] == 2) { |
161
|
|
|
$vars = array('@href' => $controller->url('admin/report/events', array('clear' => true, 'target' => $controller->path()))); |
162
|
|
|
$message .= ' | ' . $controller->text('<a href="@href">clear all saved errors</a>', $vars); |
163
|
|
|
} |
164
|
|
|
$messages[] = $message; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Returns an array of PHP errors for live reporting |
170
|
|
|
* @param array $settings |
171
|
|
|
* @param \gplcart\core\Controller $controller |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
protected function getLiveErrorMessages(array $settings, $controller) |
175
|
|
|
{ |
176
|
|
|
if (empty($settings['live'])) { |
177
|
|
|
return array(); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/* @var $logger \gplcart\core\Logger */ |
181
|
|
|
$logger = $this->getInstance('gplcart\\core\\Logger'); |
182
|
|
|
|
183
|
|
|
$errors = array(); |
184
|
|
|
if ($settings['live'] == 1) { |
185
|
|
|
$errors = $logger->getPhpErrors(false); |
186
|
|
|
} else if ($settings['live'] == 2) { |
187
|
|
|
$errors = $logger->selectPhpErrors(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $this->getFormattedErrorMessages($errors, $controller); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Returns an array of formatted error messages |
195
|
|
|
* @param array $errors |
196
|
|
|
* @param \gplcart\core\Controller $controller |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
|
|
protected function getFormattedErrorMessages(array $errors, $controller) |
200
|
|
|
{ |
201
|
|
|
$messages = array(); |
202
|
|
|
foreach ($errors as $error) { |
203
|
|
|
$vars = array( |
204
|
|
|
'@line' => $error['line'], |
205
|
|
|
'@message' => $error['message'], |
206
|
|
|
'@file' => gplcart_relative_path($error['file']) |
207
|
|
|
); |
208
|
|
|
$messages[] = $controller->text('@message on line @line in @file', $vars); |
209
|
|
|
} |
210
|
|
|
return $messages; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
} |
214
|
|
|
|