Source of file ReCaptcha.php
Size: 5,741 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Captcha/ReCaptcha.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\Captcha; use Traversable; use ZendService\ReCaptcha\ReCaptcha as ReCaptchaService; /** * ReCaptcha adapter * * Allows to insert captchas driven by ReCaptcha service * * @see http://recaptcha.net/apidocs/captcha/ */ class ReCaptcha extends AbstractAdapter { /**@+ * ReCaptcha Field names * @var string */ protected $CHALLENGE = 'recaptcha_challenge_field'; protected $RESPONSE = 'recaptcha_response_field'; /**@-*/ /** * Recaptcha service object * * @var ReCaptchaService */ protected $service; /** * Parameters defined by the service * * @var array */ protected $serviceParams = array(); /** * Options defined by the service * * @var array */ protected $serviceOptions = array(); /**#@+ * Error codes */ const MISSING_VALUE = 'missingValue'; const ERR_CAPTCHA = 'errCaptcha'; const BAD_CAPTCHA = 'badCaptcha'; /**#@-*/ /** * Error messages * @var array */ protected $messageTemplates = array( self::MISSING_VALUE => 'Missing captcha fields', self::ERR_CAPTCHA => 'Failed to validate captcha', self::BAD_CAPTCHA => 'Captcha value is wrong: %value%', ); /** * Retrieve ReCaptcha Private key * * @return string */ public function getPrivkey() { return $this->getService()->getPrivateKey(); } /** * Retrieve ReCaptcha Public key * * @return string */ public function getPubkey() { return $this->getService()->getPublicKey(); } /** * Set ReCaptcha Private key * * @param string $privkey * @return ReCaptcha */ public function setPrivkey($privkey) { $this->getService()->setPrivateKey($privkey); return $this; } /** * Set ReCaptcha public key * * @param string $pubkey * @return ReCaptcha */ public function setPubkey($pubkey) { $this->getService()->setPublicKey($pubkey); return $this; } /** * Constructor * * @param null|array|Traversable $options */ public function __construct($options = null) { $this->setService(new ReCaptchaService()); $this->serviceParams = $this->getService()->getParams(); $this->serviceOptions = $this->getService()->getOptions(); parent::__construct($options); if (!empty($options)) { if (array_key_exists('private_key', $options)) { $this->getService()->setPrivateKey($options['private_key']); } if (array_key_exists('public_key', $options)) { $this->getService()->setPublicKey($options['public_key']); } $this->setOptions($options); } } /** * Set service object * * @param ReCaptchaService $service * @return ReCaptcha */ public function setService(ReCaptchaService $service) { $this->service = $service; return $this; } /** * Retrieve ReCaptcha service object * * @return ReCaptchaService */ public function getService() { return $this->service; } /** * Set option * * If option is a service parameter, proxies to the service. The same * goes for any service options (distinct from service params) * * @param string $key * @param mixed $value * @return ReCaptcha */ public function setOption($key, $value) { $service = $this->getService(); if (isset($this->serviceParams[$key])) { $service->setParam($key, $value); return $this; } if (isset($this->serviceOptions[$key])) { $service->setOption($key, $value); return $this; } return parent::setOption($key, $value); } /** * Generate captcha * * @see AbstractAdapter::generate() * @return string */ public function generate() { return ""; } /** * Validate captcha * * @see \Zend\Validator\ValidatorInterface::isValid() * @param mixed $value * @param mixed $context * @return bool */ public function isValid($value, $context = null) { if (!is_array($value) && !is_array($context)) { $this->error(self::MISSING_VALUE); return false; } if (!is_array($value) && is_array($context)) { $value = $context; } if (empty($value[$this->CHALLENGE]) || empty($value[$this->RESPONSE])) { $this->error(self::MISSING_VALUE); return false; } $service = $this->getService(); $res = $service->verify($value[$this->CHALLENGE], $value[$this->RESPONSE]); if (!$res) { $this->error(self::ERR_CAPTCHA); return false; } if (!$res->isValid()) { $this->error(self::BAD_CAPTCHA, $res->getErrorCode()); $service->setParam('error', $res->getErrorCode()); return false; } return true; } /** * Get helper name used to render captcha * * @return string */ public function getHelperName() { return "captcha/recaptcha"; } } |