123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- class PEAR_Proxy
- {
- var $config = null;
-
- var $proxy_host;
-
- var $proxy_port;
-
- var $proxy_user;
-
- var $proxy_pass;
-
- var $proxy_schema;
- function __construct($config = null)
- {
- $this->config = $config;
- $this->_parseProxyInfo();
- }
-
- function _parseProxyInfo()
- {
- $this->proxy_host = $this->proxy_port = $this->proxy_user = $this->proxy_pass = '';
- if ($this->config->get('http_proxy')&&
- $proxy = parse_url($this->config->get('http_proxy'))
- ) {
- $this->proxy_host = isset($proxy['host']) ? $proxy['host'] : null;
- $this->proxy_port = isset($proxy['port']) ? $proxy['port'] : 8080;
- $this->proxy_user = isset($proxy['user']) ? urldecode($proxy['user']) : null;
- $this->proxy_pass = isset($proxy['pass']) ? urldecode($proxy['pass']) : null;
- $this->proxy_schema = (isset($proxy['scheme']) && $proxy['scheme'] == 'https') ? 'https' : 'http';
- }
- }
-
- function _httpConnect($fp, $host, $port)
- {
- fwrite($fp, "CONNECT $host:$port HTTP/1.1\r\n");
- fwrite($fp, "Host: $host:$port\r\n");
- if ($this->getProxyAuth()) {
- fwrite($fp, 'Proxy-Authorization: Basic ' . $this->getProxyAuth() . "\r\n");
- }
- fwrite($fp, "\r\n");
- while ($line = trim(fgets($fp, 1024))) {
- if (preg_match('|^HTTP/1.[01] ([0-9]{3}) |', $line, $matches)) {
- $code = (int)$matches[1];
-
- if ($code < 200 || $code >= 300) {
- return PEAR::raiseError("Establishing a CONNECT tunnel through proxy failed with response code $code");
- }
- }
- }
-
-
- $crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT;
- if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
- $crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
- $crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
- }
-
-
- stream_context_set_option($fp, 'ssl', 'peer_name', $host);
-
-
-
-
- stream_set_blocking ($fp, true);
- $crypto_res = stream_socket_enable_crypto($fp, true, $crypto_method);
- if (!$crypto_res) {
- return PEAR::raiseError("Could not establish SSL connection through proxy $proxy_host:$proxy_port: $crypto_res");
- }
- return true;
- }
-
- function getProxyAuth()
- {
- if ($this->isProxyConfigured() && $this->proxy_user != '') {
- return base64_encode($this->proxy_user . ':' . $this->proxy_pass);
- }
- return null;
- }
- function getProxyUser()
- {
- return $this->proxy_user;
- }
-
- function isProxyConfigured()
- {
- return $this->proxy_host != '';
- }
-
- function openSocket($host, $port, $secure = false)
- {
- if ($this->isProxyConfigured()) {
- $fp = @fsockopen(
- $this->proxy_host, $this->proxy_port,
- $errno, $errstr, 15
- );
- if (!$fp) {
- return PEAR::raiseError("Connection to `$proxy_host:$proxy_port' failed: $errstr", -9276);
- }
-
- if ($secure) {
- $res = $this->_httpConnect($fp, $host, $port);
- if (PEAR::isError($res)) {
- return $res;
- }
- }
- } else {
- if ($secure) {
- $host = 'ssl://' . $host;
- }
- $fp = @fsockopen($host, $port, $errno, $errstr);
- if (!$fp) {
- return PEAR::raiseError("Connection to `$host:$port' failed: $errstr", $errno);
- }
- }
- return $fp;
- }
- }
|