Skip to content

Commit eaa1b47

Browse files
[BC] Added form key validation to Contacts form (#3146)
Co-authored-by: Mohamed ELIDRISSI <67818913+elidrissidev@users.noreply.github.com>
1 parent c441b05 commit eaa1b47

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

app/code/core/Mage/Contacts/controllers/IndexController.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,24 @@ public function postAction()
5959
/** @var Mage_Core_Model_Translate $translate */
6060
$translate->setTranslateInline(false);
6161
try {
62+
if (!$this->_validateFormKey()) {
63+
Mage::throwException($this->__('Invalid Form Key. Please submit your request again.'));
64+
}
65+
6266
$postObject = new Varien_Object();
6367
$postObject->setData($post);
6468

6569
$error = false;
66-
6770
if (!Zend_Validate::is(trim($post['name']), 'NotEmpty')) {
6871
$error = true;
69-
}
70-
71-
if (!Zend_Validate::is(trim($post['comment']), 'NotEmpty')) {
72+
} elseif (!Zend_Validate::is(trim($post['comment']), 'NotEmpty')) {
7273
$error = true;
73-
}
74-
75-
if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
74+
} elseif (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
7675
$error = true;
7776
}
7877

7978
if ($error) {
80-
throw new Exception();
79+
Mage::throwException($this->__('Unable to submit your request. Please try again later'));
8180
}
8281
$mailTemplate = Mage::getModel('core/email_template');
8382
/** @var Mage_Core_Model_Email_Template $mailTemplate */
@@ -92,19 +91,22 @@ public function postAction()
9291
);
9392

9493
if (!$mailTemplate->getSentSuccess()) {
95-
throw new Exception();
94+
Mage::throwException($this->__('Unable to submit your request. Please try again later'));
9695
}
9796

9897
$translate->setTranslateInline(true);
9998

100-
Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
99+
Mage::getSingleton('customer/session')->addSuccess($this->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
101100
$this->_redirect('*/*/');
102101

103102
return;
104-
} catch (Exception $e) {
103+
} catch (Mage_Core_Exception $e) {
105104
$translate->setTranslateInline(true);
106-
107-
Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
105+
Mage::logException($e);
106+
Mage::getSingleton('customer/session')->addError($e->getMessage());
107+
} catch (Throwable $e) {
108+
Mage::logException($e);
109+
Mage::getSingleton('customer/session')->addError($this->__('Unable to submit your request. Please try again later'));
108110
$this->_redirect('*/*/');
109111
return;
110112
}

app/design/frontend/base/default/template/contacts/form.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<h1><?php echo Mage::helper('contacts')->__('Contact Us') ?></h1>
1919
</div>
2020
<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post">
21+
<?php echo $this->getBlockHtml('formkey') ?>
2122
<div class="fieldset">
2223
<h2 class="legend"><?php echo Mage::helper('contacts')->__('Contact Information') ?></h2>
2324
<ul class="form-list">

app/design/frontend/rwd/default/template/contacts/form.phtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<h1><?php echo Mage::helper('contacts')->__('Contact Us') ?></h1>
1919
</div>
2020
<form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post" class="scaffold-form">
21+
<?php echo $this->getBlockHtml('formkey') ?>
2122
<div class="fieldset">
2223
<h2 class="legend"><?php echo Mage::helper('contacts')->__('Contact Information') ?></h2>
2324
<p class="required"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>

app/locale/en_US/Mage_Contacts.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
"Email Sender","Email Sender"
1111
"Email Template","Email Template"
1212
"Enable Contact Us","Enable Contact Us"
13+
"Invalid Form Key. Please submit your request again.","Invalid Form Key. Please submit your request again."
1314
"Name","Name"
1415
"Send Emails To","Send Emails To"
1516
"Submit","Submit"
1617
"Telephone","Telephone"
17-
"Unable to submit your request. Please, try again later","Unable to submit your request. Please, try again later"
18+
"Unable to submit your request. Please try again later","Unable to submit your request. Please try again later."
1819
"Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.","Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us."

0 commit comments

Comments
 (0)