recaptchalib.php

Thu, 13 May 2010 09:31:01 -0500

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 13 May 2010 09:31:01 -0500
changeset 0
472198dc918e
permissions
-rwxr-xr-x

Initial commit

0
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 <?php
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 /*
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 * This is a PHP library that handles calling reCAPTCHA.
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 * - Documentation and latest version
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 * http://recaptcha.net/plugins/php/
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 * - Get a reCAPTCHA API Key
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 * http://recaptcha.net/api/getkey
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 * - Discussion group
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 * http://groups.google.com/group/recaptcha
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 *
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 * Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 * AUTHORS:
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 * Mike Crawford
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 * Ben Maurer
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 *
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 * of this software and associated documentation files (the "Software"), to deal
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 * in the Software without restriction, including without limitation the rights
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 * copies of the Software, and to permit persons to whom the Software is
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 * furnished to do so, subject to the following conditions:
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 *
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 * The above copyright notice and this permission notice shall be included in
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 * all copies or substantial portions of the Software.
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 *
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 * THE SOFTWARE.
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 */
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 /**
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 * The reCAPTCHA server URL's
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 */
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 define("RECAPTCHA_API_SERVER", "http://api.recaptcha.net");
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 define("RECAPTCHA_API_SECURE_SERVER", "https://api-secure.recaptcha.net");
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 define("RECAPTCHA_VERIFY_SERVER", "api-verify.recaptcha.net");
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 /**
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 * Encodes the given data into a query string format
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 * @param $data - array of string elements to be encoded
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 * @return string - encoded request
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 */
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 function _recaptcha_qsencode ($data) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 $req = "";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 foreach ( $data as $key => $value )
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 $req .= $key . '=' . urlencode( stripslashes($value) ) . '&';
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 // Cut the last '&'
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 $req=substr($req,0,strlen($req)-1);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 return $req;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 /**
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 * Submits an HTTP POST to a reCAPTCHA server
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 * @param string $host
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 * @param string $path
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 * @param array $data
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 * @param int port
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 * @return array response
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 */
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 function _recaptcha_http_post($host, $path, $data, $port = 80) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 $req = _recaptcha_qsencode ($data);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 $http_request = "POST $path HTTP/1.0\r\n";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 $http_request .= "Host: $host\r\n";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 $http_request .= "Content-Length: " . strlen($req) . "\r\n";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 $http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 $http_request .= "\r\n";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 $http_request .= $req;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 $response = '';
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 die ('Could not open socket');
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 fwrite($fs, $http_request);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 while ( !feof($fs) )
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 $response .= fgets($fs, 1160); // One TCP-IP packet
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 fclose($fs);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 $response = explode("\r\n\r\n", $response, 2);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 return $response;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 /**
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 * Gets the challenge HTML (javascript and non-javascript version).
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 * This is called from the browser, and the resulting reCAPTCHA HTML widget
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 * is embedded within the HTML form it was called from.
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 * @param string $pubkey A public key for reCAPTCHA
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 * @param string $error The error given by reCAPTCHA (optional, default is null)
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 * @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 * @return string - The HTML to be embedded in the user's form.
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 */
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false)
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 if ($pubkey == null || $pubkey == '') {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>");
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 if ($use_ssl) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 $server = RECAPTCHA_API_SECURE_SERVER;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 } else {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 $server = RECAPTCHA_API_SERVER;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 $errorpart = "";
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 if ($error) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 $errorpart = "&amp;error=" . $error;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 return '<script type="text/javascript" src="'. $server . '/challenge?k=' . $pubkey . $errorpart . '"></script>
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 <noscript>
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 <iframe src="'. $server . '/noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br/>
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 </noscript>';
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 /**
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 * A ReCaptchaResponse is returned from recaptcha_check_answer()
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 */
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 class ReCaptchaResponse {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 var $is_valid;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 var $error;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 /**
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 * Calls an HTTP POST function to verify if the user's guess was correct
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 * @param string $privkey
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 * @param string $remoteip
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 * @param string $challenge
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 * @param string $response
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 * @param array $extra_params an array of extra variables to post to the server
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 * @return ReCaptchaResponse
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 */
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response, $extra_params = array())
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 if ($privkey == null || $privkey == '') {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>");
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 if ($remoteip == null || $remoteip == '') {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 die ("For security reasons, you must pass the remote ip to reCAPTCHA");
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 //discard spam submissions
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 $recaptcha_response = new ReCaptchaResponse();
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 $recaptcha_response->is_valid = false;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 $recaptcha_response->error = 'incorrect-captcha-sol';
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 return $recaptcha_response;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 $response = _recaptcha_http_post (RECAPTCHA_VERIFY_SERVER, "/verify",
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 array (
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 'privatekey' => $privkey,
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 'remoteip' => $remoteip,
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 'challenge' => $challenge,
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 'response' => $response
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 ) + $extra_params
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 );
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 $answers = explode ("\n", $response [1]);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 $recaptcha_response = new ReCaptchaResponse();
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 if (trim ($answers [0]) == 'true') {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 $recaptcha_response->is_valid = true;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 else {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 $recaptcha_response->is_valid = false;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 $recaptcha_response->error = $answers [1];
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 return $recaptcha_response;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 /**
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 * gets a URL where the user can sign up for reCAPTCHA. If your application
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 * has a configuration page where you enter a key, you should provide a link
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 * using this function.
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 * @param string $domain The domain where the page is hosted
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 * @param string $appname The name of your application
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 */
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 function recaptcha_get_signup_url ($domain = null, $appname = null) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203 return "http://recaptcha.net/api/getkey?" . _recaptcha_qsencode (array ('domain' => $domain, 'app' => $appname));
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 function _recaptcha_aes_pad($val) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 $block_size = 16;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 $numpad = $block_size - (strlen ($val) % $block_size);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 return str_pad($val, strlen ($val) + $numpad, chr($numpad));
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 /* Mailhide related code */
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214 function _recaptcha_aes_encrypt($val,$ky) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 if (! function_exists ("mcrypt_encrypt")) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 die ("To use reCAPTCHA Mailhide, you need to have the mcrypt php module installed.");
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 $mode=MCRYPT_MODE_CBC;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 $enc=MCRYPT_RIJNDAEL_128;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 $val=_recaptcha_aes_pad($val);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 return mcrypt_encrypt($enc, $ky, $val, $mode, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 function _recaptcha_mailhide_urlbase64 ($x) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 return strtr(base64_encode ($x), '+/', '-_');
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 /* gets the reCAPTCHA Mailhide url for a given email, public key and private key */
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 function recaptcha_mailhide_url($pubkey, $privkey, $email) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 if ($pubkey == '' || $pubkey == null || $privkey == "" || $privkey == null) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 die ("To use reCAPTCHA Mailhide, you have to sign up for a public and private key, " .
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 "you can do so at <a href='http://mailhide.recaptcha.net/apikey'>http://mailhide.recaptcha.net/apikey</a>");
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 $ky = pack('H*', $privkey);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 $cryptmail = _recaptcha_aes_encrypt ($email, $ky);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 return "http://mailhide.recaptcha.net/d?k=" . $pubkey . "&c=" . _recaptcha_mailhide_urlbase64 ($cryptmail);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 /**
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 * gets the parts of the email to expose to the user.
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 * eg, given johndoe@example,com return ["john", "example.com"].
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 * the email is then displayed as john...@example.com
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 */
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 function _recaptcha_mailhide_email_parts ($email) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 $arr = preg_split("/@/", $email );
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 if (strlen ($arr[0]) <= 4) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 $arr[0] = substr ($arr[0], 0, 1);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253 } else if (strlen ($arr[0]) <= 6) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 $arr[0] = substr ($arr[0], 0, 3);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 } else {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 $arr[0] = substr ($arr[0], 0, 4);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 return $arr;
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 /**
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262 * Gets html to display an email address given a public an private key.
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 * to get a key, go to:
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 *
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 * http://mailhide.recaptcha.net/apikey
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266 */
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 function recaptcha_mailhide_html($pubkey, $privkey, $email) {
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 $emailparts = _recaptcha_mailhide_email_parts ($email);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 $url = recaptcha_mailhide_url ($pubkey, $privkey, $email);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 return htmlentities($emailparts[0]) . "<a href='" . htmlentities ($url) .
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 "' onclick=\"window.open('" . htmlentities ($url) . "', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;\" title=\"Reveal this e-mail address\">...</a>@" . htmlentities ($emailparts [1]);
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 }
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276
472198dc918e Initial commit
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 ?>

mercurial