diff --git a/Classes/Controller/FormController.php b/Classes/Controller/FormController.php index 279e3cfa2..ffa6e8756 100644 --- a/Classes/Controller/FormController.php +++ b/Classes/Controller/FormController.php @@ -70,7 +70,7 @@ public function formAction(): void $this->view->assignMultiple( [ 'form' => $form, - 'ttContentData' => $this->contentObject->data, + 'ttContentData' => $this->getCurrentContentObjectData(), 'messageClass' => $this->messageClass, 'action' => ($this->settings['main']['confirmation'] ? 'confirmation' : 'create') ] @@ -95,6 +95,7 @@ public function formAction(): void */ public function initializeConfirmationAction(): void { + $this->forwardIfTtContentUidDoesNotMatch(); $this->forwardIfFormParamsDoNotMatch(); $this->forwardIfMailParamEmpty(); $this->reformatParamsForAction(); @@ -152,6 +153,7 @@ public function confirmationAction(Mail $mail): void */ public function initializeCreateAction(): void { + $this->forwardIfTtContentUidDoesNotMatch(); $this->forwardIfFormParamsDoNotMatch(); $this->forwardIfMailParamEmpty(); $this->reformatParamsForAction(); @@ -202,7 +204,7 @@ public function createAction(Mail $mail, string $hash = ''): void $this->settings, $this->conf ); - $mailPreflight->sendOptinConfirmationMail($mail); + $mailPreflight->sendOptinConfirmationMail($mail, $this->contentObject->data); $this->view->assign('optinActive', true); } if ($this->isPersistActive()) { @@ -238,7 +240,7 @@ protected function sendMailPreflight(Mail $mail, string $hash = ''): void $this->settings, $this->conf ); - $mailPreflight->sendSenderMail($mail); + $mailPreflight->sendSenderMail($mail, $this->contentObject->data); } if ($this->isReceiverMailEnabled()) { $mailPreflight = $this->objectManager->get(SendReceiverMailPreflight::class, $this->settings); @@ -275,7 +277,7 @@ protected function prepareOutput(Mail $mail): void 'mail' => $mail, 'marketingInfos' => SessionUtility::getMarketingInfos(), 'messageClass' => $this->messageClass, - 'ttContentData' => $this->contentObject->data, + 'ttContentData' => $this->getCurrentContentObjectData(), 'uploadService' => $this->uploadService, 'powermail_rte' => $this->settings['thx']['body'], 'powermail_all' => TemplateUtility::powermailAll($mail, 'web', $this->settings, $this->actionMethodName) @@ -403,6 +405,35 @@ public function initializeObject() $this->signalDispatch(__CLASS__, __FUNCTION__ . 'Settings', [$this, &$this->settings]); } + /** + * Initialize Action + * + * @return void + * @codeCoverageIgnore + */ + public function initializeAction(): void + { + $this->storeCurrentContentObjectData(); + } + + /** + * Forward to formAction if content element uids do not match + * used for createAction() and confirmationAction() + * + * @return void + */ + protected function forwardIfTtContentUidDoesNotMatch(): void + { + $arguments = $this->request->getArguments(); + $currentContentObjectData = $this->getCurrentContentObjectData(); + + if (isset($arguments['field']['__ttcontentuid']) && isset($currentContentObjectData['uid']) + && (int) $arguments['field']['__ttcontentuid'] !== (int) $currentContentObjectData['uid'] + ) { + $this->forward('form'); + } + } + /** * Forward to formAction if wrong form in plugin variables given * used for createAction() and confirmationAction() @@ -555,4 +586,40 @@ public function injectPersistenceManager(PersistenceManager $persistenceManager) { $this->persistenceManager = $persistenceManager; } + + /** + * Storing the current content object data to a global variable. That is necessary + * because content object data gets lost after a request is being forwarded to its + * referring request after an errorAction + * + * @return void + */ + protected function storeCurrentContentObjectData(): void + { + if (!empty($this->contentObject->data)) { + $tsfe = ObjectUtility::getTyposcriptFrontendController(); + + if (!is_array($tsfe->applicationData['tx_powermail'])) { + $tsfe->applicationData['tx_powermail'] = []; + } + + $tsfe->applicationData['tx_powermail']['currentContentObjectData'] = $this->contentObject->data; + } + } + + /** + * Retrieving data of content object that is currently being rendered + * + * @return array + */ + protected function getCurrentContentObjectData(): array + { + $tsfe = ObjectUtility::getTyposcriptFrontendController(); + + if (isset($tsfe->applicationData['tx_powermail']['currentContentObjectData'])) { + return $tsfe->applicationData['tx_powermail']['currentContentObjectData']; + } + + return []; + } } diff --git a/Classes/ViewHelpers/Misc/PrefillFieldViewHelper.php b/Classes/ViewHelpers/Misc/PrefillFieldViewHelper.php index 6c19863ff..1f073fe79 100644 --- a/Classes/ViewHelpers/Misc/PrefillFieldViewHelper.php +++ b/Classes/ViewHelpers/Misc/PrefillFieldViewHelper.php @@ -151,7 +151,7 @@ protected function getFromMail(string $value) */ protected function getFromMarker($value) { - if (empty($value) && isset($this->variables['field'][$this->getMarker()])) { + if (empty($value) && isset($this->variables['field'][$this->getMarker()]) && $this->isSameContentElement()) { $value = $this->variables['field'][$this->getMarker()]; } return $value; @@ -165,7 +165,7 @@ protected function getFromMarker($value) */ protected function getFromRawMarker($value) { - if (empty($value) && isset($this->variables[$this->getMarker()])) { + if (empty($value) && isset($this->variables[$this->getMarker()]) && $this->isSameContentElement()) { $value = $this->variables[$this->getMarker()]; } return $value; @@ -392,4 +392,37 @@ public function initialize() $configurationService = ObjectUtility::getObjectManager()->get(ConfigurationService::class); $this->configuration = $configurationService->getTypoScriptConfiguration(); } + + /** + * Check whether GET / POST values may be used by this form, + * because they are from the same content element as the form was submitted + * + * @return bool + */ + protected function isSameContentElement(): bool + { + $currentContentObjectData = $this->getCurrentContentObjectData(); + + if (isset($currentContentObjectData['uid']) && isset($this->variables['field']['__ttcontentuid'])) { + return (int) $currentContentObjectData['uid'] === (int) $this->variables['field']['__ttcontentuid']; + } + + return true; + } + + /** + * Retrieving data of content object that is currently being rendered + * + * @return array + */ + protected function getCurrentContentObjectData(): array + { + $tsfe = ObjectUtility::getTyposcriptFrontendController(); + + if (isset($tsfe->applicationData['tx_powermail']['currentContentObjectData'])) { + return $tsfe->applicationData['tx_powermail']['currentContentObjectData']; + } + + return []; + } } diff --git a/Classes/ViewHelpers/Misc/PrefillMultiFieldViewHelper.php b/Classes/ViewHelpers/Misc/PrefillMultiFieldViewHelper.php index dc13dbdac..3bfec26c6 100644 --- a/Classes/ViewHelpers/Misc/PrefillMultiFieldViewHelper.php +++ b/Classes/ViewHelpers/Misc/PrefillMultiFieldViewHelper.php @@ -194,7 +194,7 @@ protected function isFromMail(): bool protected function isFromMarker(): bool { $selected = false; - if (isset($this->variables['field'][$this->getMarker()])) { + if (isset($this->variables['field'][$this->getMarker()]) && $this->isSameContentElement()) { if (is_array($this->variables['field'][$this->getMarker()])) { foreach (array_keys($this->variables['field'][$this->getMarker()]) as $key) { if ($this->variables['field'][$this->getMarker()][$key] === $this->options[$this->index]['value'] || @@ -225,7 +225,7 @@ protected function isFromMarker(): bool protected function isFromRawMarker(): bool { $selected = false; - if (isset($this->variables[$this->getMarker()])) { + if (isset($this->variables[$this->getMarker()]) && $this->isSameContentElement()) { if (is_array($this->variables[$this->getMarker()])) { foreach (array_keys($this->variables[$this->getMarker()]) as $key) { if ($this->variables[$this->getMarker()][$key] === $this->options[$this->index]['value'] || @@ -539,4 +539,37 @@ public function initialize(): void $configurationService = ObjectUtility::getObjectManager()->get(ConfigurationService::class); $this->configuration = $configurationService->getTypoScriptConfiguration(); } + + /** + * Check whether GET / POST values may be used by this form, + * because they are from the same content element as the form was submitted + * + * @return bool + */ + protected function isSameContentElement(): bool + { + $currentContentObjectData = $this->getCurrentContentObjectData(); + + if (isset($currentContentObjectData['uid']) && isset($this->variables['field']['__ttcontentuid'])) { + return (int) $currentContentObjectData['uid'] === (int) $this->variables['field']['__ttcontentuid']; + } + + return true; + } + + /** + * Retrieving data of content object that is currently being rendered + * + * @return array + */ + protected function getCurrentContentObjectData(): array + { + $tsfe = ObjectUtility::getTyposcriptFrontendController(); + + if (isset($tsfe->applicationData['tx_powermail']['currentContentObjectData'])) { + return $tsfe->applicationData['tx_powermail']['currentContentObjectData']; + } + + return []; + } } diff --git a/Classes/ViewHelpers/Validation/EnableParsleyAndAjaxViewHelper.php b/Classes/ViewHelpers/Validation/EnableParsleyAndAjaxViewHelper.php index a755aaac3..194a35ca9 100644 --- a/Classes/ViewHelpers/Validation/EnableParsleyAndAjaxViewHelper.php +++ b/Classes/ViewHelpers/Validation/EnableParsleyAndAjaxViewHelper.php @@ -28,6 +28,7 @@ public function initializeArguments() parent::initializeArguments(); $this->registerArgument('form', Form::class, 'Form', true); $this->registerArgument('additionalAttributes', 'array', 'additionalAttributes', false, []); + $this->registerArgument('ttContentData', 'array', 'ttContentData', false, []); } /** @@ -52,6 +53,7 @@ public function render(): array if ($this->settings['misc']['ajaxSubmit'] === '1') { $additionalAttributes['data-powermail-ajax'] = 'true'; $additionalAttributes['data-powermail-form'] = $form->getUid(); + $additionalAttributes['data-powermail-ttcontentuid'] = $this->arguments['ttContentData']['uid']; if ($this->addRedirectUri) { /** @var RedirectUriService $redirectService */ diff --git a/Resources/Private/JavaScript/Powermail/Form.js b/Resources/Private/JavaScript/Powermail/Form.js index e88cdae87..1aacd1823 100644 --- a/Resources/Private/JavaScript/Powermail/Form.js +++ b/Resources/Private/JavaScript/Powermail/Form.js @@ -189,7 +189,7 @@ function PowermailForm($) { if ($this.data('powermail-ajax-uri')) { redirectUri = $this.data('powermail-ajax-uri'); } - var formUid = $this.data('powermail-form'); + var ttContentUid = $this.data('powermail-ttcontentuid'); if (!regularSubmitOnAjax) { $.ajax({ @@ -207,9 +207,9 @@ function PowermailForm($) { fireAjaxCompleteEvent($txPowermail); }, success: function(data) { - var html = $('*[data-powermail-form="' + formUid + '"]:first', data); + var html = $('*[data-powermail-ttcontentuid="' + ttContentUid + '"]:first', data); if (html.length) { - $('*[data-powermail-form="' + formUid + '"]:first').closest('.tx-powermail').html(html); + $('*[data-powermail-ttcontentuid="' + ttContentUid + '"]:first').closest('.tx-powermail').html(html); // fire tabs and parsley again if ($.fn.powermailTabs) { $('.powermail_morestep').powermailTabs(); diff --git a/Resources/Private/Templates/Form/Confirmation.html b/Resources/Private/Templates/Form/Confirmation.html index 7fce19b19..66e063d46 100644 --- a/Resources/Private/Templates/Form/Confirmation.html +++ b/Resources/Private/Templates/Form/Confirmation.html @@ -27,7 +27,7 @@