--- piwik/core/Piwik.php        2009-06-24 06:29:22.000000000 +0200
+++ core/Piwik.php      2009-06-25 17:54:21.000000000 +0200
@@ -927,6 +927,7 @@
        /**
         * Sends http request ensuring the request will fail before $timeout seconds
         * Returns the response content (no header, trimmed)
+         * @author Uli <m [AT] il [DOT] wolf-u [DOT] li>
         * @param string $url
         * @param int $timeout
         * @return string|false false if request failed
@@ -934,24 +935,84 @@
        static public function sendHttpRequest($url, $timeout)
        {
                $response = false;
-               // we make sure the request takes less than a few seconds to fail
-               // we create a stream_context (works in php >= 5.2.1)
-               // we also set the socket_timeout (for php < 5.2.1)
-               $default_socket_timeout = @ini_get('default_socket_timeout');
-               @ini_set('default_socket_timeout', $timeout);
-
-               $ctx = null;
-               if(function_exists('stream_context_create')) {
-                       $ctx = stream_context_create(array('http' => array( 'timeout' => $timeout)));
+               // Parse the url for fsockopen
+               $fsockurl = @parse_url($url);
+               $fgetsData = "";
+               // Needs to be initialized, otherwise the while below fails
+               $streamMetaData = array('timed_out' => false);
+               if(empty($fsockurl['port']))
+               {
+                       $fsockurl['port'] = 80;
+               }
+               if(!empty($fsockurl['query']))
+               {
+                       $fsockurl['path'].="?" . $fsockurl['query'];
+               }
+               // Make the request
+               $fsock = @fsockopen($fsockurl['host'],  $fsockurl['port'], $errno, $errstr, $timeout);
+               if($fsock||!is_resource($fsock))
+               {
+                       @fputs($fsock, "GET " . $fsockurl['path'] . " HTTP/1.0\r\n");
+                       @fputs($fsock, "HOST: " . $fsockurl['host'] . "\r\n");
+                       @fputs($fsock, "Connection: close\r\n\r\n");
+                       @stream_set_blocking($fsock, TRUE);
+                       @stream_set_timeout($fsock,$timeout);
+
+                       while ((!@feof($fsock)) && (!$streamMetaData['timed_out']))
+                       {
+                               // Load data as long as there is data and the connection didn't time out
+                               $fgetsData .= @fgets($fsock, 1024);
+                               $streamMetaData = @stream_get_meta_data($fsock);
+                               @ob_flush;
+                               @flush();
+                       }
+
+                       @fclose($fsock);
+                       unset($fsockurl, $fsock);
                }
-               $response = trim(@file_get_contents($url, 0, $ctx));

-               // restore the socket_timeout value
-               if(!empty($default_socket_timeout))
+               if (!$streamMetaData['timed_out'] && $fgetsData != "")
                {
-                       @ini_set('default_socket_timeout', $default_socket_timeout);
+                       // The connection didn't time out and there is data available
+                       // Split into headers & body
+                       $hunks = @explode("\r\n\r\n",$fgetsData);
+                       if (is_array($hunks) && count($hunks) >= 2)
+                       {
+                               $headers = @explode("\n",$hunks[count($hunks) - 2]);
+                               $body = $hunks[count($hunks) - 1];
+                               unset($hunks);
+                               if (is_array($headers) && count($headers) >= 1)
+                               {
+                                       // Check if the server also told us that everything went well
+                                       switch(trim(strtolower($headers[0])))
+                                       {
+                                               case 'http/1.0 100 ok':
+                                               case 'http/1.0 200 ok':
+                                               case 'http/1.1 100 ok':
+                                               case 'http/1.1 200 ok':
+                                                       // Generally the answer will be ok
+                                                       $response = true;
+                                                       // Now check the content-length if available
+                                                       foreach($headers as $header) {
+                                                               if(substr(trim(strtolower($header)),0,15) == "content-length:") {
+                                                                       // Check the length against the content
+                                                                       if(substr(trim($header),16) != strlen($body)) {
+                                                                               //Reset the response if this is the wrong length
+                                                                               $response = false;
+                                                                       }
+                                                               }
+                                                       }
+                                               break;
+                                       }
+                               }
+                       }
+               }
+
+               if($response === true) {
+                       return trim($body);
+               } else {
+                       return false;
                }
-               return $response;
        }

        /**
