diff --git a/lib/recaptchalib.php b/lib/recaptchalib.php index 82fb9ba5579..79eb4a3fc61 100644 --- a/lib/recaptchalib.php +++ b/lib/recaptchalib.php @@ -96,6 +96,28 @@ function _recaptcha_http_post($host, $path, $data, $port = 80, $https=false) { } } +/** + * Return the recaptcha challenge and image and javascript urls + * + * @param string $server server url + * @param string $pubkey public key + * @param string $errorpart error part to append + * @return array the challenge hash, image and javascript url + * @since Moodle 3.2 + */ +function recaptcha_get_challenge_hash_and_urls($server, $pubkey, $errorpart = '') { + global $CFG; + + require_once($CFG->libdir . '/filelib.php'); + $html = download_file_content($server . '/noscript?k=' . $pubkey . $errorpart, null, null, false, 300, 20, true); + preg_match('/image\?c\=([A-Za-z0-9\-\_]*)\"/', $html, $matches); + $challengehash = $matches[1]; + $imageurl = $server . '/image?c=' . $challengehash; + + $jsurl = $server . '/challenge?k=' . $pubkey . $errorpart; + + return array($challengehash, $imageurl, $jsurl); +} /** @@ -111,7 +133,7 @@ function _recaptcha_http_post($host, $path, $data, $port = 80, $https=false) { * @return string - The HTML to be embedded in the user's form. */ function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false) { - global $CFG, $PAGE; + global $PAGE; $recaptchatype = optional_param('recaptcha', 'image', PARAM_TEXT); @@ -127,14 +149,10 @@ function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false) { $errorpart = ""; if ($error) { - $errorpart = "&error=" . $error; + $errorpart = "&error=" . $error; } - require_once $CFG->libdir . '/filelib.php'; - $html = download_file_content($server . '/noscript?k=' . $pubkey . $errorpart, null, null, false, 300, 20, true); - preg_match('/image\?c\=([A-Za-z0-9\-\_]*)\"/', $html, $matches); - $challenge_hash = $matches[1]; - $image_url = $server . '/image?c=' . $challenge_hash; + list($challengehash, $imageurl, $jsurl) = recaptcha_get_challenge_hash_and_urls($server, $pubkey, $errorpart); $strincorrectpleasetryagain = get_string('incorrectpleasetryagain', 'auth'); $strenterthewordsabove = get_string('enterthewordsabove', 'auth'); @@ -143,10 +161,10 @@ function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false) { $strgetanaudiocaptcha = get_string('getanaudiocaptcha', 'auth'); $strgetanimagecaptcha = get_string('getanimagecaptcha', 'auth'); - $return = html_writer::script('', $server . '/challenge?k=' . $pubkey . $errorpart); + $return = html_writer::script('', $jsurl); $return .= '