File indexing completed on 2025-02-02 05:48:53
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Captcha 0017 * @subpackage Adapter 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 */ 0021 0022 /** @see Zend_Captcha_Base */ 0023 // require_once 'Zend/Captcha/Base.php'; 0024 0025 /** @see Zend_Service_ReCaptcha */ 0026 // require_once 'Zend/Service/ReCaptcha.php'; 0027 0028 /** 0029 * ReCaptcha adapter 0030 * 0031 * Allows to insert captchas driven by ReCaptcha service 0032 * 0033 * @see http://recaptcha.net/apidocs/captcha/ 0034 * 0035 * @category Zend 0036 * @package Zend_Captcha 0037 * @subpackage Adapter 0038 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0039 * @license http://framework.zend.com/license/new-bsd New BSD License 0040 * @version $Id$ 0041 */ 0042 class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base 0043 { 0044 /**@+ 0045 * ReCaptcha Field names 0046 * @var string 0047 */ 0048 protected $_CHALLENGE = 'recaptcha_challenge_field'; 0049 protected $_RESPONSE = 'recaptcha_response_field'; 0050 /**@-*/ 0051 0052 /** 0053 * Recaptcha service object 0054 * 0055 * @var Zend_Service_Recaptcha 0056 */ 0057 protected $_service; 0058 0059 /** 0060 * Parameters defined by the service 0061 * 0062 * @var array 0063 */ 0064 protected $_serviceParams = array(); 0065 0066 /** 0067 * Options defined by the service 0068 * 0069 * @var array 0070 */ 0071 protected $_serviceOptions = array(); 0072 0073 /**#@+ 0074 * Error codes 0075 */ 0076 const MISSING_VALUE = 'missingValue'; 0077 const ERR_CAPTCHA = 'errCaptcha'; 0078 const BAD_CAPTCHA = 'badCaptcha'; 0079 /**#@-*/ 0080 0081 /** 0082 * Error messages 0083 * @var array 0084 */ 0085 protected $_messageTemplates = array( 0086 self::MISSING_VALUE => 'Missing captcha fields', 0087 self::ERR_CAPTCHA => 'Failed to validate captcha', 0088 self::BAD_CAPTCHA => 'Captcha value is wrong: %value%', 0089 ); 0090 0091 /** 0092 * Retrieve ReCaptcha Private key 0093 * 0094 * @return string 0095 */ 0096 public function getPrivkey() 0097 { 0098 return $this->getService()->getPrivateKey(); 0099 } 0100 0101 /** 0102 * Retrieve ReCaptcha Public key 0103 * 0104 * @return string 0105 */ 0106 public function getPubkey() 0107 { 0108 return $this->getService()->getPublicKey(); 0109 } 0110 0111 /** 0112 * Set ReCaptcha Private key 0113 * 0114 * @param string $privkey 0115 * @return Zend_Captcha_ReCaptcha 0116 */ 0117 public function setPrivkey($privkey) 0118 { 0119 $this->getService()->setPrivateKey($privkey); 0120 return $this; 0121 } 0122 0123 /** 0124 * Set ReCaptcha public key 0125 * 0126 * @param string $pubkey 0127 * @return Zend_Captcha_ReCaptcha 0128 */ 0129 public function setPubkey($pubkey) 0130 { 0131 $this->getService()->setPublicKey($pubkey); 0132 return $this; 0133 } 0134 0135 /** 0136 * Constructor 0137 * 0138 * @param array|Zend_Config $options 0139 */ 0140 public function __construct($options = null) 0141 { 0142 $this->setService(new Zend_Service_ReCaptcha()); 0143 $this->_serviceParams = $this->getService()->getParams(); 0144 $this->_serviceOptions = $this->getService()->getOptions(); 0145 0146 parent::__construct($options); 0147 0148 if ($options instanceof Zend_Config) { 0149 $options = $options->toArray(); 0150 } 0151 if (!empty($options)) { 0152 $this->setOptions($options); 0153 } 0154 } 0155 0156 /** 0157 * Set service object 0158 * 0159 * @param Zend_Service_ReCaptcha $service 0160 * @return Zend_Captcha_ReCaptcha 0161 */ 0162 public function setService(Zend_Service_ReCaptcha $service) 0163 { 0164 $this->_service = $service; 0165 return $this; 0166 } 0167 0168 /** 0169 * Retrieve ReCaptcha service object 0170 * 0171 * @return Zend_Service_ReCaptcha 0172 */ 0173 public function getService() 0174 { 0175 return $this->_service; 0176 } 0177 0178 /** 0179 * Set option 0180 * 0181 * If option is a service parameter, proxies to the service. The same 0182 * goes for any service options (distinct from service params) 0183 * 0184 * @param string $key 0185 * @param mixed $value 0186 * @return Zend_Captcha_ReCaptcha 0187 */ 0188 public function setOption($key, $value) 0189 { 0190 $service = $this->getService(); 0191 if (isset($this->_serviceParams[$key])) { 0192 $service->setParam($key, $value); 0193 return $this; 0194 } 0195 if (isset($this->_serviceOptions[$key])) { 0196 $service->setOption($key, $value); 0197 return $this; 0198 } 0199 return parent::setOption($key, $value); 0200 } 0201 0202 /** 0203 * Generate captcha 0204 * 0205 * @see Zend_Form_Captcha_Adapter::generate() 0206 * @return string 0207 */ 0208 public function generate() 0209 { 0210 return ""; 0211 } 0212 0213 /** 0214 * Validate captcha 0215 * 0216 * @see Zend_Validate_Interface::isValid() 0217 * @param mixed $value 0218 * @param array|null $context 0219 * @return boolean 0220 */ 0221 public function isValid($value, $context = null) 0222 { 0223 if (!is_array($value) && !is_array($context)) { 0224 $this->_error(self::MISSING_VALUE); 0225 return false; 0226 } 0227 0228 if (!is_array($value) && is_array($context)) { 0229 $value = $context; 0230 } 0231 0232 if (empty($value[$this->_CHALLENGE]) || empty($value[$this->_RESPONSE])) { 0233 $this->_error(self::MISSING_VALUE); 0234 return false; 0235 } 0236 0237 $service = $this->getService(); 0238 0239 $res = $service->verify($value[$this->_CHALLENGE], $value[$this->_RESPONSE]); 0240 0241 if (!$res) { 0242 $this->_error(self::ERR_CAPTCHA); 0243 return false; 0244 } 0245 0246 if (!$res->isValid()) { 0247 $this->_error(self::BAD_CAPTCHA, $res->getErrorCode()); 0248 $service->setParam('error', $res->getErrorCode()); 0249 return false; 0250 } 0251 0252 return true; 0253 } 0254 0255 /** 0256 * Render captcha 0257 * 0258 * @param Zend_View_Interface $view 0259 * @param mixed $element 0260 * @return string 0261 */ 0262 public function render(Zend_View_Interface $view = null, $element = null) 0263 { 0264 $name = null; 0265 if ($element instanceof Zend_Form_Element) { 0266 $name = $element->getBelongsTo(); 0267 } 0268 return $this->getService()->getHTML($name); 0269 } 0270 0271 /** 0272 * Get captcha decorator 0273 * 0274 * @return string 0275 */ 0276 public function getDecorator() 0277 { 0278 return "Captcha_ReCaptcha"; 0279 } 0280 }