PageRenderTime 224ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 513 lines | 226 code | 68 blank | 219 comment | 23 complexity | 4718f854d52253405a69cd41bf6a6360 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-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_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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @version $Id: ReCaptcha.php 24224 2011-07-12 17:45:49Z matthew $
  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. );
  98. /**
  99. * Response from the verify server
  100. *
  101. * @var Zend_Service_ReCaptcha_Response
  102. */
  103. protected $_response = null;
  104. /**
  105. * Class constructor
  106. *
  107. * @param string $publicKey
  108. * @param string $privateKey
  109. * @param array $params
  110. * @param array $options
  111. * @param string $ip
  112. * @param array|Zend_Config $params
  113. */
  114. public function __construct($publicKey = null, $privateKey = null,
  115. $params = null, $options = null, $ip = null)
  116. {
  117. if ($publicKey !== null) {
  118. $this->setPublicKey($publicKey);
  119. }
  120. if ($privateKey !== null) {
  121. $this->setPrivateKey($privateKey);
  122. }
  123. if ($ip !== null) {
  124. $this->setIp($ip);
  125. } else if (isset($_SERVER['REMOTE_ADDR'])) {
  126. $this->setIp($_SERVER['REMOTE_ADDR']);
  127. }
  128. if ($params !== null) {
  129. $this->setParams($params);
  130. }
  131. if ($options !== null) {
  132. $this->setOptions($options);
  133. }
  134. }
  135. /**
  136. * Serialize as string
  137. *
  138. * When the instance is used as a string it will display the recaptcha.
  139. * Since we can't throw exceptions within this method we will trigger
  140. * a user warning instead.
  141. *
  142. * @return string
  143. */
  144. public function __toString()
  145. {
  146. try {
  147. $return = $this->getHtml();
  148. } catch (Exception $e) {
  149. $return = '';
  150. trigger_error($e->getMessage(), E_USER_WARNING);
  151. }
  152. return $return;
  153. }
  154. /**
  155. * Set the ip property
  156. *
  157. * @param string $ip
  158. * @return Zend_Service_ReCaptcha
  159. */
  160. public function setIp($ip)
  161. {
  162. $this->_ip = $ip;
  163. return $this;
  164. }
  165. /**
  166. * Get the ip property
  167. *
  168. * @return string
  169. */
  170. public function getIp()
  171. {
  172. return $this->_ip;
  173. }
  174. /**
  175. * Set a single parameter
  176. *
  177. * @param string $key
  178. * @param string $value
  179. * @return Zend_Service_ReCaptcha
  180. */
  181. public function setParam($key, $value)
  182. {
  183. $this->_params[$key] = $value;
  184. return $this;
  185. }
  186. /**
  187. * Set parameters
  188. *
  189. * @param array|Zend_Config $params
  190. * @return Zend_Service_ReCaptcha
  191. * @throws Zend_Service_ReCaptcha_Exception
  192. */
  193. public function setParams($params)
  194. {
  195. if ($params instanceof Zend_Config) {
  196. $params = $params->toArray();
  197. }
  198. if (is_array($params)) {
  199. foreach ($params as $k => $v) {
  200. $this->setParam($k, $v);
  201. }
  202. } else {
  203. /** @see Zend_Service_ReCaptcha_Exception */
  204. require_once 'Zend/Service/ReCaptcha/Exception.php';
  205. throw new Zend_Service_ReCaptcha_Exception(
  206. 'Expected array or Zend_Config object'
  207. );
  208. }
  209. return $this;
  210. }
  211. /**
  212. * Get the parameter array
  213. *
  214. * @return array
  215. */
  216. public function getParams()
  217. {
  218. return $this->_params;
  219. }
  220. /**
  221. * Get a single parameter
  222. *
  223. * @param string $key
  224. * @return mixed
  225. */
  226. public function getParam($key)
  227. {
  228. return $this->_params[$key];
  229. }
  230. /**
  231. * Set a single option
  232. *
  233. * @param string $key
  234. * @param string $value
  235. * @return Zend_Service_ReCaptcha
  236. */
  237. public function setOption($key, $value)
  238. {
  239. $this->_options[$key] = $value;
  240. return $this;
  241. }
  242. /**
  243. * Set options
  244. *
  245. * @param array|Zend_Config $options
  246. * @return Zend_Service_ReCaptcha
  247. * @throws Zend_Service_ReCaptcha_Exception
  248. */
  249. public function setOptions($options)
  250. {
  251. if ($options instanceof Zend_Config) {
  252. $options = $options->toArray();
  253. }
  254. if (is_array($options)) {
  255. foreach ($options as $k => $v) {
  256. $this->setOption($k, $v);
  257. }
  258. } else {
  259. /** @see Zend_Service_ReCaptcha_Exception */
  260. require_once 'Zend/Service/ReCaptcha/Exception.php';
  261. throw new Zend_Service_ReCaptcha_Exception(
  262. 'Expected array or Zend_Config object'
  263. );
  264. }
  265. return $this;
  266. }
  267. /**
  268. * Get the options array
  269. *
  270. * @return array
  271. */
  272. public function getOptions()
  273. {
  274. return $this->_options;
  275. }
  276. /**
  277. * Get a single option
  278. *
  279. * @param string $key
  280. * @return mixed
  281. */
  282. public function getOption($key)
  283. {
  284. return $this->_options[$key];
  285. }
  286. /**
  287. * Get the public key
  288. *
  289. * @return string
  290. */
  291. public function getPublicKey()
  292. {
  293. return $this->_publicKey;
  294. }
  295. /**
  296. * Set the public key
  297. *
  298. * @param string $publicKey
  299. * @return Zend_Service_ReCaptcha
  300. */
  301. public function setPublicKey($publicKey)
  302. {
  303. $this->_publicKey = $publicKey;
  304. return $this;
  305. }
  306. /**
  307. * Get the private key
  308. *
  309. * @return string
  310. */
  311. public function getPrivateKey()
  312. {
  313. return $this->_privateKey;
  314. }
  315. /**
  316. * Set the private key
  317. *
  318. * @param string $privateKey
  319. * @return Zend_Service_ReCaptcha
  320. */
  321. public function setPrivateKey($privateKey)
  322. {
  323. $this->_privateKey = $privateKey;
  324. return $this;
  325. }
  326. /**
  327. * Get the HTML code for the captcha
  328. *
  329. * This method uses the public key to fetch a recaptcha form.
  330. *
  331. * @param null|string $name Base name for recaptcha form elements
  332. * @return string
  333. * @throws Zend_Service_ReCaptcha_Exception
  334. */
  335. public function getHtml($name = null)
  336. {
  337. if ($this->_publicKey === null) {
  338. /** @see Zend_Service_ReCaptcha_Exception */
  339. require_once 'Zend/Service/ReCaptcha/Exception.php';
  340. throw new Zend_Service_ReCaptcha_Exception('Missing public key');
  341. }
  342. $host = self::API_SERVER;
  343. if ((bool) $this->_params['ssl'] === true) {
  344. $host = self::API_SECURE_SERVER;
  345. }
  346. $htmlBreak = '<br>';
  347. $htmlInputClosing = '>';
  348. if ((bool) $this->_params['xhtml'] === true) {
  349. $htmlBreak = '<br />';
  350. $htmlInputClosing = '/>';
  351. }
  352. $errorPart = '';
  353. if (!empty($this->_params['error'])) {
  354. $errorPart = '&error=' . urlencode($this->_params['error']);
  355. }
  356. $reCaptchaOptions = '';
  357. if (!empty($this->_options)) {
  358. $encoded = Zend_Json::encode($this->_options);
  359. $reCaptchaOptions = <<<SCRIPT
  360. <script type="text/javascript">
  361. var RecaptchaOptions = {$encoded};
  362. </script>
  363. SCRIPT;
  364. }
  365. $challengeField = 'recaptcha_challenge_field';
  366. $responseField = 'recaptcha_response_field';
  367. if (!empty($name)) {
  368. $challengeField = $name . '[' . $challengeField . ']';
  369. $responseField = $name . '[' . $responseField . ']';
  370. }
  371. $return = $reCaptchaOptions;
  372. $return .= <<<HTML
  373. <script type="text/javascript"
  374. src="{$host}/challenge?k={$this->_publicKey}{$errorPart}">
  375. </script>
  376. HTML;
  377. $return .= <<<HTML
  378. <noscript>
  379. <iframe src="{$host}/noscript?k={$this->_publicKey}{$errorPart}"
  380. height="300" width="500" frameborder="0"></iframe>{$htmlBreak}
  381. <textarea name="{$challengeField}" rows="3" cols="40">
  382. </textarea>
  383. <input type="hidden" name="{$responseField}"
  384. value="manual_challenge"{$htmlInputClosing}
  385. </noscript>
  386. HTML;
  387. return $return;
  388. }
  389. /**
  390. * Post a solution to the verify server
  391. *
  392. * @param string $challengeField
  393. * @param string $responseField
  394. * @return Zend_Http_Response
  395. * @throws Zend_Service_ReCaptcha_Exception
  396. */
  397. protected function _post($challengeField, $responseField)
  398. {
  399. if ($this->_privateKey === null) {
  400. /** @see Zend_Service_ReCaptcha_Exception */
  401. require_once 'Zend/Service/ReCaptcha/Exception.php';
  402. throw new Zend_Service_ReCaptcha_Exception('Missing private key');
  403. }
  404. if ($this->_ip === null) {
  405. /** @see Zend_Service_ReCaptcha_Exception */
  406. require_once 'Zend/Service/ReCaptcha/Exception.php';
  407. throw new Zend_Service_ReCaptcha_Exception('Missing ip address');
  408. }
  409. if (empty($challengeField)) {
  410. /** @see Zend_Service_ReCaptcha_Exception */
  411. require_once 'Zend/Service/ReCaptcha/Exception.php';
  412. throw new Zend_Service_ReCaptcha_Exception('Missing challenge field');
  413. }
  414. if (empty($responseField)) {
  415. /** @see Zend_Service_ReCaptcha_Exception */
  416. require_once 'Zend/Service/ReCaptcha/Exception.php';
  417. throw new Zend_Service_ReCaptcha_Exception('Missing response field');
  418. }
  419. /* Fetch an instance of the http client */
  420. $httpClient = self::getHttpClient();
  421. $postParams = array('privatekey' => $this->_privateKey,
  422. 'remoteip' => $this->_ip,
  423. 'challenge' => $challengeField,
  424. 'response' => $responseField);
  425. /* Make the POST and return the response */
  426. return $httpClient->setUri(self::VERIFY_SERVER)
  427. ->setParameterPost($postParams)
  428. ->request(Zend_Http_Client::POST);
  429. }
  430. /**
  431. * Verify the user input
  432. *
  433. * This method calls up the post method and returns a
  434. * Zend_Service_ReCaptcha_Response object.
  435. *
  436. * @param string $challengeField
  437. * @param string $responseField
  438. * @return Zend_Service_ReCaptcha_Response
  439. */
  440. public function verify($challengeField, $responseField)
  441. {
  442. $response = $this->_post($challengeField, $responseField);
  443. return new Zend_Service_ReCaptcha_Response(null, null, $response);
  444. }
  445. }