From 8ee88311d1aeb576042c1e10c36d7ce710491bbc Mon Sep 17 00:00:00 2001 From: skodak Date: Tue, 2 Jan 2007 22:44:59 +0000 Subject: [PATCH] MDL-8018 testing and bugfixing of curl emulation library + added download_file_content() into filelib.php --- lib/filelib.php | 27 +++++++++++++++++++++++ lib/libcurlemu/class_HTTPRetriever.php | 2 +- lib/libcurlemu/libcurlexternal.inc.php | 26 ++++++++++++++-------- lib/libcurlemu/libcurlnative.inc.php | 30 +++++++++++++++++--------- lib/libcurlemu/readme_moodle.txt | 8 +++++++ 5 files changed, 73 insertions(+), 20 deletions(-) diff --git a/lib/filelib.php b/lib/filelib.php index d56a08b800..9e780a17db 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -1,7 +1,34 @@ 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 diff --git a/lib/libcurlemu/class_HTTPRetriever.php b/lib/libcurlemu/class_HTTPRetriever.php index 4b52c2aee1..99037634c0 100755 --- a/lib/libcurlemu/class_HTTPRetriever.php +++ b/lib/libcurlemu/class_HTTPRetriever.php @@ -741,7 +741,7 @@ class HTTPRetriever { // do not store expired cookies; if one exists, unset it if ( isset($cookie['expires']) && ($cookie['expires']response_cookies[ $name ][ $cookie['path'] ]); - return false; + continue;//moodlefix } $this->response_cookies[ $name ][ $cookie['path'] ] = $cookie; diff --git a/lib/libcurlemu/libcurlexternal.inc.php b/lib/libcurlemu/libcurlexternal.inc.php index f7284f939b..0ee5437de0 100755 --- a/lib/libcurlemu/libcurlexternal.inc.php +++ b/lib/libcurlemu/libcurlexternal.inc.php @@ -162,6 +162,7 @@ define('CURLOPT_SSLENGINE_DEFAULT',90); 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); @@ -198,9 +199,14 @@ function _curlopt_name($curlopt) { // 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; } @@ -208,9 +214,7 @@ function curl_init($url=false) { 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) { @@ -467,17 +471,21 @@ function curl_exec($ch) { // 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 = ""; @@ -512,7 +520,7 @@ function curl_exec($ch) { 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; @@ -581,7 +589,7 @@ function curl_close($ch) { $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]); diff --git a/lib/libcurlemu/libcurlnative.inc.php b/lib/libcurlemu/libcurlnative.inc.php index 471a8c0b11..1d6962c960 100755 --- a/lib/libcurlemu/libcurlnative.inc.php +++ b/lib/libcurlemu/libcurlnative.inc.php @@ -164,6 +164,7 @@ define('CURLOPT_SSLENGINE_DEFAULT',90); 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); @@ -199,11 +200,17 @@ function _curlopt_name($curlopt) { } // 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; } @@ -211,9 +218,7 @@ function curl_init() { 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"]; @@ -337,16 +342,17 @@ function curl_exec($ch) { $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); @@ -360,6 +366,10 @@ function curl_exec($ch) { if ($opt["fail_on_error"]) { if ($http->result_code>300) die; } + + if ($res === false) {//moodlefix + return false;//moodlefix + }//moodlefix $opt["stats"] = $http->stats; @@ -374,7 +384,7 @@ function curl_exec($ch) { 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 diff --git a/lib/libcurlemu/readme_moodle.txt b/lib/libcurlemu/readme_moodle.txt index 046eb2cbe4..5006b8d7d0 100644 --- a/lib/libcurlemu/readme_moodle.txt +++ b/lib/libcurlemu/readme_moodle.txt @@ -3,5 +3,13 @@ Description of libcurlemu v1.0.3 import into Moodle 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 -- 2.39.5