MDL-13796 Committing new recaptcha element

This commit is contained in:
nicolasconnault 2008-03-13 15:45:16 +00:00
parent 6c5a2108b5
commit 9b5f87d27e
5 changed files with 84 additions and 23 deletions

View file

@ -207,6 +207,15 @@ class auth_plugin_email extends auth_plugin_base {
return true; 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');
}
} }
?> ?>

View file

@ -378,8 +378,8 @@ $string['unlocked'] = 'Unlocked';
$string['unlockedifempty'] = 'Unlocked if empty'; $string['unlockedifempty'] = 'Unlocked if empty';
$string['locked'] = 'Locked'; $string['locked'] = 'Locked';
$string['incorrectpleasetryagain'] = 'Incorrect. Please try again.'; $string['incorrectpleasetryagain'] = 'Incorrect. Please try again.';
$string['enterthewordsabove'] = 'Enter the words above:'; $string['enterthewordsabove'] = 'Enter the words above';
$string['enterthenumbersyouhear'] = 'Enter the numbers you hear:'; $string['enterthenumbersyouhear'] = 'Enter the numbers you hear';
$string['getanothercaptcha'] = 'Get another CAPTCHA'; $string['getanothercaptcha'] = 'Get another CAPTCHA';
$string['getanaudiocaptcha'] = 'Get an audio CAPTCHA'; $string['getanaudiocaptcha'] = 'Get an audio CAPTCHA';
$string['getanimagecaptcha'] = 'Get an image CAPTCHA'; $string['getanimagecaptcha'] = 'Get an image CAPTCHA';

View file

@ -342,6 +342,17 @@ class auth_plugin_base {
} }
return $authdescription; 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;
}
} }
?> ?>

View file

@ -75,11 +75,11 @@ class MoodleQuickForm_recaptcha extends HTML_QuickForm_input {
<div id="recaptcha_image"></div> <div id="recaptcha_image"></div>
<div class="recaptcha_only_if_incorrect_sol" style="color:red">' . $strincorrectpleasetryagain . '</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_image"><label for="recaptcha_response_field">' . $strenterthewordsabove . '</label></span>
<span class="recaptcha_only_if_audio">' . $strenterthenumbersyouhear . '</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="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><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_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> <div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type(\'image\')">' . $strgetanimagecaptcha . '</a></div>

View file

@ -66,22 +66,20 @@ function _recaptcha_qsencode ($data) {
*/ */
function _recaptcha_http_post($host, $path, $data, $port = 80) { function _recaptcha_http_post($host, $path, $data, $port = 80) {
global $CFG; global $CFG;
require_once $CFG->libdir . '/snoopy/Snoopy.class.inc'; require_once $CFG->libdir . '/filelib.php';
$snoopy = new Snoopy(); $req = _recaptcha_qsencode ($data);
$snoopy->proxy_host = $CFG->proxyhost;
$snoopy->proxy_port = $CFG->proxyport;
if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) { $headers = array();
// this will probably fail, but let's try it anyway $headers['Host'] = $host;
$snoopy->proxy_user = $CFG->proxyuser; $headers['Content-Type'] = 'application/x-www-form-urlencoded';
$snoopy->proxy_password = $CFG->proxypassword; $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) { if ($results) {
return array(1 => $snoopy->results); return array(1 => $results);
} else { } else {
return false; 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. * @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 == '') { 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>"); 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) { if ($error) {
$errorpart = "&amp;error=" . $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> <noscript>
<iframe src="'. $server . '/noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br> <div id="recaptcha_widget_noscript">
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea> <div id="recaptcha_image_noscript"><img src="' . $image_url . '" alt="reCAPTCHA"/></div>';
<input type="hidden" name="recaptcha_response_field" value="manual_challenge">
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>'; </noscript>';
return $return;
} }