|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kitodo\Dlf\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* (c) Kitodo. Key to digital objects e.V. <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of the Kitodo and TYPO3 projects. |
|
9
|
|
|
* |
|
10
|
|
|
* @license GNU General Public License version 3 or later. |
|
11
|
|
|
* For the full copyright and license information, please read the |
|
12
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
use DOMDocument; |
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use Kitodo\Dlf\Validation\DOMDocumentValidationStack; |
|
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
19
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
20
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
21
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
22
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
23
|
|
|
use TYPO3\CMS\Core\Http\ResponseFactory; |
|
24
|
|
|
use TYPO3\CMS\Core\Log\LogManager; |
|
25
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
26
|
|
|
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; |
|
27
|
|
|
use TYPO3\CMS\Extbase\Error\Result; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Middleware for validation of DOMDocuments. |
|
31
|
|
|
* |
|
32
|
|
|
* @package TYPO3 |
|
33
|
|
|
* @subpackage dlf |
|
34
|
|
|
* @access public |
|
35
|
|
|
*/ |
|
36
|
|
|
class DOMDocumentValidation implements MiddlewareInterface |
|
37
|
|
|
{ |
|
38
|
|
|
use LoggerAwareTrait; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The main method of the middleware. |
|
42
|
|
|
* |
|
43
|
|
|
* @access public |
|
44
|
|
|
* |
|
45
|
|
|
* @param ServerRequestInterface $request for processing |
|
46
|
|
|
* @param RequestHandlerInterface $handler for processing |
|
47
|
|
|
* |
|
48
|
|
|
* @throws InvalidArgumentException |
|
49
|
|
|
* |
|
50
|
|
|
* @return ResponseInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
53
|
|
|
{ |
|
54
|
|
|
$this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(static::class); |
|
55
|
|
|
$response = $handler->handle($request); |
|
56
|
|
|
// parameters are sent by POST --> use getParsedBody() instead of getQueryParams() |
|
57
|
|
|
$parameters = $request->getQueryParams(); |
|
58
|
|
|
|
|
59
|
|
|
// Return if not this middleware |
|
60
|
|
|
if (!isset($parameters['middleware']) || ($parameters['middleware'] != 'dlf/domDocumentValidation')) { |
|
61
|
|
|
return $response; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$urlParam = $parameters['url']; |
|
65
|
|
|
if (!isset($urlParam)) { |
|
66
|
|
|
throw new InvalidArgumentException('URL parameter is missing.', 1724334674); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** @var ConfigurationManagerInterface $configurationManager */ |
|
70
|
|
|
$configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class); |
|
71
|
|
|
$settings = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS); |
|
72
|
|
|
|
|
73
|
|
|
if (!array_key_exists("domDocumentValidationValidators", $settings)) { |
|
74
|
|
|
$this->logger->error('DOMDocumentValidation is not configured correctly.'); |
|
75
|
|
|
throw new InvalidArgumentException('DOMDocumentValidation is not configured correctly.', 1724335601); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$validation = GeneralUtility::makeInstance(DOMDocumentValidationStack::class, $settings['domDocumentValidationValidators']); |
|
79
|
|
|
|
|
80
|
|
|
if (!GeneralUtility::isValidUrl($urlParam)) { |
|
81
|
|
|
$this->logger->debug('Parameter "' . $urlParam . '" is not a valid url.'); |
|
82
|
|
|
throw new InvalidArgumentException('Value of url parameter is not a valid url.', 1724852611); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$content = GeneralUtility::getUrl($urlParam); |
|
86
|
|
|
if ($content === false) { |
|
87
|
|
|
$this->logger->debug('Error while loading content of "' . $urlParam . '"'); |
|
88
|
|
|
throw new InvalidArgumentException('Error while loading content of url.', 1724420640); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$document = new DOMDocument(); |
|
92
|
|
|
if ($document->loadXML($content) === false) { |
|
93
|
|
|
$this->logger->debug('Error converting content of "' . $urlParam . '" to xml.'); |
|
94
|
|
|
throw new InvalidArgumentException('Error converting content to xml.', 1724420648); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$result = $validation->validate($document); |
|
98
|
|
|
return $this->getJsonResponse($result); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
protected function getJsonResponse(Result $result): ResponseInterface |
|
102
|
|
|
{ |
|
103
|
|
|
$resultContent = ["valid" => !$result->hasErrors()]; |
|
104
|
|
|
|
|
105
|
|
|
foreach ($result->getErrors() as $error) { |
|
106
|
|
|
$resultContent["results"][$error->getTitle()][] = $error->getMessage(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** @var ResponseFactory $responseFactory */ |
|
110
|
|
|
$responseFactory = GeneralUtility::makeInstance(ResponseFactory::class); |
|
111
|
|
|
$response = $responseFactory->createResponse() |
|
112
|
|
|
->withHeader('Content-Type', 'application/json; charset=utf-8'); |
|
113
|
|
|
$response->getBody()->write(json_encode($resultContent)); |
|
114
|
|
|
return $response; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|