Substracting the count of \r\n in chunked-encoded HTTP messages indicates a broken...
authorRoland Häder <roland@mxchange.org>
Tue, 8 Mar 2011 11:31:10 +0000 (11:31 +0000)
committerRoland Häder <roland@mxchange.org>
Tue, 8 Mar 2011 11:31:10 +0000 (11:31 +0000)
.gitattributes
inc/config-global.php
inc/functions.php
inc/http-functions.php [new file with mode: 0644]

index bbf9f9e09483dbe0da2f1f156ff506925cae189c..d9672442990f4d406d45d5da298ac7a33d4b7146 100644 (file)
@@ -227,6 +227,7 @@ inc/handler.php -text svneol=unset#text/plain
 inc/header.php -text svneol=unset#text/plain
 inc/hourly/.htaccess svneol=native#text/plain
 inc/hourly/hourly_ svneol=native#text/plain
+inc/http-functions.php svneol=native#text/plain
 inc/img/.htaccess -text svneol=unset#text/plain
 inc/inc-functions.php svneol=native#text/plain
 inc/install-functions.php -text svneol=unset#text/plain
index afe85d4ceccca7e52d1144b7c8263b610c129dbb..8fa172f4313322aa60234e7c6f7bd1af91e682d1 100644 (file)
@@ -60,7 +60,7 @@ error_reporting(E_ALL | E_STRICT);
 $PATH = str_replace("\\", '/', substr(dirname(__FILE__), 0, -3));
 
 // Some very important function includes
-foreach (array('config','wrapper','template','module','inc','stats') as $inc) {
+foreach (array('config','wrapper','template','module','inc','stats','http') as $inc) {
        include($PATH . 'inc/' . $inc . '-functions.php');
 } // END - foreach
 
index 79e5d8d5a7541f79bae4a3cc289f8032a9a5e27d..119c23891de8d87676deec8a70a5d493a2f3a2ee 100644 (file)
@@ -40,24 +40,6 @@ if (!defined('__SECURITY')) {
        die();
 } // END - if
 
-// Sends out all headers required for HTTP/1.1 reply
-function sendHttpHeaders () {
-       // Used later
-       $now = gmdate('D, d M Y H:i:s') . ' GMT';
-
-       // Send HTTP header
-       sendHeader('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());
-}
-
 // Init fatal message array
 function initFatalMessages () {
        $GLOBALS['fatal_messages'] = array();
@@ -842,377 +824,6 @@ function extractHostnameFromUrl (&$script) {
        return $host;
 }
 
-// Send a GET request
-function sendGetRequest ($script, $data = array(), $removeHeader = false) {
-       // Extract hostname and port from script
-       $host = extractHostnameFromUrl($script);
-
-       // Add data
-       $body = http_build_query($data, '', '&');
-
-       // There should be data, else we don't need to extend $script with $body
-       if (!empty($body)) {
-               // 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);
-               } // END - if
-       } // END - if
-
-       // 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);
-
-       // Should we remove header lines?
-       if ($removeHeader === true) {
-               // Okay, remove them
-               $response = removeHttpHeaderFromResponse($response);
-       } // END - if
-
-       // Return the result to the caller function
-       return $response;
-}
-
-// Send a POST request
-function sendPostRequest ($script, array $postData, $removeHeader = false) {
-       // 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);
-
-       // Should we remove header lines?
-       if ($removeHeader === true) {
-               // Okay, remove them
-               $response = removeHttpHeaderFromResponse($response);
-       } // END - if
-
-       // 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 = '';
-
-       // Default port is 80
-       $port = 80;
-
-       // Initialize array
-       $response = array('', '', '');
-
-       // Default is not to use proxy
-       $useProxy = false;
-
-       // Are proxy settins set?
-       if (isProxyUsed()) {
-               // Then use it
-               $useProxy = true;
-       } // END - if
-
-       // Load include
-       loadIncludeOnce('inc/classes/resolver.class.php');
-
-       // Extract port part from host
-       $portArray = explode(':', $host);
-       if (count($portArray) == 2) {
-               // Extract host and port
-               $host = $portArray[0];
-               $port = $portArray[1];
-       } elseif (count($portArray) > 2) {
-               // This should not happen!
-               debug_report_bug(__FUNCTION__, __LINE__, 'Invalid ' . $host . '. Please report this to the Mailer-Project team.');
-       }
-
-       // Get resolver instance
-       $resolver = new HostnameResolver();
-
-       // Open connection
-       //* DEBUG: */ die('SCRIPT=' . $script);
-       if ($useProxy === true) {
-               // Resolve hostname into IP address
-               $ip = $resolver->resolveHostname(compileRawCode(getProxyHost()));
-
-               // Connect to host through proxy connection
-               $fp = 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);
-       }
-
-       // 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, $port, $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[] = $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('', '', '');
-       } else {
-               // Check array for chuncked encoding
-               $response = unchunkHttpResponse($response);
-       } // END - if
-
-       // Return response
-       return $response;
-}
-
-// Sets up a proxy tunnel for given hostname and through resource
-function setupProxyTunnel ($host, $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');
-
-       // Use login data to proxy? (username at least!)
-       if (getProxyUsername() != '') {
-               // Add it as well
-               $encodedAuth = base64_encode(compileRawCode(getProxyUsername()) . ':' . compileRawCode(getProxyPassword()));
-               $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;
-}
-
-// Check array for chuncked encoding
-function unchunkHttpResponse (array $response) {
-       // Default is not chunked
-       $isChunked = false;
-
-       // Check if we have chunks
-       foreach ($response as $line) {
-               // Make lower-case and trim it
-               $line = trim(strtolower($line));
-
-               // Entry found?
-               if ((strpos($line, 'transfer-encoding') !== false) && (strpos($line, 'chunked') !== false)) {
-                       // Found!
-                       $isChunked = true;
-                       break;
-               } // END - if
-       } // END - foreach
-
-       // Is it chunked?
-       if ($isChunked === true) {
-               // 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)));
-
-               // 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>');
-
-               // Re-add the headers
-               $response = merge_array($GLOBALS['http_headers'], stringToArray("\n", $tempResponse));
-       } // END - if
-
-       // Return the unchunked array
-       return $response;
-}
-
-// Removes HTTP header lines from a response array (e.g. output from send<Get|Post>Request() )
-function removeHttpHeaderFromResponse (array $response) {
-       // Save headers for later usage
-       $GLOBALS['http_headers'] = array();
-
-       // The first array element has to contain HTTP
-       if ((isset($response[0])) && (substr(strtoupper($response[0]), 0, 5) == 'HTTP/')) {
-               // Okay, we have headers, now remove them with a second array
-               $response2 = $response;
-               foreach ($response as $line) {
-                       // Remove line
-                       array_shift($response2);
-
-                       // Add full line to temporary global array
-                       $GLOBALS['http_headers'][] = $line;
-
-                       // Trim it for testing
-                       $lineTest = trim($line);
-
-                       // Is this line empty?
-                       if (empty($lineTest)) {
-                               // Then stop here
-                               break;
-                       } // END - if
-               } // END - foreach
-
-               // Write back the array
-               $response = $response2;
-       } // END - if
-
-       // Return the modified response array
-       return $response;
-}
-
 // Taken from www.php.net isInStringIgnoreCase() user comments
 function isEmailValid ($email) {
        // Check first part of email address
@@ -2663,6 +2274,21 @@ function stringToArray ($delimiter, $string) {
        return $strArray;
 }
 
+// Detects the prefix 'mb_' if a multi-byte string is given
+function detectMultiBytePrefix ($str) {
+       // Default is without multi-byte
+       $mbPrefix = '';
+
+       // Detect multi-byte (strictly)
+       if (mb_detect_encoding($str, 'auto', true) !== false) {
+               // With multi-byte encoded string
+               $mbPrefix = 'mb_';
+       } // END - if
+
+       // Return the prefix
+       return $mbPrefix;
+}
+
 //-----------------------------------------------------------------------------
 // Automatically re-created functions, all taken from user comments on www.php.net
 //-----------------------------------------------------------------------------
@@ -2675,132 +2301,5 @@ if (!function_exists('html_entity_decode')) {
        }
 } // END - if
 
-if (!function_exists('http_build_query')) {
-       // Taken from documentation on www.php.net, credits to Marco K. (Germany) and some light mods by R.Haeder
-       function http_build_query($data, $prefix = '', $sep = '', $key = '') {
-               $ret = array();
-               foreach ((array) $data as $k => $v) {
-                       if (is_int($k) && $prefix != null) {
-                               $k = urlencode($prefix . $k);
-                       } // END - if
-
-                       if ((!empty($key)) || ($key === 0)) {
-                               $k = $key . '[' . urlencode($k) . ']';
-                       } // END - if
-
-                       if (is_array($v) || is_object($v)) {
-                               array_push($ret, http_build_query($v, '', $sep, $k));
-                       } else {
-                               array_push($ret, $k.'='.urlencode($v));
-                       }
-               } // END - foreach
-
-               if (empty($sep)) {
-                       $sep = ini_get('arg_separator.output');
-               } // END - if
-
-               return implode($sep, $ret);
-       }
-} // END - if
-
-if (!function_exists('http_chunked_decode')) {
-       /**
-        * dechunk an http 'transfer-encoding: chunked' message.
-        *
-        * @param       $chunk          The encoded message
-        * @return      $dechunk        The decoded message. If $chunk wasn't encoded properly debug_report_bug() is being called
-        * @author      Marques Johansson
-        * @link        http://php.net/manual/en/function.http-chunked-decode.php#89786
-        */
-       function http_chunked_decode ($chunk) {
-               // Init some variables
-               $offset = 0;
-               $len = mb_strlen($chunk);
-               $dechunk = '';
-
-               // Walk through all chunks
-               while ($offset < $len) {
-                       // Where does the \r\n begin?
-                       $lineEndAt = mb_strpos($chunk, getConfig('HTTP_EOL'), $offset);
-
-                       /* DEBUG: *
-                       print 'lineEndAt[<em>'.__LINE__.'</em>]='.$lineEndAt.'<br />
-offset[<em>'.__LINE__.'</em>]='.$offset.'<br />
-len='.$len.'<br />
-next[offset]=<pre>'.replaceReturnNewLine(htmlentities(mb_substr($chunk, $offset, 10))).'</pre>';
-                       /* DEBUG: */
-
-                       // Get next hex-coded chunk length
-                       $chunkLenHex = mb_substr($chunk, $offset, ($lineEndAt - $offset));
-
-                       /* DEBUG: *
-                       print 'chunkLenHex[<em>'.__LINE__.'</em>]='.replaceReturnNewLine(htmlentities($chunkLenHex)).'<br />
-';
-                       /* DEBUG: */
-
-                       // Validation if it is hexadecimal
-                       if (!isHexadecimal($chunkLenHex)) {
-                               // Please help debugging this
-                               //* DEBUG: */ die('ABORT:chunkLenHex=<pre>'.replaceReturnNewLine(htmlentities($chunkLenHex)).'</pre>');
-                               debug_report_bug(__FUNCTION__, __LINE__, 'Value ' . $chunkLenHex . ' is not properly chunk encoded.');
-
-                               // This won't be reached
-                               return $chunk;
-                       } // END - if
-
-                       // Position of next chunk is right after \r\n
-                       $offset   = $offset + strlen($chunkLenHex) + strlen(getConfig('HTTP_EOL'));
-                       $chunkLen = hexdec(rtrim($chunkLenHex, getConfig('HTTP_EOL')));
-
-                       /* DEBUG: *
-                       print 'chunkLen='.$chunkLen.'<br />
-offset[<em>'.__LINE__.'</em>]='.$offset.'<br />';
-                       /* DEBUG: */
-
-                       // Moved out for debugging
-                       $next  = mb_substr($chunk, $offset, $chunkLen);
-                       //* DEBUG: */ print 'next=<pre>'.replaceReturnNewLine(htmlentities($next)).'</pre>';
-
-                       // Count occurrences of \r\n
-                       $count = mb_substr_count($next, getConfig('HTTP_EOL'));
-
-                       // Correct it because we need to subtract occurrences of \r\n
-                       $chunkLen = hexdec(rtrim($chunkLenHex, getConfig('HTTP_EOL'))) - ($count * strlen(getConfig('HTTP_EOL')));
-
-                       $dechunk .= mb_substr($chunk, $offset, $chunkLen);
-
-                       /* DEBUG: *
-                       print('offset[<em>'.__LINE__.'</em>]='.$offset.'<br />
-lineEndAt[<em>'.__LINE__.'</em>]='.$lineEndAt.'<br />
-len='.$len.'<br />
-count='.$count.'<br />
-chunkLen='.$chunkLen.'<br />
-chunkLenHex='.$chunkLenHex.'<br />
-dechunk=<pre>'.replaceReturnNewLine(htmlentities($dechunk)).'</pre>
-chunk=<pre>'.replaceReturnNewLine(htmlentities($chunk)).'</pre>');
-                       /* DEBUG: */
-
-                       // Is $offset + $chunkLen larger than or equal $len?
-                       if (($offset + $chunkLen) >= $len) {
-                               // Then stop processing here
-                               break;
-                       } // END - if
-
-                       // Calculate next offset of chunk
-                       $offset = mb_strpos($chunk, getConfig('HTTP_EOL'), $offset + $chunkLen) + 2;
-
-                       /* DEBUG: *
-                       print('offset[<em>'.__LINE__.'</em>]='.$offset.'<br />
-next[100]=<pre>'.replaceReturnNewLine(htmlentities(mb_substr($chunk, $offset, 100))).'</pre>
----:---:---:---:---:---:---:---:---<br />
-');
-                       /* DEBUG: */
-               } // END - while
-
-               // Return de-chunked string
-               return $dechunk;
-       }
-} // END - if
-
 // [EOF]
 ?>
diff --git a/inc/http-functions.php b/inc/http-functions.php
new file mode 100644 (file)
index 0000000..b0cbca3
--- /dev/null
@@ -0,0 +1,595 @@
+<?php
+/************************************************************************
+ * Mailer v0.2.1-FINAL                                Start: 03/08/2011 *
+ * ===================                          Last change: 03/08/2011 *
+ *                                                                      *
+ * -------------------------------------------------------------------- *
+ * File              : http-functions.php                               *
+ * -------------------------------------------------------------------- *
+ * Short description : HTTP-related functions                           *
+ * -------------------------------------------------------------------- *
+ * Kurzbeschreibung  : HTTP-relevante Funktionen                        *
+ * -------------------------------------------------------------------- *
+ * $Revision::                                                        $ *
+ * $Date::                                                            $ *
+ * $Tag:: 0.2.1-FINAL                                                 $ *
+ * $Author::                                                          $ *
+ * -------------------------------------------------------------------- *
+ * Copyright (c) 2003 - 2009 by Roland Haeder                           *
+ * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
+ * For more information visit: http://www.mxchange.org                  *
+ *                                                                      *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or    *
+ * (at your option) any later version.                                  *
+ *                                                                      *
+ * This program is distributed in the hope that it will be useful,      *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
+ * GNU General Public License for more details.                         *
+ *                                                                      *
+ * You should have received a copy of the GNU General Public License    *
+ * along with this program; if not, write to the Free Software          *
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
+ * MA  02110-1301  USA                                                  *
+ ************************************************************************/
+
+// Some security stuff...
+if (!defined('__SECURITY')) {
+       die();
+} // END - if
+
+// Sends out all headers required for HTTP/1.1 reply
+function sendHttpHeaders () {
+       // Used later
+       $now = gmdate('D, d M Y H:i:s') . ' GMT';
+
+       // Send HTTP header
+       sendHeader('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());
+}
+
+// Send a GET request
+function sendGetRequest ($script, $data = array(), $removeHeader = false) {
+       // Extract hostname and port from script
+       $host = extractHostnameFromUrl($script);
+
+       // Add data
+       $body = http_build_query($data, '', '&');
+
+       // There should be data, else we don't need to extend $script with $body
+       if (!empty($body)) {
+               // 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);
+               } // END - if
+       } // END - if
+
+       // 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);
+
+       // Should we remove header lines?
+       if ($removeHeader === true) {
+               // Okay, remove them
+               $response = removeHttpHeaderFromResponse($response);
+       } // END - if
+
+       // Return the result to the caller function
+       return $response;
+}
+
+// Send a POST request
+function sendPostRequest ($script, array $postData, $removeHeader = false) {
+       // 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);
+
+       // Should we remove header lines?
+       if ($removeHeader === true) {
+               // Okay, remove them
+               $response = removeHttpHeaderFromResponse($response);
+       } // END - if
+
+       // 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 = '';
+
+       // Default port is 80
+       $port = 80;
+
+       // 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');
+
+       // Extract port part from host
+       $portArray = explode(':', $host);
+       if (count($portArray) == 2) {
+               // Extract host and port
+               $host = $portArray[0];
+               $port = $portArray[1];
+       } elseif (count($portArray) > 2) {
+               // This should not happen!
+               debug_report_bug(__FUNCTION__, __LINE__, 'Invalid ' . $host . '. Please report this to the Mailer-Project team.');
+       }
+
+       // Get resolver instance
+       $resolver = new HostnameResolver();
+
+       // Open connection
+       //* DEBUG: */ die('SCRIPT=' . $script);
+       if ($useProxy === true) {
+               // Resolve hostname into IP address
+               $ip = $resolver->resolveHostname(compileRawCode(getProxyHost()));
+
+               // Connect to host through proxy connection
+               $fp = 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);
+       }
+
+       // 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, $port, $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
+
+               // Check for broken HTTP implementations
+               if (substr(strtolower($line), 0, 7) == 'server:') {
+                       // Anomic (see http://anomic.de, http://yacy.net) is currently broken
+                       $GLOBALS['is_http_server_broken'] = in_array(trim(substr(strtolower($line), 7)), array('anomichttpd'));
+               } // END - if
+
+               // Add it to response
+               $response[] = $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('', '', '');
+       } else {
+               // Check array for chuncked encoding
+               $response = unchunkHttpResponse($response);
+       } // END - if
+
+       // Return response
+       return $response;
+}
+
+// Sets up a proxy tunnel for given hostname and through resource
+function setupProxyTunnel ($host, $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');
+
+       // Use login data to proxy? (username at least!)
+       if (getProxyUsername() != '') {
+               // Add it as well
+               $encodedAuth = base64_encode(compileRawCode(getProxyUsername()) . ':' . compileRawCode(getProxyPassword()));
+               $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;
+}
+
+// Check array for chuncked encoding
+function unchunkHttpResponse (array $response) {
+       // Default is not chunked
+       $isChunked = false;
+
+       // Check if we have chunks
+       foreach ($response as $line) {
+               // Make lower-case and trim it
+               $line = trim(strtolower($line));
+
+               // Entry found?
+               if ((strpos($line, 'transfer-encoding') !== false) && (strpos($line, 'chunked') !== false)) {
+                       // Found!
+                       $isChunked = true;
+                       break;
+               } // END - if
+       } // END - foreach
+
+       // Is it chunked?
+       if ($isChunked === true) {
+               // 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)));
+
+               // 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>');
+
+               // Re-add the headers
+               $response = merge_array($GLOBALS['http_headers'], stringToArray("\n", $tempResponse));
+       } // END - if
+
+       // Return the unchunked array
+       return $response;
+}
+
+// Removes HTTP header lines from a response array (e.g. output from send<Get|Post>Request() )
+function removeHttpHeaderFromResponse (array $response) {
+       // Save headers for later usage
+       $GLOBALS['http_headers'] = array();
+
+       // The first array element has to contain HTTP
+       if ((isset($response[0])) && (substr(strtoupper($response[0]), 0, 5) == 'HTTP/')) {
+               // Okay, we have headers, now remove them with a second array
+               $response2 = $response;
+               foreach ($response as $line) {
+                       // Remove line
+                       array_shift($response2);
+
+                       // Add full line to temporary global array
+                       $GLOBALS['http_headers'][] = $line;
+
+                       // Trim it for testing
+                       $lineTest = trim($line);
+
+                       // Is this line empty?
+                       if (empty($lineTest)) {
+                               // Then stop here
+                               break;
+                       } // END - if
+               } // END - foreach
+
+               // Write back the array
+               $response = $response2;
+       } // END - if
+
+       // Return the modified response array
+       return $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));
+
+       // ... and return it
+       return $isBroken;
+}
+
+//-----------------------------------------------------------------------------
+// Automatically re-created functions, all taken from user comments on www.php.net
+//-----------------------------------------------------------------------------
+
+if (!function_exists('http_build_query')) {
+       // Taken from documentation on www.php.net, credits to Marco K. (Germany) and some light mods by R.Haeder
+       function http_build_query($data, $prefix = '', $sep = '', $key = '') {
+               $ret = array();
+               foreach ((array) $data as $k => $v) {
+                       if (is_int($k) && $prefix != null) {
+                               $k = urlencode($prefix . $k);
+                       } // END - if
+
+                       if ((!empty($key)) || ($key === 0)) {
+                               $k = $key . '[' . urlencode($k) . ']';
+                       } // END - if
+
+                       if (is_array($v) || is_object($v)) {
+                               array_push($ret, http_build_query($v, '', $sep, $k));
+                       } else {
+                               array_push($ret, $k.'='.urlencode($v));
+                       }
+               } // END - foreach
+
+               if (empty($sep)) {
+                       $sep = ini_get('arg_separator.output');
+               } // END - if
+
+               return implode($sep, $ret);
+       }
+} // END - if
+
+if (!function_exists('http_chunked_decode')) {
+       /**
+        * dechunk an HTTP 'transfer-encoding: chunked' message.
+        *
+        * @param       $chunk          The encoded message
+        * @return      $dechunk        The decoded message. If $chunk wasn't encoded properly debug_report_bug() is being called
+        * @author      Marques Johansson
+        * @link        http://php.net/manual/en/function.http-chunked-decode.php#89786
+        */
+       function http_chunked_decode ($chunk) {
+               // Detect multi-byte encoding
+               $mbPrefix = detectMultiBytePrefix($chunk);
+               //* DEBUG: */ print 'mbPrefix=' . $mbPrefix . '<br />';
+
+               // Init some variables
+               $offset = 0;
+               $len = call_user_func_array($mbPrefix . 'strlen', array(($chunk)));
+               $dechunk = '';
+
+               // Walk through all chunks
+               while ($offset < $len) {
+                       // Where does the \r\n begin?
+                       $lineEndAt = call_user_func_array($mbPrefix . 'strpos', array($chunk, getConfig('HTTP_EOL'), $offset));
+
+                       /* DEBUG: *
+                       print 'lineEndAt[<em>'.__LINE__.'</em>]='.$lineEndAt.'<br />
+offset[<em>'.__LINE__.'</em>]='.$offset.'<br />
+len='.$len.'<br />
+next[offset,10]=<pre>'.replaceReturnNewLine(htmlentities(call_user_func_array($mbPrefix . 'substr', array($chunk, $offset, 10)))).'</pre>';
+                       /* DEBUG: */
+
+                       // Get next hex-coded chunk length
+                       $chunkLenHex = call_user_func_array($mbPrefix . 'substr', array($chunk, $offset, ($lineEndAt - $offset)));
+
+                       /* DEBUG: *
+                       print 'chunkLenHex[<em>'.__LINE__.'</em>]='.replaceReturnNewLine(htmlentities($chunkLenHex)).'<br />
+';
+                       /* DEBUG: */
+
+                       // Validation if it is hexadecimal
+                       if (!isHexadecimal($chunkLenHex)) {
+                               // Please help debugging this
+                               //* DEBUG: */ die('ABORT:chunkLenHex=<pre>'.replaceReturnNewLine(htmlentities($chunkLenHex)).'</pre>');
+                               debug_report_bug(__FUNCTION__, __LINE__, 'Value ' . $chunkLenHex . ' is no valid hexa-decimal string.');
+
+                               // This won't be reached
+                               return $chunk;
+                       } // END - if
+
+                       // Position of next chunk is right after \r\n
+                       $offset   = $offset + strlen($chunkLenHex) + strlen(getConfig('HTTP_EOL'));
+                       $chunkLen = hexdec(rtrim($chunkLenHex, getConfig('HTTP_EOL')));
+
+                       /* DEBUG: *
+                       print 'chunkLen='.$chunkLen.'<br />
+offset[<em>'.__LINE__.'</em>]='.$offset.'<br />';
+                       /* DEBUG: */
+
+                       // Moved out for debugging
+                       $next  = call_user_func_array($mbPrefix . 'substr', array($chunk, $offset, $chunkLen));
+                       //* DEBUG: */ print 'next=<pre>'.replaceReturnNewLine(htmlentities($next)).'</pre>';
+
+                       /*
+                        * Hack for e.g. YaCy HTTPDaemon (Anomic Server), this HTTP server
+                        * is currently (revision 7567) broken and does not include the \r\n
+                        * characters when it does sent "chunked" messages.
+                        */
+                       $count = 0;
+                       if (isBrokenHttpServerImplentation()) {
+                               // Count occurrences of \r\n
+                               $count = call_user_func_array($mbPrefix . 'substr_count', array($next, getConfig('HTTP_EOL')));
+                       } // END - if
+
+                       // Correct it because we need to subtract occurrences of \r\n
+                       $chunkLen = hexdec(rtrim($chunkLenHex, getConfig('HTTP_EOL'))) - ($count * strlen(getConfig('HTTP_EOL')));
+
+                       // Add next chunk to $dechunk
+                       $dechunk .= call_user_func_array($mbPrefix . 'substr', array($chunk, $offset, $chunkLen));
+
+                       /* DEBUG: *
+                       print('offset[<em>'.__LINE__.'</em>]='.$offset.'<br />
+lineEndAt[<em>'.__LINE__.'</em>]='.$lineEndAt.'<br />
+len='.$len.'<br />
+count='.$count.'<br />
+chunkLen='.$chunkLen.'<br />
+chunkLenHex='.$chunkLenHex.'<br />
+dechunk=<pre>'.replaceReturnNewLine(htmlentities($dechunk)).'</pre>
+chunk=<pre>'.replaceReturnNewLine(htmlentities($chunk)).'</pre>');
+                       /* DEBUG: */
+
+                       // Is $offset + $chunkLen larger than or equal $len?
+                       if (($offset + $chunkLen) >= $len) {
+                               // Then stop processing here
+                               break;
+                       } // END - if
+
+                       // Calculate next offset of chunk
+                       $offset = call_user_func_array($mbPrefix . 'strpos', array($chunk, getConfig('HTTP_EOL'), $offset + $chunkLen)) + 2;
+
+                       /* DEBUG: *
+                       print('offset[<em>'.__LINE__.'</em>]='.$offset.'<br />
+next[100]=<pre>'.replaceReturnNewLine(htmlentities(call_user_func_array($mbPrefix . 'substr', array($chunk, $offset, 100)))).'</pre>
+---:---:---:---:---:---:---:---:---<br />
+');
+                       /* DEBUG: */
+               } // END - while
+
+               // Return de-chunked string
+               return $dechunk;
+       }
+} // END - if
+
+// [EOF]
+?>