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-2009 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-2009 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 16201 2009-06-21 18:51:15Z thomas $
42 class Zend_Captcha_ReCaptcha
extends Zend_Captcha_Base
45 * ReCaptcha Field names
48 protected $_CHALLENGE = 'recaptcha_challenge_field';
49 protected $_RESPONSE = 'recaptcha_response_field';
53 * Recaptcha service object
55 * @var Zend_Service_Recaptcha
60 * Parameters defined by the service
64 protected $_serviceParams = array();
70 const MISSING_VALUE
= 'missingValue';
71 const ERR_CAPTCHA
= 'errCaptcha';
72 const BAD_CAPTCHA
= 'badCaptcha';
79 protected $_messageTemplates = array(
80 self
::MISSING_VALUE
=> 'Missing captcha fields',
81 self
::ERR_CAPTCHA
=> 'Failed to validate captcha',
82 self
::BAD_CAPTCHA
=> 'Captcha value is wrong: %value%',
86 * Retrieve ReCaptcha Private key
90 public function getPrivkey()
92 return $this->getService()->getPrivateKey();
96 * Retrieve ReCaptcha Public key
100 public function getPubkey()
102 return $this->getService()->getPublicKey();
106 * Set ReCaptcha Private key
108 * @param string $privkey
109 * @return Zend_Captcha_ReCaptcha
111 public function setPrivkey($privkey)
113 $this->getService()->setPrivateKey($privkey);
118 * Set ReCaptcha public key
120 * @param string $pubkey
121 * @return Zend_Captcha_ReCaptcha
123 public function setPubkey($pubkey)
125 $this->getService()->setPublicKey($pubkey);
132 * @param array|Zend_Config $options
135 public function __construct($options = null)
137 $this->setService(new Zend_Service_ReCaptcha());
138 $this->_serviceParams
= $this->getService()->getParams();
140 parent
::__construct($options);
142 if ($options instanceof Zend_Config
) {
143 $options = $options->toArray();
145 if (!empty($options)) {
146 $this->setOptions($options);
153 * @param Zend_Service_ReCaptcha $service
154 * @return Zend_Captcha_ReCaptcha
156 public function setService(Zend_Service_ReCaptcha
$service)
158 $this->_service
= $service;
163 * Retrieve ReCaptcha service object
165 * @return Zend_Service_ReCaptcha
167 public function getService()
169 return $this->_service
;
175 * If option is a service parameter, proxies to the service.
178 * @param mixed $value
179 * @return Zend_Captcha_ReCaptcha
181 public function setOption($key, $value)
183 $service = $this->getService();
184 if (isset($this->_serviceParams
[$key])) {
185 $service->setParam($key, $value);
188 return parent
::setOption($key, $value);
194 * @see Zend_Form_Captcha_Adapter::generate()
197 public function generate()
205 * @see Zend_Validate_Interface::isValid()
206 * @param mixed $value
209 public function isValid($value, $context = null)
211 if (!is_array($value) && !is_array($context)) {
212 $this->_error(self
::MISSING_VALUE
);
216 if (!is_array($value) && is_array($context)) {
220 if (empty($value[$this->_CHALLENGE
]) ||
empty($value[$this->_RESPONSE
])) {
221 $this->_error(self
::MISSING_VALUE
);
225 $service = $this->getService();
227 $res = $service->verify($value[$this->_CHALLENGE
], $value[$this->_RESPONSE
]);
230 $this->_error(self
::ERR_CAPTCHA
);
234 if (!$res->isValid()) {
235 $this->_error(self
::BAD_CAPTCHA
, $res->getErrorCode());
236 $service->setParam('error', $res->getErrorCode());
246 * @param Zend_View_Interface $view
247 * @param mixed $element
250 public function render(Zend_View_Interface
$view = null, $element = null)
252 return $this->getService()->getHTML();