]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-8018 testing and bugfixing of curl emulation library + added download_file_conten...
authorskodak <skodak>
Tue, 2 Jan 2007 22:44:59 +0000 (22:44 +0000)
committerskodak <skodak>
Tue, 2 Jan 2007 22:44:59 +0000 (22:44 +0000)
lib/filelib.php
lib/libcurlemu/class_HTTPRetriever.php
lib/libcurlemu/libcurlexternal.inc.php
lib/libcurlemu/libcurlnative.inc.php
lib/libcurlemu/readme_moodle.txt

index d56a08b8005797cae85c805516a248d276e960c5..9e780a17db5a02c0a95e41890a4836bbe98f8e20 100644 (file)
@@ -1,7 +1,34 @@
 <?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
index 4b52c2aee1338062ec1a038d264eb5da372dff5c..99037634c09a8611c0a8cc88a7450c9555d72661 100755 (executable)
@@ -741,7 +741,7 @@ class HTTPRetriever {
                                                                // 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;
index f7284f939b628f94074dd2f4149a6a7140825a65..0ee5437de0d49bc1dbdf82efa4fb166bcf8280ab 100755 (executable)
@@ -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]);
index 471a8c0b11ddba0e9326f1780b944a2948582008..1d6962c96078a010db3c700c4ad6db986ee8bec6 100755 (executable)
@@ -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
index 046eb2cbe4af21b929c11d41cfe6882a48325192..5006b8d7d07488406d6bd31d8edb4b2dc0b03616 100644 (file)
@@ -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