* @param int $connecttimeout timeout for connection to server; this is the timeout that
* usually happens if the remote server is completely down (default 20 seconds);
* may not work when using proxy
+ * @param bool $skipcertverify If true, the peer's SSL certificate will not be checked. Only use this when already in a trusted location.
* @return mixed false if request failed or content of the file as string if ok.
*/
-function download_file_content($url, $headers=null, $postdata=null, $fullresponse=false, $timeout=300, $connecttimeout=20) {
+function download_file_content($url, $headers=null, $postdata=null, $fullresponse=false, $timeout=300, $connecttimeout=20, $skipcertverify=false) {
global $CFG;
// some extra security
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers2);
}
+
+ if ($skipcertverify) {
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+ }
+
// use POST if requested
if (is_array($postdata)) {
foreach ($postdata as $k=>$v) {
* @var string
*/
var $_helpbutton='';
+
+ var $_https=false;
/**
* <code>
* array('cols'=>60, 'rows'=>10), 160);
* </code>
*/
- function HTML_QuickForm_recaptcha($elementName = null, $elementLabel = null, $attributes = null) {
+ function MoodleQuickForm_recaptcha($elementName = null, $elementLabel = null, $attributes = null) {
parent::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
$this->_type = 'recaptcha';
+ if (!empty($attributes['https'])) {
+ $this->_https = $attributes['https'];
+ }
}
/**
<div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type(\'image\')">' . $strgetanimagecaptcha . '</a></div>
</div>';
- return $html . recaptcha_get_html($CFG->recaptchapublickey, $error);
+ return $html . recaptcha_get_html($CFG->recaptchapublickey, $error, $this->_https);
}
/**
$response = recaptcha_check_answer($CFG->recaptchaprivatekey,
$_SERVER['REMOTE_ADDR'],
$challenge_field,
- $response_field);
+ $response_field,
+ $this->_https);
if (!$response->is_valid) {
$attributes = $this->getAttributes();
$attributes['error_message'] = $response->error;
* @param int port
* @return array response
*/
-function _recaptcha_http_post($host, $path, $data, $port = 80) {
+function _recaptcha_http_post($host, $path, $data, $port = 80, $https=false) {
global $CFG;
+ $protocol = 'http';
+ if ($https) {
+ $protocol = 'https';
+ }
+
require_once $CFG->libdir . '/filelib.php';
$req = _recaptcha_qsencode ($data);
$headers['Content-Length'] = strlen($req);
$headers['User-Agent'] = 'reCAPTCHA/PHP';
- $results = download_file_content('http://' . $host . $path, $headers, $data);
+ $results = download_file_content("$protocol://" . $host . $path, $headers, $data, false, 300, 20, true);
if ($results) {
return array(1 => $results);
}
require_once $CFG->libdir . '/filelib.php';
- $html = download_file_content($server . '/noscript?k=' . $pubkey . $errorpart);
+ $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;
* @param string $response
* @return ReCaptchaResponse
*/
-function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response)
+function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response, $https=false)
{
if ($privkey == null || $privkey == '') {
die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>");
return $recaptcha_response;
}
- $response = _recaptcha_http_post (RECAPTCHA_VERIFY_SERVER, "/verify",
- array (
- 'privatekey' => $privkey,
- 'remoteip' => $remoteip,
- 'challenge' => $challenge,
- 'response' => $response
- )
- );
+ $response = _recaptcha_http_post(RECAPTCHA_VERIFY_SERVER, "/verify",
+ array (
+ 'privatekey' => $privkey,
+ 'remoteip' => $remoteip,
+ 'challenge' => $challenge,
+ 'response' => $response
+ ),
+ $https
+ );
$answers = explode ("\n", $response [1]);
$recaptcha_response = new ReCaptchaResponse();