Updated copyright year.
[mailer.git] / inc / http-functions.php
index 5715c6edd0a5c48967b7ccd9abb6c0402f55c1aa..e82ca2ce198709458fd5bd41d98eb0cac2c7b1ed 100644 (file)
  * -------------------------------------------------------------------- *
  * Kurzbeschreibung  : HTTP-relevante Funktionen                        *
  * -------------------------------------------------------------------- *
- * $Revision::                                                        $ *
- * $Date::                                                            $ *
- * $Tag:: 0.2.1-FINAL                                                 $ *
- * $Author::                                                          $ *
- * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2016 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -144,22 +139,22 @@ function removeHttpHostNameFromUrl ($url) {
 }
 
 // Sends a HTTP request (GET, POST, HEAD are currently supported)
-function sendHttpRequest ($requestType, $baseUrl, $requestData = array(), $removeHeader = false) {
+function sendHttpRequest ($requestType, $baseUrl, $requestData = array(), $removeHeader = FALSE, $allowOnlyHttpOkay = TRUE) {
        // Init response
        $response = array();
 
        // Start "detecting" the request type
        switch ($requestType) {
                case 'HEAD': // Send a HTTP/1.1 HEAD request
-                       $response = sendHeadRequest($baseUrl, $requestData);
+                       $response = sendHttpHeadRequest($baseUrl, $requestData, $allowOnlyHttpOkay);
                        break;
 
                case 'GET': // Send a HTTP/1.1 GET request
-                       $response = sendGetRequest($baseUrl, $requestData, $removeHeader);
+                       $response = sendHttpGetRequest($baseUrl, $requestData, $removeHeader, $allowOnlyHttpOkay);
                        break;
 
                case 'POST': // Send a HTTP/1.1 POST request
-                       $response = sendPostRequest($baseUrl, $requestData, $removeHeader);
+                       $response = sendHttpPostRequest($baseUrl, $requestData, $removeHeader, $allowOnlyHttpOkay);
                        break;
 
                default: // Unsupported HTTP request, this is really bad and needs fixing
@@ -172,9 +167,10 @@ function sendHttpRequest ($requestType, $baseUrl, $requestData = array(), $remov
 }
 
 // Sends a HEAD request
-function sendHeadRequest ($baseUrl, $requestData = array()) {
+function sendHttpHeadRequest ($baseUrl, $requestData = array(), $allowOnlyHttpOkay = TRUE) {
        // Generate full GET URL
        $getUrl = generateGetUrlFromBaseUrlData($baseUrl, $requestData);
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getUrl=' . $getUrl);
 
        // Is there http[s]:// in front of the URL?
        if (isFullQualifiedUrl($getUrl)) {
@@ -189,7 +185,7 @@ function sendHeadRequest ($baseUrl, $requestData = array()) {
        $host = extractHostnameFromUrl($baseUrl);
 
        // Generate HEAD request header
-       $request  = 'HEAD ' . (isProxyUsed() === true ? $baseUrl : '') . trim($getUrl) . ' HTTP/1.1' . getConfig('HTTP_EOL');
+       $request  = 'HEAD ' . (isProxyUsed() === TRUE ? $baseUrl : '') . trim($getUrl) . ' HTTP/1.1' . getConfig('HTTP_EOL');
        $request .= 'Host: ' . $host . getConfig('HTTP_EOL');
        $request .= 'Referer: ' . getUrl() . '/admin.php' . getConfig('HTTP_EOL');
        if (isConfigEntrySet('FULL_VERSION')) {
@@ -204,16 +200,17 @@ function sendHeadRequest ($baseUrl, $requestData = array()) {
        $request .= getConfig('HTTP_EOL');
 
        // Send the raw request
-       $response = sendRawRequest($host, $request);
+       $response = sendRawRequest($host, $request, $allowOnlyHttpOkay);
 
        // Return the result to the caller function
        return $response;
 }
 
 // Send a GET request
-function sendGetRequest ($baseUrl, $requestData = array(), $removeHeader = false) {
+function sendHttpGetRequest ($baseUrl, $requestData = array(), $removeHeader = FALSE, $allowOnlyHttpOkay = TRUE) {
        // Generate full GET URL
        $getUrl = generateGetUrlFromBaseUrlData($baseUrl, $requestData);
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getUrl=' . $getUrl);
 
        // Is there http[s]:// in front of the URL?
        if (isFullQualifiedUrl($getUrl)) {
@@ -228,7 +225,7 @@ function sendGetRequest ($baseUrl, $requestData = array(), $removeHeader = false
        $host = extractHostnameFromUrl($baseUrl);
 
        // Generate GET request header
-       $request  = 'GET ' . (isProxyUsed() === true ? $baseUrl : '') . trim($getUrl) . ' HTTP/1.1' . getConfig('HTTP_EOL');
+       $request  = 'GET ' . (isProxyUsed() === TRUE ? $baseUrl : '') . trim($getUrl) . ' HTTP/1.1' . getConfig('HTTP_EOL');
        $request .= 'Host: ' . $host . getConfig('HTTP_EOL');
        $request .= 'Referer: ' . getUrl() . '/admin.php' . getConfig('HTTP_EOL');
        if (isConfigEntrySet('FULL_VERSION')) {
@@ -243,10 +240,10 @@ function sendGetRequest ($baseUrl, $requestData = array(), $removeHeader = false
        $request .= getConfig('HTTP_EOL');
 
        // Send the raw request
-       $response = sendRawRequest($host, $request);
+       $response = sendRawRequest($host, $request, $allowOnlyHttpOkay);
 
        // Should we remove header lines?
-       if ($removeHeader === true) {
+       if ($removeHeader === TRUE) {
                // Okay, remove them
                $response = removeHttpHeaderFromResponse($response);
        } // END - if
@@ -256,7 +253,7 @@ function sendGetRequest ($baseUrl, $requestData = array(), $removeHeader = false
 }
 
 // Send a POST request, sometimes even POST requests have no parameters
-function sendPostRequest ($baseUrl, $requestData = array(), $removeHeader = false) {
+function sendHttpPostRequest ($baseUrl, $requestData = array(), $removeHeader = FALSE, $allowOnlyHttpOkay = TRUE) {
        // Copy baseUrl to getUrl
        $getUrl = $baseUrl;
 
@@ -276,7 +273,7 @@ function sendPostRequest ($baseUrl, $requestData = array(), $removeHeader = fals
        $body = http_build_query($requestData, '', '&');
 
        // Generate POST request header
-       $request  = 'POST ' . (isProxyUsed() === true ? $baseUrl : '') . trim($baseUrl) . ' HTTP/1.0' . getConfig('HTTP_EOL');
+       $request  = 'POST ' . (isProxyUsed() === TRUE ? $getUrl : '') . trim($getUrl) . ' HTTP/1.0' . getConfig('HTTP_EOL');
        $request .= 'Host: ' . $host . getConfig('HTTP_EOL');
        $request .= 'Referer: ' . getUrl() . '/admin.php' . getConfig('HTTP_EOL');
        if (isConfigEntrySet('FULL_VERSION')) {
@@ -296,10 +293,10 @@ function sendPostRequest ($baseUrl, $requestData = array(), $removeHeader = fals
        $request .= $body;
 
        // Send the raw request
-       $response = sendRawRequest($host, $request);
+       $response = sendRawRequest($host, $request, $allowOnlyHttpOkay);
 
        // Should we remove header lines?
-       if ($removeHeader === true) {
+       if ($removeHeader === TRUE) {
                // Okay, remove them
                $response = removeHttpHeaderFromResponse($response);
        } // END - if
@@ -309,7 +306,7 @@ function sendPostRequest ($baseUrl, $requestData = array(), $removeHeader = fals
 }
 
 // Sends a raw request (string) to given host (hostnames will be solved)
-function sendRawRequest ($host, $request) {
+function sendRawRequest ($host, $request, $allowOnlyHttpOkay = TRUE) {
        //* DEBUG: */ die('host='.$host.',request=<pre>'.$request.'</pre>');
        // Init errno and errdesc with 'all fine' values
        $errno = '0';
@@ -319,10 +316,10 @@ function sendRawRequest ($host, $request) {
        $port = 80;
 
        // Initialize array
-       $response = array('', '', '');
+       $response = array();
 
        // Default is non-broken HTTP server implementation
-       $GLOBALS['is_http_server_broken'] = false;
+       $GLOBALS['is_http_server_broken'] = FALSE;
 
        // Load include
        loadIncludeOnce('inc/classes/resolver.class.php');
@@ -341,11 +338,17 @@ function sendRawRequest ($host, $request) {
        // Get resolver instance
        $resolver = new HostnameResolver();
 
-       // Get proxy host
-       $proxyHost = compileRawCode(getProxyHost());
+       // Default is no proxy
+       $proxyHost = NULL;
+
+       // Is the configuration entry set?
+       if ((!isInstaller()) && (isConfigEntrySet('proxy_host'))) {
+               // Get proxy host
+               $proxyHost = compileRawCode(getProxyHost());
+       } // END - if
 
        // Open connection
-       if (isProxyUsed() === true) {
+       if (isProxyUsed() === TRUE) {
                // Resolve hostname into IP address
                $ip = $resolver->resolveHostname($proxyHost);
 
@@ -364,15 +367,15 @@ function sendRawRequest ($host, $request) {
        if (!is_resource($resource)) {
                // Failed!
                logDebugMessage(__FUNCTION__, __LINE__, $errdesc . ' (' . $errno . ')');
-               return $response;
+               return array('', '', '');
        } 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;
+               return array('', '', '');
        }
 
        // Shall proxy be used?
-       if (isProxyUsed() === true) {
+       if (isProxyUsed() === TRUE) {
                // Setup proxy tunnel
                $response = setupProxyTunnel($host, $proxyHost, $port, $resource);
 
@@ -388,7 +391,7 @@ function sendRawRequest ($host, $request) {
        fwrite($resource, $request);
 
        // Start counting
-       $start = microtime(true);
+       $start = microtime(TRUE);
 
        // Read response
        while (!feof($resource)) {
@@ -396,7 +399,7 @@ function sendRawRequest ($host, $request) {
                $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) {
+               if (($info['timed_out'] == TRUE) || (microtime(TRUE) - $start) > 15) {
                        // Timeout
                        logDebugMessage(__FUNCTION__, __LINE__, 'Timed out to get data from host ' . $host);
 
@@ -407,8 +410,11 @@ function sendRawRequest ($host, $request) {
                // Get line from stream
                $line = fgets($resource, 128);
 
-               // Ignore empty lines because of non-blocking mode
-               if (empty($line)) {
+               /*
+                * Ignore empty lines because of non-blocking mode, you cannot use
+                * empty() here as it would also see \r\n as "empty".
+                */
+               if (strlen($line) == 0) {
                        // uslepp a little to avoid 100% CPU load
                        usleep(10);
 
@@ -423,7 +429,7 @@ function sendRawRequest ($host, $request) {
                } // END - if
 
                // Add it to response
-               //* DEBUG: */ print 'line='.$line.'<br />';
+               //* DEBUG: */ print 'line(' . strlen($line) . ')='.$line.'<br />';
                array_push($response, $line);
        } // END - while
 
@@ -433,41 +439,25 @@ function sendRawRequest ($host, $request) {
        // 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).');
+               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>');
+       //* 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])) {
+       if (!isFilledArray($response)) {
                // 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') && (isProxyUsed() === true)) {
+       } elseif ((substr(strtolower($response[0]), 0, 11) == 'proxy-agent') && (isProxyUsed() === 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]))) {
+       if ((!isHttpStatusOkay($response[0])) && ($allowOnlyHttpOkay === TRUE)) {
                // Not found / access forbidden
                logDebugMessage(__FUNCTION__, __LINE__, 'Unexpected status code ' . $response[0] . ' detected. "200 OK" was expected.');
                $response = array('', '', '');
@@ -480,6 +470,12 @@ function sendRawRequest ($host, $request) {
        return $response;
 }
 
+// Is HTTP status okay?
+function isHttpStatusOkay ($header) {
+       // Determine it
+       return in_array(strtoupper(trim($header)), array('HTTP/1.1 200 OK', 'HTTP/1.0 200 OK'));
+}
+
 // Sets up a proxy tunnel for given hostname and through resource
 function setupProxyTunnel ($host, $proxyHost, $port, $resource) {
        // Initialize array
@@ -525,7 +521,7 @@ function setupProxyTunnel ($host, $proxyHost, $port, $resource) {
 // Check array for chuncked encoding
 function unchunkHttpResponse ($response) {
        // Default is not chunked
-       $isChunked = false;
+       $isChunked = FALSE;
 
        // Check if we have chunks
        foreach ($response as $line) {
@@ -535,29 +531,62 @@ function unchunkHttpResponse ($response) {
                // Entry found?
                if ((isInStringIgnoreCase('transfer-encoding', $line)) && (isInStringIgnoreCase('chunked', $line))) {
                        // Found!
-                       $isChunked = true;
+                       $isChunked = TRUE;
                        break;
-               } // END - if
+               } elseif (empty($line)) {
+                       // Empty line found (header->body)
+                       break;
+               }
        } // END - foreach
 
+       // Save whole body
+       $body = removeHttpHeaderFromResponse($response);
+
        // Is it chunked?
-       if ($isChunked === true) {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'isChunked=' . intval($isChunked));
+       if ($isChunked === TRUE) {
+               // Make sure, that body is an array
+               assert(is_array($body));
+
                // Good, we still have the HTTP headers in there, so we need to get rid
                // of them temporarly
-               //* DEBUG: */ die('<pre>'.htmlentities(print_r(removeHttpHeaderFromResponse($response), true)).'</pre>');
-               $tempResponse = http_chunked_decode(implode('', removeHttpHeaderFromResponse($response)));
+               //* DEBUG: */ die('<pre>'.htmlentities(print_r(removeHttpHeaderFromResponse($response), TRUE)).'</pre>');
+               $tempResponse = http_chunked_decode(implode('', $body));
 
                // We got a string back from http_chunked_decode(), so we need to convert it back to an array
-               //* DEBUG: */ die('tempResponse['.strlen($tempResponse).']=<pre>'.replaceReturnNewLine(htmlentities($tempResponse)).'</pre>');
+               //* DEBUG: */ die('tempResponse['.strlen($tempResponse).'/'.gettype($tempResponse).']=<pre>'.replaceReturnNewLine(htmlentities($tempResponse)).'</pre>');
 
                // Re-add the headers
-               $response = merge_array($GLOBALS['http_headers'], stringToArray(chr(10), $tempResponse));
-       } // END - if
+               $response = mergeHttpHeadersWithBody($tempResponse);
+       } elseif (is_array($body)) {
+               /*
+                * Make sure the body is in one array element as many other functions
+                * get disturbed by it.
+                */
+
+               // Put all array elements from body together
+               $body = implode('', $body);
+
+               // Now merge the extracted headers + fixed body together
+               $response = mergeHttpHeadersWithBody($body);
+       }
 
        // Return the unchunked array
        return $response;
 }
 
+// Merges HTTP header lines with given body (string)
+function mergeHttpHeadersWithBody ($body) {
+       // Add empty entry to mimic header->body
+       $GLOBALS['http_headers'][] = getConfig('HTTP_EOL');
+
+       // Make sure at least one header is there (which is still not valid but okay here)
+       assert(isFilledArray($GLOBALS['http_headers']));
+
+       // Merge both together
+       return merge_array($GLOBALS['http_headers'], array(count($GLOBALS['http_headers']) => $body));
+}
+
 // Removes HTTP header lines from a response array (e.g. output from send<Get|Post>Request() )
 function removeHttpHeaderFromResponse ($response) {
        // Save headers for later usage
@@ -571,9 +600,6 @@ function removeHttpHeaderFromResponse ($response) {
                        // Remove line
                        array_shift($response2);
 
-                       // Add full line to temporary global array
-                       array_push($GLOBALS['http_headers'], $line);
-
                        // Trim it for testing
                        $lineTest = trim($line);
 
@@ -582,6 +608,15 @@ function removeHttpHeaderFromResponse ($response) {
                                // Then stop here
                                break;
                        } // END - if
+
+                       // Is the last line set and is not ending with \r\n?
+                       if ((isset($GLOBALS['http_headers'][count($GLOBALS['http_headers']) - 1])) && (substr($GLOBALS['http_headers'][count($GLOBALS['http_headers']) - 1], -2, 2) != getConfig('HTTP_EOL'))) {
+                               // Add it to previous one
+                               $GLOBALS['http_headers'][count($GLOBALS['http_headers']) - 1] .= $line;
+                       } else {
+                               // Add full line to temporary global array
+                               array_push($GLOBALS['http_headers'], $line);
+                       }
                } // END - foreach
 
                // Write back the array
@@ -595,7 +630,7 @@ function removeHttpHeaderFromResponse ($response) {
 // Returns the flag if a broken HTTP server implementation was detected
 function isBrokenHttpServerImplentation () {
        // Determine it
-       $isBroken = ((isset($GLOBALS['is_http_server_broken'])) && ($GLOBALS['is_http_server_broken'] === true));
+       $isBroken = ((isset($GLOBALS['is_http_server_broken'])) && ($GLOBALS['is_http_server_broken'] === TRUE));
 
        // ... and return it
        return $isBroken;
@@ -609,18 +644,29 @@ function extractHostnameFromUrl (&$script) {
        // 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);
+               $extract = explode('/', substr($script, 7));
                $url = $extract[0];
-               // Done extracting the URL :)
-       } // END - if
+       } elseif (substr($script, 0, 8) == 'https://') {
+               // Use the hostname from script URL as new hostname
+               $extract = explode('/', substr($script, 8));
+               $url = $extract[0];
+       }
 
        // Extract host name
-       $host = str_replace('http://', '', $url);
+       $host = str_replace(array('http://', 'https://'), array('', ''), $url);
+
+       // Is there a slash at the end?
        if (isInString('/', $host)) {
                $host = substr($host, 0, strpos($host, '/'));
        } // END - if
 
+       // Is there a double-dot in? (Means port number)
+       if (strpos($host, ':') !== FALSE) {
+               // Detected a double-dot
+               $hostArray = explode(':', $host);
+               $host = $hostArray[0];
+       } // END - if
+
        // Generate relative URL
        //* DEBUG: */ debugOutput('SCRIPT=' . $script);
        if (substr(strtolower($script), 0, 7) == 'http://') {
@@ -643,7 +689,7 @@ function extractHostnameFromUrl (&$script) {
 // Adds a HTTP header to array
 function addHttpHeader ($header) {
        // Send the header
-       //* DEBUG: */ logDebugMessage(__FUNCTION__ . ': header=' . $header);
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, ': header=' . $header);
        array_push($GLOBALS['http_header'], trim($header));
 }
 
@@ -677,7 +723,7 @@ if (!function_exists('http_build_query')) {
        function http_build_query($requestData, $prefix = '', $sep = '', $key = '') {
                $ret = array();
                foreach ((array) $requestData as $k => $v) {
-                       if (is_int($k) && $prefix != null) {
+                       if (is_int($k) && !is_null($prefix)) {
                                $k = urlencode($prefix . $k);
                        } // END - if
 
@@ -857,5 +903,53 @@ function getContentType () {
        return $GLOBALS['content_type'];
 }
 
+// Logs wrong SERVER_NAME attempts
+function logWrongServerNameRedirect () {
+       // Is ext-sql_patches at least version 0.9.2?
+       if (isExtensionInstalledAndNewer('sql_patches', '0.9.2')) {
+               // Is there an entry?
+               if (countSumTotalData(detectServerName(), 'server_name_log', 'server_name_id', 'server_name', TRUE, str_replace('%', '{PER}', sprintf(" AND `server_name_remote_addr`='%s' AND `server_name_ua`='%s' AND `server_name_referrer`='%s'", sqlEscapeString(detectRemoteAddr(TRUE)), sqlEscapeString(detectUserAgent(TRUE)), sqlEscapeString(detectReferer(TRUE))))) == 1) {
+                       // Update counter, as all are the same
+                       sqlQueryEscaped("UPDATE
+       `{?_MYSQL_PREFIX?}_server_name_log`
+SET
+       `server_name_counter`=`server_name_counter`+1
+WHERE
+       `server_name`='%s' AND
+       `server_name_remote_addr`='%s' AND
+       `server_name_ua`='%s' AND
+       `server_name_referrer`='%s'
+LIMIT 1",
+                               array(
+                                       detectServerName(),
+                                       detectRemoteAddr(TRUE),
+                                       detectUserAgent(TRUE),
+                                       detectReferer(TRUE)
+                               ), __FUNCTION__, __LINE__);
+               } else {
+                       // Then log it away
+                       sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_server_name_log` (`server_name`, `server_name_remote_addr`, `server_name_ua`, `server_name_referrer`) VALUES('%s','%s', '%s', '%s')",
+                               array(
+                                       detectServerName(),
+                                       detectRemoteAddr(TRUE),
+                                       detectUserAgent(TRUE),
+                                       detectReferer(TRUE)
+                               ), __FUNCTION__, __LINE__);
+               }
+       } // END - if
+}
+
+// Check if response status OK and array index 'response' is set
+function isHttpResponseStatusOkay ($response) {
+       // Assertion on array
+       assert(is_array($response));
+
+       // Test it
+       $isOkay = ((isset($response['status'])) && ($response['status'] == 'OK'));
+
+       // Return result
+       return $isOkay;
+}
+
 // [EOF]
 ?>