• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

cms/modules/form/captcha/recaptcha/recaptchalib.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003  * This is a PHP library that handles calling reCAPTCHA.
00004  *    - Documentation and latest version
00005  *          http://recaptcha.net/plugins/php/
00006  *    - Get a reCAPTCHA API Key
00007  *          https://www.google.com/recaptcha/admin/create
00008  *    - Discussion group
00009  *          http://groups.google.com/group/recaptcha
00010  *
00011  * Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
00012  * AUTHORS:
00013  *   Mike Crawford
00014  *   Ben Maurer
00015  *
00016  * Permission is hereby granted, free of charge, to any person obtaining a copy
00017  * of this software and associated documentation files (the "Software"), to deal
00018  * in the Software without restriction, including without limitation the rights
00019  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00020  * copies of the Software, and to permit persons to whom the Software is
00021  * furnished to do so, subject to the following conditions:
00022  *
00023  * The above copyright notice and this permission notice shall be included in
00024  * all copies or substantial portions of the Software.
00025  *
00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00027  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00028  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00029  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00030  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00031  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00032  * THE SOFTWARE.
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         // Cut the last '&'
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); // One TCP-IP packet
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 = "&amp;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         //discard spam submissions
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 /* Mailhide related code */
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 /* gets the reCAPTCHA Mailhide url for a given email, public key and private key */
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 ?>

Generated on Sun Jan 2 2011 04:55:32 for Pragyan CMS by  doxygen 1.7.1