Last active
November 11, 2016 08:09
-
-
Save razbakov/5685787 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Your_Module_Model_Observer | |
{ | |
/** | |
* Get Captcha String | |
* | |
* @param Varien_Object $request | |
* @param string $formId | |
* @return string | |
*/ | |
protected function _getCaptchaString($request, $formId) | |
{ | |
$captchaParams = $request->getPost(Mage_Captcha_Helper_Data::INPUT_NAME_FIELD_VALUE); | |
return $captchaParams[$formId]; | |
} | |
/** | |
* Break the execution in case of incorrect CAPTCHA | |
* | |
* @param Varien_Event_Observer $observer | |
* @return Your_Module_Model_Observer | |
*/ | |
public function checkCaptcha($observer) | |
{ | |
$formId = 'contact_form'; | |
$captchaModel = Mage::helper('captcha')->getCaptcha($formId); | |
if ($captchaModel->isRequired()) { | |
$controller = $observer->getControllerAction(); | |
if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) { | |
Mage::getSingleton('customer/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.')); | |
$controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true); | |
Mage::getSingleton('customer/session')->setCustomerFormData($controller->getRequest()->getPost()); | |
$controller->getResponse()->setRedirect(Mage::getUrl('*/*/index')); | |
} | |
} | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment