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