PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Captcha/ReCaptcha.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 280 lines | 114 code | 29 blank | 137 comment | 13 complexity | 9f8a16018ac11769f4607328be508b39 MD5 | raw file
  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-2011 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @version $Id: ReCaptcha.php 24242 2011-07-14 13:44:06Z matthew $
  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. * @return void
  127. */
  128. public function __construct($options = null)
  129. {
  130. $this->setService(new Zend_Service_ReCaptcha());
  131. $this->_serviceParams = $this->getService()->getParams();
  132. $this->_serviceOptions = $this->getService()->getOptions();
  133. parent::__construct($options);
  134. if ($options instanceof Zend_Config) {
  135. $options = $options->toArray();
  136. }
  137. if (!empty($options)) {
  138. $this->setOptions($options);
  139. }
  140. }
  141. /**
  142. * Set service object
  143. *
  144. * @param Zend_Service_ReCaptcha $service
  145. * @return Zend_Captcha_ReCaptcha
  146. */
  147. public function setService(Zend_Service_ReCaptcha $service)
  148. {
  149. $this->_service = $service;
  150. return $this;
  151. }
  152. /**
  153. * Retrieve ReCaptcha service object
  154. *
  155. * @return Zend_Service_ReCaptcha
  156. */
  157. public function getService()
  158. {
  159. return $this->_service;
  160. }
  161. /**
  162. * Set option
  163. *
  164. * If option is a service parameter, proxies to the service. The same
  165. * goes for any service options (distinct from service params)
  166. *
  167. * @param string $key
  168. * @param mixed $value
  169. * @return Zend_Captcha_ReCaptcha
  170. */
  171. public function setOption($key, $value)
  172. {
  173. $service = $this->getService();
  174. if (isset($this->_serviceParams[$key])) {
  175. $service->setParam($key, $value);
  176. return $this;
  177. }
  178. if (isset($this->_serviceOptions[$key])) {
  179. $service->setOption($key, $value);
  180. return $this;
  181. }
  182. return parent::setOption($key, $value);
  183. }
  184. /**
  185. * Generate captcha
  186. *
  187. * @see Zend_Form_Captcha_Adapter::generate()
  188. * @return string
  189. */
  190. public function generate()
  191. {
  192. return "";
  193. }
  194. /**
  195. * Validate captcha
  196. *
  197. * @see Zend_Validate_Interface::isValid()
  198. * @param mixed $value
  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. }