1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33:
34:
35: 36: 37:
38:
39: define('RECAPTCHA_API_SERVER', "http://www.google.com/recaptcha/api");
40: define('RECAPTCHA_API_SECURE_SERVER', "https://www.google.com/recaptcha/api");
41: define('RECAPTCHA_VERIFY_SERVER', "www.google.com");
42:
43: 44: 45: 46: 47: 48:
49: function _recaptcha_qsencode($data)
50: {
51: $req = "";
52: foreach ($data as $key => $value) {
53: $req .= $key . '=' . urlencode(stripslashes($value)) . '&';
54: }
55:
56:
57: $req = substr($req, 0, strlen($req) - 1);
58: return $req;
59: }
60:
61:
62: 63: 64: 65: 66: 67: 68: 69: 70:
71: function _recaptcha_http_post($host, $path, $data, $port = 80)
72: {
73:
74: $req = _recaptcha_qsencode($data);
75:
76: $http_request = "POST $path HTTP/1.0\r\n";
77: $http_request .= "Host: $host\r\n";
78: $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
79: $http_request .= "Content-Length: " . strlen($req) . "\r\n";
80: $http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
81: $http_request .= "\r\n";
82: $http_request .= $req;
83:
84: $response = '';
85: if (false == ($fs = @fsockopen($host, $port, $errno, $errstr, 10))) {
86: die ('Could not open socket');
87: }
88:
89: fwrite($fs, $http_request);
90:
91: while (!feof($fs)) {
92: $response .= fgets($fs, 1160);
93: }
94: fclose($fs);
95: $response = explode("\r\n\r\n", $response, 2);
96:
97: return $response;
98: }
99:
100:
101: 102: 103: 104: 105: 106: 107: 108: 109: 110:
111: function recaptcha_get_html($pubkey, $error = null, $use_ssl = false)
112: {
113: if ($pubkey == null || $pubkey == '') {
114: die ("To use reCAPTCHA you must get an API key from <a href='https://www.google.com/recaptcha/admin/create'>https://www.google.com/recaptcha/admin/create</a>");
115: }
116:
117: if ($use_ssl) {
118: $server = RECAPTCHA_API_SECURE_SERVER;
119: } else {
120: $server = RECAPTCHA_API_SERVER;
121: }
122:
123: $errorpart = "";
124: if ($error) {
125: $errorpart = "&error=" . $error;
126: }
127: return '<script type="text/javascript" src="' . $server . '/challenge?k=' . $pubkey . $errorpart . '"></script>
128:
129: <noscript>
130: <iframe src="' . $server . '/noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br/>
131: <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
132: <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
133: </noscript>';
134: }
135:
136:
137: 138: 139:
140: class ReCaptchaResponse
141: {
142: var $is_valid;
143: var $error;
144: }
145:
146: 147: 148: 149: 150: 151: 152: 153: 154: 155:
156: function recaptcha_check_answer($privkey, $remoteip, $challenge, $response, $extra_params = array())
157: {
158: if ($privkey == null || $privkey == '') {
159: die ("To use reCAPTCHA you must get an API key from <a href='https://www.google.com/recaptcha/admin/create'>https://www.google.com/recaptcha/admin/create</a>");
160: }
161:
162: if ($remoteip == null || $remoteip == '') {
163: die ("For security reasons, you must pass the remote ip to reCAPTCHA");
164: }
165:
166:
167: if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) {
168: $recaptcha_response = new ReCaptchaResponse();
169: $recaptcha_response->is_valid = false;
170: $recaptcha_response->error = 'incorrect-captcha-sol';
171: return $recaptcha_response;
172: }
173:
174: $response = _recaptcha_http_post(RECAPTCHA_VERIFY_SERVER,
175: "/recaptcha/api/verify",
176: array(
177: 'privatekey' => $privkey,
178: 'remoteip' => $remoteip,
179: 'challenge' => $challenge,
180: 'response' => $response
181: ) + $extra_params);
182:
183: $answers = explode("\n", $response [1]);
184: $recaptcha_response = new ReCaptchaResponse();
185:
186: if (trim($answers [0]) === 'true') {
187: $recaptcha_response->is_valid = true;
188: } else {
189: $recaptcha_response->is_valid = false;
190: $recaptcha_response->error = $answers [1];
191: }
192: return $recaptcha_response;
193:
194: }
195:
196: 197: 198: 199: 200: 201: 202: 203:
204: function recaptcha_get_signup_url($domain = null, $appname = null)
205: {
206: return "https://www.google.com/recaptcha/admin/create?" . _recaptcha_qsencode(
207: array('domains' => $domain, 'app' => $appname));
208: }
209:
210: 211: 212: 213:
214: function _recaptcha_aes_pad($val)
215: {
216: $block_size = 16;
217: $numpad = $block_size - (strlen($val) % $block_size);
218: return str_pad($val, strlen($val) + $numpad, chr($numpad));
219: }
220:
221: 222: 223: 224: 225: 226: 227:
228: function _recaptcha_aes_encrypt($val, $ky)
229: {
230: if (!function_exists("mcrypt_encrypt")) {
231: die ("To use reCAPTCHA Mailhide, you need to have the mcrypt php module installed.");
232: }
233: $mode = MCRYPT_MODE_CBC;
234: $enc = MCRYPT_RIJNDAEL_128;
235: $val = _recaptcha_aes_pad($val);
236: return mcrypt_encrypt($enc, $ky, $val, $mode, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
237: }
238:
239: 240: 241: 242:
243: function _recaptcha_mailhide_urlbase64($x)
244: {
245: return strtr(base64_encode($x), '+/', '-_');
246: }
247:
248: 249: 250: 251: 252: 253: 254: 255:
256: function recaptcha_mailhide_url($pubkey, $privkey, $email)
257: {
258: if ($pubkey == '' || $pubkey == null || $privkey == "" || $privkey == null) {
259: die ("To use reCAPTCHA Mailhide, you have to sign up for a public and private key, " . "you can do so at <a href='http://www.google.com/recaptcha/mailhide/apikey'>http://www.google.com/recaptcha/mailhide/apikey</a>");
260: }
261:
262: $ky = pack('H*', $privkey);
263: $cryptmail = _recaptcha_aes_encrypt($email, $ky);
264:
265: return "http://www.google.com/recaptcha/mailhide/d?k=" . $pubkey . "&c=" . _recaptcha_mailhide_urlbase64($cryptmail);
266: }
267:
268: 269: 270: 271: 272: 273: 274: 275:
276: function _recaptcha_mailhide_email_parts($email)
277: {
278: $arr = preg_split("/@/", $email);
279:
280: if (strlen($arr[0]) <= 4) {
281: $arr[0] = substr($arr[0], 0, 1);
282: } else {
283: if (strlen($arr[0]) <= 6) {
284: $arr[0] = substr($arr[0], 0, 3);
285: } else {
286: $arr[0] = substr($arr[0], 0, 4);
287: }
288: }
289: return $arr;
290: }
291:
292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302:
303: function recaptcha_mailhide_html($pubkey, $privkey, $email)
304: {
305: $emailparts = _recaptcha_mailhide_email_parts($email);
306: $url = recaptcha_mailhide_url($pubkey, $privkey, $email);
307:
308: return htmlentities($emailparts[0]) . "<a href='" . htmlentities($url) . "' 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]);
309: }
310: