Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Captcha / ReCaptcha.php
blob64d2f56ff7759701f1353dd77ca97814276a1c41
1 <?php
2 /**
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
8 */
10 namespace Zend\Captcha;
12 use Traversable;
13 use ZendService\ReCaptcha\ReCaptcha as ReCaptchaService;
15 /**
16 * ReCaptcha adapter
18 * Allows to insert captchas driven by ReCaptcha service
20 * @see http://recaptcha.net/apidocs/captcha/
22 class ReCaptcha extends AbstractAdapter
24 /**@+
25 * ReCaptcha Field names
26 * @var string
28 protected $CHALLENGE = 'recaptcha_challenge_field';
29 protected $RESPONSE = 'recaptcha_response_field';
30 /**@-*/
32 /**
33 * Recaptcha service object
35 * @var ReCaptchaService
37 protected $service;
39 /**
40 * Parameters defined by the service
42 * @var array
44 protected $serviceParams = array();
46 /**
47 * Options defined by the service
49 * @var array
51 protected $serviceOptions = array();
53 /**#@+
54 * Error codes
56 const MISSING_VALUE = 'missingValue';
57 const ERR_CAPTCHA = 'errCaptcha';
58 const BAD_CAPTCHA = 'badCaptcha';
59 /**#@-*/
61 /**
62 * Error messages
63 * @var array
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%',
71 /**
72 * Retrieve ReCaptcha Private key
74 * @return string
76 public function getPrivkey()
78 return $this->getService()->getPrivateKey();
81 /**
82 * Retrieve ReCaptcha Public key
84 * @return string
86 public function getPubkey()
88 return $this->getService()->getPublicKey();
91 /**
92 * Set ReCaptcha Private key
94 * @param string $privkey
95 * @return ReCaptcha
97 public function setPrivkey($privkey)
99 $this->getService()->setPrivateKey($privkey);
100 return $this;
104 * Set ReCaptcha public key
106 * @param string $pubkey
107 * @return ReCaptcha
109 public function setPubkey($pubkey)
111 $this->getService()->setPublicKey($pubkey);
112 return $this;
116 * Constructor
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);
140 * Set service object
142 * @param ReCaptchaService $service
143 * @return ReCaptcha
145 public function setService(ReCaptchaService $service)
147 $this->service = $service;
148 return $this;
152 * Retrieve ReCaptcha service object
154 * @return ReCaptchaService
156 public function getService()
158 return $this->service;
162 * Set option
164 * If option is a service parameter, proxies to the service. The same
165 * goes for any service options (distinct from service params)
167 * @param string $key
168 * @param mixed $value
169 * @return ReCaptcha
171 public function setOption($key, $value)
173 $service = $this->getService();
174 if (isset($this->serviceParams[$key])) {
175 $service->setParam($key, $value);
176 return $this;
178 if (isset($this->serviceOptions[$key])) {
179 $service->setOption($key, $value);
180 return $this;
182 return parent::setOption($key, $value);
186 * Generate captcha
188 * @see AbstractAdapter::generate()
189 * @return string
191 public function generate()
193 return "";
197 * Validate captcha
199 * @see \Zend\Validator\ValidatorInterface::isValid()
200 * @param mixed $value
201 * @param mixed $context
202 * @return bool
204 public function isValid($value, $context = null)
206 if (!is_array($value) && !is_array($context)) {
207 $this->error(self::MISSING_VALUE);
208 return false;
211 if (!is_array($value) && is_array($context)) {
212 $value = $context;
215 if (empty($value[$this->CHALLENGE]) || empty($value[$this->RESPONSE])) {
216 $this->error(self::MISSING_VALUE);
217 return false;
220 $service = $this->getService();
222 $res = $service->verify($value[$this->CHALLENGE], $value[$this->RESPONSE]);
224 if (!$res) {
225 $this->error(self::ERR_CAPTCHA);
226 return false;
229 if (!$res->isValid()) {
230 $this->error(self::BAD_CAPTCHA, $res->getErrorCode());
231 $service->setParam('error', $res->getErrorCode());
232 return false;
235 return true;
239 * Get helper name used to render captcha
241 * @return string
243 public function getHelperName()
245 return "captcha/recaptcha";