$string['getanothercaptcha'] = 'Get another CAPTCHA';
$string['getanaudiocaptcha'] = 'Get an audio CAPTCHA';
$string['getanimagecaptcha'] = 'Get an image CAPTCHA';
+$string['recaptcha'] = 'reCAPTCHA';
?>
--- /dev/null
+<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>
*/
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',
};
</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');
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;
+ }
}
?>
* @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;
}
}
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);
}
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;
}
}