<?php //$Id$
+require_once($CFG->libdir.'/libcurlemu/libcurlemu.inc.php'); // might be moved to setup.php later
+
define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7'); //unique string constant
+/**
+ * Fetches content of file from Internet (using proxy if defined).
+ *
+ * @return mixed false if request failed or content of the file as string if ok.
+ */
+function download_file_content($url) {
+ $ch = curl_init($url);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_HEADER, false);
+ if (!empty($CFG->proxyhost)) {
+ curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
+ if (empty($CFG->proxyport)) {
+ curl_setopt($ch, CURLOPT_PROXY, $CFG->proxy);
+ } else {
+ curl_setopt($ch, CURLOPT_PROXY, $CFG->proxy.':'.$CFG->proxyport);
+ }
+ if(!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
+ curl_setopt($ch, CURLOPT_PROXYUSERPWD, $CFG->proxyuser.':'.$CFG->proxypassword);
+ }
+ }
+ $result = curl_exec($ch);
+ curl_close($ch);
+ return $result;
+}
+
/**
* @return List of information about file types based on extensions.
* Associative array of extension (lower-case) to associative array
// do not store expired cookies; if one exists, unset it
if ( isset($cookie['expires']) && ($cookie['expires']<time()) ) {
unset($this->response_cookies[ $name ][ $cookie['path'] ]);
- return false;
+ continue;//moodlefix
}
$this->response_cookies[ $name ][ $cookie['path'] ] = $cookie;
define('CURLOPT_DNS_USE_GLOBAL_CACHE',91);
define('CURLOPT_DNS_CACHE_TIMEOUT',92);
define('CURLOPT_PREQUOTE',10093);
+define('CURLOPT_RETURNTRANSFER', 19913);//moodlefix
define('CURLINFO_EFFECTIVE_URL',1);
define('CURLINFO_HTTP_CODE',2);
// Initialize a CURL emulation session
function curl_init($url=false) {
+ if(!isset($GLOBALS["_CURLEXT_OPT"])) {//moodlefix
+ $GLOBALS["_CURLEXT_OPT"] = array();//moodlefix
+ $GLOBALS["_CURLEXT_OPT"]["index"] = 0;//moodlefix
+ }//moodlefix
$i = $GLOBALS["_CURLEXT_OPT"]["index"]++;
- $GLOBALS["_CURLEXT_OPT"][$i] = array("url"=>$url);
-
+ $GLOBALS["_CURLEXT_OPT"][$i] = array("url"=>$url, "verbose"=>false, "fail_on_error"=>false);//moodlefix
+ $GLOBALS["_CURLEXT_OPT"][$i]["args"] = array();//moodlefix
+ $GLOBALS["_CURLEXT_OPT"][$i]["settings"] = array();//moodlefix
return $i;
}
function curl_setopt($ch,$option,$value) {
$opt = &$GLOBALS["_CURLEXT_OPT"][$ch];
- if (!$opt["args"]) $opt["args"] = array();
$args = &$opt["args"];
- if (!$opt["settings"]) $opt["settings"] = array();
$settings = &$opt["settings"];
switch($option) {
// if the CURLOPT_NOBODY option was specified (to remove the body from the output),
// but an output file handle was set, we need to tell CURL to return the body so
// that we can write it to the output handle and strip it from the output
- if ($opt["settings"]["head"] && $opt["output_handle"]) {
+ if (!empty($opt["settings"]["head"]) && $opt["output_handle"]) {//moodlefix
unset($opt["settings"]["head"]);
$strip_body = true;
- }
+ } else {
+ $strip_body = false;
+ }
// if the CURLOPT_HEADER option was NOT specified, but a header file handle was
// specified, we again need to tell CURL to return the headers so we can write
// them, then strip them from the output
if (!isset($opt["settings"]["include"]) && isset($opt["header_handle"])) {
$opt["settings"]["include"] = true;
$strip_headers = true;
- }
+ } else {
+ $strip_headers = false;//moodlefix
+ }
// build the CURL argument list
$arguments = "";
if ($ret) $opt["error"] = "CURL error #$ret";
// die if CURLOPT_FAILONERROR is set and the HTTP result code is greater than 300
- if ($opt["fail_on_error"]) {
+ if ($opt["fail_on_error"]) {//moodlefix
if (preg_match("/^HTTP\/1.[0-9]+ ([0-9]{3}) /",$output[0],$matches)) {
$resultcode = (int) $matches[1];
if ($resultcode>300) die;
$settings = &$opt["settings"];
// if the user used CURLOPT_INFILE to specify a file to upload, remove the
// temporary file created for the CURL binary
- if ($settings["upload-file"]["value"] && file_exists($settings["upload-file"]["value"])) unlink($settings["upload-file"]["value"]);
+ if (!empty($settings["upload-file"]["value"]) && file_exists($settings["upload-file"]["value"])) unlink($settings["upload-file"]["value"]);//moodlefix
}
unset($GLOBALS["_CURLEXT_OPT"][$ch]);
define('CURLOPT_DNS_USE_GLOBAL_CACHE',91);
define('CURLOPT_DNS_CACHE_TIMEOUT',92);
define('CURLOPT_PREQUOTE',10093);
+define('CURLOPT_RETURNTRANSFER', 19913);//moodlefix
define('CURLINFO_EFFECTIVE_URL',1);
define('CURLINFO_HTTP_CODE',2);
}
// Initialize a CURL emulation session
-function curl_init() {
+function curl_init($url=false) {
+ if(!isset($GLOBALS["_CURLNAT_OPT"])) {//moodlefix
+ $GLOBALS["_CURLNAT_OPT"] = array();//moodlefix
+ $GLOBALS["_CURLNAT_OPT"]["index"] = 0;//moodlefix
+ }//moodlefix
$i = $GLOBALS["_CURLNAT_OPT"]["index"]++;
- $GLOBALS["_CURLNAT_OPT"][$i] = array();
- $GLOBALS["_CURLNAT_OPT"][$i]["http"] = &new HTTPRetriever();
+ $GLOBALS["_CURLNAT_OPT"][$i] = array("url"=>$url, "fail_on_error"=>false);//moodlefix
+ $GLOBALS["_CURLNAT_OPT"][$i]["http"] = &new HTTPRetriever();
$GLOBALS["_CURLNAT_OPT"][$i]["include_body"] = true;
+ $GLOBALS["_CURLNAT_OPT"][$i]["args"] = array();//moodlefix
+ $GLOBALS["_CURLNAT_OPT"][$i]["settings"] = array();//moodlefix
return $i;
}
function curl_setopt($ch,$option,$value) {
$opt = &$GLOBALS["_CURLNAT_OPT"][$ch];
- if (!$opt["args"]) $opt["args"] = array();
$args = &$opt["args"];
- if (!$opt["settings"]) $opt["settings"] = array();
$settings = &$opt["settings"];
$http = &$opt["http"];
$http = &$opt["http"];
$http->disable_curl = true; // avoid problems with recursion, since we *ARE* CURL
-
+ $http->error = false;//moodlefix
+
// set time limits if requested
- if ($opt["max-time"]) {
+ if (!empty($opt["max-time"])) {//moodlefix
$http->connect_timeout = $opt["max-time"];
$http->max_time = $opt["max-time"];
}
- if ($opt["post"]) {
+ if (!empty($opt["post"])) {//moodlefix
$res = $http->post($url,$opt["postdata"]);
- } elseif ($opt["method"]) {
+ } elseif (!empty($opt["method"])) {
$res = $http->custom($opt["method"],$url,$opt["postdata"]);
} else {
$res = $http->get($url);
if ($opt["fail_on_error"]) {
if ($http->result_code>300) die;
}
+
+ if ($res === false) {//moodlefix
+ return false;//moodlefix
+ }//moodlefix
$opt["stats"] = $http->stats;
if (isset($opt["header_handle"])) {
fwrite($opt["header_handle"],$headers);
}
-
+
$output = ($opt["include_headers"] ? $headers."\r\n" : "") . ($opt["include_body"] ? $http->response : "");
// if a file handle was provided for output, write the output to it
Changes:
* example.php - removed
* original HTTPRetriever v1.1.5 replaced by standalone package v1.1.9
+ * fixed many warnings and cookie problem in HTTPRetriever - marked by //moodlefix (to be reported later upstream after some more testing)
+
+Note to developers:
+ 1/ if you want to test binary curl, disable curl in PHP config
+ 2/ if you want to test php emulation, do 1/ and define("CURL_PATH","/usr/bin/curlxxxxxxx"); in config.php
+
+TODO:
+ * test the proxy function and add admin tree settings for $CFG->proxyuser and $CFG->proxypassword
$Id$
\ No newline at end of file