|
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_Response |
|
16
|
|
|
* |
|
17
|
|
|
* @category Studioforty9 |
|
18
|
|
|
* @package Studioforty9_Recaptcha |
|
19
|
|
|
* @subpackage Helper |
|
20
|
|
|
* @author StudioForty9 <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Studioforty9_Recaptcha_Helper_Response extends Mage_Core_Helper_Abstract |
|
23
|
|
|
{ |
|
24
|
|
|
const MISSING_INPUT_SECRET = 'missing-input-secret'; |
|
25
|
|
|
const INVALID_INPUT_SECRET = 'invalid-input-secret'; |
|
26
|
|
|
const MISSING_INPUT_RESPONSE = 'missing-input-response'; |
|
27
|
|
|
const INVALID_INPUT_RESPONSE = 'invalid-input-response'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var bool $_success |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $_success = false; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array $_errorCodes |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $_errorCodes = array(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array $_errorDescriptions |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $_errorDescriptions = array( |
|
43
|
|
|
self::MISSING_INPUT_SECRET => 'The secret parameter is missing.', |
|
44
|
|
|
self::INVALID_INPUT_SECRET => 'The secret parameter is invalid or malformed.', |
|
45
|
|
|
self::MISSING_INPUT_RESPONSE => 'The response parameter is missing.', |
|
46
|
|
|
self::INVALID_INPUT_RESPONSE => 'The response parameter is invalid or malformed.' |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The constructor allows for a shortcut method of setting the $success and |
|
51
|
|
|
* $errorCodes properties. |
|
52
|
|
|
* |
|
53
|
|
|
* @param bool $success A boolean flag to determine success |
|
54
|
|
|
* @param array $errorCodes An array of error codes |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct($success, $errorCodes = array()) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->setSuccess($success); |
|
59
|
|
|
$this->setErrorCodes($errorCodes); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set the success flag. |
|
64
|
|
|
* |
|
65
|
|
|
* @param bool $success A boolean flag to determine success |
|
66
|
|
|
* |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setSuccess($success) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->_success = $success; |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Set the array of error codes from the response. |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $errorCodes An array of error codes |
|
80
|
|
|
* |
|
81
|
|
|
* @return $this |
|
82
|
|
|
*/ |
|
83
|
|
|
public function setErrorCodes($errorCodes) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->_errorCodes = $errorCodes; |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Is the response a success. |
|
92
|
|
|
* |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public function isSuccess() |
|
96
|
|
|
{ |
|
97
|
|
|
return ($this->_success === true); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Is the response a failure. |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
|
|
public function isFailure() |
|
106
|
|
|
{ |
|
107
|
|
|
return !$this->isSuccess(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Are there any errors passed in from the response. |
|
112
|
|
|
* |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
|
|
public function hasErrors() |
|
116
|
|
|
{ |
|
117
|
|
|
return ! empty($this->_errorCodes); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* An array of descriptive errors. |
|
122
|
|
|
* |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getErrors() |
|
126
|
|
|
{ |
|
127
|
|
|
$errors = array(); |
|
128
|
|
|
foreach ($this->_errorCodes as $errorCode) { |
|
129
|
|
|
$errors[] = $this->getErrorDescription($errorCode); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $errors; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get the error description. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $errorCode The human readable translation of the error code |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getErrorDescription($errorCode) |
|
143
|
|
|
{ |
|
144
|
|
|
if (!array_key_exists($errorCode, $this->_errorDescriptions)) { |
|
145
|
|
|
return 'Unknown error.'; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $this->_errorDescriptions[$errorCode]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Log the error to file. |
|
153
|
|
|
* |
|
154
|
|
|
* @codeCoverageIgnore |
|
155
|
|
|
* @return bool |
|
156
|
|
|
*/ |
|
157
|
|
|
public function log() |
|
158
|
|
|
{ |
|
159
|
|
|
if (Mage::getStoreConfigFlag('dev/log/active')) { |
|
160
|
|
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
Mage::log( |
|
164
|
|
|
sprintf( |
|
165
|
|
|
'reCAPTCHA Errors: %1$s', |
|
166
|
|
|
implode(', ', $this->getErrors()) |
|
167
|
|
|
) |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
return true; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|