2
0

ReCaptcha.php 6.6 KB

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