· 6 years ago · Nov 10, 2019, 05:26 PM
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
36 // your secret key
37$secret = "6LcSPMAUAAAAAJa-Q1PGRJ1_4H71jp2TW3UY6LL3";
38
39// empty response
40$response = null;
41
42// check secret key
43$reCaptcha = new ReCaptcha($secret);
44
45
46class ReCaptchaResponse
47{
48 public $success;
49 public $errorCodes;
50}
51class ReCaptcha
52{
53 private static $_signupUrl = "https://www.google.com/recaptcha/admin";
54 private static $_siteVerifyUrl =
55 "https://www.google.com/recaptcha/api/siteverify?";
56 private $_secret;
57 private static $_version = "php_1.0";
58 /**
59 * Constructor.
60 *
61 * @param string $secret shared secret between site and ReCAPTCHA server.
62 */
63 function ReCaptcha($secret)
64 {
65 if ($secret == null || $secret == "") {
66 die("To use reCAPTCHA you must get an API key from <a href='"
67 . self::$_signupUrl . "'>" . self::$_signupUrl . "</a>");
68 }
69 $this->_secret=$secret;
70 }
71 /**
72 * Encodes the given data into a query string format.
73 *
74 * @param array $data array of string elements to be encoded.
75 *
76 * @return string - encoded request.
77 */
78 private function _encodeQS($data)
79 {
80 $req = "";
81 foreach ($data as $key => $value) {
82 $req .= $key . '=' . urlencode(stripslashes($value)) . '&';
83 }
84 // Cut the last '&'
85 $req=substr($req, 0, strlen($req)-1);
86 return $req;
87 }
88 /**
89 * Submits an HTTP GET to a reCAPTCHA server.
90 *
91 * @param string $path url path to recaptcha server.
92 * @param array $data array of parameters to be sent.
93 *
94 * @return array response
95 */
96 private function _submitHTTPGet($path, $data)
97 {
98 $req = $this->_encodeQS($data);
99 $response = file_get_contents($path . $req);
100 return $response;
101 }
102 /**
103 * Calls the reCAPTCHA siteverify API to verify whether the user passes
104 * CAPTCHA test.
105 *
106 * @param string $remoteIp IP address of end user.
107 * @param string $response response string from recaptcha verification.
108 *
109 * @return ReCaptchaResponse
110 */
111 public function verifyResponse($remoteIp, $response)
112 {
113 // Discard empty solution submissions
114 if ($response == null || strlen($response) == 0) {
115 $recaptchaResponse = new ReCaptchaResponse();
116 $recaptchaResponse->success = false;
117 $recaptchaResponse->errorCodes = 'missing-input';
118 return $recaptchaResponse;
119 }
120 $getResponse = $this->_submitHttpGet(
121 self::$_siteVerifyUrl,
122 array (
123 'secret' => $this->_secret,
124 'remoteip' => $remoteIp,
125 'v' => self::$_version,
126 'response' => $response
127 )
128 );
129 $answers = json_decode($getResponse, true);
130 $recaptchaResponse = new ReCaptchaResponse();
131 if (trim($answers ['success']) == true) {
132 $recaptchaResponse->success = true;
133 } else {
134 $recaptchaResponse->success = false;
135 $recaptchaResponse->errorCodes = $answers [error-codes];
136 }
137 return $recaptchaResponse;
138 }
139}
140?>