TYPO3  7.6
FormViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU General Public License as published by the Free *
9  * Software Foundation, either version 3 of the License, or (at your *
10  * *
11  * *
12  * This script is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
15  * Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with the script. *
19  * If not, see http://www.gnu.org/licenses/gpl.html *
20  * *
21  * The TYPO3 project - inspiring people to share! *
22  * */
51 {
55  protected $tagName = 'form';
56 
61  protected $hashService;
62 
68 
73  protected $extensionService;
74 
82 
88  public function initializeArguments()
89  {
90  $this->registerTagAttribute('enctype', 'string', 'MIME type with which the form is submitted');
91  $this->registerTagAttribute('method', 'string', 'Transfer type (GET or POST)');
92  $this->registerTagAttribute('name', 'string', 'Name of form');
93  $this->registerTagAttribute('onreset', 'string', 'JavaScript: On reset of the form');
94  $this->registerTagAttribute('onsubmit', 'string', 'JavaScript: On submit of the form');
96  }
97 
123  public function render($action = null, array $arguments = array(), $controller = null, $extensionName = null, $pluginName = null, $pageUid = null, $object = null, $pageType = 0, $noCache = false, $noCacheHash = false, $section = '', $format = '', array $additionalParams = array(), $absolute = false, $addQueryString = false, array $argumentsToBeExcludedFromQueryString = array(), $fieldNamePrefix = null, $actionUri = null, $objectName = null, $hiddenFieldClassName = null)
124  {
125  $this->setFormActionUri();
126  if (strtolower($this->arguments['method']) === 'get') {
127  $this->tag->addAttribute('method', 'get');
128  } else {
129  $this->tag->addAttribute('method', 'post');
130  }
135  $formContent = $this->renderChildren();
136 
137  if ($this->arguments['hiddenFieldClassName'] !== null) {
138  $content = LF . '<div class="' . htmlspecialchars($this->arguments['hiddenFieldClassName']) . '">';
139  } else {
140  $content = LF . '<div>';
141  }
142 
143  $content .= $this->renderHiddenIdentityField($this->arguments['object'], $this->getFormObjectName());
144  $content .= $this->renderAdditionalIdentityFields();
145  $content .= $this->renderHiddenReferrerFields();
146 
147  // Render the trusted list of all properties after everything else has been rendered
148  $content .= $this->renderTrustedPropertiesField();
149 
150  $content .= LF . '</div>' . LF;
151  $content .= $formContent;
152  $this->tag->setContent($content);
158  return $this->tag->render();
159  }
160 
166  protected function setFormActionUri()
167  {
168  if ($this->hasArgument('actionUri')) {
169  $formActionUri = $this->arguments['actionUri'];
170  } else {
171  $uriBuilder = $this->controllerContext->getUriBuilder();
172  $formActionUri = $uriBuilder->reset()->setTargetPageUid($this->arguments['pageUid'])->setTargetPageType($this->arguments['pageType'])->setNoCache($this->arguments['noCache'])->setUseCacheHash(!$this->arguments['noCacheHash'])->setSection($this->arguments['section'])->setCreateAbsoluteUri($this->arguments['absolute'])->setArguments((array)$this->arguments['additionalParams'])->setAddQueryString($this->arguments['addQueryString'])->setArgumentsToBeExcludedFromQueryString((array)$this->arguments['argumentsToBeExcludedFromQueryString'])->setFormat($this->arguments['format'])->uriFor($this->arguments['action'], $this->arguments['arguments'], $this->arguments['controller'], $this->arguments['extensionName'], $this->arguments['pluginName']);
173  $this->formActionUriArguments = $uriBuilder->getArguments();
174  }
175  $this->tag->addAttribute('action', $formActionUri);
176  }
177 
184  protected function renderAdditionalIdentityFields()
185  {
186  if ($this->viewHelperVariableContainer->exists(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties')) {
187  $additionalIdentityProperties = $this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties');
188  $output = '';
189  foreach ($additionalIdentityProperties as $identity) {
190  $output .= LF . $identity;
191  }
192  return $output;
193  }
194  return '';
195  }
196 
204  protected function renderHiddenReferrerFields()
205  {
206  $request = $this->controllerContext->getRequest();
207  $extensionName = $request->getControllerExtensionName();
208  $vendorName = $request->getControllerVendorName();
209  $controllerName = $request->getControllerName();
210  $actionName = $request->getControllerActionName();
211  $result = LF;
212  $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[@extension]') . '" value="' . $extensionName . '" />' . LF;
213  if ($vendorName !== null) {
214  $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[@vendor]') . '" value="' . $vendorName . '" />' . LF;
215  }
216  $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[@controller]') . '" value="' . $controllerName . '" />' . LF;
217  $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[@action]') . '" value="' . $actionName . '" />' . LF;
218  $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[arguments]') . '" value="' . htmlspecialchars($this->hashService->appendHmac(base64_encode(serialize($request->getArguments())))) . '" />' . LF;
219 
220  return $result;
221  }
222 
229  {
230  $formObjectName = $this->getFormObjectName();
231  if ($formObjectName !== null) {
232  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName', $formObjectName);
233  }
234  }
235 
242  {
243  $formObjectName = $this->getFormObjectName();
244  if ($formObjectName !== null) {
245  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObjectName');
246  }
247  }
248 
256  protected function getFormObjectName()
257  {
258  $formObjectName = null;
259  if ($this->hasArgument('objectName')) {
260  $formObjectName = $this->arguments['objectName'];
261  } elseif ($this->hasArgument('name')) {
262  $formObjectName = $this->arguments['name'];
263  }
264  return $formObjectName;
265  }
266 
273  {
274  if ($this->hasArgument('object')) {
275  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject', $this->arguments['object']);
276  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties', array());
277  }
278  }
279 
286  {
287  if ($this->hasArgument('object')) {
288  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formObject');
289  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'additionalIdentityProperties');
290  }
291  }
292 
299  {
300  $fieldNamePrefix = $this->getFieldNamePrefix();
301  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix', $fieldNamePrefix);
302  }
303 
309  protected function getFieldNamePrefix()
310  {
311  if ($this->hasArgument('fieldNamePrefix')) {
312  return $this->arguments['fieldNamePrefix'];
313  } else {
314  return $this->getDefaultFieldNamePrefix();
315  }
316  }
317 
324  {
325  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'fieldNamePrefix');
326  }
327 
334  {
335  $this->viewHelperVariableContainer->add(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formFieldNames', array());
336  }
337 
344  {
345  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formFieldNames');
346  }
347 
353  protected function renderRequestHashField()
354  {
355  $formFieldNames = $this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formFieldNames');
356  $this->postProcessUriArgumentsForRequesthash($this->formActionUriArguments, $formFieldNames);
357  $requestHash = $this->requestHashService->generateRequestHash($formFieldNames, $this->getFieldNamePrefix());
358  // in v4, we need to prefix __hmac as well to make it show up in the request object.
359  return '<input type="hidden" name="' . $this->prefixFieldName('__hmac') . '" value="' . htmlspecialchars($requestHash) . '" />';
360  }
361 
365  protected function postProcessUriArgumentsForRequestHash($arguments, &$results, $currentPrefix = '', $level = 0)
366  {
367  if (!count($arguments)) {
368  return;
369  }
370  foreach ($arguments as $argumentName => $argumentValue) {
371  if (is_array($argumentValue)) {
372  $prefix = $level == 0 ? $argumentName : $currentPrefix . '[' . $argumentName . ']';
373  $this->postProcessUriArgumentsForRequestHash($argumentValue, $results, $prefix, $level + 1);
374  } else {
375  $results[] = $level == 0 ? $argumentName : $currentPrefix . '[' . $argumentName . ']';
376  }
377  }
378  }
379 
385  protected function getDefaultFieldNamePrefix()
386  {
387  $request = $this->controllerContext->getRequest();
388  if ($this->hasArgument('extensionName')) {
389  $extensionName = $this->arguments['extensionName'];
390  } else {
391  $extensionName = $request->getControllerExtensionName();
392  }
393  if ($this->hasArgument('pluginName')) {
394  $pluginName = $this->arguments['pluginName'];
395  } else {
396  $pluginName = $request->getPluginName();
397  }
398  if ($extensionName !== null && $pluginName != null) {
399  return $this->extensionService->getPluginNamespace($extensionName, $pluginName);
400  } else {
401  return '';
402  }
403  }
404 
409  {
410  if ($this->viewHelperVariableContainer->exists(\TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper::class, 'checkboxFieldNames')) {
411  $this->viewHelperVariableContainer->remove(\TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper::class, 'checkboxFieldNames');
412  }
413  }
414 
420  protected function renderTrustedPropertiesField()
421  {
422  $formFieldNames = $this->viewHelperVariableContainer->get(\TYPO3\CMS\Fluid\ViewHelpers\FormViewHelper::class, 'formFieldNames');
423  $requestHash = $this->mvcPropertyMappingConfigurationService->generateTrustedPropertiesToken($formFieldNames, $this->getFieldNamePrefix());
424  return '<input type="hidden" name="' . $this->prefixFieldName('__trustedProperties') . '" value="' . htmlspecialchars($requestHash) . '" />';
425  }
426 }