ReCaptcha.php 5.9 KB

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