]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-13796 Committing new recaptcha element
authornicolasconnault <nicolasconnault>
Thu, 13 Mar 2008 15:45:16 +0000 (15:45 +0000)
committernicolasconnault <nicolasconnault>
Thu, 13 Mar 2008 15:45:16 +0000 (15:45 +0000)
auth/email/auth.php
lang/en_utf8/auth.php
lib/authlib.php
lib/form/recaptcha.php
lib/recaptchalib.php

index 82e0ba29a6cab27e8da49280c7e067259b7f9cb3..8806e8b41cae206f0ced1be5369292a001a7d306 100644 (file)
@@ -206,6 +206,15 @@ class auth_plugin_email extends auth_plugin_base {
         set_config('recaptcha', $config->recaptcha, 'auth/email');
         return true;
     }
+    
+    /**
+     * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
+     * @return bool
+     */
+    function is_captcha_enabled() {
+        global $CFG;
+        return isset($CFG->recaptchapublickey) && isset($CFG->recaptchaprivatekey) && get_config("auth/{$this->authtype}", 'recaptcha');
+    }
 
 }
 
index 50816392101ed7b50866383f318e7b07de8bc9d8..f8f917769e558380d6ff62377d30234a560aff2f 100644 (file)
@@ -378,8 +378,8 @@ $string['unlocked'] = 'Unlocked';
 $string['unlockedifempty'] = 'Unlocked if empty';
 $string['locked'] = 'Locked';
 $string['incorrectpleasetryagain'] = 'Incorrect. Please try again.';
-$string['enterthewordsabove'] = 'Enter the words above:';
-$string['enterthenumbersyouhear'] = 'Enter the numbers you hear:';
+$string['enterthewordsabove'] = 'Enter the words above';
+$string['enterthenumbersyouhear'] = 'Enter the numbers you hear';
 $string['getanothercaptcha'] = 'Get another CAPTCHA';
 $string['getanaudiocaptcha'] = 'Get an audio CAPTCHA';
 $string['getanimagecaptcha'] = 'Get an image CAPTCHA';
index 242d2cd3a4fb5f9b97c284201128ade48d34de1f..2cbdda31d13dcab55eda177329465dc74ee70502 100644 (file)
@@ -342,6 +342,17 @@ class auth_plugin_base {
         }
         return $authdescription;
     }
+    
+    /**
+     * Returns whether or not the captcha element is enabled, and the admin settings fulfil its requirements.
+     * @abstract Implement in child classes
+     * @return bool
+     */
+    function is_captcha_enabled() {
+        return false;
+    }
+
+
 }
 
 ?>
index 71f3c10e79981c5ef9b3dab4b2b3c038f5893d50..f3c4ad703b2eea373bf32b0e61fef00b15f32b5f 100644 (file)
@@ -75,11 +75,11 @@ class MoodleQuickForm_recaptcha extends HTML_QuickForm_input {
 <div id="recaptcha_image"></div>
 <div class="recaptcha_only_if_incorrect_sol" style="color:red">' . $strincorrectpleasetryagain . '</div>
 
-<span class="recaptcha_only_if_image">' . $strenterthewordsabove . '</span>
-<span class="recaptcha_only_if_audio">' . $strenterthenumbersyouhear . '</span>
+<span class="recaptcha_only_if_image"><label for="recaptcha_response_field">' . $strenterthewordsabove . '</label></span>
+<span class="recaptcha_only_if_audio"><label for="recaptcha_response_field">' . $strenterthenumbersyouhear . '</label></span>
 
 <input type="text" id="recaptcha_response_field" name="recaptcha_response_field" />
-
+<input type="hidden" name="recaptcha_element" value="dummyvalue" /> <!-- Dummy value to fool formslib -->
 <div><a href="javascript:Recaptcha.reload()">' . $strgetanothercaptcha . '</a></div>
 <div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type(\'audio\')">' . $strgetanaudiocaptcha . '</a></div>
 <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type(\'image\')">' . $strgetanimagecaptcha . '</a></div>
index b3b9bca649ecdf8478ba3711ad03da2080bf89aa..63374f257d14f0b1c9cc1cd7ab3a39acf7400902 100644 (file)
@@ -66,22 +66,20 @@ function _recaptcha_qsencode ($data) {
  */
 function _recaptcha_http_post($host, $path, $data, $port = 80) {
         global $CFG;
-        require_once $CFG->libdir . '/snoopy/Snoopy.class.inc';
+        require_once $CFG->libdir . '/filelib.php';
 
-        $snoopy = new Snoopy();
-        $snoopy->proxy_host = $CFG->proxyhost;
-        $snoopy->proxy_port = $CFG->proxyport;
+        $req = _recaptcha_qsencode ($data);
         
-        if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
-            // this will probably fail, but let's try it anyway
-            $snoopy->proxy_user     = $CFG->proxyuser;
-            $snoopy->proxy_password = $CFG->proxypassword;
-        }
+        $headers = array();
+        $headers['Host'] = $host;
+        $headers['Content-Type'] = 'application/x-www-form-urlencoded';
+        $headers['Content-Length'] = strlen($req);
+        $headers['User-Agent'] = 'reCAPTCHA/PHP';
         
-        $submit = $snoopy->submit('http://' . $host . $path, $data);
+        $results = download_file_content('http://' . $host . $path, $headers, $data);
         
-        if ($submit) {
-            return array(1 => $snoopy->results);
+        if ($results) {
+            return array(1 => $results);
         } else {
             return false;
         }
@@ -99,8 +97,11 @@ function _recaptcha_http_post($host, $path, $data, $port = 80) {
 
  * @return string - The HTML to be embedded in the user's form.
  */
-function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false)
-{
+function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false) {
+    global $CFG;
+
+    $recaptchatype = optional_param('recaptcha', 'image', PARAM_TEXT);
+
        if ($pubkey == null || $pubkey == '') {
                die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>");
        }
@@ -115,13 +116,53 @@ function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false)
         if ($error) {
            $errorpart = "&amp;error=" . $error;
         }
-        return '<script type="text/javascript" src="'. $server . '/challenge?k=' . $pubkey . $errorpart . '"></script>
 
+    require_once $CFG->libdir . '/filelib.php';
+    $html = download_file_content($server . '/noscript?k=' . $pubkey . $errorpart);
+    preg_match('/image\?c\=([A-Za-z0-9\-\_]*)\"/', $html, $matches);
+    $challenge_hash = $matches[1];
+    $image_url = $server . '/image?c=' . $challenge_hash;
+    
+    $strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth');
+    $strenterthewordsabove = get_string('enterthewordsabove', 'auth');
+    $strenterthenumbersyouhear = get_string('enterthenumbersyouhear', 'auth');
+    $strgetanothercaptcha = get_string('getanothercaptcha', 'auth');
+    $strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth');
+    $strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth');
+    
+    $return = '<script type="text/javascript" src="'. $server . '/challenge?k=' . $pubkey . $errorpart . '"></script> 
        <noscript>
-               <iframe src="'. $server . '/noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br>
-               <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
-               <input type="hidden" name="recaptcha_response_field" value="manual_challenge">
+        <div id="recaptcha_widget_noscript">
+        <div id="recaptcha_image_noscript"><img src="' . $image_url . '" alt="reCAPTCHA"/></div>';
+    
+    if ($error == 'incorrect-captcha-sol') {
+        $return .= '<div class="recaptcha_only_if_incorrect_sol" style="color:red">' . $strincorrectpleasetryagain . '</div>';
+    }
+
+    if ($recaptchatype == 'image') {
+        $return .= '<span class="recaptcha_only_if_image">' . $strenterthewordsabove . '</span>';
+    } elseif ($recaptchatype == 'audio') {
+        $return .= '<span class="recaptcha_only_if_audio">' . $strenterthenumbersyouhear . '</span>'; 
+    }
+    
+    $return .= '<input type="text" id="recaptcha_response_field_noscript" name="recaptcha_response_field" />';
+    $return .= '<input type="hidden" id="recaptcha_challenge_field_noscript" name="recaptcha_challenge_field" value="' . $challenge_hash . '" />';
+    $return .= '<div><a href="signup.php">' . $strgetanothercaptcha . '</a></div>';
+    
+    // Disabling audio recaptchas for now: not language-independent
+    /*
+    if ($recaptchatype == 'image') {
+        $return .= '<div class="recaptcha_only_if_image"><a href="signup.php?recaptcha=audio">' . $strgetanaudiocaptcha . '</a></div>';
+    } elseif ($recaptchatype == 'audio') {
+        $return .= '<div class="recaptcha_only_if_audio"><a href="signup.php?recaptcha=image">' . $strgetanimagecaptcha . '</a></div>';
+    }
+    */
+
+    $return .= '
+        </div>
        </noscript>';
+
+    return $return;
 }