-// Extract host from script name
-function extractHostnameFromUrl (&$script) {
- // Use default SERVER_URL by default... ;) So?
- $url = getServerUrl();
-
- // Is this URL valid?
- if (substr($script, 0, 7) == 'http://') {
- // Use the hostname from script URL as new hostname
- $url = substr($script, 7);
- $extract = explode('/', $url);
- $url = $extract[0];
- // Done extracting the URL :)
- } // END - if
-
- // Extract host name
- $host = str_replace('http://', '', $url);
- if (isInString('/', $host)) $host = substr($host, 0, strpos($host, '/'));
-
- // Generate relative URL
- //* DEBUG: */ debugOutput('SCRIPT=' . $script);
- if (substr(strtolower($script), 0, 7) == 'http://') {
- // But only if http:// is in front!
- $script = substr($script, (strlen($url) + 7));
- } elseif (substr(strtolower($script), 0, 8) == 'https://') {
- // Does this work?!
- $script = substr($script, (strlen($url) + 8));
- }
-
- //* DEBUG: */ debugOutput('SCRIPT=' . $script);
- if (substr($script, 0, 1) == '/') $script = substr($script, 1);
-
- // Return host name
- return $host;
-}
-
-// Send a GET request
-function sendGetRequest ($script, $data = array()) {
- // Extract host name from script
- $host = extractHostnameFromUrl($script);
-
- // Add data
- $body = http_build_query($data, '', '&');
-
- // Do we have a question-mark in the script?
- if (strpos($script, '?') === false) {
- // No, so first char must be question mark
- $body = '?' . $body;
- } else {
- // Ok, add &
- $body = '&' . $body;
- }
-
- // Add script data
- $script .= $body;
-
- // Remove trailed & to make it more conform
- if (substr($script, -1, 1) == '&') $script = substr($script, 0, -1);
-
- // Generate GET request header
- $request = 'GET /' . trim($script) . ' HTTP/1.1' . getConfig('HTTP_EOL');
- $request .= 'Host: ' . $host . getConfig('HTTP_EOL');
- $request .= 'Referer: ' . getUrl() . '/admin.php' . getConfig('HTTP_EOL');
- if (isConfigEntrySet('FULL_VERSION')) {
- $request .= 'User-Agent: ' . getTitle() . '/' . getFullVersion() . getConfig('HTTP_EOL');
- } else {
- $request .= 'User-Agent: ' . getTitle() . '/' . getConfig('VERSION') . getConfig('HTTP_EOL');
- }
- $request .= 'Accept: image/png,image/*;q=0.8,text/plain,text/html,*/*;q=0.5' . getConfig('HTTP_EOL');
- $request .= 'Accept-Charset: UTF-8,*' . getConfig('HTTP_EOL');
- $request .= 'Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0' . getConfig('HTTP_EOL');
- $request .= 'Connection: close' . getConfig('HTTP_EOL');
- $request .= getConfig('HTTP_EOL');
-
- // Send the raw request
- $response = sendRawRequest($host, $request);
-
- // Return the result to the caller function
- return $response;
-}
-
-// Send a POST request
-function sendPostRequest ($script, $postData) {
- // Is postData an array?
- if (!is_array($postData)) {
- // Abort here
- logDebugMessage(__FUNCTION__, __LINE__, sprintf("postData is not an array. Type: %s", gettype($postData)));
- return array('', '', '');
- } // END - if
-
- // Extract host name from script
- $host = extractHostnameFromUrl($script);
-
- // Construct request body
- $body = http_build_query($postData, '', '&');
-
- // Generate POST request header
- $request = 'POST /' . trim($script) . ' HTTP/1.0' . getConfig('HTTP_EOL');
- $request .= 'Host: ' . $host . getConfig('HTTP_EOL');
- $request .= 'Referer: ' . getUrl() . '/admin.php' . getConfig('HTTP_EOL');
- $request .= 'User-Agent: ' . getTitle() . '/' . getFullVersion() . getConfig('HTTP_EOL');
- $request .= 'Accept: text/plain;q=0.8' . getConfig('HTTP_EOL');
- $request .= 'Accept-Charset: UTF-8,*' . getConfig('HTTP_EOL');
- $request .= 'Cache-Control: no-cache' . getConfig('HTTP_EOL');
- $request .= 'Content-Type: application/x-www-form-urlencoded' . getConfig('HTTP_EOL');
- $request .= 'Content-Length: ' . strlen($body) . getConfig('HTTP_EOL');
- $request .= 'Connection: close' . getConfig('HTTP_EOL');
- $request .= getConfig('HTTP_EOL');
-
- // Add body
- $request .= $body;
-
- // Send the raw request
- $response = sendRawRequest($host, $request);
-
- // Return the result to the caller function
- return $response;
-}
-
-// Sends a raw request to another host
-function sendRawRequest ($host, $request) {
- // Init errno and errdesc with 'all fine' values
- $errno = '0'; $errdesc = '';
-
- // Initialize array
- $response = array('', '', '');
-
- // Default is not to use proxy
- $useProxy = false;
-
- // Are proxy settins set?
- if ((isConfigEntrySet('proxy_host')) && (getConfig('proxy_host') != '') && (isConfigEntrySet('proxy_port')) && (getConfig('proxy_port') > 0)) {
- // Then use it
- $useProxy = true;
- } // END - if
-
- // Load include
- loadIncludeOnce('inc/classes/resolver.class.php');
-
- // Get resolver instance
- $resolver = new HostnameResolver();
-
- // Open connection
- //* DEBUG: */ die('SCRIPT=' . $script);
- if ($useProxy === true) {
- // Resolve hostname into IP address
- $ip = $resolver->resolveHostname(compileRawCode(getConfig('proxy_host')));
-
- // Connect to host through proxy connection
- $fp = fsockopen($ip, bigintval(getConfig('proxy_port')), $errno, $errdesc, 30);
- } else {
- // Resolve hostname into IP address
- $ip = $resolver->resolveHostname($host);
-
- // Connect to host directly
- $fp = fsockopen($ip, 80, $errno, $errdesc, 30);
- }
-
- // Is there a link?
- if (!is_resource($fp)) {
- // Failed!
- logDebugMessage(__FUNCTION__, __LINE__, $errdesc . ' (' . $errno . ')');
- return $response;
- } elseif ((!stream_set_blocking($fp, 0)) || (!stream_set_timeout($fp, 1))) {
- // Cannot set non-blocking mode or timeout
- logDebugMessage(__FUNCTION__, __LINE__, socket_strerror(socket_last_error()));
- return $response;
- }
-
- // Do we use proxy?
- if ($useProxy === true) {
- // Setup proxy tunnel
- $response = setupProxyTunnel($host, $fp);
-
- // If the response is invalid, abort
- if ((count($response) == 3) && (empty($response[0])) && (empty($response[1])) && (empty($response[2]))) {
- // Invalid response!
- logDebugMessage(__FUNCTION__, __LINE__, 'Proxy tunnel not working?');
- return $response;
- } // END - if
- } // END - if
-
- // Write request
- fwrite($fp, $request);
-
- // Start counting
- $start = microtime(true);
-
- // Read response
- while (!feof($fp)) {
- // Get info from stream
- $info = stream_get_meta_data($fp);
-
- // Is it timed out? 15 seconds is a really patient...
- if (($info['timed_out'] == true) || (microtime(true) - $start) > 15) {
- // Timeout
- logDebugMessage(__FUNCTION__, __LINE__, 'Timed out to get data from host ' . $host);
-
- // Abort here
- break;
- } // END - if
-
- // Get line from stream
- $line = fgets($fp, 128);
-
- // Ignore empty lines because of non-blocking mode
- if (empty($line)) {
- // uslepp a little to avoid 100% CPU load
- usleep(10);
-
- // Skip this
- continue;
- } // END - if
-
- // Add it to response
- $response[] = trim($line);
- } // END - while
-
- // Close socket
- fclose($fp);
-
- // Time request if debug-mode is enabled
- if (isDebugModeEnabled()) {
- // Add debug message...
- logDebugMessage(__FUNCTION__, __LINE__, 'Request took ' . (microtime(true) - $start) . ' seconds and returned ' . count($response) . ' line(s).');
- } // END - if
-
- // Skip first empty lines
- $resp = $response;
- foreach ($resp as $idx => $line) {
- // Trim space away
- $line = trim($line);
-
- // Is this line empty?
- if (empty($line)) {
- // Then remove it
- array_shift($response);
- } else {
- // Abort on first non-empty line
- break;
- }
- } // END - foreach
-
- //* DEBUG: */ debugOutput('<strong>Request:</strong><pre>'.print_r($request, true).'</pre>');
- //* DEBUG: */ debugOutput('<strong>Response:</strong><pre>'.print_r($response, true).'</pre>');
-
- // Proxy agent found or something went wrong?
- if (!isset($response[0])) {
- // No response, maybe timeout
- $response = array('', '', '');
- logDebugMessage(__FUNCTION__, __LINE__, 'Invalid empty response array, maybe timed out?');
- } elseif ((substr(strtolower($response[0]), 0, 11) == 'proxy-agent') && ($useProxy === true)) {
- // Proxy header detected, so remove two lines
- array_shift($response);
- array_shift($response);
- } // END - if
-
- // Was the request successfull?
- if ((!isInStringIgnoreCase('200 OK', $response[0])) || (empty($response[0]))) {
- // Not found / access forbidden
- logDebugMessage(__FUNCTION__, __LINE__, 'Unexpected status code ' . $response[0] . ' detected. "200 OK" was expected.');
- $response = array('', '', '');
- } // END - if
-
- // Return response
- return $response;
-}
-
-// Sets up a proxy tunnel for given hostname and through resource
-function setupProxyTunnel ($host, $resource) {
- // Initialize array
- $response = array('', '', '');
-
- // Generate CONNECT request header
- $proxyTunnel = 'CONNECT ' . $host . ':80 HTTP/1.0' . getConfig('HTTP_EOL');
- $proxyTunnel .= 'Host: ' . $host . getConfig('HTTP_EOL');
-
- // Use login data to proxy? (username at least!)
- if (getConfig('proxy_username') != '') {
- // Add it as well
- $encodedAuth = base64_encode(compileRawCode(getConfig('proxy_username')) . ':' . compileRawCode(getConfig('proxy_password')));
- $proxyTunnel .= 'Proxy-Authorization: Basic ' . $encodedAuth . getConfig('HTTP_EOL');
- } // END - if
-
- // Add last new-line
- $proxyTunnel .= getConfig('HTTP_EOL');
- //* DEBUG: */ debugOutput('<strong>proxyTunnel=</strong><pre>' . $proxyTunnel.'</pre>');
-
- // Write request
- fwrite($fp, $proxyTunnel);
-
- // Got response?
- if (feof($fp)) {
- // No response received
- return $response;
- } // END - if
-
- // Read the first line
- $resp = trim(fgets($fp, 10240));
- $respArray = explode(' ', $resp);
- if ((strtolower($respArray[0]) !== 'http/1.0') || ($respArray[1] != '200')) {
- // Invalid response!
- return $response;
- } // END - if
-
- // All fine!
- return $respArray;
-}
-