]> git.mxchange.org Git - mailer.git/blobdiff - inc/http-functions.php
Extension ext-cprping introduced (dummy), 'install' directory excluded from GNU GPL:
[mailer.git] / inc / http-functions.php
index 36f0296eab2ae6045f84da8abc0f529ab6ad98c7..9398678b10d3ec170bf7e798b1508ddf25a3e39e 100644 (file)
@@ -46,16 +46,16 @@ function sendHttpHeaders () {
        $now = gmdate('D, d M Y H:i:s') . ' GMT';
 
        // Send HTTP header
-       sendHeader('HTTP/1.1 ' . getHttpStatus());
+       addHttpHeader('HTTP/1.1 ' . getHttpStatus());
 
        // General headers for no caching
-       sendHeader('Expires: ' . $now); // RFC2616 - Section 14.21
-       sendHeader('Last-Modified: ' . $now);
-       sendHeader('Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
-       sendHeader('Pragma: no-cache'); // HTTP/1.0
-       sendHeader('Connection: Close');
-       sendHeader('Content-Type: ' . getContentType() . '; charset=UTF-8');
-       sendHeader('Content-Language: ' . getLanguage());
+       addHttpHeader('Expires: ' . $now); // RFC2616 - Section 14.21
+       addHttpHeader('Last-Modified: ' . $now);
+       addHttpHeader('Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
+       addHttpHeader('Pragma: no-cache'); // HTTP/1.0
+       addHttpHeader('Connection: Close');
+       addHttpHeader('Content-Type: ' . getContentType() . '; charset=UTF-8');
+       addHttpHeader('Content-Language: ' . getLanguage());
 }
 
 // Checks wether the URL is full-qualified (http[s]:// + hostname [+ request data])
@@ -130,14 +130,42 @@ function removeHttpHostNameFromUrl ($url) {
        return $url;
 }
 
-// Send a HEAD request
+// Sends a HTTP request (GET, POST, HEAD are currently supported)
+function sendHttpRequest ($requestType, $baseUrl, $requestData = array(), $removeHeader = false) {
+       // Init response
+       $response = array();
+
+       // Start "detecting" the request type
+       switch ($requestType) {
+               case 'HEAD': // Send a HTTP/1.1 HEAD request
+                       $response = sendHeadRequest($baseUrl, $requestData);
+                       break;
+
+               case 'GET': // Send a HTTP/1.1 GET request
+                       $response = sendGetRequest($baseUrl, $requestData, $removeHeader);
+                       break;
+
+               case 'POST': // Send a HTTP/1.1 POST request
+                       $response = sendPostRequest($baseUrl, $requestData, $removeHeader);
+                       break;
+
+               default: // Unsupported HTTP request, this is really bad and needs fixing
+                       debug_report_bug(__FUNCTION__, __LINE__, 'Unsupported request detected. requestType=' . $requestType . ',baseUrl=' . $baseUrl . ',requestData()=' . count($requestData));
+                       break;
+       } // END - switch
+
+       // Return response
+       return $response;
+}
+
+// Sends a HEAD request
 function sendHeadRequest ($baseUrl, $requestData = array()) {
        // Generate full GET URL
        $getUrl = generateGetUrlFromBaseUrlData($baseUrl, $requestData);
 
        // Do we have http[s]:// in front of the URL?
        if (isFullQualifiedUrl($getUrl)) {
-               // Remove http[s]://<hostname> from url
+               // Remove http[s]://<hostname> from URL
                $getUrl = removeHttpHostNameFromUrl($getUrl);
        } elseif (substr($getUrl, 0, 1) != '/') {
                // Prepend a slash
@@ -280,18 +308,9 @@ function sendRawRequest ($host, $request) {
        // Initialize array
        $response = array('', '', '');
 
-       // Default is not to use proxy
-       $useProxy = false;
-
        // Default is non-broken HTTP server implementation
        $GLOBALS['is_http_server_broken'] = false;
 
-       // Are proxy settins set?
-       if (isProxyUsed()) {
-               // Then use it
-               $useProxy = true;
-       } // END - if
-
        // Load include
        loadIncludeOnce('inc/classes/resolver.class.php');
 
@@ -310,7 +329,7 @@ function sendRawRequest ($host, $request) {
        $resolver = new HostnameResolver();
 
        // Open connection
-       if ($useProxy === true) {
+       if (isProxyUsed() === true) {
                // Resolve hostname into IP address
                $ip = $resolver->resolveHostname(compileRawCode(getProxyHost()));
 
@@ -323,6 +342,7 @@ function sendRawRequest ($host, $request) {
                // Connect to host directly
                $fp = 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)) {
@@ -336,7 +356,7 @@ function sendRawRequest ($host, $request) {
        }
 
        // Do we use proxy?
-       if ($useProxy === true) {
+       if (isProxyUsed() === true) {
                // Setup proxy tunnel
                $response = setupProxyTunnel($host, $port, $fp);
 
@@ -424,7 +444,7 @@ function sendRawRequest ($host, $request) {
                // 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)) {
+       } elseif ((substr(strtolower($response[0]), 0, 11) == 'proxy-agent') && (isProxyUsed() === true)) {
                // Proxy header detected, so remove two lines
                array_shift($response);
                array_shift($response);
@@ -564,6 +584,71 @@ function isBrokenHttpServerImplentation () {
        return $isBroken;
 }
 
+// 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, '/'));
+       } // END - if
+
+       // 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);
+       } // END - if
+
+       // Return host name
+       return $host;
+}
+
+// Adds a HTTP header to array
+function addHttpHeader ($header) {
+       // Send the header
+       //* DEBUG: */ logDebugMessage(__FUNCTION__ . ': header=' . $header);
+       $GLOBALS['http_header'][] = trim($header);
+}
+
+// Flushes all HTTP headers
+function flushHttpHeaders () {
+       // Is the header already sent?
+       if (headers_sent()) {
+               // Then abort here
+               debug_report_bug(__FUNCTION__, __LINE__, 'Headers already sent!');
+       } // END - if
+
+       // Flush all headers if found
+       if ((isset($GLOBALS['http_header'])) && (is_array($GLOBALS['http_header']))) {
+               foreach ($GLOBALS['http_header'] as $header) {
+                       header($header);
+               } // END - foreach
+       } // END - if
+
+       // Mark them as flushed
+       $GLOBALS['http_header'] = array();
+}
+
 //-----------------------------------------------------------------------------
 // Automatically re-created functions, all taken from user comments on www.php.net
 //-----------------------------------------------------------------------------
@@ -714,44 +799,5 @@ next[100]=<pre>'.replaceReturnNewLine(htmlentities(call_user_func_array($mbPrefi
        }
 } // END - if
 
-// 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, '/'));
-       } // END - if
-
-       // 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);
-       } // END - if
-
-       // Return host name
-       return $host;
-}
-
 // [EOF]
 ?>