-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorLogger.php
More file actions
232 lines (191 loc) · 6.49 KB
/
Copy pathErrorLogger.php
File metadata and controls
232 lines (191 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
namespace ADT;
use DateTime;
use Exception;
use Nette\DI\Container;
use RuntimeException;
use Tracy\Debugger;
use Tracy\Dumper;
use Tracy\Helpers;
use Tracy\Logger;
class ErrorLogger extends Logger
{
/**
* Maximální počet odeslaných emailů denně
* @var int
*/
protected int $maxEmailsPerDay;
/**
* Maximální počet odeslaných emailů v rámci jednoho requestu
* @var int
*/
protected int $maxEmailsPerRequest;
/**
* Počet odeslaných emailů v rámci aktuálního requestu
* @var int
*/
protected int $sentEmailsPerRequest = 0;
/**
* Pole s citlivými údaji, jejižch hodnoty se nemají zobrazovat ve výpisu.
* @var array
*/
protected array $sensitiveFields = [];
/**
* Ma se vlozit error message do emailu
*/
protected bool $includeErrorMessage = true;
protected ?Container $container = null;
public static function install($email, $maxEmailsPerDay = NULL, $maxEmailsPerRequest = NULL, $sensitiveFields = [], $includeErrorMessage = true): ?Logger
{
if (!Debugger::$productionMode) {
return null;
}
Debugger::$email = $email;
$logger = new static(Debugger::$logDirectory, Debugger::$email, Debugger::getBlueScreen());
$logger->maxEmailsPerDay = $maxEmailsPerDay ?: 10;
$logger->maxEmailsPerRequest = $maxEmailsPerRequest ?: 10;
$logger->sensitiveFields = $sensitiveFields;
$logger->includeErrorMessage = $includeErrorMessage;
Debugger::setLogger($logger);
return $logger;
}
public function setup(Container $container)
{
$this->container = $container;
}
protected function sendEmail($message): void
{
if (
$this->email
&&
$this->mailer
) {
$exceptionFile = $this->getExceptionFile($message);
if (!file_exists($exceptionFile)) {
$exceptionFile = null;
}
$line = static::formatLogLine($message, $exceptionFile);
$logFile = $this->directory . '/email-sent';
// we delete email-sent file from yesterday
if (date('Y-m-d', @filemtime($logFile)) < (new DateTime('midnight'))->format('Y-m-d')) {
@unlink($logFile);
}
$messageHash = md5(preg_replace('~(Resource id #)\d+~', '$1', $message));
if (
// ještě se vejdeme do limitu v rámci aktuálního requestu
$this->sentEmailsPerRequest < $this->maxEmailsPerRequest
&&
// tento hash jsme ještě neposlali
(
!($logContent = @file_get_contents($logFile))
||
(strstr($logContent, $messageHash) === false)
)
&&
// ještě se vejdeme do limitu v rámci aktuálního dne
substr_count($logContent, date('Y-m-d')) < $this->maxEmailsPerDay
) {
if (!@file_put_contents($logFile, $line . ' ' . $messageHash . PHP_EOL, FILE_APPEND | LOCK_EX)) {
throw new RuntimeException("Unable to write to log file '" . $logFile . "'. Is directory writable?");
}
// sestavíme zprávu
if (is_array($message)) {
$stringMessage = implode(' ', $message);
} else {
$stringMessage = $message;
}
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
if (count($backtrace) > 3) { //pokud jsou 3 tak jde pouze o exception a je ulozena nette chybova stranka
$backtraceString = "";
for ($i = 0; $i < count($backtrace); $i++) {
$backtraceData = $backtrace[$i] + [
'file' => '_unknown_',
'line' => '_unknown_',
'function' => '_unknown_',
];
$backtraceString = "#$i {$backtraceData['file']}({$backtraceData['line']}): "
. (isset($backtraceData['class']) ? $backtraceData['class'] . '::' : '')
. "{$backtraceData['function']}()\n";
}
$stringMessage .= "\n\n" . $backtraceString;
}
// přidáme doplnující info - referer, browser...
$stringMessage .= "\n\n" .
(isset($_SERVER['HTTP_HOST']) ? 'LINK:' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . "\n" : '') .
'SERVER:' . Dumper::toText($_SERVER) . "\n\n" .
'GET:' . Dumper::toText($_GET, [Dumper::DEPTH => 10]) . "\n\n" .
'POST:' . Dumper::toText($this->hideSensitiveFieldValue($_POST), [Dumper::DEPTH => 10]);
if ($this->container && ($securityUser = $this->container->getByType('\Nette\Security\User', FALSE))) {
// obalujeme do try protoze SecurityUser je zavisly na databazi a pokud je chyba v db, tak nam error nedojde
try {
$stringMessage .= "\n\n" .
'securityUser:' . Dumper::toText($securityUser->identity, [Dumper::DEPTH => 1]);
} catch (Exception $e) {}
}
if ($this->container && ($git = $this->container->getByType('\ADT\TracyGit\Git', FALSE)) !== NULL && ($gitInfo = $git->getInfo())) {
$stringMessage .= "\n\n";
foreach ($gitInfo as $key => $value) {
$stringMessage .= $key . ": " . $value . "\n";
}
}
// odešleme chybu emailem
call_user_func($this->mailer, $stringMessage, implode(', ', (array)$this->email), $exceptionFile);
$this->sentEmailsPerRequest++;
}
}
}
/**
* @internal
*/
public function defaultMailer($message, string $email, ?string $attachment = null): void
{
$host = preg_replace('#[^\w.-]+#', '', $_SERVER['HTTP_HOST'] ?? php_uname('n'));
$separator = md5(time());
$eol = "\n";
$body = '';
if ($this->includeErrorMessage) {
$body =
"--" . $separator . $eol .
// Text email
"Content-Type: text/plain; charset=\"UTF-8\"" . $eol .
"Content-Transfer-Encoding: 8bit" . $eol . $eol .
$this->formatMessage($message) . "\n\nsource: " . Helpers::getSource() . $eol .
"--" . $separator . $eol;
if ($attachment) {
$body .=
// Attachment
"Content-Type: application/octet-stream; name=\"" . basename($attachment) . "\"" . $eol .
"Content-Transfer-Encoding: base64" . $eol .
"Content-Disposition: attachment" . $eol . $eol .
chunk_split(base64_encode(file_get_contents($attachment))) . $eol .
"--" . $separator . "--";
}
}
$parts = str_replace(
["\r\n", "\n"],
["\n", PHP_EOL],
[
'headers' => implode("\n", [
'X-Mailer: Tracy',
'MIME-Version: 1.0',
'Content-Type: multipart/mixed; boundary="' . $separator . '"',
'Content-Transfer-Encoding: 7bit',
]) . "\n",
'subject' => "PHP: An error occurred on the server $host",
'body' => $body
]
);
mail($email, $parts['subject'], $parts['body'], $parts['headers']);
}
protected function hideSensitiveFieldValue($array): array
{
$sensitiveFields = $this->sensitiveFields;
$replacement = '*****';
array_walk_recursive($array, function (&$value, $key) use ($sensitiveFields, $replacement) {
if (in_array($key, $sensitiveFields, TRUE)) {
$value = $replacement;
}
});
return $array;
}
}