ReCaptcha.php 5.9 KB

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