PluginProbe
Contact Forms by Cimatti / 2.2.4
Contact Forms by Cimatti v2.2.4
2.3.6 2.3.5 2.3.0 2.2.32 2.2.4 2.2.0 2.1.2 2.1.1 trunk 1.0 1.1 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 All 62 releases
contact-forms / PFBC / Resources / recaptchalib.php

recaptchalib.php in Contact Forms by Cimatti 2.2.4, at PFBC/Resources/recaptchalib.php

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