-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTErrorPresenter.php
More file actions
88 lines (71 loc) · 2.99 KB
/
Copy pathTErrorPresenter.php
File metadata and controls
88 lines (71 loc) · 2.99 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
<?php
namespace ADT\Utils;
use Nette\Application\BadRequestException;
use Nette\Application\Helpers;
use Nette\Routing\Router;
use Tracy\Debugger;
use Tracy\ILogger;
trait TErrorPresenter
{
protected $exception;
protected bool $log404 = true;
protected bool $log500 = true;
/** @persistent */
public $url;
public function __construct(Router $router)
{
parent::__construct();
$this->onStartup[] = function() use ($router) {
// kvuli url typu /adadsa%0D%0ASet-Cookie:crlfinjection=crlfinjection, ktera ani nematchne [<url .*>] catchall routu
preg_match('#^[^\s]+#', $this->getHttpRequest()->getUrl(), $matches);
if ($matches[0] !== (string) $this->getHttpRequest()->getUrl()) {
$this->redirectUrl($matches[0]);
}
$this->exception = $this->getRequest()->getParameter('exception');
// nemusi existovat zadna routa odpovidajici zadane url
// abychom mohli pouzivat $this->link('this'), musime vytvorit routu, ktera matchne zadanou url
[$moduleName, $presenterName] = Helpers::splitName($this->getName());
if ($moduleName) {
foreach ($router->getRouters() as $_routeList) {
if ($_routeList->getModule() === $moduleName . ':') {
/** @var \ADT\Routing\RouteList $routeList */
$routeList = $_routeList;
break;
}
}
} else {
/** @var \ADT\Routing\RouteList $routeList */
$routeList = $router;
}
// vytvorime routu v presnem zneni soucasne url adresy
$route = $routeList->createRoute('[<url .*>]', $presenterName . ':' . $this->getAction());
$routeList->prepend($route);
$params = $route->match($this->getHttpRequest());
// je potreba, aby fungovaly persistentni parametry, napriklad "locale"
$this->loadState($params);
// BadRequst muze mit bud kod 404 (neexistuji stranka) nebo 403 (neexistujici handle)
if ($this->exception instanceof BadRequestException) {
// je potreba resit rucne, protoze vyhodnocovani signalu probehlo jeste pred FORWARDovanim do ErrorPresenteru
// v ErrorPresenteru uz se nic nevyhodnocuje
if (isset($params[static::SIGNAL_KEY]) && $params['do'] === '404') {
$this->handle404($params['referrer'] ?? null);
}
}
register_shutdown_function(function () {
if ($this->exception instanceof BadRequestException && $this->log404) {
echo "<script>" . PHP_EOL;
require __DIR__ . '/assets/bot-detector.js';
echo "new BotDetector({ callback: function(result) { if (!result.isBot) navigator.sendBeacon('" . $this->link('404!', ['referrer' => $this->getHttpRequest()->getReferer() ? $this->getHttpRequest()->getReferer()->getAbsoluteUrl() : null]) . "'); } }).monitor();" . PHP_EOL;
echo "</script>";
} elseif (!$this->exception instanceof BadRequestException && $this->log500) {
Debugger::log($this->exception, ILogger::EXCEPTION);
}
});
};
}
public function handle404(?string $referrer)
{
Debugger::log('Error 404 with ' . ($referrer ?: 'no' ) . ' referrer (' . $_SERVER['HTTP_USER_AGENT'] . '; ' . $_SERVER['REMOTE_ADDR'] . ')', '404');
die();
}
}