[TASK] Have eIDs with PSR-7 without ControllerInterface
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Controller / FileDumpController.php
1 <?php
2 namespace TYPO3\CMS\Core\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Resource\Hook\FileDumpEIDHookInterface;
20 use TYPO3\CMS\Core\Resource\ProcessedFileRepository;
21 use TYPO3\CMS\Core\Resource\ResourceFactory;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Core\Utility\HttpUtility;
24
25 /**
26 * Class FileDumpController
27 */
28 class FileDumpController {
29
30 /**
31 * Main method to dump a file
32 *
33 * @param ServerRequestInterface $request
34 * @param ResponseInterface $response
35 * @return NULL|ResponseInterface
36 *
37 * @throws \InvalidArgumentException
38 * @throws \RuntimeException
39 * @throws \TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
40 * @throws \UnexpectedValueException
41 */
42 public function dumpAction(ServerRequestInterface $request, ResponseInterface $response) {
43 $parameters = array('eID' => 'dumpFile');
44 $t = $this->getGetOrPost($request, 't');
45 if ($t) {
46 $parameters['t'] = $t;
47 }
48 $f = $this->getGetOrPost($request, 'f');
49 if ($f) {
50 $parameters['f'] = $f;
51 }
52 $p = $this->getGetOrPost($request, 'p');
53 if ($p) {
54 $parameters['p'] = $p;
55 }
56
57 if (GeneralUtility::hmac(implode('|', $parameters), 'resourceStorageDumpFile') === $this->getGetOrPost($request, 'token')) {
58 if (isset($parameters['f'])) {
59 $file = ResourceFactory::getInstance()->getFileObject($parameters['f']);
60 if ($file->isDeleted() || $file->isMissing()) {
61 $file = NULL;
62 }
63 } else {
64 $file = GeneralUtility::makeInstance(ProcessedFileRepository::class)->findByUid($parameters['p']);
65 if ($file->isDeleted()) {
66 $file = NULL;
67 }
68 }
69
70 if ($file === NULL) {
71 HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_404);
72 }
73
74 // Hook: allow some other process to do some security/access checks. Hook should issue 403 if access is rejected
75 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['FileDumpEID.php']['checkFileAccess'])) {
76 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['FileDumpEID.php']['checkFileAccess'] as $classRef) {
77 $hookObject = GeneralUtility::getUserObj($classRef);
78 if (!$hookObject instanceof FileDumpEIDHookInterface) {
79 throw new \UnexpectedValueException('FileDump hook object must implement interface ' . FileDumpEIDHookInterface::class, 1394442417);
80 }
81 $hookObject->checkFileAccess($file);
82 }
83 }
84 $file->getStorage()->dumpFileContents($file);
85 // @todo Refactor FAL to not echo directly, but to implement a stream for output here and use response
86 return NULL;
87 } else {
88 return $response->withStatus(403);
89 }
90 }
91
92 /**
93 * @param ServerRequestInterface $request
94 * @param string $parameter
95 * @return NULL|mixed
96 */
97 protected function getGetOrPost(ServerRequestInterface $request, $parameter) {
98 return isset($request->getParsedBody()[$parameter])
99 ? $request->getParsedBody()[$parameter]
100 : (isset($request->getQueryParams()[$parameter]) ? $request->getQueryParams()[$parameter] : NULL);
101 }
102
103 }