[TASK] Rename ImageManipulationWizard to ImageManipulationController
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Controller / Wizard / ImageManipulationController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Controller\Wizard;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Core\Http\HtmlResponse;
21 use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
22 use TYPO3\CMS\Core\Resource\ResourceFactory;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Core\Utility\MathUtility;
25 use TYPO3\CMS\Fluid\View\StandaloneView;
26
27 /**
28 * Wizard for rendering image manipulation view
29 */
30 class ImageManipulationController
31 {
32 /**
33 * @var StandaloneView
34 */
35 private $templateView;
36
37 /**
38 * @param StandaloneView $templateView
39 */
40 public function __construct(StandaloneView $templateView = null)
41 {
42 if (!$templateView) {
43 $templateView = GeneralUtility::makeInstance(StandaloneView::class);
44 $templateView->setLayoutRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Layouts/')]);
45 $templateView->setPartialRootPaths([GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Partials/ImageManipulation/')]);
46 $templateView->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:backend/Resources/Private/Templates/ImageManipulation/ImageManipulationWizard.html'));
47 }
48 $this->templateView = $templateView;
49 }
50
51 /**
52 * Returns the HTML for the wizard inside the modal
53 *
54 * @param ServerRequestInterface $request
55 * @return ResponseInterface $response
56 */
57 public function getWizardContent(ServerRequestInterface $request): ResponseInterface
58 {
59 if ($this->isSignatureValid($request)) {
60 $queryParams = json_decode($request->getQueryParams()['arguments'], true);
61 $fileUid = $queryParams['image'];
62 $image = null;
63 if (MathUtility::canBeInterpretedAsInteger($fileUid)) {
64 try {
65 $image = ResourceFactory::getInstance()->getFileObject($fileUid);
66 } catch (FileDoesNotExistException $e) {
67 }
68 }
69 $viewData = [
70 'image' => $image,
71 'cropVariants' => $queryParams['cropVariants']
72 ];
73 $content = $this->templateView->renderSection('Main', $viewData);
74 return new HtmlResponse($content);
75 }
76 return new HtmlResponse('', 403);
77 }
78
79 /**
80 * Check if hmac signature is correct
81 *
82 * @param ServerRequestInterface $request the request with the GET parameters
83 * @return bool
84 */
85 protected function isSignatureValid(ServerRequestInterface $request): bool
86 {
87 $token = GeneralUtility::hmac($request->getQueryParams()['arguments'], 'ajax_wizard_image_manipulation');
88 return hash_equals($token, $request->getQueryParams()['signature']);
89 }
90 }