Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
add Email a friend reCaptcha
Browse files Browse the repository at this point in the history
  • Loading branch information
ProxiBlue committed Jan 27, 2018
1 parent 6634e1d commit 47a997a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 6 deletions.
44 changes: 40 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Since 1.4.0 all v1 captcha (pre I am not a Robot) has been removed. There is thu
Disable / Enable form submit buttons
------------------------------------

From 1.4.0 you can add teh class 'enable-captcha-clicked' to any element and add the 'disabled' property to that element.
From 1.4.0 you can add the class 'enable-captcha-clicked' to any element and add the 'disabled' property to that element.
After clicked, the element will be enabled.

example:
Expand Down Expand Up @@ -157,7 +157,7 @@ An example is as such:
###Submitting Contact Us via AJAX

From version 1.3.0, you can pass two additional params via an AJAX submitted form.
The response form the module will then be a JSON string denoting if teh captcha failed.
The response form the module will then be a JSON string denoting if the captcha failed.

Example AJAX call to submit a contact us form:

Expand Down Expand Up @@ -223,8 +223,12 @@ Product Review Captcha
Customer Wishlist Sharing Captcha
-------------------------

The core functionality can easily be used to produce spam.
The process is that an account is created, then a product is added, then spam is generated via the share functionality, with spam messages in the message field.
Adding reCaptcha allows you to block this.

* Enable in admin under Customer Configuration by selecting 'Wishlist Sharing' in available forms list
* Unfortunately the core wishlist sharing form does not have an after form elements block, so you will need to adjust your reviews form to display the captcha.
* Unfortunately the core wishlist sharing form does not have an after form elements block, so you will need to adjust your form to display the captcha.

Edit the wishlist sharing form located here:

Expand All @@ -235,7 +239,39 @@ Customer Wishlist Sharing Captcha

<?php echo $this->getChildHtml('recaptcha'); ?>

Entered values will be retained upon incorrect captcha entry
Entered values will be retained upon incorrect captcha entry

Product Email a Friend Captcha
------------------------------

The core functionality can easily be used to produce spam.
The process is that an account is created, then a product is Emailed to a Friend, then spam is generated via the send functionality, with spam messages in the message field.
Adding reCaptcha allows you to block this.

* Enable in admin under Customer Configuration by selecting 'Email a Friend' in available forms list

Unfortunately the core Send a Friend form does not have an after form elements block, so you will need to adjust your form to display the captcha.
Additionally, the form allows you to add/remove recipients by adding/removing more recipient rows.
Some changes need to be made to the base form (copy to your own theme folder) to accommodate this functionality, else the recipients will be lost upon incorrect captcha.

The changes are done to retain the core add/remove functionality of the form.
Since these changes are fairly big, I have provided a diff which you can apply, and for reference there is also a gist with the complete form ready to use (if you have no custom changes in your theme for the ```sendfiend/send.phtml``` template, you can simply just place the provided gist file to your theme as the template ```sendfiend\send.phtml```)

[Full file](https://gist.github.com/ProxiBlue/bff88b184c9c0e44997ff6ae05468b01)
[diff](https://gist.github.com/ProxiBlue/fdf28f9bf8b678e9aa30c475d681f974)

####Explaining the changes:

[recipients counter](https://gist.github.com/ProxiBlue/bff88b184c9c0e44997ff6ae05468b01#file-gistfile1-txt-L35)
This change pre-sets the counter used to add new recipients. By default this is 0, since no recipients are initially present

[pre propulate set receipients](https://gist.github.com/ProxiBlue/bff88b184c9c0e44997ff6ae05468b01#file-gistfile1-txt-L108) Injects the already defined recipients back into the form, after any failed captcha

[inject captcha form](https://gist.github.com/ProxiBlue/bff88b184c9c0e44997ff6ae05468b01#file-gistfile1-txt-L102) ads the reCaptcha form to the display



Entered values will be retained upon incorrect captcha entry

Captcha is still not appearing, even after I did the steps above!
-----------------------------------------------------------------
Expand Down
33 changes: 33 additions & 0 deletions app/code/community/ProxiBlue/ReCaptcha/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,38 @@ public function checkWishlist($observer)
return $this;
}

/**
* Check Captcha On Send to Friend
*
* @param Varien_Event_Observer $observer
*
* @return Mage_Captcha_Model_Observer
*/
public function checkSendFriend($observer)
{
$formId = 'product_sendtofriend';
$captchaModel = Mage::helper('captcha')->getCaptcha($formId);
if ($captchaModel->isRequired()) {
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->_getCaptchaString($controller->getRequest(), $formId))) {
$request = $controller->getRequest();
$isAjax = $request->getParam('json');
// insert form data to session, allowing to re-populate the contact us form
$data = $controller->getRequest()->getPost();
$productId = (int)$controller->getRequest()->getParam('id');
$catId = (int)$controller->getRequest()->getParam('cat_id');
Mage::getSingleton('catalog/session')->setData('sendfriend_form_data', $data);
$controller->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
if($isAjax) {
$controller->getResponse()->setBody(Zend_Json::encode(array('error'=>Mage::helper('captcha')->__('Incorrect CAPTCHA.'))));
} else {
Mage::getSingleton('catalog/session')->addError(Mage::helper('captcha')->__('Incorrect CAPTCHA.'));
$controller->getResponse()->setRedirect(Mage::getUrl('*/*/send',array('id' => $productId, 'cat_id' => $catId)));
}
}
}
return $this;
}


}
2 changes: 1 addition & 1 deletion app/code/community/ProxiBlue/ReCaptcha/Model/Recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ProxiBlue_ReCaptcha_Model_Recaptcha extends Mage_Captcha_Model_Zend implem
* normally recaptcha will not show for logged in users. Form ids listed here will also appear when logged in
*
*/
protected $_alwaysShow = array('user_wishlist');
protected $_alwaysShow = array('user_wishlist', 'product_sendtofriend');
/**
* Key in session for captcha code
*/
Expand Down
16 changes: 15 additions & 1 deletion app/code/community/ProxiBlue/ReCaptcha/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config>
<modules>
<ProxiBlue_ReCaptcha>
<version>1.5.1</version>
<version>1.6.0</version>
<depends>
<Mage_Captcha/>
</depends>
Expand Down Expand Up @@ -54,6 +54,14 @@
</captcha>
</observers>
</controller_action_predispatch_wishlist_index_send>
<controller_action_predispatch_sendfriend_product_sendmail>
<observers>
<captcha>
<class>proxiblue_recaptcha/observer</class>
<method>checkSendFriend</method>
</captcha>
</observers>
</controller_action_predispatch_sendfriend_product_sendmail>
</events>
</global>
<adminhtml>
Expand Down Expand Up @@ -126,6 +134,12 @@
<user_wishlist>
<label>Wishlist Sharing</label>
</user_wishlist>
<user_wishlist>
<label>Wishlist Sharing</label>
</user_wishlist>
<product_sendtofriend>
<label>Product Email to a Friend</label>
</product_sendtofriend>
</areas>
</frontend>
</captcha>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@
</block>
</reference>
</wishlist_index_share>
<sendfriend_product_send translate="label">
<reference name="sendfriend.send">
<block type="captcha/captcha" name="recaptcha">
<action method="setFormId">
<formId>product_sendtofriend</formId>
</action>
</block>
</reference>
</sendfriend_product_send>
</layout>

0 comments on commit 47a997a

Please sign in to comment.