Skip to content
Browse files

MDL-13796 Committing new recaptcha element. Merging from HEAD

  • Loading branch information...
1 parent c147575 commit 7ee7cb758a4e39a810543d55777bcb4be8cd6291 nicolasconnault committed Mar 13, 2008
Showing with 84 additions and 23 deletions.
  1. +9 −0 auth/email/auth.php
  2. +2 −2 lang/en_utf8/auth.php
  3. +11 −0 lib/authlib.php
  4. +3 −3 lib/form/recaptcha.php
  5. +59 −18 lib/recaptchalib.php
View
9 auth/email/auth.php
@@ -206,6 +206,15 @@ function process_config($config) {
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.
+ * @abstract Implement in child classes
+ * @return bool
+ */
+ function is_captcha_enabled() {
+ return false;
+ }
}
View
4 lang/en_utf8/auth.php
@@ -378,8 +378,8 @@
$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
11 lib/authlib.php
@@ -342,6 +342,17 @@ function get_description() {
}
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
6 lib/form/recaptcha.php
@@ -75,11 +75,11 @@ function toHtml() {
<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
77 lib/recaptchalib.php
@@ -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;
}

0 comments on commit 7ee7cb7

Please sign in to comment.
Something went wrong with that request. Please try again.