TYPO3 CMS  TYPO3_8-7
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 
25 
30 {
34  protected function initCurrentUrl()
35  {
36  $currentLink = isset($this->parameters['currentValue']) ? trim($this->parameters['currentValue']) : '';
37  $currentLinkParts = GeneralUtility::makeInstance(TypoLinkCodecService::class)->decode($currentLink);
38  $currentLinkParts['params'] = $currentLinkParts['additionalParams'];
39  unset($currentLinkParts['additionalParams']);
40 
41  if (!empty($currentLinkParts['url'])) {
42  $linkService = GeneralUtility::makeInstance(LinkService::class);
43  $data = $linkService->resolve($currentLinkParts['url']);
44  $currentLinkParts['type'] = $data['type'];
45  unset($data['type']);
46  $currentLinkParts['url'] = $data;
47  }
48 
49  $this->currentLinkParts = $currentLinkParts;
50 
51  parent::initCurrentUrl();
52  }
53 
57  protected function initDocumentTemplate()
58  {
59  parent::initDocumentTemplate();
60 
61  if (!$this->areFieldChangeFunctionsValid() && !$this->areFieldChangeFunctionsValid(true)) {
62  $this->parameters['fieldChangeFunc'] = [];
63  }
64  unset($this->parameters['fieldChangeFunc']['alert']);
65  $update = [];
66  foreach ($this->parameters['fieldChangeFunc'] as $v) {
67  $update[] = 'parent.opener.' . $v;
68  }
69  $inlineJS = implode(LF, $update);
70 
71  $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
72  $pageRenderer->loadJquery();
73  $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/FormEngineLinkBrowserAdapter', 'function(FormEngineLinkBrowserAdapter) {
74  FormEngineLinkBrowserAdapter.updateFunctions = function() {' . $inlineJS . '};
75  }');
76  }
77 
87  public function encodeTypoLink(ServerRequestInterface $request, ResponseInterface $response)
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 
97  $response->getBody()->write(json_encode(['typoLink' => $typoLink]));
98  return $response;
99  }
100 
108  protected function areFieldChangeFunctionsValid($handleFlexformSections = false)
109  {
110  $result = false;
111  if (isset($this->parameters['fieldChangeFunc']) && is_array($this->parameters['fieldChangeFunc']) && isset($this->parameters['fieldChangeFuncHash'])) {
112  $matches = [];
113  $pattern = '#\\[el\\]\\[(([^]-]+-[^]-]+-)(idx\\d+-)([^]]+))\\]#i';
114  $fieldChangeFunctions = $this->parameters['fieldChangeFunc'];
115  // Special handling of flexform sections:
116  // Field change functions are modified in JavaScript, thus the hash is always invalid
117  if ($handleFlexformSections && preg_match($pattern, $this->parameters['itemName'], $matches)) {
118  $originalName = $matches[1];
119  $cleanedName = $matches[2] . $matches[4];
120  foreach ($fieldChangeFunctions as &$value) {
121  $value = str_replace($originalName, $cleanedName, $value);
122  }
123  unset($value);
124  }
125  $result = hash_equals(GeneralUtility::hmac(serialize($fieldChangeFunctions)), $this->parameters['fieldChangeFuncHash']);
126  }
127  return $result;
128  }
129 
135  protected function getBodyTagAttributes()
136  {
137  $parameters = parent::getBodyTagAttributes();
138 
139  $formEngineParameters['fieldChangeFunc'] = $this->parameters['fieldChangeFunc'];
140  $formEngineParameters['fieldChangeFuncHash'] = GeneralUtility::hmac(serialize($this->parameters['fieldChangeFunc']));
141 
142  $parameters['data-add-on-params'] .= GeneralUtility::implodeArrayForUrl('P', $formEngineParameters);
143 
144  return $parameters;
145  }
146 
152  protected function getCurrentPageId()
153  {
154  $pageId = 0;
155  $browserParameters = $this->parameters;
156  if (isset($browserParameters['pid'])) {
157  $pageId = $browserParameters['pid'];
158  } elseif (isset($browserParameters['itemName'])) {
159  // parse data[<table>][<uid>]
160  if (preg_match('~data\[([^]]*)\]\[([^]]*)\]~', $browserParameters['itemName'], $matches)) {
161  $recordArray = BackendUtility::getRecord($matches['1'], $matches['2']);
162  if (is_array($recordArray)) {
163  $pageId = $recordArray['pid'];
164  }
165  }
166  }
167  return (int)BackendUtility::getTSCpidCached($browserParameters['table'], $browserParameters['uid'], $pageId)[0];
168  }
169 }
static hmac($input, $additionalSecret='')
static makeInstance($className,... $constructorArguments)
static implodeArrayForUrl($name, array $theArray, $str='', $skipBlank=false, $rawurlencodeParamName=false)
static getTSCpidCached($table, $uid, $pid)
static getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)