1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the TYPO3 CMS project. |
5
|
|
|
* |
6
|
|
|
* It is free software; you can redistribute it and/or modify it under |
7
|
|
|
* the terms of the GNU General Public License, either version 2 |
8
|
|
|
* of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please read the |
11
|
|
|
* LICENSE.txt file that was distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* The TYPO3 project - inspiring people to share! |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace ApacheSolrForTypo3\Solr\Eid; |
17
|
|
|
|
18
|
|
|
use ApacheSolrForTypo3\Solr\Api; |
19
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Site\SiteHashService; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
22
|
|
|
use TYPO3\CMS\Core\Http\ImmediateResponseException; |
23
|
|
|
use TYPO3\CMS\Core\Http\JsonResponse; |
24
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
25
|
|
|
|
26
|
|
|
class ApiEid |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Globally required params |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected const REQUIRED_PARAMS_GLOBAL = [ |
33
|
|
|
'api', |
34
|
|
|
'apiKey', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Available methods and params |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected const API_METHODS = [ |
43
|
|
|
'siteHash' => [ |
44
|
|
|
'params' => [ |
45
|
|
|
'required' => [ |
46
|
|
|
'domain', |
47
|
|
|
], |
48
|
|
|
], |
49
|
|
|
], |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The main method for eID scripts. |
54
|
|
|
* |
55
|
|
|
* @throws ImmediateResponseException |
56
|
|
|
*/ |
57
|
|
|
public function main(ServerRequestInterface $request): ResponseInterface |
58
|
|
|
{ |
59
|
|
|
$this->validateRequest($request); |
60
|
|
|
return $this->{'get' . ucfirst($request->getQueryParams()['api']) . 'Response'}($request); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the site hash |
65
|
|
|
* |
66
|
|
|
* @param ServerRequestInterface $request |
67
|
|
|
* @return JsonResponse |
68
|
|
|
* @noinspection PhpUnused |
69
|
|
|
*/ |
70
|
|
|
protected function getSiteHashResponse(ServerRequestInterface $request): JsonResponse |
71
|
|
|
{ |
72
|
|
|
$domain = $request->getQueryParams()['domain']; |
73
|
|
|
|
74
|
|
|
/* @var SiteHashService $siteHashService */ |
75
|
|
|
$siteHashService = GeneralUtility::makeInstance(SiteHashService::class); |
76
|
|
|
$siteHash = $siteHashService->getSiteHashForDomain($domain); |
77
|
|
|
return new JsonResponse( |
78
|
|
|
['sitehash' => $siteHash] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Validates request. |
84
|
|
|
* |
85
|
|
|
* @throws ImmediateResponseException |
86
|
|
|
*/ |
87
|
|
|
protected function validateRequest(ServerRequestInterface $request): void |
88
|
|
|
{ |
89
|
|
|
$params = $request->getQueryParams(); |
90
|
|
|
if (!Api::isValidApiKey($params['apiKey'])) { |
91
|
|
|
throw new ImmediateResponseException( |
92
|
|
|
new JsonResponse( |
93
|
|
|
['errorMessage' => 'Invalid API key'], |
94
|
|
|
403, |
95
|
|
|
), |
96
|
|
|
403 |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($params['api'] === null || !array_key_exists($params['api'], self::API_METHODS)) { |
101
|
|
|
throw new ImmediateResponseException( |
102
|
|
|
new JsonResponse( |
103
|
|
|
[ |
104
|
|
|
'errorMessage' => 'You must provide an available API method, e.g. siteHash. See: available methods in methods key.', |
105
|
|
|
'methods' => $this->getApiMethodDefinitions(), |
106
|
|
|
], |
107
|
|
|
400 |
108
|
|
|
), |
109
|
|
|
400 |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$requiredApiParams = $this->getApiMethodDefinitions()[$params['api']]['params']['required'] ?? []; |
114
|
|
|
$requiredApiParams[] = 'eID'; |
115
|
|
|
$missingParams = array_values(array_diff($requiredApiParams, array_keys($request->getQueryParams()))); |
116
|
|
|
if (!empty($missingParams)) { |
117
|
|
|
throw new ImmediateResponseException( |
118
|
|
|
new JsonResponse( |
119
|
|
|
[ |
120
|
|
|
'errorMessage' => 'Required API params are not provided. See: methods.', |
121
|
|
|
'missing_params' => $missingParams, |
122
|
|
|
'methods' => $this->getApiMethodDefinitions()[$params['api']], |
123
|
|
|
], |
124
|
|
|
400 |
125
|
|
|
), |
126
|
|
|
400 |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the available methods and their params. |
133
|
|
|
*/ |
134
|
|
|
protected function getApiMethodDefinitions(): array |
135
|
|
|
{ |
136
|
|
|
$apiMethodDefinitions = self::API_METHODS; |
137
|
|
|
foreach ($apiMethodDefinitions as $apiMethodName => $apiMethodDefinition) { |
138
|
|
|
$apiMethodDefinitions[$apiMethodName]['params']['required'] = array_merge( |
139
|
|
|
self::REQUIRED_PARAMS_GLOBAL, |
140
|
|
|
$apiMethodDefinition['params']['required'] ?? [] |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
return $apiMethodDefinitions; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|