ReCaptcha.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Captcha
  17. * @subpackage Adapter
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Captcha_Base */
  22. require_once 'Zend/Captcha/Base.php';
  23. /** Zend_Service_ReCaptcha */
  24. require_once 'Zend/Service/ReCaptcha.php';
  25. /**
  26. * ReCaptcha adapter
  27. *
  28. * Allows to insert captchas driven by ReCaptcha service
  29. *
  30. * @see http://recaptcha.net/apidocs/captcha/
  31. *
  32. * @category Zend
  33. * @package Zend_Captcha
  34. * @subpackage Adapter
  35. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @version $Id: $
  38. */
  39. class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base
  40. {
  41. /**
  42. * Recaptcha public key
  43. *
  44. * @var string
  45. */
  46. protected $_pubkey;
  47. /**
  48. * Recaptcha private key
  49. *
  50. * @var string
  51. */
  52. protected $_privkey;
  53. /**@+
  54. * ReCaptcha Field names
  55. * @var string
  56. */
  57. protected $_CHALLENGE = 'recaptcha_challenge_field';
  58. protected $_RESPONSE = 'recaptcha_response_field';
  59. /**@-*/
  60. /**
  61. * Recaptcha service object
  62. *
  63. * @var Zend_Service_Recaptcha
  64. */
  65. protected $_service;
  66. /**
  67. * Parameters defined by the service
  68. *
  69. * @var array
  70. */
  71. protected $_serviceParams = array();
  72. /**#@+
  73. * Error codes
  74. * @const string
  75. */
  76. const MISSING_VALUE = 'missingValue';
  77. const ERR_CAPTCHA = 'errCaptcha';
  78. const BAD_CAPTCHA = 'badCaptcha';
  79. /**#@-*/
  80. /**
  81. * Error messages
  82. * @var array
  83. */
  84. protected $_messageTemplates = array(
  85. self::MISSING_VALUE => 'Missing captcha fields',
  86. self::ERR_CAPTCHA => 'Failed to validate captcha',
  87. self::BAD_CAPTCHA => 'Captcha value is wrong: %value%',
  88. );
  89. /**
  90. * Retrieve ReCaptcha Private key
  91. *
  92. * @return string
  93. */
  94. public function getPrivkey()
  95. {
  96. return $this->_privkey;
  97. }
  98. /**
  99. * Retrieve ReCaptcha Public key
  100. *
  101. * @return string
  102. */
  103. public function getPubkey()
  104. {
  105. return $this->_pubkey;
  106. }
  107. /**
  108. * Set ReCaptcha Private key
  109. *
  110. * @param string $_privkey
  111. * @return Zend_Captcha_ReCaptcha
  112. */
  113. public function setPrivkey($privkey)
  114. {
  115. $this->_privkey = $privkey;
  116. return $this;
  117. }
  118. /**
  119. * Set ReCaptcha public key
  120. *
  121. * @param string $_pubkey
  122. * @return Zend_Captcha_ReCaptcha
  123. */
  124. public function setPubkey($pubkey)
  125. {
  126. $this->_pubkey = $pubkey;
  127. return $this;
  128. }
  129. /**
  130. * Constructor
  131. *
  132. * @param array|Zend_Config $options
  133. * @return void
  134. */
  135. public function __construct($options = null)
  136. {
  137. parent::__construct($options);
  138. $this->setService(new Zend_Service_ReCaptcha($this->getPubKey(), $this->getPrivKey()));
  139. $this->_serviceParams = $this->getService()->getParams();
  140. if ($options instanceof Zend_Config) {
  141. $options = $options->toArray();
  142. }
  143. if (!empty($options)) {
  144. $this->setOptions($options);
  145. }
  146. }
  147. /**
  148. * Set service object
  149. *
  150. * @param Zend_Service_ReCaptcha $service
  151. * @return Zend_Captcha_ReCaptcha
  152. */
  153. public function setService(Zend_Service_ReCaptcha $service)
  154. {
  155. $this->_service = $service;
  156. return $this;
  157. }
  158. /**
  159. * Retrieve ReCaptcha service object
  160. *
  161. * @return Zend_Service_ReCaptcha
  162. */
  163. public function getService()
  164. {
  165. return $this->_service;
  166. }
  167. /**
  168. * Set option
  169. *
  170. * If option is a service parameter, proxies to the service.
  171. *
  172. * @param string $key
  173. * @param mixed $value
  174. * @return Zend_Captcha_ReCaptcha
  175. */
  176. public function setOption($key, $value)
  177. {
  178. $service = $this->getService();
  179. if (isset($this->_serviceParams[$key])) {
  180. $service->setParam($key, $value);
  181. return $this;
  182. }
  183. return parent::setOption($key, $value);
  184. }
  185. /**
  186. * Generate captcha
  187. *
  188. * @see Zend_Form_Captcha_Adapter::generate()
  189. * @return string
  190. */
  191. public function generate()
  192. {
  193. return "";
  194. }
  195. /**
  196. * Validate captcha
  197. *
  198. * @see Zend_Validate_Interface::isValid()
  199. * @param mixed $value
  200. * @return boolean
  201. */
  202. public function isValid($value, $context = null)
  203. {
  204. if (!is_array($value) && !is_array($context)) {
  205. $this->_error(self::MISSING_VALUE);
  206. return false;
  207. }
  208. if (!is_array($value) && is_array($context)) {
  209. $value = $context;
  210. }
  211. if (empty($value[$this->_CHALLENGE]) || empty($value[$this->_RESPONSE])) {
  212. $this->_error(self::MISSING_VALUE);
  213. return false;
  214. }
  215. $service = $this->getService();
  216. $res = $service->verify($value[$this->_CHALLENGE], $value[$this->_RESPONSE]);
  217. if (!$res) {
  218. $this->_error(self::ERR_CAPTCHA);
  219. return false;
  220. }
  221. if (!$res->isValid()) {
  222. $this->_error(self::BAD_CAPTCHA, $res->getErrorCode());
  223. $service->setParam('error', $res->getErrorCode());
  224. return false;
  225. }
  226. return true;
  227. }
  228. /**
  229. * Render captcha
  230. *
  231. * @param Zend_View_Interface $view
  232. * @param mixed $element
  233. * @return string
  234. */
  235. public function render(Zend_View_Interface $view, $element = null)
  236. {
  237. return $this->getService()->getHTML();
  238. }
  239. }