|
1
|
|
|
<?php |
|
2
|
|
|
namespace Aoe\Restler\System; |
|
3
|
|
|
|
|
4
|
|
|
/*************************************************************** |
|
5
|
|
|
* Copyright notice |
|
6
|
|
|
* |
|
7
|
|
|
* (c) 2015 AOE GmbH <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* All rights reserved |
|
10
|
|
|
* |
|
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
|
12
|
|
|
* free software; you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU General Public License as published by |
|
14
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
15
|
|
|
* (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* The GNU General Public License can be found at |
|
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
|
19
|
|
|
* |
|
20
|
|
|
* This script is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
|
26
|
|
|
***************************************************************/ |
|
27
|
|
|
|
|
28
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
29
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
30
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
31
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
32
|
|
|
use TYPO3\CMS\Core\Http\Response; |
|
33
|
|
|
use TYPO3\CMS\Core\Http\Stream; |
|
34
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManager; |
|
35
|
|
|
|
|
36
|
|
|
class Dispatcher extends RestlerBuilderAware implements MiddlewareInterface |
|
37
|
|
|
{ |
|
38
|
|
|
public function __construct(ObjectManager $objectManager = null) |
|
39
|
|
|
{ |
|
40
|
|
|
parent::__construct($objectManager); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
/** |
|
44
|
|
|
* Process an incoming server request. |
|
45
|
1 |
|
* |
|
46
|
1 |
|
* Processes an incoming server request in order to produce a response. |
|
47
|
|
|
* If unable to produce the response itself, it may delegate to the provided |
|
48
|
|
|
* request handler to do so. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
51
|
1 |
|
{ |
|
52
|
|
|
if ($this->isRestlerPrefix($this->extractSiteUrl($request))) { |
|
53
|
1 |
|
$restlerObj = $this->getRestlerBuilder()->build($request); |
|
54
|
1 |
|
|
|
55
|
1 |
|
if ($this->isRestlerUrl('/' . $restlerObj->url)) { |
|
56
|
|
|
/** |
|
57
|
|
|
* We might end up with a loaded TSFE->config but an empty |
|
58
|
|
|
* TSFE->tmpl->setup. That is depending on the state of the caches. |
|
59
|
|
|
* This in turn will lead to an empty extbase configuration. |
|
60
|
|
|
* And this will lead to failures loading sys_file_reference |
|
61
|
|
|
* as it will use the default tableName of tx_extbase_domain_model_filereference |
|
62
|
|
|
*/ |
|
63
|
|
|
// See https://review.typo3.org/c/Packages/TYPO3.CMS/+/60713 for reasons |
|
64
|
|
|
|
|
65
|
|
|
// check for proper template config state |
|
66
|
|
|
if (!$GLOBALS['TSFE']->tmpl->loaded) { |
|
67
|
|
|
if (empty($GLOBALS['TSFE']->rootLine) && !empty($GLOBALS['TSFE']->id)) { |
|
68
|
|
|
$GLOBALS['TSFE']->determineId(); |
|
69
|
|
|
if ($GLOBALS['TSFE']->tmpl === null) { |
|
70
|
|
|
$GLOBALS['TSFE']->getConfigArray(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (!empty($GLOBALS['TSFE']->tmpl) && !empty($GLOBALS['TSFE']->rootLine)) { |
|
75
|
|
|
$GLOBALS['TSFE']->tmpl->start($GLOBALS['TSFE']->rootLine); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// wrap reponse into a stream to pass along to the rest of the Typo3 framework |
|
80
|
|
|
$body = new Stream('php://temp', 'wb+'); |
|
81
|
|
|
$body->write($restlerObj->handle()); |
|
82
|
|
|
$body->rewind(); |
|
83
|
|
|
|
|
84
|
|
|
return new Response($body, $restlerObj->responseCode); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $handler->handle($request); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function isRestlerUrl($uri): bool |
|
92
|
|
|
{ |
|
93
|
|
|
return \Aoe\Restler\System\Restler\Routes::containsUrl($uri); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function extractSiteUrl($request) |
|
97
|
|
|
{ |
|
98
|
|
|
// set base path depending on site config |
|
99
|
|
|
$site = $request->getAttribute('site'); |
|
100
|
|
|
if ($site !== null && $site instanceof \TYPO3\CMS\Core\Site\Entity\Site) { |
|
101
|
|
|
$siteBasePath = $request->getAttribute('site')->getBase()->getPath(); |
|
102
|
|
|
if ($siteBasePath !== '/' && $siteBasePath[-1] !== '/') { |
|
103
|
|
|
$siteBasePath .= '/'; |
|
104
|
|
|
} |
|
105
|
|
|
} else { |
|
106
|
|
|
$siteBasePath = '/'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// set url with base path removed |
|
110
|
|
|
return '/' . rtrim(preg_replace('%^' . preg_quote($siteBasePath, '%') . '%', '', $request->getUri()->getPath()), '/'); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|