From 588d39547ddf2c3c5b0b54f04efc4556e16fbec3 Mon Sep 17 00:00:00 2001 From: dongsheng Date: Tue, 15 Jul 2008 07:14:29 +0000 Subject: [PATCH] MDL-15349, add proxy support to curl class. --- repository/curl.class.php | 44 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/repository/curl.class.php b/repository/curl.class.php index 1214b51891..93b59f4fdc 100644 --- a/repository/curl.class.php +++ b/repository/curl.class.php @@ -20,6 +20,10 @@ class curl { private $options; public $cache = false; + public $proxy = false; + private $proxy_host = ''; + private $proxy_auth = ''; + private $proxy_type = ''; private $debug = false; private $cookie = false; public $version = '0.2 dev'; @@ -28,11 +32,14 @@ class curl { public $info; public $error; public function __construct($options = array()){ + global $CFG; if(!function_exists('curl_init')) { $this->error = 'cURL module must be enabled!'; trigger_error($this->error, E_USER_ERROR); return false; } + // the options of curl should be init here. + $this->resetopt(); if(!empty($options['debug'])) { $this->debug = true; } @@ -46,7 +53,33 @@ class curl { $this->cache = new repository_cache; } } - $this->resetopt(); + if(!empty($options['proxy'])) { + if(!empty($CFG->proxyhost)) { + if (empty($CFG->proxyport)) { + $this->proxy_host = $CFG->proxyhost; + } else { + $this->proxy_host = $CFG->proxyhost.':'.$CFG->proxyport; + } + if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) { + $this->proxy_auth = $CFG->proxyuser.':'.$CFG->proxypassword; + $this->setopt(array( + 'proxyauth'=> CURLAUTH_BASIC | CURLAUTH_NTLM, + 'proxyuserpwd'=>$this->proxy_auth)); + } + if (!empty($CFG->proxytype)) { + if($CFG->proxytype == 'SOCKS5') { + $this->proxy_type = CURLPROXY_SOCKS5; + } else { + $this->proxy_type = CURLPROXY_HTTP; + $this->setopt(array('httpproxytunnel'=>true)); + } + $this->setopt(array('proxytype'=>$this->proxy_type)); + } + } + if (!empty($this->proxy_host)) { + $this->proxy = array('proxy'=>$this->proxy_host); + } + } } public function resetopt(){ $this->options = array(); @@ -176,6 +209,10 @@ class curl { * */ protected function request($url, $options = array()){ + if (!preg_match('#^https?://#i', $url)) { + $this->error = 'Invalid protocol specified in url'; + return false; + } // Clean up $this->cleanopt(); // create curl instance @@ -203,6 +240,11 @@ class curl { )); } + // set proxy + if(!empty($this->proxy)) { + $this->setopt($this->proxy); + } + // set options foreach($this->options as $name => $val) { if (is_string($name)) { -- 2.39.5