‪TYPO3CMS  9.5
LinkBrowserController.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 
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
27 
32 {
36  protected function ‪initCurrentUrl()
37  {
38  $currentLink = isset($this->parameters['currentValue']) ? trim($this->parameters['currentValue']) : '';
39  ‪$currentLinkParts = GeneralUtility::makeInstance(TypoLinkCodecService::class)->decode($currentLink);
40  ‪$currentLinkParts['params'] = ‪$currentLinkParts['additionalParams'];
41  unset(‪$currentLinkParts['additionalParams']);
42 
43  if (!empty(‪$currentLinkParts['url'])) {
44  $linkService = GeneralUtility::makeInstance(LinkService::class);
45  $data = $linkService->resolve(‪$currentLinkParts['url']);
46  ‪$currentLinkParts['type'] = $data['type'];
47  unset($data['type']);
48  ‪$currentLinkParts['url'] = $data;
49  }
50 
51  $this->currentLinkParts = ‪$currentLinkParts;
52 
53  parent::initCurrentUrl();
54  }
55 
59  protected function ‪initDocumentTemplate()
60  {
61  parent::initDocumentTemplate();
62 
64  $this->parameters['fieldChangeFunc'] = [];
65  }
66  unset($this->parameters['fieldChangeFunc']['alert']);
67  $update = [];
68  foreach ($this->parameters['fieldChangeFunc'] as $v) {
69  $update[] = 'parent.opener.' . $v;
70  }
71  $inlineJS = implode(LF, $update);
72 
73  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
74  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/FormEngineLinkBrowserAdapter', 'function(FormEngineLinkBrowserAdapter) {
75  FormEngineLinkBrowserAdapter.updateFunctions = function() {' . $inlineJS . '};
76  }');
77  }
78 
87  public function ‪encodeTypoLink(ServerRequestInterface $request): ResponseInterface
88  {
89  $typoLinkParts = $request->getQueryParams();
90  if (isset($typoLinkParts['params'])) {
91  $typoLinkParts['additionalParams'] = $typoLinkParts['params'];
92  unset($typoLinkParts['params']);
93  }
94 
95  $typoLink = GeneralUtility::makeInstance(TypoLinkCodecService::class)->encode($typoLinkParts);
96  return new ‪JsonResponse(['typoLink' => $typoLink]);
97  }
98 
106  protected function ‪areFieldChangeFunctionsValid($handleFlexformSections = false)
107  {
108  $result = false;
109  if (isset($this->parameters['fieldChangeFunc']) && is_array($this->parameters['fieldChangeFunc']) && isset($this->parameters['fieldChangeFuncHash'])) {
110  $matches = [];
111  $pattern = '#\\[el\\]\\[(([^]-]+-[^]-]+-)(idx\\d+-)([^]]+))\\]#i';
112  $fieldChangeFunctions = $this->parameters['fieldChangeFunc'];
113  // Special handling of flexform sections:
114  // Field change functions are modified in JavaScript, thus the hash is always invalid
115  if ($handleFlexformSections && preg_match($pattern, $this->parameters['itemName'], $matches)) {
116  $originalName = $matches[1];
117  $cleanedName = $matches[2] . $matches[4];
118  foreach ($fieldChangeFunctions as &$value) {
119  $value = str_replace($originalName, $cleanedName, $value);
120  }
121  unset($value);
122  }
123  $result = hash_equals(GeneralUtility::hmac(serialize($fieldChangeFunctions), 'backend-link-browser'), $this->parameters['fieldChangeFuncHash']);
124  }
125  return $result;
126  }
127 
133  protected function ‪getBodyTagAttributes()
134  {
135  ‪$parameters = parent::getBodyTagAttributes();
136 
137  $formEngineParameters['fieldChangeFunc'] = $this->parameters['fieldChangeFunc'];
138  $formEngineParameters['fieldChangeFuncHash'] = GeneralUtility::hmac(serialize($this->parameters['fieldChangeFunc']), 'backend-link-browser');
139 
140  ‪$parameters['data-add-on-params'] .= ‪HttpUtility::buildQueryString(['P' => $formEngineParameters], '&');
141 
142  return ‪$parameters;
143  }
144 
150  protected function ‪getCurrentPageId()
151  {
152  $pageId = 0;
153  $browserParameters = ‪$this->parameters;
154  if (isset($browserParameters['pid'])) {
155  $pageId = $browserParameters['pid'];
156  } elseif (isset($browserParameters['itemName'])) {
157  // parse data[<table>][<uid>]
158  if (preg_match('~data\[([^]]*)\]\[([^]]*)\]~', $browserParameters['itemName'], $matches)) {
159  $recordArray = ‪BackendUtility::getRecord($matches['1'], $matches['2']);
160  if (is_array($recordArray)) {
161  $pageId = $recordArray['pid'];
162  }
163  }
164  }
165  return (int)‪BackendUtility::getTSCpidCached($browserParameters['table'], $browserParameters['uid'], $pageId)[0];
166  }
167 
172  public function ‪getConfiguration(): array
173  {
175  return $tsConfig['TCEMAIN.']['linkHandler.']['page.']['configuration.'] ?? [];
176  }
177 }
‪TYPO3\CMS\Backend\Utility\BackendUtility\getTSCpidCached
‪static array getTSCpidCached($table, $uid, $pid)
Definition: BackendUtility.php:3584
‪TYPO3\CMS\Core\Page\PageRenderer
Definition: PageRenderer.php:35
‪TYPO3\CMS\Core\Utility\HttpUtility\buildQueryString
‪static string buildQueryString(array $parameters, string $prependCharacter='', bool $skipEmptyParameters=false)
Definition: HttpUtility.php:160
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:72
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecord
‪static array null getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
Definition: BackendUtility.php:130
‪TYPO3\CMS\Backend\Utility\BackendUtility\getPagesTSconfig
‪static array getPagesTSconfig($id, $rootLine=null, $returnPartArray=false)
Definition: BackendUtility.php:864
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:25
‪TYPO3\CMS\Core\Utility\HttpUtility
Definition: HttpUtility.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Controller
Definition: AbstractFormEngineAjaxController.php:3