recaptchalib.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * This is a PHP library that handles calling reCAPTCHA.
  4. * - Documentation and latest version
  5. * https://developers.google.com/recaptcha/docs/php
  6. * - Get a reCAPTCHA API Key
  7. * https://www.google.com/recaptcha/admin/create
  8. * - Discussion group
  9. * http://groups.google.com/group/recaptcha
  10. *
  11. * @copyright Copyright (c) 2014, Google Inc.
  12. * @link http://www.google.com/recaptcha
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this software and associated documentation files (the "Software"), to deal
  16. * in the Software without restriction, including without limitation the rights
  17. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. * copies of the Software, and to permit persons to whom the Software is
  19. * furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  30. * THE SOFTWARE.
  31. */
  32. /**
  33. * A ReCaptchaResponse is returned from checkAnswer().
  34. */
  35. class ReCaptchaResponse
  36. {
  37. public $success;
  38. public $errorCodes;
  39. }
  40. class ReCaptcha
  41. {
  42. private static $_signupUrl = "https://www.google.com/recaptcha/admin";
  43. private static $_siteVerifyUrl =
  44. "https://www.google.com/recaptcha/api/siteverify?";
  45. private $_secret;
  46. private static $_version = "php_1.0";
  47. /**
  48. * Constructor.
  49. *
  50. * @param string $secret shared secret between site and ReCAPTCHA server.
  51. */
  52. function ReCaptcha($secret)
  53. {
  54. if ($secret == null || $secret == "") {
  55. die("To use reCAPTCHA you must get an API key from <a href='"
  56. . self::$_signupUrl . "'>" . self::$_signupUrl . "</a>");
  57. }
  58. $this->_secret=$secret;
  59. }
  60. /**
  61. * Encodes the given data into a query string format.
  62. *
  63. * @param array $data array of string elements to be encoded.
  64. *
  65. * @return string - encoded request.
  66. */
  67. private function _encodeQS($data)
  68. {
  69. $req = "";
  70. foreach ($data as $key => $value) {
  71. $req .= $key . '=' . urlencode(stripslashes($value)) . '&';
  72. }
  73. // Cut the last '&'
  74. $req=substr($req, 0, strlen($req)-1);
  75. return $req;
  76. }
  77. /**
  78. * Submits an HTTP GET to a reCAPTCHA server.
  79. *
  80. * @param string $path url path to recaptcha server.
  81. * @param array $data array of parameters to be sent.
  82. *
  83. * @return array response
  84. */
  85. private function _submitHTTPGet($path, $data)
  86. {
  87. $req = $this->_encodeQS($data);
  88. $response = file_get_contents($path . $req);
  89. return $response;
  90. }
  91. /**
  92. * Calls the reCAPTCHA siteverify API to verify whether the user passes
  93. * CAPTCHA test.
  94. *
  95. * @param string $remoteIp IP address of end user.
  96. * @param string $response response string from recaptcha verification.
  97. *
  98. * @return ReCaptchaResponse
  99. */
  100. public function verifyResponse($remoteIp, $response)
  101. {
  102. // Discard empty solution submissions
  103. if ($response == null || strlen($response) == 0) {
  104. $recaptchaResponse = new ReCaptchaResponse();
  105. $recaptchaResponse->success = false;
  106. $recaptchaResponse->errorCodes = 'missing-input';
  107. return $recaptchaResponse;
  108. }
  109. $getResponse = $this->_submitHttpGet(
  110. self::$_siteVerifyUrl,
  111. array (
  112. 'secret' => $this->_secret,
  113. 'remoteip' => $remoteIp,
  114. 'v' => self::$_version,
  115. 'response' => $response
  116. )
  117. );
  118. $answers = json_decode($getResponse, true);
  119. $recaptchaResponse = new ReCaptchaResponse();
  120. if (trim($answers ['success']) == true) {
  121. $recaptchaResponse->success = true;
  122. } else {
  123. $recaptchaResponse->success = false;
  124. $recaptchaResponse->errorCodes = $answers [error-codes];
  125. }
  126. return $recaptchaResponse;
  127. }
  128. }
  129. ?>