|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Studioforty9_Recaptcha |
|
4
|
|
|
* |
|
5
|
|
|
* @category Studioforty9 |
|
6
|
|
|
* @package Studioforty9_Recaptcha |
|
7
|
|
|
* @author StudioForty9 <[email protected]> |
|
8
|
|
|
* @copyright 2015 StudioForty9 (http://www.studioforty9.com) |
|
9
|
|
|
* @license https://github.com/studioforty9/recaptcha/blob/master/LICENCE BSD |
|
10
|
|
|
* @version 1.5.7 |
|
11
|
|
|
* @link https://github.com/studioforty9/recaptcha |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Studioforty9_Recaptcha_Helper_Request |
|
16
|
|
|
* |
|
17
|
|
|
* @category Studioforty9 |
|
18
|
|
|
* @package Studioforty9_Recaptcha |
|
19
|
|
|
* @subpackage Helper |
|
20
|
|
|
* @author StudioForty9 <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Studioforty9_Recaptcha_Helper_Request extends Mage_Core_Helper_Abstract |
|
23
|
|
|
{ |
|
24
|
|
|
const REQUEST_URL = 'https://www.google.com/recaptcha/api/siteverify'; |
|
25
|
|
|
const REQUEST_RESPONSE = 'g-recaptcha-response'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Varien_Http_Client $_client |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $_client; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Set the client to make the request to recaptca. |
|
34
|
|
|
* |
|
35
|
|
|
* @param Varien_Http_Client $client The http client to use |
|
36
|
|
|
* |
|
37
|
|
|
* @return $this |
|
38
|
|
|
*/ |
|
39
|
|
|
public function setHttpClient(Varien_Http_Client $client) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->_client = $client; |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the Http Client to use for the request to reCAPTCHA |
|
48
|
|
|
* |
|
49
|
|
|
* @return Zend_Http_Client |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getHttpClient() |
|
52
|
|
|
{ |
|
53
|
|
|
if (is_null($this->_client)) { |
|
54
|
|
|
$this->_client = new Zend_Http_Client(); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$this->_client->setUri(self::REQUEST_URL); |
|
58
|
|
|
|
|
59
|
|
|
return $this->_client; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Verify the details of the recaptcha request. |
|
64
|
|
|
* |
|
65
|
|
|
* @return Studioforty9_Recaptcha_Helper_Response |
|
66
|
|
|
*/ |
|
67
|
|
|
public function verify() |
|
68
|
|
|
{ |
|
69
|
|
|
$params = array( |
|
70
|
|
|
'secret' => $this->_getHelper()->getSecretKey(), |
|
71
|
|
|
'response' => $this->_getRequest()->getPost(self::REQUEST_RESPONSE), |
|
72
|
|
|
'remoteip' => $this->_getRequest()->getClientIp(true), |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$client = $this->getHttpClient(); |
|
76
|
|
|
$client->setParameterPost($params); |
|
77
|
|
|
$errors = array(); |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
|
|
$response = $client->request('POST'); |
|
81
|
|
|
$body = $response->getBody(); |
|
82
|
|
|
$data = Mage::helper('core')->jsonDecode($body); |
|
83
|
|
|
if (array_key_exists('error-codes', $data)) { |
|
84
|
|
|
$errors = $data['error-codes']; |
|
85
|
|
|
} |
|
86
|
|
|
} catch (Exception $e) { |
|
87
|
|
|
Mage::logException($e); |
|
88
|
|
|
$data = array('success' => false); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return new Studioforty9_Recaptcha_Helper_Response($data['success'], $errors); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Get the module data helper. |
|
96
|
|
|
* |
|
97
|
|
|
* @codeCoverageIgnore |
|
98
|
|
|
* @return Studioforty9_Recaptcha_Helper_Data |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function _getHelper() |
|
101
|
|
|
{ |
|
102
|
|
|
return Mage::helper('studioforty9_recaptcha'); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..