*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Captcha / ReCaptcha.php
blob0c3da13e1fcf0736f1e9bfcb77158a88c3c528df
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Captcha
17 * @subpackage Adapter
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';
28 /**
29 * ReCaptcha adapter
31 * Allows to insert captchas driven by ReCaptcha service
33 * @see http://recaptcha.net/apidocs/captcha/
35 * @category Zend
36 * @package Zend_Captcha
37 * @subpackage Adapter
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
44 /**@+
45 * ReCaptcha Field names
46 * @var string
48 protected $_CHALLENGE = 'recaptcha_challenge_field';
49 protected $_RESPONSE = 'recaptcha_response_field';
50 /**@-*/
52 /**
53 * Recaptcha service object
55 * @var Zend_Service_Recaptcha
57 protected $_service;
59 /**
60 * Parameters defined by the service
62 * @var array
64 protected $_serviceParams = array();
66 /**#@+
67 * Error codes
68 * @const string
70 const MISSING_VALUE = 'missingValue';
71 const ERR_CAPTCHA = 'errCaptcha';
72 const BAD_CAPTCHA = 'badCaptcha';
73 /**#@-*/
75 /**
76 * Error messages
77 * @var array
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%',
85 /**
86 * Retrieve ReCaptcha Private key
88 * @return string
90 public function getPrivkey()
92 return $this->getService()->getPrivateKey();
95 /**
96 * Retrieve ReCaptcha Public key
98 * @return string
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);
114 return $this;
118 * Set ReCaptcha public key
120 * @param string $pubkey
121 * @return Zend_Captcha_ReCaptcha
123 public function setPubkey($pubkey)
125 $this->getService()->setPublicKey($pubkey);
126 return $this;
130 * Constructor
132 * @param array|Zend_Config $options
133 * @return void
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);
151 * Set service object
153 * @param Zend_Service_ReCaptcha $service
154 * @return Zend_Captcha_ReCaptcha
156 public function setService(Zend_Service_ReCaptcha $service)
158 $this->_service = $service;
159 return $this;
163 * Retrieve ReCaptcha service object
165 * @return Zend_Service_ReCaptcha
167 public function getService()
169 return $this->_service;
173 * Set option
175 * If option is a service parameter, proxies to the service.
177 * @param string $key
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);
186 return $this;
188 return parent::setOption($key, $value);
192 * Generate captcha
194 * @see Zend_Form_Captcha_Adapter::generate()
195 * @return string
197 public function generate()
199 return "";
203 * Validate captcha
205 * @see Zend_Validate_Interface::isValid()
206 * @param mixed $value
207 * @return boolean
209 public function isValid($value, $context = null)
211 if (!is_array($value) && !is_array($context)) {
212 $this->_error(self::MISSING_VALUE);
213 return false;
216 if (!is_array($value) && is_array($context)) {
217 $value = $context;
220 if (empty($value[$this->_CHALLENGE]) || empty($value[$this->_RESPONSE])) {
221 $this->_error(self::MISSING_VALUE);
222 return false;
225 $service = $this->getService();
227 $res = $service->verify($value[$this->_CHALLENGE], $value[$this->_RESPONSE]);
229 if (!$res) {
230 $this->_error(self::ERR_CAPTCHA);
231 return false;
234 if (!$res->isValid()) {
235 $this->_error(self::BAD_CAPTCHA, $res->getErrorCode());
236 $service->setParam('error', $res->getErrorCode());
237 return false;
240 return true;
244 * Render captcha
246 * @param Zend_View_Interface $view
247 * @param mixed $element
248 * @return string
250 public function render(Zend_View_Interface $view = null, $element = null)
252 return $this->getService()->getHTML();