-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathErrorHandler.php
More file actions
167 lines (143 loc) · 5.75 KB
/
ErrorHandler.php
File metadata and controls
167 lines (143 loc) · 5.75 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
<?php
namespace Kitzberger\FourOhExHandler;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface;
use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerNotConfiguredException;
use TYPO3\CMS\Core\Http\RedirectResponse;
use TYPO3\CMS\Core\Log\LogLevel;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
class ErrorHandler implements PageErrorHandlerInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* @var int
*/
protected $statusCode;
/**
* @var array
*/
protected $errorHandlerConfiguration;
/**
* @var array
*/
protected $extConf;
public function __construct(int $statusCode = null, array $configuration = [])
{
$this->statusCode = $statusCode;
$this->errorHandlerConfiguration = $configuration;
}
/**
* Modern error handler (TYPO3 9+)
*
* @param ServerRequestInterface $request
* @param string $message
* @param array $reasons
* @return [type]
*/
public function handlePageError(ServerRequestInterface $request, string $message, array $reasons = []): ResponseInterface
{
$this->log(LogLevel::INFO, 'fox_handler triggered!');
$this->log(LogLevel::DEBUG, print_r([$message, $reasons], true));
if ($this->statusCode === 403) {
if (isset($reasons['fe_group'])) {
if ($reasons['fe_group'] != ['' => 0]) {
$redirect_url = rawurlencode((string)$request->getUri());
$fe_groups = $reasons['fe_group'];
$fe_groups = array_pop($fe_groups);
$fe_groups = GeneralUtility::intExplode(',', $fe_groups, true);
foreach ($fe_groups as $fe_group) {
$login_pid = $this->getPageUid_403($fe_group);
if (is_numeric($login_pid)) {
$login_url = $this->getTypoLink($login_pid, ['redirect_url' => $redirect_url]);
return $this->handle403($login_url);
}
}
}
}
}
// Fallback to configured 404 handler
$this->log(LogLevel::INFO, 'Fallback to 404 handler');
$site = $request->getAttribute('site');
if ($site instanceof Site) {
try {
$errorHandler = $site->getErrorHandler(404);
if ($errorHandler instanceof PageErrorHandlerInterface) {
return $errorHandler->handlePageError($request, $message, $reasons);
}
} catch (PageErrorHandlerNotConfiguredException $e) {
// No error handler found, so fallback back to the generic TYPO3 error handler.
}
}
throw new \Exception('Somethings really wrong!');
}
protected function handle403(string $login_url, int $status = 307): ResponseInterface
{
$this->log(LogLevel::INFO, 'Redirecting to 403 page: ' . $login_url);
return new RedirectResponse($login_url, $status);
}
protected function getPageUid_403($fe_group)
{
$page403 = null;
// Read setting from extension configuration
if (isset($this->getExtConf()['403_pages'])) {
$page403 = $this->getExtConf()['403_pages'];
$this->log(LogLevel::DEBUG, 'Read 403 settings from extension configuration: ' . $page403);
}
// Override with setting from site configuration (if given)
if (isset($this->errorHandlerConfiguration['403_pages'])) {
$page403 = $this->errorHandlerConfiguration['403_pages'];
$this->log(LogLevel::DEBUG, 'Override 403 settings via site configuration: ' . $page403);
}
if ($page403) {
if (is_numeric($page403)) {
$this->log(LogLevel::DEBUG, 'Found global 403 page: ' . $page403);
return $page403;
} else {
$mappings = GeneralUtility::trimExplode(',', $page403, true);
if (!empty($mappings)) {
foreach ($mappings as $mapping) {
list($group, $loginPage) = GeneralUtility::trimExplode('=', $mapping, true);
if ($group == $fe_group) {
$this->log(LogLevel::DEBUG, 'Found 403 page for group ' . $group . ': ' . $loginPage);
return $loginPage;
}
if ($group == '*') {
$this->log(LogLevel::DEBUG, 'Found 403 page for group wildcard: ' . $loginPage);
return $loginPage;
}
}
}
}
}
return false;
}
protected function getExtConf()
{
if (is_null($this->extConf)) {
$this->extConf = $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS']['fox_handler'] ?? [];
}
return $this->extConf;
}
protected function getTypoLink($pid, $params)
{
$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
$additionalParams = '';
foreach ($params as $key => $value) {
$additionalParams .= '&' . $key . '=' . $value;
}
return $cObj->typoLink_URL([
'forceAbsoluteUrl' => true,
'parameter' => $pid,
'additionalParams' => $additionalParams
]);
}
protected function log($level = LogLevel::DEBUG, $message = '')
{
$this->logger->log($level, $message);
}
}