// Get resolver instance
$resolver = new HostnameResolver();
+ // Get proxy host
+ $proxyHost = compileRawCode(getProxyHost());
+
// Open connection
if (isProxyUsed() === true) {
// Resolve hostname into IP address
- $ip = $resolver->resolveHostname(compileRawCode(getProxyHost()));
+ $ip = $resolver->resolveHostname($proxyHost);
// Connect to host through proxy connection
- $fp = fsockopen($ip, bigintval(getProxyPort()), $errno, $errdesc, 30);
+ $resource = fsockopen($ip, bigintval(getProxyPort()), $errno, $errdesc, 30);
} else {
// Resolve hostname into IP address
$ip = $resolver->resolveHostname($host);
// Connect to host directly
- $fp = fsockopen($ip, $port, $errno, $errdesc, 30);
+ $resource = fsockopen($ip, $port, $errno, $errdesc, 30);
}
//* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ip=' . $ip . ',host=' . $host . ',isProxyUsed()=' . intval(isProxyUsed()));
// Is there a link?
- if (!is_resource($fp)) {
+ if (!is_resource($resource)) {
// Failed!
logDebugMessage(__FUNCTION__, __LINE__, $errdesc . ' (' . $errno . ')');
return $response;
- } elseif ((!stream_set_blocking($fp, 0)) || (!stream_set_timeout($fp, 1))) {
+ } elseif ((!stream_set_blocking($resource, 0)) || (!stream_set_timeout($resource, 1))) {
// Cannot set non-blocking mode or timeout
logDebugMessage(__FUNCTION__, __LINE__, socket_strerror(socket_last_error()));
return $response;
// Do we use proxy?
if (isProxyUsed() === true) {
// Setup proxy tunnel
- $response = setupProxyTunnel($host, $port, $fp);
+ $response = setupProxyTunnel($host, $proxyHost, $port, $resource);
// If the response is invalid, abort
if ((count($response) == 3) && (empty($response[0])) && (empty($response[1])) && (empty($response[2]))) {
} // END - if
// Write request
- fwrite($fp, $request);
+ fwrite($resource, $request);
// Start counting
$start = microtime(true);
// Read response
- while (!feof($fp)) {
+ while (!feof($resource)) {
// Get info from stream
- $info = stream_get_meta_data($fp);
+ $info = stream_get_meta_data($resource);
// Is it timed out? 15 seconds is a really patient...
if (($info['timed_out'] == true) || (microtime(true) - $start) > 15) {
} // END - if
// Get line from stream
- $line = fgets($fp, 128);
+ $line = fgets($resource, 128);
// Ignore empty lines because of non-blocking mode
if (empty($line)) {
} // END - while
// Close socket
- fclose($fp);
+ fclose($resource);
// Time request if debug-mode is enabled
if (isDebugModeEnabled()) {
}
// Sets up a proxy tunnel for given hostname and through resource
-function setupProxyTunnel ($host, $port, $resource) {
+function setupProxyTunnel ($host, $proxyHost, $port, $resource) {
// Initialize array
$response = array('', '', '');
// Generate CONNECT request header
$proxyTunnel = 'CONNECT ' . $host . ':' . $port . ' HTTP/1.0' . getConfig('HTTP_EOL');
- $proxyTunnel .= 'Host: ' . $host . getConfig('HTTP_EOL');
+ $proxyTunnel .= 'Host: ' . $proxyHost . getConfig('HTTP_EOL');
// Use login data to proxy? (username at least!)
if (getProxyUsername() != '') {
//* DEBUG: */ debugOutput('<strong>proxyTunnel=</strong><pre>' . $proxyTunnel.'</pre>');
// Write request
- fwrite($fp, $proxyTunnel);
+ fwrite($resource, $proxyTunnel);
// Got response?
- if (feof($fp)) {
+ if (feof($resource)) {
// No response received
return $response;
} // END - if
// Read the first line
- $resp = trim(fgets($fp, 10240));
+ $resp = trim(fgets($resource, 10240));
$respArray = explode(' ', $resp);
if ((strtolower($respArray[0]) !== 'http/1.0') || ($respArray[1] != '200')) {
// Invalid response!