]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-13796 Added help button and help file, used Snoopy in recaptchalib for proxy...
authornicolasconnault <nicolasconnault>
Tue, 11 Mar 2008 10:34:50 +0000 (10:34 +0000)
committernicolasconnault <nicolasconnault>
Tue, 11 Mar 2008 10:34:50 +0000 (10:34 +0000)
lang/en_utf8/auth.php
lang/en_utf8/help/recaptcha.html [new file with mode: 0644]
lib/form/recaptcha.php
lib/recaptchalib.php
login/signup_form.php

index bf0c32399d44821c3dca3518ac444970994cd0a4..50816392101ed7b50866383f318e7b07de8bc9d8 100644 (file)
@@ -383,4 +383,5 @@ $string['enterthenumbersyouhear'] = 'Enter the numbers you hear:';
 $string['getanothercaptcha'] = 'Get another CAPTCHA';
 $string['getanaudiocaptcha'] = 'Get an audio CAPTCHA';
 $string['getanimagecaptcha'] = 'Get an image CAPTCHA';
+$string['recaptcha'] = 'reCAPTCHA';
 ?>
diff --git a/lang/en_utf8/help/recaptcha.html b/lang/en_utf8/help/recaptcha.html
new file mode 100644 (file)
index 0000000..32b2880
--- /dev/null
@@ -0,0 +1,10 @@
+<h1>reCAPTCHA</h1>
+<h2>Description</h2>
+<p>A CAPTCHA is a program that can tell whether its user is a human or a computer. CAPTCHAs are used by many websites to prevent abuse from "bots," or automated programs usually written to generate spam. No computer program can read distorted text as well as humans can, so bots cannot navigate sites protected by CAPTCHAs.</p>
+
+<h2>Instructions</h2>
+<p>Please enter the words you see in the box, in order and separated by a space. Doing so helps prevent automated programs from abusing this service.</p>
+
+<p>If you are not sure what the words are, either enter your best guess or click the reload button next to the distorted words.</p>
+
+<p>Visually impaired users can click the audio button to hear a set of digits that can be entered instead of the visual challenge.</p>
index 34ce626e770693b043b46facf1f158d1a3c086f7..71f3c10e79981c5ef9b3dab4b2b3c038f5893d50 100644 (file)
  */
 class MoodleQuickForm_recaptcha extends HTML_QuickForm_input {
 
+    /**
+     * html for help button, if empty then no help
+     *
+     * @var string
+     */
+    var $_helpbutton='';
+
     /**
      * <code>
      * $form->addElement('textarea_counter', 'message', 'Message',
@@ -47,12 +54,13 @@ class MoodleQuickForm_recaptcha extends HTML_QuickForm_input {
             };
               </script>' . "\n";
 
-              
-        if (empty($_SESSION['recaptcha_error'])) {
-            $_SESSION['recaptcha_error'] = null;
+        $attributes = $this->getAttributes();
+        if (empty($attributes['error_message'])) {
+            $attributes['error_message'] = null;
+            $this->setAttributes($attributes);
         }
-        $error = $_SESSION['recaptcha_error'];
-        unset($_SESSION['recaptcha_error']);
+        $error = $attributes['error_message'];
+        unset($attributes['error_message']);
         
         $strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth');
         $strenterthewordsabove = get_string('enterthewordsabove', 'auth');
@@ -79,6 +87,53 @@ class MoodleQuickForm_recaptcha extends HTML_QuickForm_input {
 
         return $html . recaptcha_get_html($CFG->recaptchapublickey, $error);
     }
+    
+    /**
+     * set html for help button
+     *
+     * @access   public
+     * @param array $help array of arguments to make a help button
+     * @param string $function function name to call to get html
+     */
+    function setHelpButton($helpbuttonargs, $function='helpbutton'){
+        if (!is_array($helpbuttonargs)){
+            $helpbuttonargs=array($helpbuttonargs);
+        }else{
+            $helpbuttonargs=$helpbuttonargs;
+        }
+        //we do this to to return html instead of printing it
+        //without having to specify it in every call to make a button.
+        if ('helpbutton' == $function){
+            $defaultargs=array('', '', 'moodle', true, false, '', true);
+            $helpbuttonargs=$helpbuttonargs + $defaultargs ;
+        }
+        $this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
+    }
+    /**
+     * get html for help button
+     *
+     * @access   public
+     * @return  string html for help button
+     */
+    function getHelpButton(){
+        return $this->_helpbutton;
+    }
+
+    function verify($challenge_field, $response_field) {
+        global $CFG;
+        require_once $CFG->libdir . '/recaptchalib.php';
+        $response = recaptcha_check_answer($CFG->recaptchaprivatekey,
+                                           $_SERVER['REMOTE_ADDR'],
+                                           $challenge_field,
+                                           $response_field);
+        if (!$response->is_valid) {
+            $attributes = $this->getAttributes();
+            $attributes['error_message'] = $response->error;
+            $this->setAttributes($attributes);
+            return $response->error;
+        }
+        return true;
+    }
 }
 
 ?>
index aea6e0d75ed78ac38c2e53f34406a11074266764..b3b9bca649ecdf8478ba3711ad03da2080bf89aa 100644 (file)
@@ -65,30 +65,26 @@ function _recaptcha_qsencode ($data) {
  * @return array response
  */
 function _recaptcha_http_post($host, $path, $data, $port = 80) {
-
-        $req = _recaptcha_qsencode ($data);
-
-        $http_request  = "POST $path HTTP/1.0\r\n";
-        $http_request .= "Host: $host\r\n";
-        $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
-        $http_request .= "Content-Length: " . strlen($req) . "\r\n";
-        $http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
-        $http_request .= "\r\n";
-        $http_request .= $req;
-
-        $response = '';
-        if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
-                die ('Could not open socket');
+        global $CFG;
+        require_once $CFG->libdir . '/snoopy/Snoopy.class.inc';
+
+        $snoopy = new Snoopy();
+        $snoopy->proxy_host = $CFG->proxyhost;
+        $snoopy->proxy_port = $CFG->proxyport;
+        
+        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;
+        }
+        
+        $submit = $snoopy->submit('http://' . $host . $path, $data);
+        
+        if ($submit) {
+            return array(1 => $snoopy->results);
+        } else {
+            return false;
         }
-
-        fwrite($fs, $http_request);
-
-        while ( !feof($fs) )
-                $response .= fgets($fs, 1160); // One TCP-IP packet
-        fclose($fs);
-        $response = explode("\r\n\r\n", $response, 2);
-
-        return $response;
 }
 
 
index 5a5e3ac3e3a1f3be6e45a9fcd201ecd06ea9bf05..467188f1980bedcb527be7acf0f1a8f15de4915f 100644 (file)
@@ -64,7 +64,8 @@ class login_signup_form extends moodleform {
         }
         
         if (signup_captcha_enabled()) {
-            $mform->addElement('recaptcha', 'recaptcha_element', get_string('visualconfirmation'));
+            $mform->addElement('recaptcha', 'recaptcha_element', get_string('recaptcha', 'auth'));
+            $mform->setHelpButton('recaptcha_element', array('recaptcha', get_string('recaptcha', 'auth'))); 
         }
 
         profile_signup_fields($mform);
@@ -137,20 +138,15 @@ class login_signup_form extends moodleform {
         }
         
         if (signup_captcha_enabled()) {
-            require_once $CFG->libdir . '/recaptchalib.php';
-            $response = recaptcha_check_answer($CFG->recaptchaprivatekey,
-                                               $_SERVER['REMOTE_ADDR'],
-                                               $this->_form->_submitValues['recaptcha_challenge_field'],
-                                               $this->_form->_submitValues['recaptcha_response_field']);
-            if (!$response->is_valid) {
-                $_SESSION['recaptcha_error'] = $response->error;
-                $errors['recaptcha'] = $response->error;
+            $recaptcha_element = $this->_form->getElement('recaptcha_element'); 
+            $challenge_field = $this->_form->_submitValues['recaptcha_challenge_field'];
+            $response_field = $this->_form->_submitValues['recaptcha_response_field'];
+            if (true !== ($result = $recaptcha_element->verify($challenge_field, $response_field))) {
+                $errors['recaptcha'] = $result;
             }
         }
 
-        return $errors;
-
-
+        return $errors; 
     }
 }