mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-15349, add proxy support to curl class.
This commit is contained in:
parent
34c688c959
commit
588d39547d
1 changed files with 43 additions and 1 deletions
|
@ -20,6 +20,10 @@
|
||||||
class curl {
|
class curl {
|
||||||
private $options;
|
private $options;
|
||||||
public $cache = false;
|
public $cache = false;
|
||||||
|
public $proxy = false;
|
||||||
|
private $proxy_host = '';
|
||||||
|
private $proxy_auth = '';
|
||||||
|
private $proxy_type = '';
|
||||||
private $debug = false;
|
private $debug = false;
|
||||||
private $cookie = false;
|
private $cookie = false;
|
||||||
public $version = '0.2 dev';
|
public $version = '0.2 dev';
|
||||||
|
@ -28,11 +32,14 @@ class curl {
|
||||||
public $info;
|
public $info;
|
||||||
public $error;
|
public $error;
|
||||||
public function __construct($options = array()){
|
public function __construct($options = array()){
|
||||||
|
global $CFG;
|
||||||
if(!function_exists('curl_init')) {
|
if(!function_exists('curl_init')) {
|
||||||
$this->error = 'cURL module must be enabled!';
|
$this->error = 'cURL module must be enabled!';
|
||||||
trigger_error($this->error, E_USER_ERROR);
|
trigger_error($this->error, E_USER_ERROR);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// the options of curl should be init here.
|
||||||
|
$this->resetopt();
|
||||||
if(!empty($options['debug'])) {
|
if(!empty($options['debug'])) {
|
||||||
$this->debug = true;
|
$this->debug = true;
|
||||||
}
|
}
|
||||||
|
@ -46,7 +53,33 @@ class curl {
|
||||||
$this->cache = new repository_cache;
|
$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(){
|
public function resetopt(){
|
||||||
$this->options = array();
|
$this->options = array();
|
||||||
|
@ -176,6 +209,10 @@ class curl {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
protected function request($url, $options = array()){
|
protected function request($url, $options = array()){
|
||||||
|
if (!preg_match('#^https?://#i', $url)) {
|
||||||
|
$this->error = 'Invalid protocol specified in url';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
// Clean up
|
// Clean up
|
||||||
$this->cleanopt();
|
$this->cleanopt();
|
||||||
// create curl instance
|
// create curl instance
|
||||||
|
@ -203,6 +240,11 @@ class curl {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set proxy
|
||||||
|
if(!empty($this->proxy)) {
|
||||||
|
$this->setopt($this->proxy);
|
||||||
|
}
|
||||||
|
|
||||||
// set options
|
// set options
|
||||||
foreach($this->options as $name => $val) {
|
foreach($this->options as $name => $val) {
|
||||||
if (is_string($name)) {
|
if (is_string($name)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue