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.
16
* @package Zend_Captcha
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
22
/** Zend_Captcha_Base */
23
require_once 'Zend/Captcha/Base.php';
25
/** Zend_Service_ReCaptcha */
26
require_once 'Zend/Service/ReCaptcha.php';
31
* Allows to insert captchas driven by ReCaptcha service
33
* @see http://recaptcha.net/apidocs/captcha/
36
* @package Zend_Captcha
38
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
39
* @license http://framework.zend.com/license/new-bsd New BSD License
42
class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
45
* Recaptcha public key
52
* Recaptcha private key
59
* ReCaptcha Field names
62
protected $_CHALLENGE = 'recaptcha_challenge_field';
63
protected $_RESPONSE = 'recaptcha_response_field';
67
* Recaptcha service object
69
* @var Zend_Service_Recaptcha
74
* Parameters defined by the service
78
protected $_serviceParams = array();
84
const MISSING_VALUE = 'missingValue';
85
const ERR_CAPTCHA = 'errCaptcha';
86
const BAD_CAPTCHA = 'badCaptcha';
93
protected $_messageTemplates = array(
94
self::MISSING_VALUE => 'Missing captcha fields',
95
self::ERR_CAPTCHA => 'Failed to validate captcha',
96
self::BAD_CAPTCHA => 'Captcha value is wrong: %value%',
100
* Retrieve ReCaptcha Private key
104
public function getPrivkey()
106
return $this->_privkey;
110
* Retrieve ReCaptcha Public key
114
public function getPubkey()
116
return $this->_pubkey;
120
* Set ReCaptcha Private key
122
* @param string $_privkey
123
* @return Zend_Captcha_ReCaptcha
125
public function setPrivkey($privkey)
127
$this->_privkey = $privkey;
132
* Set ReCaptcha public key
134
* @param string $_pubkey
135
* @return Zend_Captcha_ReCaptcha
137
public function setPubkey($pubkey)
139
$this->_pubkey = $pubkey;
146
* @param array|Zend_Config $options
149
public function __construct($options = null)
151
parent::__construct($options);
153
$this->setService(new Zend_Service_ReCaptcha($this->getPubKey(), $this->getPrivKey()));
154
$this->_serviceParams = $this->getService()->getParams();
156
if ($options instanceof Zend_Config) {
157
$options = $options->toArray();
159
if (!empty($options)) {
160
$this->setOptions($options);
167
* @param Zend_Service_ReCaptcha $service
168
* @return Zend_Captcha_ReCaptcha
170
public function setService(Zend_Service_ReCaptcha $service)
172
$this->_service = $service;
177
* Retrieve ReCaptcha service object
179
* @return Zend_Service_ReCaptcha
181
public function getService()
183
return $this->_service;
189
* If option is a service parameter, proxies to the service.
192
* @param mixed $value
193
* @return Zend_Captcha_ReCaptcha
195
public function setOption($key, $value)
197
$service = $this->getService();
198
if (isset($this->_serviceParams[$key])) {
199
$service->setParam($key, $value);
202
return parent::setOption($key, $value);
208
* @see Zend_Form_Captcha_Adapter::generate()
211
public function generate()
219
* @see Zend_Validate_Interface::isValid()
220
* @param mixed $value
223
public function isValid($value, $context = null)
225
if (!is_array($value) && !is_array($context)) {
226
$this->_error(self::MISSING_VALUE);
229
if (!is_array($value) && is_array($context)) {
233
if (empty($value[$this->_CHALLENGE]) || empty($value[$this->_RESPONSE])) {
234
$this->_error(self::MISSING_VALUE);
238
$service = $this->getService();
240
$res = $service->verify($value[$this->_CHALLENGE], $value[$this->_RESPONSE]);
243
$this->_error(self::ERR_CAPTCHA);
247
if (!$res->isValid()) {
248
$this->_error(self::BAD_CAPTCHA, $res->getErrorCode());
249
$service->setParam('error', $res->getErrorCode());
259
* @param Zend_View $view
260
* @param mixed $element
263
public function render(Zend_View_Interface $view, $element = null)
265
return $this->getService()->getHTML();