5a3d9cd78cd03e9f9bf9393d1dd90cc86f02c458
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / FormViewHelper.php
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 * */
23 /**
24 * Form view helper. Generates a <form> Tag.
25 *
26 * = Basic usage =
27 *
28 * Use <f:form> to output an HTML <form> tag which is targeted at the specified action, in the current controller and package.
29 * It will submit the form data via a POST request. If you want to change this, use method="get" as an argument.
30 * <code title="Example">
31 * <f:form action="...">...</f:form>
32 * </code>
33 *
34 * = A complex form with a specified encoding type =
35 *
36 * <code title="Form with enctype set">
37 * <f:form action=".." controller="..." package="..." enctype="multipart/form-data">...</f:form>
38 * </code>
39 *
40 * = A Form which should render a domain object =
41 *
42 * <code title="Binding a domain object to a form">
43 * <f:form action="..." name="customer" object="{customer}">
44 * <f:form.hidden property="id" />
45 * <f:form.textbox property="name" />
46 * </f:form>
47 * </code>
48 * This automatically inserts the value of {customer.name} inside the textbox and adjusts the name of the textbox accordingly.
49 */
50 class FormViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormViewHelper {
51
52 /**
53 * @var string
54 */
55 protected $tagName = 'form';
56
57 /**
58 * @var \TYPO3\CMS\Extbase\Security\Channel\RequestHashService
59 */
60 protected $requestHashService;
61
62 /**
63 * @var \TYPO3\CMS\Extbase\Security\Cryptography\HashService
64 */
65 protected $hashService;
66
67 /**
68 * @var \TYPO3\CMS\Extbase\Service\ExtensionService
69 */
70 protected $extensionService;
71
72 /**
73 * We need the arguments of the formActionUri on requesthash calculation
74 * therefore we will store them in here right after calling uriBuilder
75 *
76 * @var array
77 */
78 protected $formActionUriArguments;
79
80 /**
81 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
82 */
83 protected $configurationManager;
84
85 /**
86 * Inject a request hash service
87 *
88 * @param \TYPO3\CMS\Extbase\Security\Channel\RequestHashService $requestHashService The request hash service
89 * @return void
90 */
91 public function injectRequestHashService(\TYPO3\CMS\Extbase\Security\Channel\RequestHashService $requestHashService) {
92 $this->requestHashService = $requestHashService;
93 }
94
95 /**
96 * @param \TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService
97 */
98 public function injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService) {
99 $this->hashService = $hashService;
100 }
101
102 /**
103 * @param \TYPO3\CMS\Extbase\Service\ExtensionService $extensionService
104 * @return void
105 */
106 public function injectExtensionService(\TYPO3\CMS\Extbase\Service\ExtensionService $extensionService) {
107 $this->extensionService = $extensionService;
108 }
109
110 /**
111 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
112 * @return void
113 */
114 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) {
115 $this->configurationManager = $configurationManager;
116 }
117
118 /**
119 * Initialize arguments.
120 *
121 * @return void
122 */
123 public function initializeArguments() {
124 $this->registerTagAttribute('enctype', 'string', 'MIME type with which the form is submitted');
125 $this->registerTagAttribute('method', 'string', 'Transfer type (GET or POST)');
126 $this->registerTagAttribute('name', 'string', 'Name of form');
127 $this->registerTagAttribute('onreset', 'string', 'JavaScript: On reset of the form');
128 $this->registerTagAttribute('onsubmit', 'string', 'JavaScript: On submit of the form');
129 $this->registerUniversalTagAttributes();
130 }
131
132 /**
133 * Render the form.
134 *
135 * @param string $action Target action
136 * @param array $arguments Arguments
137 * @param string $controller Target controller
138 * @param string $extensionName Target Extension Name (without "tx_" prefix and no underscores). If NULL the current extension name is used
139 * @param string $pluginName Target plugin. If empty, the current plugin name is used
140 * @param integer $pageUid Target page uid
141 * @param mixed $object Object to use for the form. Use in conjunction with the "property" attribute on the sub tags
142 * @param integer $pageType Target page type
143 * @param boolean $noCache set this to disable caching for the target page. You should not need this.
144 * @param boolean $noCacheHash set this to supress the cHash query parameter created by TypoLink. You should not need this.
145 * @param string $section The anchor to be added to the action URI (only active if $actionUri is not set)
146 * @param string $format The requested format (e.g. ".html") of the target page (only active if $actionUri is not set)
147 * @param array $additionalParams additional action URI query parameters that won't be prefixed like $arguments (overrule $arguments) (only active if $actionUri is not set)
148 * @param boolean $absolute If set, an absolute action URI is rendered (only active if $actionUri is not set)
149 * @param boolean $addQueryString If set, the current query parameters will be kept in the action URI (only active if $actionUri is not set)
150 * @param array $argumentsToBeExcludedFromQueryString arguments to be removed from the action URI. Only active if $addQueryString = TRUE and $actionUri is not set
151 * @param string $fieldNamePrefix Prefix that will be added to all field names within this form. If not set the prefix will be tx_yourExtension_plugin
152 * @param string $actionUri can be used to overwrite the "action" attribute of the form tag
153 * @param string $objectName name of the object that is bound to this form. If this argument is not specified, the name attribute of this form is used to determine the FormObjectName
154 * @return string rendered form
155 */
156 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) {
157 $this->setFormActionUri();
158 if (strtolower($this->arguments['method']) === 'get') {
159 $this->tag->addAttribute('method', 'get');
160 } else {
161 $this->tag->addAttribute('method', 'post');
162 }
163 $this->addFormObjectNameToViewHelperVariableContainer();
164 $this->addFormObjectToViewHelperVariableContainer();
165 $this->addFieldNamePrefixToViewHelperVariableContainer();
166 $this->addFormFieldNamesToViewHelperVariableContainer();
167 $formContent = $this->renderChildren();
168 $content = chr(10) . '<div style="display: none">';
169 $content .= $this->renderHiddenIdentityField($this->arguments['object'], $this->getFormObjectName());
170 $content .= $this->renderAdditionalIdentityFields();
171 $content .= $this->renderHiddenReferrerFields();
172 $content .= $this->renderRequestHashField();
173 // Render hmac after everything else has been rendered
174 $content .= chr(10) . '</div>' . chr(10);
175 $content .= $formContent;
176 $this->tag->setContent($content);
177 $this->removeFieldNamePrefixFromViewHelperVariableContainer();
178 $this->removeFormObjectFromViewHelperVariableContainer();
179 $this->removeFormObjectNameFromViewHelperVariableContainer();
180 $this->removeFormFieldNamesFromViewHelperVariableContainer();
181 $this->removeCheckboxFieldNamesFromViewHelperVariableContainer();
182 return $this->tag->render();
183 }
184
185 /**
186 * Sets the "action" attribute of the form tag
187 *
188 * @return void
189 */
190 protected function setFormActionUri() {
191 if ($this->hasArgument('actionUri')) {
192 $formActionUri = $this->arguments['actionUri'];
193 } else {
194 $uriBuilder = $this->controllerContext->getUriBuilder();
195 $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']);
196 $this->formActionUriArguments = $uriBuilder->getArguments();
197 }
198 $this->tag->addAttribute('action', $formActionUri);
199 }
200
201 /**
202 * Render additional identity fields which were registered by form elements.
203 * This happens if a form field is defined like property="bla.blubb" - then we might need an identity property for the sub-object "bla".
204 *
205 * @return string HTML-string for the additional identity properties
206 */
207 protected function renderAdditionalIdentityFields() {
208 if ($this->viewHelperVariableContainer->exists('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties')) {
209 $additionalIdentityProperties = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties');
210 $output = '';
211 foreach ($additionalIdentityProperties as $identity) {
212 $output .= chr(10) . $identity;
213 }
214 return $output;
215 }
216 return '';
217 }
218
219 /**
220 * Renders hidden form fields for referrer information about
221 * the current controller and action.
222 *
223 * @return string Hidden fields with referrer information
224 * @todo filter out referrer information that is equal to the target (e.g. same packageKey)
225 */
226 protected function renderHiddenReferrerFields() {
227 $request = $this->controllerContext->getRequest();
228 $extensionName = $request->getControllerExtensionName();
229 $controllerName = $request->getControllerName();
230 $actionName = $request->getControllerActionName();
231 $result = chr(10);
232 if ($this->configurationManager->isFeatureEnabled('rewrittenPropertyMapper')) {
233 $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[@extension]') . '" value="' . $extensionName . '" />' . chr(10);
234 $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[@controller]') . '" value="' . $controllerName . '" />' . chr(10);
235 $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[@action]') . '" value="' . $actionName . '" />' . chr(10);
236 $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[arguments]') . '" value="' . htmlspecialchars($this->hashService->appendHmac(base64_encode(serialize($request->getArguments())))) . '" />' . chr(10);
237 } else {
238 // @deprecated since Fluid 1.4.0, will be removed with Fluid 6.1.
239 $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[extensionName]') . '" value="' . $extensionName . '" />' . chr(10);
240 $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[controllerName]') . '" value="' . $controllerName . '" />' . chr(10);
241 $result .= '<input type="hidden" name="' . $this->prefixFieldName('__referrer[actionName]') . '" value="' . $actionName . '" />' . chr(10);
242 }
243 return $result;
244 }
245
246 /**
247 * Adds the form object name to the ViewHelperVariableContainer if "objectName" argument or "name" attribute is specified.
248 *
249 * @return void
250 */
251 protected function addFormObjectNameToViewHelperVariableContainer() {
252 $formObjectName = $this->getFormObjectName();
253 if ($formObjectName !== NULL) {
254 $this->viewHelperVariableContainer->add('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName', $formObjectName);
255 }
256 }
257
258 /**
259 * Removes the form name from the ViewHelperVariableContainer.
260 *
261 * @return void
262 */
263 protected function removeFormObjectNameFromViewHelperVariableContainer() {
264 $formObjectName = $this->getFormObjectName();
265 if ($formObjectName !== NULL) {
266 $this->viewHelperVariableContainer->remove('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObjectName');
267 }
268 }
269
270 /**
271 * Returns the name of the object that is bound to this form.
272 * If the "objectName" argument has been specified, this is returned. Otherwise the name attribute of this form.
273 * If neither objectName nor name arguments have been set, NULL is returned.
274 *
275 * @return string specified Form name or NULL if neither $objectName nor $name arguments have been specified
276 */
277 protected function getFormObjectName() {
278 $formObjectName = NULL;
279 if ($this->hasArgument('objectName')) {
280 $formObjectName = $this->arguments['objectName'];
281 } elseif ($this->hasArgument('name')) {
282 $formObjectName = $this->arguments['name'];
283 }
284 return $formObjectName;
285 }
286
287 /**
288 * Adds the object that is bound to this form to the ViewHelperVariableContainer if the formObject attribute is specified.
289 *
290 * @return void
291 */
292 protected function addFormObjectToViewHelperVariableContainer() {
293 if ($this->hasArgument('object')) {
294 $this->viewHelperVariableContainer->add('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject', $this->arguments['object']);
295 $this->viewHelperVariableContainer->add('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties', array());
296 }
297 }
298
299 /**
300 * Removes the form object from the ViewHelperVariableContainer.
301 *
302 * @return void
303 */
304 protected function removeFormObjectFromViewHelperVariableContainer() {
305 if ($this->hasArgument('object')) {
306 $this->viewHelperVariableContainer->remove('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formObject');
307 $this->viewHelperVariableContainer->remove('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'additionalIdentityProperties');
308 }
309 }
310
311 /**
312 * Adds the field name prefix to the ViewHelperVariableContainer
313 *
314 * @return void
315 */
316 protected function addFieldNamePrefixToViewHelperVariableContainer() {
317 $fieldNamePrefix = $this->getFieldNamePrefix();
318 $this->viewHelperVariableContainer->add('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix', $fieldNamePrefix);
319 }
320
321 /**
322 * Get the field name prefix
323 *
324 * @return string
325 */
326 protected function getFieldNamePrefix() {
327 if ($this->hasArgument('fieldNamePrefix')) {
328 return $this->arguments['fieldNamePrefix'];
329 } else {
330 return $this->getDefaultFieldNamePrefix();
331 }
332 }
333
334 /**
335 * Removes field name prefix from the ViewHelperVariableContainer
336 *
337 * @return void
338 */
339 protected function removeFieldNamePrefixFromViewHelperVariableContainer() {
340 $this->viewHelperVariableContainer->remove('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'fieldNamePrefix');
341 }
342
343 /**
344 * Adds a container for form field names to the ViewHelperVariableContainer
345 *
346 * @return void
347 */
348 protected function addFormFieldNamesToViewHelperVariableContainer() {
349 $this->viewHelperVariableContainer->add('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formFieldNames', array());
350 }
351
352 /**
353 * Removes the container for form field names from the ViewHelperVariableContainer
354 *
355 * @return void
356 */
357 protected function removeFormFieldNamesFromViewHelperVariableContainer() {
358 $this->viewHelperVariableContainer->remove('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formFieldNames');
359 }
360
361 /**
362 * Render the request hash field
363 *
364 * @return string the hmac field
365 */
366 protected function renderRequestHashField() {
367 $formFieldNames = $this->viewHelperVariableContainer->get('TYPO3\\CMS\\Fluid\\ViewHelpers\\FormViewHelper', 'formFieldNames');
368 $this->postProcessUriArgumentsForRequesthash($this->formActionUriArguments, $formFieldNames);
369 $requestHash = $this->requestHashService->generateRequestHash($formFieldNames, $this->getFieldNamePrefix());
370 // in v4, we need to prefix __hmac as well to make it show up in the request object.
371 return '<input type="hidden" name="' . $this->prefixFieldName('__hmac') . '" value="' . htmlspecialchars($requestHash) . '" />';
372 }
373
374 /**
375 * Add the URI arguments after postprocessing to the request hash as well.
376 */
377 protected function postProcessUriArgumentsForRequestHash($arguments, &$results, $currentPrefix = '', $level = 0) {
378 if (!count($arguments)) {
379 return;
380 }
381 foreach ($arguments as $argumentName => $argumentValue) {
382 if (is_array($argumentValue)) {
383 $prefix = $level == 0 ? $argumentName : $currentPrefix . '[' . $argumentName . ']';
384 $this->postProcessUriArgumentsForRequestHash($argumentValue, $results, $prefix, $level + 1);
385 } else {
386 $results[] = $level == 0 ? $argumentName : $currentPrefix . '[' . $argumentName . ']';
387 }
388 }
389 }
390
391 /**
392 * Retrieves the default field name prefix for this form
393 *
394 * @return string default field name prefix
395 */
396 protected function getDefaultFieldNamePrefix() {
397 $request = $this->controllerContext->getRequest();
398 if ($this->hasArgument('extensionName')) {
399 $extensionName = $this->arguments['extensionName'];
400 } else {
401 $extensionName = $request->getControllerExtensionName();
402 }
403 if ($this->hasArgument('pluginName')) {
404 $pluginName = $this->arguments['pluginName'];
405 } else {
406 $pluginName = $request->getPluginName();
407 }
408 if ($extensionName !== NULL && $pluginName != NULL) {
409 return $this->extensionService->getPluginNamespace($extensionName, $pluginName);
410 } else {
411 return '';
412 }
413 }
414
415 /**
416 * Remove Checkbox field names from ViewHelper variable container, to start from scratch when a new form starts.
417 */
418 protected function removeCheckboxFieldNamesFromViewHelperVariableContainer() {
419 if ($this->viewHelperVariableContainer->exists('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\CheckboxViewHelper', 'checkboxFieldNames')) {
420 $this->viewHelperVariableContainer->remove('TYPO3\\CMS\\Fluid\\ViewHelpers\\Form\\CheckboxViewHelper', 'checkboxFieldNames');
421 }
422 }
423 }
424
425 ?>