‪TYPO3CMS  9.5
AjaxWidgetContextHolder.php
Go to the documentation of this file.
1 <?php
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 
20 
29 {
36  protected ‪$widgetContexts = [];
37 
41  protected ‪$widgetContextsStorageKey = 'TYPO3\\CMS\\Fluid\\Core\\Widget\\AjaxWidgetContextHolder_widgetContexts';
42 
46  protected ‪$hashService;
47 
51  public function ‪__construct()
52  {
53  $this->hashService = GeneralUtility::makeInstance(HashService::class);
54  $this->‪loadWidgetContexts();
55  }
56 
60  protected function ‪loadWidgetContexts()
61  {
62  if (isset(‪$GLOBALS['TSFE']) && ‪$GLOBALS['TSFE'] instanceof ‪TypoScriptFrontendController) {
63  $this->widgetContexts = $this->‪unserializeWithHmac(‪$GLOBALS['TSFE']->fe_user->getKey('ses', $this->widgetContextsStorageKey));
64  } else {
65  $this->widgetContexts = isset(‪$GLOBALS['BE_USER']->uc[$this->widgetContextsStorageKey]) ? $this->‪unserializeWithHmac(‪$GLOBALS['BE_USER']->uc[$this->widgetContextsStorageKey]) : [];
66  ‪$GLOBALS['BE_USER']->writeUC();
67  }
68  }
69 
76  public function get($ajaxWidgetId)
77  {
78  if (!isset($this->widgetContexts[$ajaxWidgetId])) {
79  throw new \TYPO3\CMS\Fluid\Core\Widget\Exception\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);
80  }
81  return $this->widgetContexts[$ajaxWidgetId];
82  }
83 
90  public function ‪store(‪WidgetContext $widgetContext)
91  {
92  $ajaxWidgetId = md5(uniqid(mt_rand(), true));
93  $widgetContext->‪setAjaxWidgetIdentifier($ajaxWidgetId);
94  $this->widgetContexts[$ajaxWidgetId] = $widgetContext;
95  $this->‪storeWidgetContexts();
96  }
97 
101  protected function ‪storeWidgetContexts()
102  {
103  if (isset(‪$GLOBALS['TSFE']) && ‪$GLOBALS['TSFE'] instanceof ‪TypoScriptFrontendController) {
104  ‪$GLOBALS['TSFE']->fe_user->setKey('ses', $this->widgetContextsStorageKey, $this->‪serializeWithHmac($this->widgetContexts));
105  ‪$GLOBALS['TSFE']->fe_user->storeSessionData();
106  } else {
107  ‪$GLOBALS['BE_USER']->uc[‪$this->widgetContextsStorageKey] = $this->‪serializeWithHmac($this->widgetContexts);
108  ‪$GLOBALS['BE_USER']->writeUc();
109  }
110  }
111 
112  protected function ‪serializeWithHmac(array ‪$widgetContexts): string
113  {
114  $data = serialize(‪$widgetContexts);
115  return $this->hashService->appendHmac($data);
116  }
117 
118  protected function ‪unserializeWithHmac(?string $data): array
119  {
120  if ($data === null || $data === '') {
121  return [];
122  }
123  try {
124  $data = $this->hashService->validateAndStripHmac($data);
125  // widget contexts literally can contain everything, that why using
126  // HMAC-signed `unserialize()` is the only option here unless Extbase
127  // structures have been refactored further
128  ‪$widgetContexts = unserialize($data);
129  } finally {
130  return is_array(‪$widgetContexts ?? null) ? ‪$widgetContexts : [];
131  }
132  }
133 }
‪TYPO3\CMS\Fluid\Core\Widget
Definition: AbstractWidgetController.php:2
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\__construct
‪__construct()
Definition: AjaxWidgetContextHolder.php:48
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\store
‪store(WidgetContext $widgetContext)
Definition: AjaxWidgetContextHolder.php:87
‪TYPO3\CMS\Fluid\Core\Widget\WidgetContext\setAjaxWidgetIdentifier
‪setAjaxWidgetIdentifier($ajaxWidgetIdentifier)
Definition: WidgetContext.php:117
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\loadWidgetContexts
‪loadWidgetContexts()
Definition: AjaxWidgetContextHolder.php:57
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:24
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\storeWidgetContexts
‪storeWidgetContexts()
Definition: AjaxWidgetContextHolder.php:98
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\serializeWithHmac
‪serializeWithHmac(array $widgetContexts)
Definition: AjaxWidgetContextHolder.php:109
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\unserializeWithHmac
‪unserializeWithHmac(?string $data)
Definition: AjaxWidgetContextHolder.php:115
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\$widgetContextsStorageKey
‪string $widgetContextsStorageKey
Definition: AjaxWidgetContextHolder.php:39
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\$widgetContexts
‪WidgetContext[] $widgetContexts
Definition: AjaxWidgetContextHolder.php:35
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder
Definition: AjaxWidgetContextHolder.php:29
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Fluid\Core\Widget\WidgetContext
Definition: WidgetContext.php:29
‪TYPO3\CMS\Fluid\Core\Widget\AjaxWidgetContextHolder\$hashService
‪HashService $hashService
Definition: AjaxWidgetContextHolder.php:43
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45