Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/recaptchalib.php
Go to the documentation of this file.
00001 <?php
00002 
00042 define("RECAPTCHA_API_SERVER", "http://www.google.com/recaptcha/api");
00043 define("RECAPTCHA_API_SECURE_SERVER", "https://www.google.com/recaptcha/api");
00044 define("RECAPTCHA_VERIFY_SERVER", "www.google.com");
00045 
00051 function _recaptcha_qsencode ($data) {
00052         $req = "";
00053         foreach ( $data as $key => $value )
00054                 $req .= $key . '=' . urlencode( $value ) . '&';
00055 
00056         // Cut the last '&'
00057         $req=substr($req,0,strlen($req)-1);
00058         return $req;
00059 }
00060 
00061 
00062 
00073 function _recaptcha_http_post($host, $path, $data, $port = 80, $https=false) {
00074         global $CFG;
00075         $protocol = 'http';
00076         if ($https) {
00077             $protocol = 'https';
00078         }
00079 
00080         require_once $CFG->libdir . '/filelib.php';
00081 
00082         $req = _recaptcha_qsencode ($data);
00083 
00084         $headers = array();
00085         $headers['Host'] = $host;
00086         $headers['Content-Type'] = 'application/x-www-form-urlencoded';
00087         $headers['Content-Length'] = strlen($req);
00088         $headers['User-Agent'] = 'reCAPTCHA/PHP';
00089 
00090         $results = download_file_content("$protocol://" . $host . $path, $headers, $data, false, 300, 20, true);
00091 
00092         if ($results) {
00093             return array(1 => $results);
00094         } else {
00095             return false;
00096         }
00097 }
00098 
00099 
00100 
00113 function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false) {
00114     global $CFG, $PAGE;
00115 
00116     $recaptchatype = optional_param('recaptcha', 'image', PARAM_TEXT);
00117 
00118     if ($pubkey == null || $pubkey == '') {
00119                 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>");
00120     }
00121 
00122     if ($use_ssl) {
00123         $server = RECAPTCHA_API_SECURE_SERVER;
00124     } else {
00125         $server = RECAPTCHA_API_SERVER;
00126     }
00127 
00128     $errorpart = "";
00129     if ($error) {
00130        $errorpart = "&amp;error=" . $error;
00131     }
00132 
00133     require_once $CFG->libdir . '/filelib.php';
00134     $html = download_file_content($server . '/noscript?k=' . $pubkey . $errorpart, null, null, false, 300, 20, true);
00135     preg_match('/image\?c\=([A-Za-z0-9\-\_]*)\"/', $html, $matches);
00136     $challenge_hash = $matches[1];
00137     $image_url = $server . '/image?c=' . $challenge_hash;
00138 
00139     $strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth');
00140     $strenterthewordsabove = get_string('enterthewordsabove', 'auth');
00141     $strenterthenumbersyouhear = get_string('enterthenumbersyouhear', 'auth');
00142     $strgetanothercaptcha = get_string('getanothercaptcha', 'auth');
00143     $strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth');
00144     $strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth');
00145 
00146     $return = html_writer::script('', $server . '/challenge?k=' . $pubkey . $errorpart);
00147     $return .= '<noscript>
00148         <div id="recaptcha_widget_noscript">
00149         <div id="recaptcha_image_noscript"><img src="' . $image_url . '" alt="reCAPTCHA"/></div>';
00150 
00151     if ($error == 'incorrect-captcha-sol') {
00152         $return .= '<div class="recaptcha_only_if_incorrect_sol" style="color:red">' . $strincorrectpleasetryagain . '</div>';
00153     }
00154 
00155     if ($recaptchatype == 'image') {
00156         $return .= '<span class="recaptcha_only_if_image">' . $strenterthewordsabove . '</span>';
00157     } elseif ($recaptchatype == 'audio') {
00158         $return .= '<span class="recaptcha_only_if_audio">' . $strenterthenumbersyouhear . '</span>'; 
00159     }
00160     
00161     $return .= '<input type="text" id="recaptcha_response_field_noscript" name="recaptcha_response_field" />';
00162     $return .= '<input type="hidden" id="recaptcha_challenge_field_noscript" name="recaptcha_challenge_field" value="' . $challenge_hash . '" />';
00163     $return .= '<div><a href="signup.php">' . $strgetanothercaptcha . '</a></div>';
00164     
00165     // Disabling audio recaptchas for now: not language-independent
00166     /*
00167     if ($recaptchatype == 'image') {
00168         $return .= '<div class="recaptcha_only_if_image"><a href="signup.php?recaptcha=audio">' . $strgetanaudiocaptcha . '</a></div>';
00169     } elseif ($recaptchatype == 'audio') {
00170         $return .= '<div class="recaptcha_only_if_audio"><a href="signup.php?recaptcha=image">' . $strgetanimagecaptcha . '</a></div>';
00171     }
00172     */
00173 
00174     $return .= '
00175         </div>
00176     </noscript>';
00177 
00178     return $return;
00179 }
00180 
00181 
00182 
00183 
00190 class ReCaptchaResponse {
00191         var $is_valid;
00192         var $error;
00193 }
00194 
00195 
00204 function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response, $https=false)
00205 {
00206     if ($privkey == null || $privkey == '') {
00207                 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>");
00208     }
00209 
00210     if ($remoteip == null || $remoteip == '') {
00211         die ("For security reasons, you must pass the remote ip to reCAPTCHA");
00212     }
00213 
00214         //discard spam submissions
00215         if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) {
00216                 $recaptcha_response = new ReCaptchaResponse();
00217                 $recaptcha_response->is_valid = false;
00218                 $recaptcha_response->error = 'incorrect-captcha-sol';
00219                 return $recaptcha_response;
00220         }
00221 
00222         $response = _recaptcha_http_post(RECAPTCHA_VERIFY_SERVER, "/recaptcha/api/verify",
00223                                          array (
00224                                                 'privatekey' => $privkey,
00225                                                 'remoteip' => $remoteip,
00226                                                 'challenge' => $challenge,
00227                                                 'response' => $response
00228                                                 ),
00229                                          $https        
00230                                         );
00231 
00232         $answers = explode ("\n", $response [1]);
00233         $recaptcha_response = new ReCaptchaResponse();
00234 
00235         if (trim ($answers [0]) == 'true') {
00236                 $recaptcha_response->is_valid = true;
00237         }
00238         else {
00239                 $recaptcha_response->is_valid = false;
00240                 $recaptcha_response->error = $answers [1];
00241         }
00242         return $recaptcha_response;
00243 
00244 }
00245 
00253 function recaptcha_get_signup_url ($domain = null, $appname = null) {
00254         return "https://www.google.com/recaptcha/admin/create?" .  _recaptcha_qsencode (array ('domains' => $domain, 'app' => $appname));
00255 }
00256 
00257 function _recaptcha_aes_pad($val) {
00258     $block_size = 16;
00259     $numpad = $block_size - (strlen ($val) % $block_size);
00260     return str_pad($val, strlen ($val) + $numpad, chr($numpad));
00261 }
00262 
00263 /* Mailhide related code */
00264 
00265 function _recaptcha_aes_encrypt($val,$ky) {
00266     if (! function_exists ("mcrypt_encrypt")) {
00267         die ("To use reCAPTCHA Mailhide, you need to have the mcrypt php module installed.");
00268     }
00269     $mode=MCRYPT_MODE_CBC;
00270     $enc=MCRYPT_RIJNDAEL_128;
00271     $val=_recaptcha_aes_pad($val);
00272     return mcrypt_encrypt($enc, $ky, $val, $mode, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
00273 }
00274 
00275 
00276 function _recaptcha_mailhide_urlbase64 ($x) {
00277     return strtr(base64_encode ($x), '+/', '-_');
00278 }
00279 
00280 /* gets the reCAPTCHA Mailhide url for a given email, public key and private key */
00281 function recaptcha_mailhide_url($pubkey, $privkey, $email) {
00282     if ($pubkey == '' || $pubkey == null || $privkey == "" || $privkey == null) {
00283         die ("To use reCAPTCHA Mailhide, you have to sign up for a public and private key, " .
00284                      "you can do so at <a href='http://www.google.com/recaptcha/mailhide/apikey'>http://www.google.com/recaptcha/mailhide/apikey</a>");
00285     }
00286 
00287 
00288     $ky = pack('H*', $privkey);
00289     $cryptmail = _recaptcha_aes_encrypt ($email, $ky);
00290 
00291         return "http://www.google.com/recaptcha/mailhide/d?k=" . $pubkey . "&c=" . _recaptcha_mailhide_urlbase64 ($cryptmail);
00292 }
00293 
00299 function _recaptcha_mailhide_email_parts ($email) {
00300     $arr = preg_split("/@/", $email );
00301 
00302     if (strlen ($arr[0]) <= 4) {
00303         $arr[0] = substr ($arr[0], 0, 1);
00304     } else if (strlen ($arr[0]) <= 6) {
00305         $arr[0] = substr ($arr[0], 0, 3);
00306     } else {
00307         $arr[0] = substr ($arr[0], 0, 4);
00308     }
00309     return $arr;
00310 }
00311 
00318 function recaptcha_mailhide_html($pubkey, $privkey, $email) {
00319     $emailparts = _recaptcha_mailhide_email_parts ($email);
00320     $url = recaptcha_mailhide_url ($pubkey, $privkey, $email);
00321 
00322     return htmlentities($emailparts[0]) . "<a href='" . htmlentities ($url) .
00323         "' 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]);
00324 
00325 }
 All Data Structures Namespaces Files Functions Variables Enumerations