|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Captcha |
|
17 * @subpackage Adapter |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 */ |
|
21 |
|
22 /** @see Zend_Captcha_Base */ |
|
23 require_once 'Zend/Captcha/Base.php'; |
|
24 |
|
25 /** @see Zend_Service_ReCaptcha */ |
|
26 require_once 'Zend/Service/ReCaptcha.php'; |
|
27 |
|
28 /** |
|
29 * ReCaptcha adapter |
|
30 * |
|
31 * Allows to insert captchas driven by ReCaptcha service |
|
32 * |
|
33 * @see http://recaptcha.net/apidocs/captcha/ |
|
34 * |
|
35 * @category Zend |
|
36 * @package Zend_Captcha |
|
37 * @subpackage Adapter |
|
38 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
39 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
40 * @version $Id: ReCaptcha.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
41 */ |
|
42 class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base |
|
43 { |
|
44 /**@+ |
|
45 * ReCaptcha Field names |
|
46 * @var string |
|
47 */ |
|
48 protected $_CHALLENGE = 'recaptcha_challenge_field'; |
|
49 protected $_RESPONSE = 'recaptcha_response_field'; |
|
50 /**@-*/ |
|
51 |
|
52 /** |
|
53 * Recaptcha service object |
|
54 * |
|
55 * @var Zend_Service_Recaptcha |
|
56 */ |
|
57 protected $_service; |
|
58 |
|
59 /** |
|
60 * Parameters defined by the service |
|
61 * |
|
62 * @var array |
|
63 */ |
|
64 protected $_serviceParams = array(); |
|
65 |
|
66 /** |
|
67 * Options defined by the service |
|
68 * |
|
69 * @var array |
|
70 */ |
|
71 protected $_serviceOptions = array(); |
|
72 |
|
73 /**#@+ |
|
74 * Error codes |
|
75 */ |
|
76 const MISSING_VALUE = 'missingValue'; |
|
77 const ERR_CAPTCHA = 'errCaptcha'; |
|
78 const BAD_CAPTCHA = 'badCaptcha'; |
|
79 /**#@-*/ |
|
80 |
|
81 /** |
|
82 * Error messages |
|
83 * @var array |
|
84 */ |
|
85 protected $_messageTemplates = array( |
|
86 self::MISSING_VALUE => 'Missing captcha fields', |
|
87 self::ERR_CAPTCHA => 'Failed to validate captcha', |
|
88 self::BAD_CAPTCHA => 'Captcha value is wrong: %value%', |
|
89 ); |
|
90 |
|
91 /** |
|
92 * Retrieve ReCaptcha Private key |
|
93 * |
|
94 * @return string |
|
95 */ |
|
96 public function getPrivkey() |
|
97 { |
|
98 return $this->getService()->getPrivateKey(); |
|
99 } |
|
100 |
|
101 /** |
|
102 * Retrieve ReCaptcha Public key |
|
103 * |
|
104 * @return string |
|
105 */ |
|
106 public function getPubkey() |
|
107 { |
|
108 return $this->getService()->getPublicKey(); |
|
109 } |
|
110 |
|
111 /** |
|
112 * Set ReCaptcha Private key |
|
113 * |
|
114 * @param string $privkey |
|
115 * @return Zend_Captcha_ReCaptcha |
|
116 */ |
|
117 public function setPrivkey($privkey) |
|
118 { |
|
119 $this->getService()->setPrivateKey($privkey); |
|
120 return $this; |
|
121 } |
|
122 |
|
123 /** |
|
124 * Set ReCaptcha public key |
|
125 * |
|
126 * @param string $pubkey |
|
127 * @return Zend_Captcha_ReCaptcha |
|
128 */ |
|
129 public function setPubkey($pubkey) |
|
130 { |
|
131 $this->getService()->setPublicKey($pubkey); |
|
132 return $this; |
|
133 } |
|
134 |
|
135 /** |
|
136 * Constructor |
|
137 * |
|
138 * @param array|Zend_Config $options |
|
139 * @return void |
|
140 */ |
|
141 public function __construct($options = null) |
|
142 { |
|
143 $this->setService(new Zend_Service_ReCaptcha()); |
|
144 $this->_serviceParams = $this->getService()->getParams(); |
|
145 $this->_serviceOptions = $this->getService()->getOptions(); |
|
146 |
|
147 parent::__construct($options); |
|
148 |
|
149 if ($options instanceof Zend_Config) { |
|
150 $options = $options->toArray(); |
|
151 } |
|
152 if (!empty($options)) { |
|
153 $this->setOptions($options); |
|
154 } |
|
155 } |
|
156 |
|
157 /** |
|
158 * Set service object |
|
159 * |
|
160 * @param Zend_Service_ReCaptcha $service |
|
161 * @return Zend_Captcha_ReCaptcha |
|
162 */ |
|
163 public function setService(Zend_Service_ReCaptcha $service) |
|
164 { |
|
165 $this->_service = $service; |
|
166 return $this; |
|
167 } |
|
168 |
|
169 /** |
|
170 * Retrieve ReCaptcha service object |
|
171 * |
|
172 * @return Zend_Service_ReCaptcha |
|
173 */ |
|
174 public function getService() |
|
175 { |
|
176 return $this->_service; |
|
177 } |
|
178 |
|
179 /** |
|
180 * Set option |
|
181 * |
|
182 * If option is a service parameter, proxies to the service. The same |
|
183 * goes for any service options (distinct from service params) |
|
184 * |
|
185 * @param string $key |
|
186 * @param mixed $value |
|
187 * @return Zend_Captcha_ReCaptcha |
|
188 */ |
|
189 public function setOption($key, $value) |
|
190 { |
|
191 $service = $this->getService(); |
|
192 if (isset($this->_serviceParams[$key])) { |
|
193 $service->setParam($key, $value); |
|
194 return $this; |
|
195 } |
|
196 if (isset($this->_serviceOptions[$key])) { |
|
197 $service->setOption($key, $value); |
|
198 return $this; |
|
199 } |
|
200 return parent::setOption($key, $value); |
|
201 } |
|
202 |
|
203 /** |
|
204 * Generate captcha |
|
205 * |
|
206 * @see Zend_Form_Captcha_Adapter::generate() |
|
207 * @return string |
|
208 */ |
|
209 public function generate() |
|
210 { |
|
211 return ""; |
|
212 } |
|
213 |
|
214 /** |
|
215 * Validate captcha |
|
216 * |
|
217 * @see Zend_Validate_Interface::isValid() |
|
218 * @param mixed $value |
|
219 * @return boolean |
|
220 */ |
|
221 public function isValid($value, $context = null) |
|
222 { |
|
223 if (!is_array($value) && !is_array($context)) { |
|
224 $this->_error(self::MISSING_VALUE); |
|
225 return false; |
|
226 } |
|
227 |
|
228 if (!is_array($value) && is_array($context)) { |
|
229 $value = $context; |
|
230 } |
|
231 |
|
232 if (empty($value[$this->_CHALLENGE]) || empty($value[$this->_RESPONSE])) { |
|
233 $this->_error(self::MISSING_VALUE); |
|
234 return false; |
|
235 } |
|
236 |
|
237 $service = $this->getService(); |
|
238 |
|
239 $res = $service->verify($value[$this->_CHALLENGE], $value[$this->_RESPONSE]); |
|
240 |
|
241 if (!$res) { |
|
242 $this->_error(self::ERR_CAPTCHA); |
|
243 return false; |
|
244 } |
|
245 |
|
246 if (!$res->isValid()) { |
|
247 $this->_error(self::BAD_CAPTCHA, $res->getErrorCode()); |
|
248 $service->setParam('error', $res->getErrorCode()); |
|
249 return false; |
|
250 } |
|
251 |
|
252 return true; |
|
253 } |
|
254 |
|
255 /** |
|
256 * Render captcha |
|
257 * |
|
258 * @param Zend_View_Interface $view |
|
259 * @param mixed $element |
|
260 * @return string |
|
261 */ |
|
262 public function render(Zend_View_Interface $view = null, $element = null) |
|
263 { |
|
264 return $this->getService()->getHTML(); |
|
265 } |
|
266 } |