| 937 | | // we make sure the request takes less than a few seconds to fail |
| 938 | | // we create a stream_context (works in php >= 5.2.1) |
| 939 | | // we also set the socket_timeout (for php < 5.2.1) |
| 940 | | $default_socket_timeout = @ini_get('default_socket_timeout'); |
| 941 | | @ini_set('default_socket_timeout', $timeout); |
| 942 | | |
| 943 | | $ctx = null; |
| 944 | | if(function_exists('stream_context_create')) { |
| 945 | | $ctx = stream_context_create(array('http' => array( 'timeout' => $timeout))); |
| | 938 | // Parse the url for fsockopen |
| | 939 | $fsockurl = @parse_url($url); |
| | 940 | $fgetsData = ""; |
| | 941 | // Needs to be initialized, otherwise the while below fails |
| | 942 | $streamMetaData = array('timed_out' => false); |
| | 943 | if(empty($fsockurl['port'])) |
| | 944 | { |
| | 945 | $fsockurl['port'] = 80; |
| | 946 | } |
| | 947 | if(!empty($fsockurl['query'])) |
| | 948 | { |
| | 949 | $fsockurl['path'].="?" . $fsockurl['query']; |
| | 950 | } |
| | 951 | // Make the request |
| | 952 | $fsock = @fsockopen($fsockurl['host'], $fsockurl['port'], $errno, $errstr, $timeout); |
| | 953 | if($fsock||!is_resource($fsock)) |
| | 954 | { |
| | 955 | @fputs($fsock, "GET " . $fsockurl['path'] . " HTTP/1.0\r\n"); |
| | 956 | @fputs($fsock, "HOST: " . $fsockurl['host'] . "\r\n"); |
| | 957 | @fputs($fsock, "Connection: close\r\n\r\n"); |
| | 958 | @stream_set_blocking($fsock, TRUE); |
| | 959 | @stream_set_timeout($fsock,$timeout); |
| | 960 | |
| | 961 | while ((!@feof($fsock)) && (!$streamMetaData['timed_out'])) |
| | 962 | { |
| | 963 | // Load data as long as there is data and the connection didn't time out |
| | 964 | $fgetsData .= @fgets($fsock, 1024); |
| | 965 | $streamMetaData = @stream_get_meta_data($fsock); |
| | 966 | @ob_flush; |
| | 967 | @flush(); |
| | 968 | } |
| | 969 | |
| | 970 | @fclose($fsock); |
| | 971 | unset($fsockurl, $fsock); |
| 952 | | @ini_set('default_socket_timeout', $default_socket_timeout); |
| | 976 | // The connection didn't time out and there is data available |
| | 977 | // Split into headers & body |
| | 978 | $hunks = @explode("\r\n\r\n",$fgetsData); |
| | 979 | if (is_array($hunks) && count($hunks) >= 2) |
| | 980 | { |
| | 981 | $headers = @explode("\n",$hunks[count($hunks) - 2]); |
| | 982 | $body = $hunks[count($hunks) - 1]; |
| | 983 | unset($hunks); |
| | 984 | if (is_array($headers) && count($headers) >= 1) |
| | 985 | { |
| | 986 | // Check if the server also told us that everything went well |
| | 987 | switch(trim(strtolower($headers[0]))) |
| | 988 | { |
| | 989 | case 'http/1.0 100 ok': |
| | 990 | case 'http/1.0 200 ok': |
| | 991 | case 'http/1.1 100 ok': |
| | 992 | case 'http/1.1 200 ok': |
| | 993 | // Generally the answer will be ok |
| | 994 | $response = true; |
| | 995 | // Now check the content-length if available |
| | 996 | foreach($headers as $header) { |
| | 997 | if(substr(trim(strtolower($header)),0,15) == "content-length:") { |
| | 998 | // Check the length against the content |
| | 999 | if(substr(trim($header),16) != strlen($body)) { |
| | 1000 | //Reset the response if this is the wrong length |
| | 1001 | $response = false; |
| | 1002 | } |
| | 1003 | } |
| | 1004 | } |
| | 1005 | break; |
| | 1006 | } |
| | 1007 | } |
| | 1008 | } |
| | 1009 | } |
| | 1010 | |
| | 1011 | if($response === true) { |
| | 1012 | return trim($body); |
| | 1013 | } else { |
| | 1014 | return false; |