‪TYPO3CMS  10.4
AjaxWidgetContextHolder.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
24 
33 {
40  protected ‪$widgetContexts = [];
41 
45  protected ‪$widgetContextsStorageKey = 'TYPO3\\CMS\\Fluid\\Core\\Widget\\AjaxWidgetContextHolder_widgetContexts';
46 
50  protected ‪$hashService;
51 
55  public function ‪__construct()
56  {
57  $this->hashService = GeneralUtility::makeInstance(HashService::class);
58  $this->‪loadWidgetContexts();
59  }
60 
64  protected function ‪loadWidgetContexts()
65  {
66  if (isset(‪$GLOBALS['TSFE']) && ‪$GLOBALS['TSFE'] instanceof ‪TypoScriptFrontendController) {
67  $this->widgetContexts = $this->‪unserializeWithHmac(‪$GLOBALS['TSFE']->fe_user->getKey('ses', $this->widgetContextsStorageKey));
68  } else {
69  $this->widgetContexts = isset(‪$GLOBALS['BE_USER']->uc[$this->widgetContextsStorageKey]) ? $this->‪unserializeWithHmac(‪$GLOBALS['BE_USER']->uc[$this->widgetContextsStorageKey]) : [];
70  ‪$GLOBALS['BE_USER']->writeUC();
71  }
72  }
73 
80  public function get($ajaxWidgetId)
81  {
82  if (!isset($this->widgetContexts[$ajaxWidgetId])) {
83  throw new ‪WidgetContextNotFoundException('No widget context was found for the Ajax Widget Identifier "' . $ajaxWidgetId . '". This only happens if AJAX URIs are called without including the widget on a page.', 1284793775);
84  }
85  return $this->widgetContexts[$ajaxWidgetId];
86  }
87 
94  public function ‪store(‪WidgetContext $widgetContext)
95  {
96  $ajaxWidgetId = md5(‪StringUtility::getUniqueId());
97  $widgetContext->‪setAjaxWidgetIdentifier($ajaxWidgetId);
98  $this->widgetContexts[$ajaxWidgetId] = $widgetContext;
99  $this->‪storeWidgetContexts();
100  }
101 
105  protected function ‪storeWidgetContexts()
106  {
107  if (isset(‪$GLOBALS['TSFE']) && ‪$GLOBALS['TSFE'] instanceof ‪TypoScriptFrontendController) {
108  ‪$GLOBALS['TSFE']->fe_user->setKey('ses', $this->widgetContextsStorageKey, $this->‪serializeWithHmac($this->widgetContexts));
109  ‪$GLOBALS['TSFE']->fe_user->storeSessionData();
110  } else {
111  ‪$GLOBALS['BE_USER']->uc[‪$this->widgetContextsStorageKey] = $this->‪serializeWithHmac($this->widgetContexts);
112  ‪$GLOBALS['BE_USER']->writeUC();
113  }
114  }
115 
116  protected function ‪serializeWithHmac(array ‪$widgetContexts): string
117  {
118  $data = serialize(‪$widgetContexts);
119  return $this->hashService->appendHmac($data);
120  }
121 
122  protected function ‪unserializeWithHmac(?string $data): array
123  {
124  if ($data === null || $data === '') {
125  return [];
126  }
127  try {
128  $data = $this->hashService->validateAndStripHmac($data);
129  // widget contexts literally can contain everything, that why using
130  // HMAC-signed `unserialize()` is the only option here unless Extbase
131  // structures have been refactored further
132  ‪$widgetContexts = unserialize($data);
133  } finally {
134  return is_array(‪$widgetContexts ?? null) ? ‪$widgetContexts : [];
135  }
136  }
137 }
‪TYPO3\CMS\Fluid\Core\Widget
Definition: AbstractWidgetController.php:16
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\__construct
‪__construct()
Definition: AjaxWidgetContextHolder.php:52
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\store
‪store(WidgetContext $widgetContext)
Definition: AjaxWidgetContextHolder.php:91
‪TYPO3\CMS\Fluid\Core\Widget\WidgetContext\setAjaxWidgetIdentifier
‪setAjaxWidgetIdentifier($ajaxWidgetIdentifier)
Definition: WidgetContext.php:117
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\loadWidgetContexts
‪loadWidgetContexts()
Definition: AjaxWidgetContextHolder.php:61
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:31
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\storeWidgetContexts
‪storeWidgetContexts()
Definition: AjaxWidgetContextHolder.php:102
‪TYPO3\CMS\Fluid\Core\Widget\Exception\WidgetContextNotFoundException
Definition: WidgetContextNotFoundException.php:26
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\serializeWithHmac
‪serializeWithHmac(array $widgetContexts)
Definition: AjaxWidgetContextHolder.php:113
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\unserializeWithHmac
‪unserializeWithHmac(?string $data)
Definition: AjaxWidgetContextHolder.php:119
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\$widgetContextsStorageKey
‪string $widgetContextsStorageKey
Definition: AjaxWidgetContextHolder.php:43
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\$widgetContexts
‪WidgetContext[] $widgetContexts
Definition: AjaxWidgetContextHolder.php:39
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
Definition: AjaxWidgetContextHolder.php:33
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Fluid\Core\Widget\WidgetContext
Definition: WidgetContext.php:33
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\$hashService
‪HashService $hashService
Definition: AjaxWidgetContextHolder.php:47
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22