Bearsampp 2026.9.23
Loading...
Searching...
No Matches
Proxy.php
Go to the documentation of this file.
1<?php
14
16{
17 var $config = null;
18
39
40 function __construct($config = null)
41 {
42 $this->config = $config;
43 $this->_parseProxyInfo();
44 }
45
49 function _parseProxyInfo()
50 {
51 $this->proxy_host = $this->proxy_port = $this->proxy_user = $this->proxy_pass = '';
52 if ($this->config->get('http_proxy')&&
53 $proxy = parse_url($this->config->get('http_proxy'))
54 ) {
55 $this->proxy_host = isset($proxy['host']) ? $proxy['host'] : null;
56
57 $this->proxy_port = isset($proxy['port']) ? $proxy['port'] : 8080;
58 $this->proxy_user = isset($proxy['user']) ? urldecode($proxy['user']) : null;
59 $this->proxy_pass = isset($proxy['pass']) ? urldecode($proxy['pass']) : null;
60 $this->proxy_schema = (isset($proxy['scheme']) && $proxy['scheme'] == 'https') ? 'https' : 'http';
61 }
62 }
63
67 function _httpConnect($fp, $host, $port)
68 {
69 fwrite($fp, "CONNECT $host:$port HTTP/1.1\r\n");
70 fwrite($fp, "Host: $host:$port\r\n");
71 if ($this->getProxyAuth()) {
72 fwrite($fp, 'Proxy-Authorization: Basic ' . $this->getProxyAuth() . "\r\n");
73 }
74 fwrite($fp, "\r\n");
75
76 while ($line = trim(fgets($fp, 1024))) {
77 if (preg_match('|^HTTP/1.[01] ([0-9]{3}) |', $line, $matches)) {
78 $code = (int)$matches[1];
79
80 /* as per RFC 2817 */
81 if ($code < 200 || $code >= 300) {
82 return PEAR::raiseError("Establishing a CONNECT tunnel through proxy failed with response code $code");
83 }
84 }
85 }
86
87 // connection was successful -- establish SSL through
88 // the tunnel
89 $crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT;
90
91 if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
92 $crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
93 $crypto_method |= STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
94 }
95
96 // set the correct hostname for working hostname
97 // verification
98 stream_context_set_option($fp, 'ssl', 'peer_name', $host);
99
100 // blocking socket needed for
101 // stream_socket_enable_crypto()
102 // see
103 // <http://php.net/manual/en/function.stream-socket-enable-crypto.php>
104 stream_set_blocking ($fp, true);
105 $crypto_res = stream_socket_enable_crypto($fp, true, $crypto_method);
106 if (!$crypto_res) {
107 return PEAR::raiseError("Could not establish SSL connection through proxy: $crypto_res");
108 }
109
110 return true;
111 }
112
120 function getProxyAuth()
121 {
122 if ($this->isProxyConfigured() && $this->proxy_user != '') {
123 return base64_encode($this->proxy_user . ':' . $this->proxy_pass);
124 }
125 return null;
126 }
127
128 function getProxyUser()
129 {
130 return $this->proxy_user;
131 }
132
141 {
142 return $this->proxy_host != '';
143 }
144
158 function openSocket($host, $port, $secure = false)
159 {
160 if ($this->isProxyConfigured()) {
161 $fp = @fsockopen(
162 $this->proxy_host, $this->proxy_port,
163 $errno, $errstr, 15
164 );
165
166 if (!$fp) {
167 return PEAR::raiseError("Connection to the proxy failed: $errstr", -9276);
168 }
169
170 /* HTTPS is to be used and we have a proxy, use CONNECT verb */
171 if ($secure) {
172 $res = $this->_httpConnect($fp, $host, $port);
173
174 if (PEAR::isError($res)) {
175 return $res;
176 }
177 }
178 } else {
179 if ($secure) {
180 $host = 'ssl://' . $host;
181 }
182
183 $fp = @fsockopen($host, $port, $errno, $errstr);
184 if (!$fp) {
185 return PEAR::raiseError("Connection to `$host:$port' failed: $errstr", $errno);
186 }
187 }
188
189 return $fp;
190 }
191}
$port
isProxyConfigured()
Definition Proxy.php:140
getProxyUser()
Definition Proxy.php:128
_parseProxyInfo()
Definition Proxy.php:49
$proxy_schema
Definition Proxy.php:38
openSocket($host, $port, $secure=false)
Definition Proxy.php:158
_httpConnect($fp, $host, $port)
Definition Proxy.php:67
__construct($config=null)
Definition Proxy.php:40
getProxyAuth()
Definition Proxy.php:120