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

@ -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');
}
}

View 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';

View 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;
}
}
?>

View 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>

View 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;
}