PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/Service/ReCaptcha.php

https://gitlab.com/blingbang2016/shop
PHP | 502 lines | 212 code | 65 blank | 225 comment | 21 complexity | 518a917f321d64422010babcdc82921a 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_Service
  17. * @subpackage ReCaptcha
  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_Service_Abstract */
  22. #require_once 'Zend/Service/Abstract.php';
  23. /** @see Zend_Json */
  24. #require_once 'Zend/Json.php';
  25. /** @see Zend_Service_ReCaptcha_Response */
  26. #require_once 'Zend/Service/ReCaptcha/Response.php';
  27. /**
  28. * Zend_Service_ReCaptcha
  29. *
  30. * @category Zend
  31. * @package Zend_Service
  32. * @subpackage ReCaptcha
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @version $Id$
  36. */
  37. class Zend_Service_ReCaptcha extends Zend_Service_Abstract
  38. {
  39. /**
  40. * URI to the regular API
  41. *
  42. * @var string
  43. */
  44. const API_SERVER = 'http://www.google.com/recaptcha/api';
  45. /**
  46. * URI to the secure API
  47. *
  48. * @var string
  49. */
  50. const API_SECURE_SERVER = 'https://www.google.com/recaptcha/api';
  51. /**
  52. * URI to the verify server
  53. *
  54. * @var string
  55. */
  56. const VERIFY_SERVER = 'http://www.google.com/recaptcha/api/verify';
  57. /**
  58. * Public key used when displaying the captcha
  59. *
  60. * @var string
  61. */
  62. protected $_publicKey = null;
  63. /**
  64. * Private key used when verifying user input
  65. *
  66. * @var string
  67. */
  68. protected $_privateKey = null;
  69. /**
  70. * Ip address used when verifying user input
  71. *
  72. * @var string
  73. */
  74. protected $_ip = null;
  75. /**
  76. * Parameters for the object
  77. *
  78. * @var array
  79. */
  80. protected $_params = array(
  81. 'ssl' => false, /* Use SSL or not when generating the recaptcha */
  82. 'error' => null, /* The error message to display in the recaptcha */
  83. 'xhtml' => false /* Enable XHTML output (this will not be XHTML Strict
  84. compliant since the IFRAME is necessary when
  85. Javascript is disabled) */
  86. );
  87. /**
  88. * Options for tailoring reCaptcha
  89. *
  90. * See the different options on http://recaptcha.net/apidocs/captcha/client.html
  91. *
  92. * @var array
  93. */
  94. protected $_options = array(
  95. 'theme' => 'red',
  96. 'lang' => 'en',
  97. 'custom_translations' => array(),
  98. );
  99. /**
  100. * Response from the verify server
  101. *
  102. * @var Zend_Service_ReCaptcha_Response
  103. */
  104. protected $_response = null;
  105. /**
  106. * Class constructor
  107. *
  108. * @param string $publicKey
  109. * @param string $privateKey
  110. * @param array $params
  111. * @param array $options
  112. * @param string $ip
  113. * @param array|Zend_Config $params
  114. */
  115. public function __construct($publicKey = null, $privateKey = null,
  116. $params = null, $options = null, $ip = null)
  117. {
  118. if ($publicKey !== null) {
  119. $this->setPublicKey($publicKey);
  120. }
  121. if ($privateKey !== null) {
  122. $this->setPrivateKey($privateKey);
  123. }
  124. if ($ip !== null) {
  125. $this->setIp($ip);
  126. } else if (isset($_SERVER['REMOTE_ADDR'])) {
  127. $this->setIp($_SERVER['REMOTE_ADDR']);
  128. }
  129. if ($params !== null) {
  130. $this->setParams($params);
  131. }
  132. if ($options !== null) {
  133. $this->setOptions($options);
  134. }
  135. }
  136. /**
  137. * Serialize as string
  138. *
  139. * When the instance is used as a string it will display the recaptcha.
  140. * Since we can't throw exceptions within this method we will trigger
  141. * a user warning instead.
  142. *
  143. * @return string
  144. */
  145. public function __toString()
  146. {
  147. try {
  148. $return = $this->getHtml();
  149. } catch (Exception $e) {
  150. $return = '';
  151. trigger_error($e->getMessage(), E_USER_WARNING);
  152. }
  153. return $return;
  154. }
  155. /**
  156. * Set the ip property
  157. *
  158. * @param string $ip
  159. * @return Zend_Service_ReCaptcha
  160. */
  161. public function setIp($ip)
  162. {
  163. $this->_ip = $ip;
  164. return $this;
  165. }
  166. /**
  167. * Get the ip property
  168. *
  169. * @return string
  170. */
  171. public function getIp()
  172. {
  173. return $this->_ip;
  174. }
  175. /**
  176. * Set a single parameter
  177. *
  178. * @param string $key
  179. * @param string $value
  180. * @return Zend_Service_ReCaptcha
  181. */
  182. public function setParam($key, $value)
  183. {
  184. $this->_params[$key] = $value;
  185. return $this;
  186. }
  187. /**
  188. * Set parameters
  189. *
  190. * @param array|Zend_Config $params
  191. * @return Zend_Service_ReCaptcha
  192. * @throws Zend_Service_ReCaptcha_Exception
  193. */
  194. public function setParams($params)
  195. {
  196. if ($params instanceof Zend_Config) {
  197. $params = $params->toArray();
  198. }
  199. if (is_array($params)) {
  200. foreach ($params as $k => $v) {
  201. $this->setParam($k, $v);
  202. }
  203. } else {
  204. /** @see Zend_Service_ReCaptcha_Exception */
  205. #require_once 'Zend/Service/ReCaptcha/Exception.php';
  206. throw new Zend_Service_ReCaptcha_Exception(
  207. 'Expected array or Zend_Config object'
  208. );
  209. }
  210. return $this;
  211. }
  212. /**
  213. * Get the parameter array
  214. *
  215. * @return array
  216. */
  217. public function getParams()
  218. {
  219. return $this->_params;
  220. }
  221. /**
  222. * Get a single parameter
  223. *
  224. * @param string $key
  225. * @return mixed
  226. */
  227. public function getParam($key)
  228. {
  229. return $this->_params[$key];
  230. }
  231. /**
  232. * Set a single option
  233. *
  234. * @param string $key
  235. * @param string $value
  236. * @return Zend_Service_ReCaptcha
  237. */
  238. public function setOption($key, $value)
  239. {
  240. $this->_options[$key] = $value;
  241. return $this;
  242. }
  243. /**
  244. * Set options
  245. *
  246. * @param array|Zend_Config $options
  247. * @return Zend_Service_ReCaptcha
  248. * @throws Zend_Service_ReCaptcha_Exception
  249. */
  250. public function setOptions($options)
  251. {
  252. if ($options instanceof Zend_Config) {
  253. $options = $options->toArray();
  254. }
  255. if (is_array($options)) {
  256. foreach ($options as $k => $v) {
  257. $this->setOption($k, $v);
  258. }
  259. } else {
  260. /** @see Zend_Service_ReCaptcha_Exception */
  261. #require_once 'Zend/Service/ReCaptcha/Exception.php';
  262. throw new Zend_Service_ReCaptcha_Exception(
  263. 'Expected array or Zend_Config object'
  264. );
  265. }
  266. return $this;
  267. }
  268. /**
  269. * Get the options array
  270. *
  271. * @return array
  272. */
  273. public function getOptions()
  274. {
  275. return $this->_options;
  276. }
  277. /**
  278. * Get a single option
  279. *
  280. * @param string $key
  281. * @return mixed
  282. */
  283. public function getOption($key)
  284. {
  285. return $this->_options[$key];
  286. }
  287. /**
  288. * Get the public key
  289. *
  290. * @return string
  291. */
  292. public function getPublicKey()
  293. {
  294. return $this->_publicKey;
  295. }
  296. /**
  297. * Set the public key
  298. *
  299. * @param string $publicKey
  300. * @return Zend_Service_ReCaptcha
  301. */
  302. public function setPublicKey($publicKey)
  303. {
  304. $this->_publicKey = $publicKey;
  305. return $this;
  306. }
  307. /**
  308. * Get the private key
  309. *
  310. * @return string
  311. */
  312. public function getPrivateKey()
  313. {
  314. return $this->_privateKey;
  315. }
  316. /**
  317. * Set the private key
  318. *
  319. * @param string $privateKey
  320. * @return Zend_Service_ReCaptcha
  321. */
  322. public function setPrivateKey($privateKey)
  323. {
  324. $this->_privateKey = $privateKey;
  325. return $this;
  326. }
  327. /**
  328. * Get the HTML code for the captcha
  329. *
  330. * This method uses the public key to fetch a recaptcha form.
  331. *
  332. * @param null|string $name Base name for recaptcha form elements
  333. * @return string
  334. * @throws Zend_Service_ReCaptcha_Exception
  335. */
  336. public function getHtml($name = null)
  337. {
  338. if ($this->_publicKey === null) {
  339. /** @see Zend_Service_ReCaptcha_Exception */
  340. #require_once 'Zend/Service/ReCaptcha/Exception.php';
  341. throw new Zend_Service_ReCaptcha_Exception('Missing public key');
  342. }
  343. $host = self::API_SERVER;
  344. if ((bool) $this->_params['ssl'] === true) {
  345. $host = self::API_SECURE_SERVER;
  346. }
  347. $htmlBreak = '<br>';
  348. $htmlInputClosing = '>';
  349. if ((bool) $this->_params['xhtml'] === true) {
  350. $htmlBreak = '<br />';
  351. $htmlInputClosing = '/>';
  352. }
  353. $errorPart = '';
  354. if (!empty($this->_params['error'])) {
  355. $errorPart = '&error=' . urlencode($this->_params['error']);
  356. }
  357. $reCaptchaOptions = '';
  358. if (!empty($this->_options)) {
  359. $encoded = Zend_Json::encode($this->_options);
  360. $reCaptchaOptions = <<<SCRIPT
  361. <script type="text/javascript">
  362. var RecaptchaOptions = {$encoded};
  363. </script>
  364. SCRIPT;
  365. }
  366. $challengeField = 'recaptcha_challenge_field';
  367. $responseField = 'recaptcha_response_field';
  368. if (!empty($name)) {
  369. $challengeField = $name . '[' . $challengeField . ']';
  370. $responseField = $name . '[' . $responseField . ']';
  371. }
  372. $return = $reCaptchaOptions;
  373. $return .= <<<HTML
  374. <script type="text/javascript"
  375. src="{$host}/challenge?k={$this->_publicKey}{$errorPart}">
  376. </script>
  377. HTML;
  378. $return .= <<<HTML
  379. <noscript>
  380. <iframe src="{$host}/noscript?k={$this->_publicKey}{$errorPart}"
  381. height="300" width="500" frameborder="0"></iframe>{$htmlBreak}
  382. <textarea name="{$challengeField}" rows="3" cols="40">
  383. </textarea>
  384. <input type="hidden" name="{$responseField}"
  385. value="manual_challenge"{$htmlInputClosing}
  386. </noscript>
  387. HTML;
  388. return $return;
  389. }
  390. /**
  391. * Post a solution to the verify server
  392. *
  393. * @param string $challengeField
  394. * @param string $responseField
  395. * @return Zend_Http_Response
  396. * @throws Zend_Service_ReCaptcha_Exception
  397. */
  398. protected function _post($challengeField, $responseField)
  399. {
  400. if ($this->_privateKey === null) {
  401. /** @see Zend_Service_ReCaptcha_Exception */
  402. #require_once 'Zend/Service/ReCaptcha/Exception.php';
  403. throw new Zend_Service_ReCaptcha_Exception('Missing private key');
  404. }
  405. if ($this->_ip === null) {
  406. /** @see Zend_Service_ReCaptcha_Exception */
  407. #require_once 'Zend/Service/ReCaptcha/Exception.php';
  408. throw new Zend_Service_ReCaptcha_Exception('Missing ip address');
  409. }
  410. /* Fetch an instance of the http client */
  411. $httpClient = self::getHttpClient();
  412. $httpClient->resetParameters(true);
  413. $postParams = array('privatekey' => $this->_privateKey,
  414. 'remoteip' => $this->_ip,
  415. 'challenge' => $challengeField,
  416. 'response' => $responseField);
  417. /* Make the POST and return the response */
  418. return $httpClient->setUri(self::VERIFY_SERVER)
  419. ->setParameterPost($postParams)
  420. ->request(Zend_Http_Client::POST);
  421. }
  422. /**
  423. * Verify the user input
  424. *
  425. * This method calls up the post method and returns a
  426. * Zend_Service_ReCaptcha_Response object.
  427. *
  428. * @param string $challengeField
  429. * @param string $responseField
  430. * @return Zend_Service_ReCaptcha_Response
  431. */
  432. public function verify($challengeField, $responseField)
  433. {
  434. $response = $this->_post($challengeField, $responseField);
  435. return new Zend_Service_ReCaptcha_Response(null, null, $response);
  436. }
  437. }