‪TYPO3CMS  ‪main
ResourceController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
29 
35 {
36  public function ‪__construct(
37  protected readonly ‪ScssProcessor $scssProcessor
38  ) {
39  }
40 
41  public function ‪stylesheetAction(ServerRequestInterface $request): ResponseInterface
42  {
43  if ($request->getMethod() !== 'GET') {
44  return (new ‪NullResponse())->withStatus(404);
45  }
46  $queryParams = $request->getQueryParams();
47  $params = (string)($queryParams['params'] ?? '');
48  $hmac = (string)($queryParams['hmac'] ?? '');
49  if ($hmac !== $this->‪hmac($params, 'stylesheet')) {
50  return (new ‪NullResponse())->withStatus(400);
51  }
52  // @todo additional checks whether file is local, not remote...
53 
54  $styleSrcParams = json_decode($queryParams['params'] ?? '', true);
55  $styleSrc = (string)($styleSrcParams['styleSrc'] ?? '');
56  $cssPrefix = (string)($styleSrcParams['cssPrefix'] ?? '');
57  $styleSrcPath = ‪Environment::getPublicPath() . $styleSrc;
58  $styleSrcContent = file_get_contents($styleSrcPath);
59  $styleSrcHash = sha1(json_encode([$cssPrefix, $styleSrcContent]));
60 
61  $cacheIdentifier = 'rte-resource-stylesheet%' . $styleSrcHash;
62  $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('assets');
63 
64  if ($cache->has($cacheIdentifier)) {
65  $compiledStyles = $cache->get($cacheIdentifier);
66  } else {
67  if (trim($cssPrefix) !== '') {
68  $source = $this->scssProcessor->prefixCssForScss($cssPrefix, $styleSrcContent);
69  $compiledStyles = $this->scssProcessor->compileToCss($source);
70  } else {
71  $compiledStyles = $styleSrcContent;
72  }
73  $cache->set($cacheIdentifier, $compiledStyles);
74  }
75 
76  $stylesStream = new ‪Stream('php://temp', 'w');
77  $stylesStream->write($compiledStyles);
78  // @todo consider sending cache/expiration headers to browser
79  return new ‪Response($stylesStream, 200, ['Content-Type' => 'text/css']);
80  }
81 
82  // this is shit and needs to be a general token/payload component, having signatures (JWT?)
83  public function ‪hmac(string $payload, string $scope): string
84  {
85  return ‪GeneralUtility::hmac($payload, self::class . '::' . $scope);
86  }
87 }
‪TYPO3\CMS\RteCKEditor\Controller\ResourceController
Definition: ResourceController.php:35
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\RteCKEditor\Controller\ResourceController\__construct
‪__construct(protected readonly ScssProcessor $scssProcessor)
Definition: ResourceController.php:36
‪TYPO3\CMS\Core\Http\NullResponse
Definition: NullResponse.php:26
‪TYPO3\CMS\RteCKEditor\Controller\ResourceController\hmac
‪hmac(string $payload, string $scope)
Definition: ResourceController.php:83
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility\hmac
‪static string hmac($input, $additionalSecret='')
Definition: GeneralUtility.php:475
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:31
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\RteCKEditor\Controller\ResourceController\stylesheetAction
‪stylesheetAction(ServerRequestInterface $request)
Definition: ResourceController.php:41
‪TYPO3\CMS\RteCKEditor\Controller
Definition: BrowseLinksController.php:18
‪TYPO3\CMS\RteCKEditor\Service\ScssProcessor
Definition: ScssProcessor.php:29
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51