[TASK] Create own response instance in controller actions
[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\Http\HtmlResponse;
20 use TYPO3\CMS\Core\Resource\Hook\FileDumpEIDHookInterface;
21 use TYPO3\CMS\Core\Resource\ProcessedFileRepository;
22 use TYPO3\CMS\Core\Resource\ResourceFactory;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Core\Utility\HttpUtility;
25
26 /**
27 * Class FileDumpController
28 */
29 class FileDumpController
30 {
31 /**
32 * Main method to dump a file
33 *
34 * @param ServerRequestInterface $request
35 * @return ResponseInterface|null
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)
43 {
44 $parameters = ['eID' => 'dumpFile'];
45 $t = $this->getGetOrPost($request, 't');
46 if ($t) {
47 $parameters['t'] = $t;
48 }
49 $f = $this->getGetOrPost($request, 'f');
50 if ($f) {
51 $parameters['f'] = $f;
52 }
53 $p = $this->getGetOrPost($request, 'p');
54 if ($p) {
55 $parameters['p'] = $p;
56 }
57
58 if (hash_equals(GeneralUtility::hmac(implode('|', $parameters), 'resourceStorageDumpFile'), $this->getGetOrPost($request, 'token'))) {
59 if (isset($parameters['f'])) {
60 try {
61 $file = ResourceFactory::getInstance()->getFileObject($parameters['f']);
62 if ($file->isDeleted() || $file->isMissing()) {
63 $file = null;
64 }
65 } catch (\Exception $e) {
66 $file = null;
67 }
68 } else {
69 $file = GeneralUtility::makeInstance(ProcessedFileRepository::class)->findByUid($parameters['p']);
70 if (!$file || $file->isDeleted()) {
71 $file = null;
72 }
73 }
74
75 if ($file === null) {
76 HttpUtility::setResponseCodeAndExit(HttpUtility::HTTP_STATUS_404);
77 }
78
79 // Hook: allow some other process to do some security/access checks. Hook should issue 403 if access is rejected
80 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['FileDumpEID.php']['checkFileAccess'] ?? [] as $className) {
81 $hookObject = GeneralUtility::makeInstance($className);
82 if (!$hookObject instanceof FileDumpEIDHookInterface) {
83 throw new \UnexpectedValueException($className . ' must implement interface ' . FileDumpEIDHookInterface::class, 1394442417);
84 }
85 $hookObject->checkFileAccess($file);
86 }
87 $file->getStorage()->dumpFileContents($file);
88 // @todo Refactor FAL to not echo directly, but to implement a stream for output here and use response
89 return null;
90 }
91 return new HtmlResponse('', 403);
92 }
93
94 /**
95 * @param ServerRequestInterface $request
96 * @param string $parameter
97 * @return mixed|null
98 */
99 protected function getGetOrPost(ServerRequestInterface $request, $parameter)
100 {
101 return $request->getParsedBody()[$parameter] ?? $request->getQueryParams()[$parameter] ?? null;
102 }
103 }