3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Captcha
;
13 use ZendService\ReCaptcha\ReCaptcha
as ReCaptchaService
;
18 * Allows to insert captchas driven by ReCaptcha service
20 * @see http://recaptcha.net/apidocs/captcha/
22 class ReCaptcha
extends AbstractAdapter
25 * ReCaptcha Field names
28 protected $CHALLENGE = 'recaptcha_challenge_field';
29 protected $RESPONSE = 'recaptcha_response_field';
33 * Recaptcha service object
35 * @var ReCaptchaService
40 * Parameters defined by the service
44 protected $serviceParams = array();
47 * Options defined by the service
51 protected $serviceOptions = array();
56 const MISSING_VALUE
= 'missingValue';
57 const ERR_CAPTCHA
= 'errCaptcha';
58 const BAD_CAPTCHA
= 'badCaptcha';
65 protected $messageTemplates = array(
66 self
::MISSING_VALUE
=> 'Missing captcha fields',
67 self
::ERR_CAPTCHA
=> 'Failed to validate captcha',
68 self
::BAD_CAPTCHA
=> 'Captcha value is wrong: %value%',
72 * Retrieve ReCaptcha Private key
76 public function getPrivkey()
78 return $this->getService()->getPrivateKey();
82 * Retrieve ReCaptcha Public key
86 public function getPubkey()
88 return $this->getService()->getPublicKey();
92 * Set ReCaptcha Private key
94 * @param string $privkey
97 public function setPrivkey($privkey)
99 $this->getService()->setPrivateKey($privkey);
104 * Set ReCaptcha public key
106 * @param string $pubkey
109 public function setPubkey($pubkey)
111 $this->getService()->setPublicKey($pubkey);
118 * @param null|array|Traversable $options
120 public function __construct($options = null)
122 $this->setService(new ReCaptchaService());
123 $this->serviceParams
= $this->getService()->getParams();
124 $this->serviceOptions
= $this->getService()->getOptions();
126 parent
::__construct($options);
128 if (!empty($options)) {
129 if (array_key_exists('private_key', $options)) {
130 $this->getService()->setPrivateKey($options['private_key']);
132 if (array_key_exists('public_key', $options)) {
133 $this->getService()->setPublicKey($options['public_key']);
135 $this->setOptions($options);
142 * @param ReCaptchaService $service
145 public function setService(ReCaptchaService
$service)
147 $this->service
= $service;
152 * Retrieve ReCaptcha service object
154 * @return ReCaptchaService
156 public function getService()
158 return $this->service
;
164 * If option is a service parameter, proxies to the service. The same
165 * goes for any service options (distinct from service params)
168 * @param mixed $value
171 public function setOption($key, $value)
173 $service = $this->getService();
174 if (isset($this->serviceParams
[$key])) {
175 $service->setParam($key, $value);
178 if (isset($this->serviceOptions
[$key])) {
179 $service->setOption($key, $value);
182 return parent
::setOption($key, $value);
188 * @see AbstractAdapter::generate()
191 public function generate()
199 * @see \Zend\Validator\ValidatorInterface::isValid()
200 * @param mixed $value
201 * @param mixed $context
204 public function isValid($value, $context = null)
206 if (!is_array($value) && !is_array($context)) {
207 $this->error(self
::MISSING_VALUE
);
211 if (!is_array($value) && is_array($context)) {
215 if (empty($value[$this->CHALLENGE
]) ||
empty($value[$this->RESPONSE
])) {
216 $this->error(self
::MISSING_VALUE
);
220 $service = $this->getService();
222 $res = $service->verify($value[$this->CHALLENGE
], $value[$this->RESPONSE
]);
225 $this->error(self
::ERR_CAPTCHA
);
229 if (!$res->isValid()) {
230 $this->error(self
::BAD_CAPTCHA
, $res->getErrorCode());
231 $service->setParam('error', $res->getErrorCode());
239 * Get helper name used to render captcha
243 public function getHelperName()
245 return "captcha/recaptcha";