productreviewcaptcha - Version 1.3.0

Version Notes

Implemented reCaptcha V2.0.

Download this release

Release Info

Developer Magento Core Team
Extension productreviewcaptcha
Version 1.3.0
Comparing to
See all releases


Code changes from version 1.2.2 to 1.3.0

app/code/community/OlegKoval/ProductReviewCaptcha/Block/Form.php CHANGED
@@ -5,7 +5,7 @@
5
  *
6
  * @category OlegKoval
7
  * @package OlegKoval_ProductReviewCaptcha
8
- * @copyright Copyright (c) 2012 Oleg Koval
9
  * @author Oleg Koval <oleh.koval@gmail.com>
10
  */
11
 
@@ -30,7 +30,7 @@ class OlegKoval_ProductReviewCaptcha_Block_Form extends Mage_Review_Block_Form {
30
  if ($data == null) {
31
  $data = Mage::getSingleton('core/session')->getFormData(true);
32
  }
33
-
34
  $data = new Varien_Object($data);
35
 
36
  // add logged in customer name as nickname
@@ -55,17 +55,13 @@ class OlegKoval_ProductReviewCaptcha_Block_Form extends Mage_Review_Block_Form {
55
 
56
  //if "Product Review Captcha" module is enabled - then we display template with reCAPTCHA
57
  if (Mage::getStoreConfigFlag(self::XML_PATH_PRC_ENABLED)) {
58
- //include reCaptcha library
59
- require_once(Mage::getModuleDir('', 'OlegKoval_ProductReviewCaptcha') . DS .'Helper'. DS .'recaptchalib.php');
60
-
61
- //create captcha html-code
62
- $publickey = Mage::getStoreConfig(self::XML_PATH_PRC_PUBLIC_KEY);
63
- $captcha_code = recaptcha_get_html($publickey, null, Mage::app()->getStore()->isCurrentlySecure());
64
 
65
  //get reCaptcha theme name
66
  $theme = Mage::getStoreConfig(self::XML_PATH_PRC_THEME);
67
- if (strlen($theme) == 0 || !in_array($theme, array('red', 'white', 'blackglass', 'clean'))) {
68
- $theme = 'red';
69
  }
70
 
71
  //get reCaptcha lang name
@@ -74,13 +70,10 @@ class OlegKoval_ProductReviewCaptcha_Block_Form extends Mage_Review_Block_Form {
74
  $lang = 'en';
75
  }
76
 
77
- //small hack for language feature - because it's not working as described in documentation
78
- $captcha_code = str_replace('?k=', '?hl='. $lang .'&amp;k=', $captcha_code);
79
-
80
  $this->setTemplate('productreviewcaptcha/form.phtml')
81
  ->assign('data', $data)
82
  ->assign('messages', Mage::getSingleton('review/session')->getMessages(true))
83
- ->setCaptchaCode($captcha_code)
84
  ->setCaptchaTheme($theme)
85
  ->setCaptchaLang($lang);
86
  }
5
  *
6
  * @category OlegKoval
7
  * @package OlegKoval_ProductReviewCaptcha
8
+ * @copyright Copyright (c) 2012 - 2016 Oleg Koval
9
  * @author Oleg Koval <oleh.koval@gmail.com>
10
  */
11
 
30
  if ($data == null) {
31
  $data = Mage::getSingleton('core/session')->getFormData(true);
32
  }
33
+
34
  $data = new Varien_Object($data);
35
 
36
  // add logged in customer name as nickname
55
 
56
  //if "Product Review Captcha" module is enabled - then we display template with reCAPTCHA
57
  if (Mage::getStoreConfigFlag(self::XML_PATH_PRC_ENABLED)) {
58
+ //get site key
59
+ $siteKey = Mage::getStoreConfig(self::XML_PATH_PRC_PUBLIC_KEY);
 
 
 
 
60
 
61
  //get reCaptcha theme name
62
  $theme = Mage::getStoreConfig(self::XML_PATH_PRC_THEME);
63
+ if (strlen($theme) == 0 || !in_array($theme, array('dark', 'light'))) {
64
+ $theme = 'light';
65
  }
66
 
67
  //get reCaptcha lang name
70
  $lang = 'en';
71
  }
72
 
 
 
 
73
  $this->setTemplate('productreviewcaptcha/form.phtml')
74
  ->assign('data', $data)
75
  ->assign('messages', Mage::getSingleton('review/session')->getMessages(true))
76
+ ->setSiteKey($siteKey)
77
  ->setCaptchaTheme($theme)
78
  ->setCaptchaLang($lang);
79
  }
app/code/community/OlegKoval/ProductReviewCaptcha/Model/System/Config/Source/Dropdown/Lang.php CHANGED
@@ -4,7 +4,7 @@
4
  *
5
  * @category OlegKoval
6
  * @package OlegKoval_ProductReviewCaptcha
7
- * @copyright Copyright (c) 2012 Oleg Koval
8
  * @author Oleg Koval <oleh.koval@gmail.com>
9
  */
10
  class OlegKoval_ProductReviewCaptcha_Model_System_Config_Source_Dropdown_Lang {
4
  *
5
  * @category OlegKoval
6
  * @package OlegKoval_ProductReviewCaptcha
7
+ * @copyright Copyright (c) 2012 - 2016 Oleg Koval
8
  * @author Oleg Koval <oleh.koval@gmail.com>
9
  */
10
  class OlegKoval_ProductReviewCaptcha_Model_System_Config_Source_Dropdown_Lang {
app/code/community/OlegKoval/ProductReviewCaptcha/Model/System/Config/Source/Dropdown/Theme.php CHANGED
@@ -4,27 +4,19 @@
4
  *
5
  * @category OlegKoval
6
  * @package OlegKoval_ProductReviewCaptcha
7
- * @copyright Copyright (c) 2012 Oleg Koval
8
  * @author Oleg Koval <oleh.koval@gmail.com>
9
  */
10
  class OlegKoval_ProductReviewCaptcha_Model_System_Config_Source_Dropdown_Theme {
11
  public function toOptionArray() {
12
  return array(
13
  array(
14
- 'value' => 'red',
15
- 'label' => 'Red (default)',
16
  ),
17
  array(
18
- 'value' => 'white',
19
- 'label' => 'White',
20
- ),
21
- array(
22
- 'value' => 'blackglass',
23
- 'label' => 'Blackglass',
24
- ),
25
- array(
26
- 'value' => 'clean',
27
- 'label' => 'Clean',
28
  ),
29
  );
30
  }
4
  *
5
  * @category OlegKoval
6
  * @package OlegKoval_ProductReviewCaptcha
7
+ * @copyright Copyright (c) 2012- 2016 Oleg Koval
8
  * @author Oleg Koval <oleh.koval@gmail.com>
9
  */
10
  class OlegKoval_ProductReviewCaptcha_Model_System_Config_Source_Dropdown_Theme {
11
  public function toOptionArray() {
12
  return array(
13
  array(
14
+ 'value' => 'light',
15
+ 'label' => 'Light (default)',
16
  ),
17
  array(
18
+ 'value' => 'dark',
19
+ 'label' => 'Dark',
 
 
 
 
 
 
 
 
20
  ),
21
  );
22
  }
app/code/community/OlegKoval/ProductReviewCaptcha/controllers/ProductController.php CHANGED
@@ -4,7 +4,7 @@
4
  *
5
  * @category OlegKoval
6
  * @package OlegKoval_ProductReviewCaptcha
7
- * @copyright Copyright (c) 2012 Oleg Koval
8
  * @author Oleg Koval <oleh.koval@gmail.com>
9
  */
10
  //include controller to override it
@@ -15,8 +15,7 @@ class OlegKoval_ProductReviewCaptcha_ProductController extends Mage_Review_Produ
15
  const XML_PATH_PRC_PRIVATE_KEY = 'catalog/review/prc_private_key';
16
 
17
  /**
18
- * Call parent preDispatch() method
19
- *
20
  */
21
  public function preDispatch() {
22
  parent::preDispatch();
@@ -26,23 +25,14 @@ class OlegKoval_ProductReviewCaptcha_ProductController extends Mage_Review_Produ
26
 
27
  /**
28
  * Submit new review action
29
- *
30
  */
31
  public function postAction() {
32
  if (Mage::getStoreConfigFlag(self::XML_PATH_PRC_ENABLED)) {
33
  try {
34
  $post = $this->getRequest()->getPost();
35
  if ($post) {
36
- //include reCaptcha library
37
- require_once(Mage::getModuleDir('', 'OlegKoval_ProductReviewCaptcha') . DS .'Helper'. DS .'recaptchalib.php');
38
-
39
  //validate captcha
40
- $privatekey = Mage::getStoreConfig(self::XML_PATH_PRC_PRIVATE_KEY);
41
- $remote_addr = $this->getRequest()->getServer('REMOTE_ADDR');
42
- $captcha = recaptcha_check_answer($privatekey, $remote_addr, $post["recaptcha_challenge_field"], $post["recaptcha_response_field"]);
43
-
44
-
45
- if (!$captcha->is_valid) {
46
  throw new Exception($this->__("The reCAPTCHA wasn't entered correctly."), 1);
47
  }
48
  }
@@ -68,4 +58,39 @@ class OlegKoval_ProductReviewCaptcha_ProductController extends Mage_Review_Produ
68
  parent::postAction();
69
  }
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  }
4
  *
5
  * @category OlegKoval
6
  * @package OlegKoval_ProductReviewCaptcha
7
+ * @copyright Copyright (c) 2012 - 2016 Oleg Koval
8
  * @author Oleg Koval <oleh.koval@gmail.com>
9
  */
10
  //include controller to override it
15
  const XML_PATH_PRC_PRIVATE_KEY = 'catalog/review/prc_private_key';
16
 
17
  /**
18
+ * @see parent::preDispatch
 
19
  */
20
  public function preDispatch() {
21
  parent::preDispatch();
25
 
26
  /**
27
  * Submit new review action
 
28
  */
29
  public function postAction() {
30
  if (Mage::getStoreConfigFlag(self::XML_PATH_PRC_ENABLED)) {
31
  try {
32
  $post = $this->getRequest()->getPost();
33
  if ($post) {
 
 
 
34
  //validate captcha
35
+ if (!isset($post['g-recaptcha-response']) || !$this->isCaptchaValid($post['g-recaptcha-response'])) {
 
 
 
 
 
36
  throw new Exception($this->__("The reCAPTCHA wasn't entered correctly."), 1);
37
  }
38
  }
58
  parent::postAction();
59
  }
60
 
61
+ /**
62
+ * Check if captcha is valid
63
+ * @param string $captchaResponse
64
+ * @return boolean
65
+ */
66
+ private function isCaptchaValid($captchaResponse) {
67
+ $result = false;
68
+
69
+ $params = array(
70
+ 'secret' => Mage::getStoreConfig(self::XML_PATH_PRC_PRIVATE_KEY),
71
+ 'response' => $captchaResponse
72
+ );
73
+
74
+ $ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
75
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1) ;
76
+ curl_setopt($ch, CURLOPT_HEADER, 0);
77
+ curl_setopt($ch, CURLOPT_POST, 1);
78
+ curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
79
+ curl_setopt($ch, CURLOPT_VERBOSE, 1);
80
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
81
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
82
+ curl_setopt($ch, CURLOPT_TIMEOUT, 30);
83
+ $requestResult = trim(curl_exec($ch));
84
+ curl_close($ch);
85
+
86
+ if (is_array(json_decode($requestResult, true))) {
87
+ $response = json_decode($requestResult, true);
88
+
89
+ if (isset($response['success']) && $response['success'] === true) {
90
+ $result = true;
91
+ }
92
+ }
93
+
94
+ return $result;
95
+ }
96
  }
app/code/community/OlegKoval/ProductReviewCaptcha/etc/config.xml CHANGED
@@ -5,14 +5,14 @@
5
  *
6
  * @category OlegKoval
7
  * @package OlegKoval_ProductReviewCaptcha
8
- * @copyright Copyright (c) 2012 Oleg Koval
9
  * @author Oleg Koval <oleh.koval@gmail.com>
10
  */
11
  -->
12
  <config>
13
  <modules>
14
  <OlegKoval_ProductReviewCaptcha>
15
- <version>1.2.1</version>
16
  <depends>
17
  <Mage_Contacts/>
18
  </depends>
5
  *
6
  * @category OlegKoval
7
  * @package OlegKoval_ProductReviewCaptcha
8
+ * @copyright Copyright (c) 2012 - 2016 Oleg Koval
9
  * @author Oleg Koval <oleh.koval@gmail.com>
10
  */
11
  -->
12
  <config>
13
  <modules>
14
  <OlegKoval_ProductReviewCaptcha>
15
+ <version>1.3.0</version>
16
  <depends>
17
  <Mage_Contacts/>
18
  </depends>
app/code/community/OlegKoval/ProductReviewCaptcha/etc/system.xml CHANGED
@@ -5,7 +5,7 @@
5
  *
6
  * @category OlegKoval
7
  * @package OlegKoval_ProductReviewCaptcha
8
- * @copyright Copyright (c) 2012 Oleg Koval
9
  * @author Oleg Koval <oleh.koval@gmail.com>
10
  */
11
  -->
5
  *
6
  * @category OlegKoval
7
  * @package OlegKoval_ProductReviewCaptcha
8
+ * @copyright Copyright (c) 2012 - 2016 Oleg Koval
9
  * @author Oleg Koval <oleh.koval@gmail.com>
10
  */
11
  -->
app/design/frontend/base/default/template/productreviewcaptcha/form.phtml CHANGED
@@ -27,13 +27,15 @@
27
  <div class="form-add">
28
  <h2><?php echo $this->__('Write Your Own Review') ?></h2>
29
  <?php if ($this->getAllowWriteReviewFlag()): ?>
 
30
  <script type="text/javascript">
31
- //<![CDATA[
32
- var RecaptchaOptions = {
33
- theme : '<?php echo $this->getCaptchaTheme(); ?>',
34
- lang : '<?php echo $this->getCaptchaLang(); ?>',
35
- };
36
- //]]>
 
37
  </script>
38
  <form action="<?php echo $this->getAction() ?>" method="post" id="review-form">
39
  <fieldset>
@@ -95,7 +97,7 @@
95
  </li>
96
  <li class="wide">
97
  <div class="input-box">
98
- <?php echo $this->getCaptchaCode(); ?>
99
  </div>
100
  </li>
101
  </ul>
27
  <div class="form-add">
28
  <h2><?php echo $this->__('Write Your Own Review') ?></h2>
29
  <?php if ($this->getAllowWriteReviewFlag()): ?>
30
+ <script src="https://www.google.com/recaptcha/api.js?onload=renderReCaptcha&render=explicit" async defer></script>
31
  <script type="text/javascript">
32
+ function renderReCaptcha() {
33
+ grecaptcha.render('re_captcha', {
34
+ sitekey: '<?php echo $this->getSiteKey(); ?>',
35
+ theme: '<?php echo $this->getCaptchaTheme(); ?>',
36
+ lang: '<?php echo $this->getCaptchaLang(); ?>'
37
+ });
38
+ }
39
  </script>
40
  <form action="<?php echo $this->getAction() ?>" method="post" id="review-form">
41
  <fieldset>
97
  </li>
98
  <li class="wide">
99
  <div class="input-box">
100
+ <div id="re_captcha"></div>
101
  </div>
102
  </li>
103
  </ul>
app/etc/modules/OlegKoval_ProductReviewCaptcha.xml CHANGED
@@ -5,7 +5,7 @@
5
  *
6
  * @category OlegKoval
7
  * @package OlegKoval_ProductReviewCaptcha
8
- * @copyright Copyright (c) 2012 Oleg Koval
9
  * @author Oleg Koval <oleh.koval@gmail.com>
10
  */
11
  -->
5
  *
6
  * @category OlegKoval
7
  * @package OlegKoval_ProductReviewCaptcha
8
+ * @copyright Copyright (c) 2012 - 2016 Oleg Koval
9
  * @author Oleg Koval <oleh.koval@gmail.com>
10
  */
11
  -->
package.xml CHANGED
@@ -1,18 +1,18 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>productreviewcaptcha</name>
4
- <version>1.2.2</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/OSL-3.0">OSL v3.0</license>
7
  <channel>community</channel>
8
  <extends/>
9
  <summary>This module add captcha to "Product Reviews" form.</summary>
10
  <description>"Product Review Captcha" extension add in easy way the captcha to "Product Reviews" form and will protect this form from unwanted spambots.This extension uses reCaptcha library (http://www.google.com/recaptcha).&lt;/p&gt;</description>
11
- <notes>Added visibility/accessibility of settings on store view level; added translations of property labels (page of module configuration on admin side);</notes>
12
  <authors><author><name>Oleg Koval</name><user>auto-converted</user><email>oleh.koval@gmail.com</email></author></authors>
13
- <date>2016-01-09</date>
14
- <time>14:23:51</time>
15
- <contents><target name="magecommunity"><dir name="OlegKoval"><dir name="ProductReviewCaptcha"><dir name="Block"><file name="Form.php" hash="5e015c8b316403e44998b3c78050a9b9"/></dir><dir name="Helper"><file name="recaptchalib.php" hash="4392db1d69875fd683b2728bd9a5d5f9"/></dir><dir name="Model"><dir name="System"><dir name="Config"><dir name="Source"><dir name="Dropdown"><file name="Lang.php" hash="2378589b507b2c57c04831cf6ac544b0"/><file name="Theme.php" hash="be96ab4e09cd14b48a033c467ae74901"/></dir></dir></dir></dir></dir><dir name="controllers"><file name="ProductController.php" hash="560236e12a38cbbe7d07597413043e8d"/></dir><dir name="etc"><file name="config.xml" hash="449f579876655522028977e1e543a7be"/><file name="system.xml" hash="50de1f643b20daab4030ec3fbdc99127"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="OlegKoval_ProductReviewCaptcha.xml" hash="f83ed30bb9b3b3fbecd4099cf23fa162"/></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="template"><dir name="productreviewcaptcha"><file name="form.phtml" hash="b403758b33f501cfeb92544b8a62f70d"/></dir></dir></dir></dir></dir></target><target name="magelocale"><dir name="en_US"><file name="OlegKoval_ProductReviewCaptcha.csv" hash="f2362617a07150c1783bb5e029e72cb5"/></dir></target></contents>
16
  <compatible/>
17
  <dependencies/>
18
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>productreviewcaptcha</name>
4
+ <version>1.3.0</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/OSL-3.0">OSL v3.0</license>
7
  <channel>community</channel>
8
  <extends/>
9
  <summary>This module add captcha to "Product Reviews" form.</summary>
10
  <description>"Product Review Captcha" extension add in easy way the captcha to "Product Reviews" form and will protect this form from unwanted spambots.This extension uses reCaptcha library (http://www.google.com/recaptcha).&lt;/p&gt;</description>
11
+ <notes>Implemented reCaptcha V2.0.</notes>
12
  <authors><author><name>Oleg Koval</name><user>auto-converted</user><email>oleh.koval@gmail.com</email></author></authors>
13
+ <date>2016-04-07</date>
14
+ <time>14:55:18</time>
15
+ <contents><target name="magecommunity"><dir name="OlegKoval"><dir name="ProductReviewCaptcha"><dir name="Block"><file name="Form.php" hash="61505a0995a61b063d8dbe60c60568a2"/></dir><dir name="Helper"><file name="recaptchalib.php" hash="4392db1d69875fd683b2728bd9a5d5f9"/></dir><dir name="Model"><dir name="System"><dir name="Config"><dir name="Source"><dir name="Dropdown"><file name="Lang.php" hash="240c74354d46bddeb63727ae619ffca3"/><file name="Theme.php" hash="e229a672efdf3dd160b4f4273412524c"/></dir></dir></dir></dir></dir><dir name="controllers"><file name="ProductController.php" hash="c484abd3cf2f6b5721948ce09007fd24"/></dir><dir name="etc"><file name="config.xml" hash="420da3893a601a7a8a7bf8e156ad1283"/><file name="system.xml" hash="94d244c2d56c1b195e8829b93d089a1a"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="OlegKoval_ProductReviewCaptcha.xml" hash="b0b62e4e4aaeab0598074837942c1d9b"/></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="template"><dir name="productreviewcaptcha"><file name="form.phtml" hash="9bb88ce8c4db528902b52738ccfff30c"/></dir></dir></dir></dir></dir></target><target name="magelocale"><dir name="en_US"><file name="OlegKoval_ProductReviewCaptcha.csv" hash="f2362617a07150c1783bb5e029e72cb5"/></dir></target></contents>
16
  <compatible/>
17
  <dependencies/>
18
  </package>