00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00038 define("RECAPTCHA_API_SERVER", "http://www.google.com/recaptcha/api");
00039 define("RECAPTCHA_API_SECURE_SERVER", "https://www.google.com/recaptcha/api");
00040 define("RECAPTCHA_VERIFY_SERVER", "www.google.com");
00041
00047 function _recaptcha_qsencode ($data) {
00048 $req = "";
00049 foreach ( $data as $key => $value )
00050 $req .= $key . '=' . urlencode( stripslashes($value) ) . '&';
00051
00052
00053 $req=substr($req,0,strlen($req)-1);
00054 return $req;
00055 }
00056
00057
00058
00067 function _recaptcha_http_post($host, $path, $data, $port = 80) {
00068
00069 $req = _recaptcha_qsencode ($data);
00070
00071 $http_request = "POST $path HTTP/1.0\r\n";
00072 $http_request .= "Host: $host\r\n";
00073 $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
00074 $http_request .= "Content-Length: " . strlen($req) . "\r\n";
00075 $http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
00076 $http_request .= "\r\n";
00077 $http_request .= $req;
00078
00079 $response = '';
00080 if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
00081 die ('Could not open socket');
00082 }
00083
00084 fwrite($fs, $http_request);
00085
00086 while ( !feof($fs) )
00087 $response .= fgets($fs, 1160);
00088 fclose($fs);
00089 $response = explode("\r\n\r\n", $response, 2);
00090
00091 return $response;
00092 }
00093
00094
00095
00106 function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false)
00107 {
00108 if ($pubkey == null || $pubkey == '') {
00109 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>");
00110 }
00111
00112 if ($use_ssl) {
00113 $server = RECAPTCHA_API_SECURE_SERVER;
00114 } else {
00115 $server = RECAPTCHA_API_SERVER;
00116 }
00117
00118 $errorpart = "";
00119 if ($error) {
00120 $errorpart = "&error=" . $error;
00121 }
00122 return '<script type="text/javascript" src="'. $server . '/challenge?k=' . $pubkey . $errorpart . '"></script>
00123
00124 <noscript>
00125 <iframe src="'. $server . '/noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br/>
00126 <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
00127 <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
00128 </noscript>';
00129 }
00130
00131
00132
00133
00137 class ReCaptchaResponse {
00138 var $is_valid;
00139 var $error;
00140 }
00141
00142
00152 function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response, $extra_params = array())
00153 {
00154 if ($privkey == null || $privkey == '') {
00155 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>");
00156 }
00157
00158 if ($remoteip == null || $remoteip == '') {
00159 die ("For security reasons, you must pass the remote ip to reCAPTCHA");
00160 }
00161
00162
00163
00164
00165 if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) {
00166 $recaptcha_response = new ReCaptchaResponse();
00167 $recaptcha_response->is_valid = false;
00168 $recaptcha_response->error = 'incorrect-captcha-sol';
00169 return $recaptcha_response;
00170 }
00171
00172 $response = _recaptcha_http_post (RECAPTCHA_VERIFY_SERVER, "/recaptcha/api/verify",
00173 array (
00174 'privatekey' => $privkey,
00175 'remoteip' => $remoteip,
00176 'challenge' => $challenge,
00177 'response' => $response
00178 ) + $extra_params
00179 );
00180
00181 $answers = explode ("\n", $response [1]);
00182 $recaptcha_response = new ReCaptchaResponse();
00183
00184 if (trim ($answers [0]) == 'true') {
00185 $recaptcha_response->is_valid = true;
00186 }
00187 else {
00188 $recaptcha_response->is_valid = false;
00189 $recaptcha_response->error = $answers [1];
00190 }
00191 return $recaptcha_response;
00192
00193 }
00194
00202 function recaptcha_get_signup_url ($domain = null, $appname = null) {
00203 return "https://www.google.com/recaptcha/admin/create?" . _recaptcha_qsencode (array ('domains' => $domain, 'app' => $appname));
00204 }
00205
00206 function _recaptcha_aes_pad($val) {
00207 $block_size = 16;
00208 $numpad = $block_size - (strlen ($val) % $block_size);
00209 return str_pad($val, strlen ($val) + $numpad, chr($numpad));
00210 }
00211
00212
00213
00214 function _recaptcha_aes_encrypt($val,$ky) {
00215 if (! function_exists ("mcrypt_encrypt")) {
00216 die ("To use reCAPTCHA Mailhide, you need to have the mcrypt php module installed.");
00217 }
00218 $mode=MCRYPT_MODE_CBC;
00219 $enc=MCRYPT_RIJNDAEL_128;
00220 $val=_recaptcha_aes_pad($val);
00221 return mcrypt_encrypt($enc, $ky, $val, $mode, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
00222 }
00223
00224
00225 function _recaptcha_mailhide_urlbase64 ($x) {
00226 return strtr(base64_encode ($x), '+/', '-_');
00227 }
00228
00229
00230 function recaptcha_mailhide_url($pubkey, $privkey, $email) {
00231 if ($pubkey == '' || $pubkey == null || $privkey == "" || $privkey == null) {
00232 die ("To use reCAPTCHA Mailhide, you have to sign up for a public and private key, " .
00233 "you can do so at <a href='http://www.google.com/recaptcha/mailhide/apikey'>http://www.google.com/recaptcha/mailhide/apikey</a>");
00234 }
00235
00236
00237 $ky = pack('H*', $privkey);
00238 $cryptmail = _recaptcha_aes_encrypt ($email, $ky);
00239
00240 return "http://www.google.com/recaptcha/mailhide/d?k=" . $pubkey . "&c=" . _recaptcha_mailhide_urlbase64 ($cryptmail);
00241 }
00242
00248 function _recaptcha_mailhide_email_parts ($email) {
00249 $arr = preg_split("/@/", $email );
00250
00251 if (strlen ($arr[0]) <= 4) {
00252 $arr[0] = substr ($arr[0], 0, 1);
00253 } else if (strlen ($arr[0]) <= 6) {
00254 $arr[0] = substr ($arr[0], 0, 3);
00255 } else {
00256 $arr[0] = substr ($arr[0], 0, 4);
00257 }
00258 return $arr;
00259 }
00260
00267 function recaptcha_mailhide_html($pubkey, $privkey, $email) {
00268 $emailparts = _recaptcha_mailhide_email_parts ($email);
00269 $url = recaptcha_mailhide_url ($pubkey, $privkey, $email);
00270
00271 return htmlentities($emailparts[0]) . "<a href='" . htmlentities ($url) .
00272 "' 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]);
00273
00274 }
00275
00276
00277 ?>