|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
|
7
|
|
|
* |
|
8
|
|
|
* @license GNU General Public License version 3 or later. |
|
9
|
|
|
* For the full copyright and license information, please read the |
|
10
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Kitodo\Dlf\Plugin\Eid; |
|
14
|
|
|
|
|
15
|
|
|
use Kitodo\Dlf\Common\StdOutStream; |
|
16
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
19
|
|
|
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; |
|
20
|
|
|
use TYPO3\CMS\Core\Http\JsonResponse; |
|
21
|
|
|
use TYPO3\CMS\Core\Http\RequestFactory; |
|
22
|
|
|
use TYPO3\CMS\Core\Http\Response; |
|
23
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
24
|
|
|
use TYPO3\CMS\Core\Utility\MathUtility; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* eID image proxy for plugin 'Page View' of the 'dlf' extension |
|
28
|
|
|
* |
|
29
|
|
|
* Supported query parameters: |
|
30
|
|
|
* - `url` (mandatory): The URL to be proxied |
|
31
|
|
|
* - `header` (optional) |
|
32
|
|
|
* - `1`: Request headers and body (via GET) |
|
33
|
|
|
* - `2`: Request only headers (via HEAD) |
|
34
|
|
|
* |
|
35
|
|
|
* @author Alexander Bigga <[email protected]> |
|
36
|
|
|
* @package TYPO3 |
|
37
|
|
|
* @subpackage dlf |
|
38
|
|
|
* @access public |
|
39
|
|
|
*/ |
|
40
|
|
|
class PageViewProxy |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* @var RequestFactoryInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $requestFactory; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var mixed |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $extConf; |
|
51
|
|
|
|
|
52
|
|
|
public function __construct() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->requestFactory = GeneralUtility::makeInstance(RequestFactory::class); |
|
55
|
|
|
$this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('dlf'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get headers to be sent to the proxied target. |
|
60
|
|
|
* |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function getRequestHeaders(): array |
|
64
|
|
|
{ |
|
65
|
|
|
return [ |
|
66
|
|
|
'User-Agent' => $this->extConf['useragent'] ?? 'Kitodo.Presentation Proxy', |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The main method of the eID script |
|
72
|
|
|
* |
|
73
|
|
|
* @access public |
|
74
|
|
|
* |
|
75
|
|
|
* @param ServerRequestInterface $request |
|
76
|
|
|
* @return ResponseInterface |
|
77
|
|
|
*/ |
|
78
|
|
|
public function main(ServerRequestInterface $request) |
|
79
|
|
|
{ |
|
80
|
|
|
$queryParams = $request->getQueryParams(); |
|
81
|
|
|
$url = (string) ($queryParams['url'] ?? ''); |
|
82
|
|
|
|
|
83
|
|
|
if (!GeneralUtility::isValidUrl($url)) { |
|
84
|
|
|
return new JsonResponse(['message' => 'Did not receive a valid URL.'], 400); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// This has previously been used as parameter for `GeneralUtility::getUrl()` |
|
88
|
|
|
// A value of 0 would indicate not to fetch headers, which we'll ignore. |
|
89
|
|
|
$header = (int) ($queryParams['header'] ?? 1); |
|
90
|
|
|
$headerOnly = MathUtility::forceIntegerInRange($header, 0, 2, 0) === 2; |
|
91
|
|
|
|
|
92
|
|
|
$httpMethod = $headerOnly ? 'HEAD' : 'GET'; |
|
93
|
|
|
try { |
|
94
|
|
|
$response = $this->requestFactory->request($url, $httpMethod, [ |
|
|
|
|
|
|
95
|
|
|
'headers' => $this->getRequestHeaders(), |
|
96
|
|
|
|
|
97
|
|
|
// For performance, don't download content up-front. Rather, we'll |
|
98
|
|
|
// download and upload simultaneously. |
|
99
|
|
|
// https://docs.guzzlephp.org/en/6.5/request-options.html#stream |
|
100
|
|
|
'stream' => true, |
|
101
|
|
|
|
|
102
|
|
|
// Don't throw exceptions when a non-success status code is |
|
103
|
|
|
// received. We handle these manually. |
|
104
|
|
|
'http_errors' => false, |
|
105
|
|
|
]); |
|
106
|
|
|
} catch (\Exception $e) { |
|
107
|
|
|
return new JsonResponse(['message' => 'Could not fetch resource of given URL.'], 500); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$body = new StdOutStream($response->getBody()); |
|
111
|
|
|
|
|
112
|
|
|
return GeneralUtility::makeInstance(Response::class) |
|
113
|
|
|
->withStatus($response->getStatusCode()) |
|
114
|
|
|
->withHeader('Access-Control-Allow-Methods', 'GET') |
|
115
|
|
|
->withHeader('Access-Control-Allow-Origin', (string) ($request->getHeaderLine('Origin') ? : '*')) |
|
116
|
|
|
->withHeader('Access-Control-Max-Age', '86400') |
|
117
|
|
|
->withHeader('Content-Type', (string) $response->getHeader('Content-Type')[0]) |
|
118
|
|
|
->withHeader('Last-Modified', (string) $response->getHeader('Last-Modified')[0]) |
|
119
|
|
|
->withBody($body); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.