Skip to content

Commit

Permalink
MDL-15349, add proxy support to curl class.
Browse files Browse the repository at this point in the history
  • Loading branch information
dongsheng committed Jul 15, 2008
1 parent 34c688c commit 588d395
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion repository/curl.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
}
Expand All @@ -46,7 +53,33 @@ public function __construct($options = array()){
$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();
Expand Down Expand Up @@ -176,6 +209,10 @@ private function formatHeader($ch, $header)
*
*/
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
Expand Down Expand Up @@ -203,6 +240,11 @@ protected function request($url, $options = array()){
));
}

// set proxy
if(!empty($this->proxy)) {
$this->setopt($this->proxy);
}

// set options
foreach($this->options as $name => $val) {
if (is_string($name)) {
Expand Down

0 comments on commit 588d395

Please sign in to comment.