From 7c572b8c90cea830740a6ea12f10568dc36b6567 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Tue, 11 Mar 2008 10:34:50 +0000 Subject: [PATCH] MDL-13796 Added help button and help file, used Snoopy in recaptchalib for proxy support, fixed lang strings, refactored validation code, used element property to pass error string around. --- lang/en_utf8/auth.php | 1 + lang/en_utf8/help/recaptcha.html | 10 +++++ lib/form/recaptcha.php | 65 +++++++++++++++++++++++++++++--- lib/recaptchalib.php | 42 ++++++++++----------- login/signup_form.php | 20 ++++------ 5 files changed, 98 insertions(+), 40 deletions(-) create mode 100644 lang/en_utf8/help/recaptcha.html diff --git a/lang/en_utf8/auth.php b/lang/en_utf8/auth.php index bf0c32399d..5081639210 100644 --- a/lang/en_utf8/auth.php +++ b/lang/en_utf8/auth.php @@ -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 index 0000000000..32b28807e0 --- /dev/null +++ b/lang/en_utf8/help/recaptcha.html @@ -0,0 +1,10 @@ +

reCAPTCHA

+

Description

+

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.

+ +

Instructions

+

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.

+ +

If you are not sure what the words are, either enter your best guess or click the reload button next to the distorted words.

+ +

Visually impaired users can click the audio button to hear a set of digits that can be entered instead of the visual challenge.

diff --git a/lib/form/recaptcha.php b/lib/form/recaptcha.php index 34ce626e77..71f3c10e79 100644 --- a/lib/form/recaptcha.php +++ b/lib/form/recaptcha.php @@ -17,6 +17,13 @@ */ class MoodleQuickForm_recaptcha extends HTML_QuickForm_input { + /** + * html for help button, if empty then no help + * + * @var string + */ + var $_helpbutton=''; + /** * * $form->addElement('textarea_counter', 'message', 'Message', @@ -47,12 +54,13 @@ class MoodleQuickForm_recaptcha extends HTML_QuickForm_input { }; ' . "\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; + } } ?> diff --git a/lib/recaptchalib.php b/lib/recaptchalib.php index aea6e0d75e..b3b9bca649 100644 --- a/lib/recaptchalib.php +++ b/lib/recaptchalib.php @@ -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; } diff --git a/login/signup_form.php b/login/signup_form.php index 5a5e3ac3e3..467188f198 100644 --- a/login/signup_form.php +++ b/login/signup_form.php @@ -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; } } -- 2.39.5