cvs: phpweb / results.php /include site.inc

From: Date: Mon, 21 Apr 2008 11:00:33 +0000
Subject: cvs: phpweb / results.php /include site.inc
Groups: php.webmaster 
Request: Send a blank email to php-webmaster+get-1582@lists.php.net to get a copy of this message
bjori Mon Apr 21 11:00:33 2008 UTC Modified files: /phpweb/include site.inc /phpweb results.php Log: Add compatibility function for file_get_conents() and get_headers() http://cvs.php.net/viewvc.cgi/phpweb/include/site.inc?r1=1.255&r2=1.256&diff_format=u Index: phpweb/include/site.inc diff -u phpweb/include/site.inc:1.255 phpweb/include/site.inc:1.256 --- phpweb/include/site.inc:1.255 Tue Feb 21 20:46:45 2006 +++ phpweb/include/site.inc Mon Apr 21 11:00:33 2008 @@ -1,6 +1,6 @@ <?php // -*- C++ -*- -// $Id: site.inc,v 1.255 2006/02/21 20:46:45 rasmus Exp $ +// $Id: site.inc,v 1.256 2008/04/21 11:00:33 bjori Exp $ // Define $MIRRORS array, and some constants include $_SERVER['DOCUMENT_ROOT'] . '/include/mirrors.inc'; @@ -173,6 +173,104 @@ } } + +// Compatibility function to fetch data from external source +function fetch_contents($url, $headers = false) { + if(function_exists('file_get_contents') && ini_get('allow_url_fopen')) { + $data = @file_get_contents($url); + if ($headers) { + return $http_response_header; + } + + return $data; + } + + // Let's find another way + if(function_exists('curl_exec')) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_HEADER, (int)$headers); + + $data = curl_exec($ch); + curl_close($ch); + + if ($headers) { + $array = explode("\n", $data); + foreach($array as $line) { + if (strlen($line)<=2) { + return $header; + } else { + $header[] = $line; + } + } + } + return $data; + } + + $array = parse_url($url); + if(function_exists('fsockopen') && $fd = @fsockopen($array["host"], 80, $errno, $errstr, 15)) { + $data = ""; + $header = array(); + $body = false; + + $path = $array["path"]; + if (isset($array["query"])) { + $path .= "?" . $array["query"]; + } + fputs($fd,"GET {$path} HTTP/1.0\r\n"); + fputs($fd,"Host: {$array["host"]}\r\n"); + fputs($fd,"Connection: close\r\n\r\n"); + + while($line = fgets($fd)) { + if($body) { + $data .= $line; + } elseif(strlen($line)<=2) { + $body = true; + } else { + $header[] = $line; + } + } + fclose($fd); + + if ($headers) { + return $header; + } + + return $data; + } + + // Redirect to www.php.net ? + return array("ERROR" => "Unable to find a way to retrieve data"); +} + +// Compatibility function to fetch headers from external source +function fetch_header($url, $header) { + $headers = array(); + + if(function_exists("get_headers") && ini_get('allow_url_fopen')) { + $headers = get_headers($url, 1); + } else { + $data = fetch_contents($url, true); + foreach($data as $line) { + if (($pos = strpos($line, ":")) !== false) { + $headers[substr($line, 0, $pos)] = trim(substr($line, $pos+1)); + } else { + $headers[0] = trim($line); + } + } + } + if (!is_array($headers) || empty($headers)) { + return null; + } + if (is_string($header)) { + $headers = array_change_key_case($headers, CASE_LOWER); + } + + return isset($headers[$header]) ? $headers[$header] : null; +} + + // Guess the current site from what Apache tells us. // This should be the main name of the mirror (in case // it works under more then one name). SERVER_NAME is @@ -218,3 +316,4 @@ if (isset($_SERVER['BASE_PAGE'])) { $_SERVER['BASE_HREF'] = $MYSITE . $_SERVER['BASE_PAGE']; } else { unset($_SERVER['BASE_HREF']); } + http://cvs.php.net/viewvc.cgi/phpweb/results.php?r1=1.43&r2=1.44&diff_format=u Index: phpweb/results.php diff -u phpweb/results.php:1.43 phpweb/results.php:1.44 --- phpweb/results.php:1.43 Fri Apr 11 17:04:40 2008 +++ phpweb/results.php Mon Apr 21 11:00:33 2008 @@ -1,5 +1,5 @@ <?php -// $Id: results.php,v 1.43 2008/04/11 17:04:40 bjori Exp $ +// $Id: results.php,v 1.44 2008/04/21 11:00:33 bjori Exp $ $_SERVER['BASE_PAGE'] = 'results.php'; include $_SERVER['DOCUMENT_ROOT'] . '/include/prepend.inc'; #include $_SERVER['DOCUMENT_ROOT'] . '/include/loadavg.inc'; @@ -45,30 +45,9 @@ $srch_rqst = "/ws.php?profile=$scope&q=$q&lang=$l&results=$per_page&start=$s&mirror=".trim(substr($MYSITE,7),'/'); $url = "http://".$srch_host.$srch_rqst; -if(function_exists('file_get_contents') && ini_get('allow_url_fopen')) { - $data = @file_get_contents($url); -} else { - // Let's find another way - if(function_exists('curl_exec')) { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_HEADER, 0); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - $data = curl_exec($ch); - curl_close($ch); - } else if(function_exists('fsockopen') && $fd = @fsockopen($srch_host, 80, $errno, $errstr, 15)) { - $data = ''; $header = false; - fputs($fd,"GET $srch_rqst HTTP/1.0\r\n"); - fputs($fd,"Host: $srch_host\r\n"); - fputs($fd,"Connection: close\r\n\r\n"); - while($line = fgets($fd)) { - if($header) $data .= $line; - if(strlen($line)<=2) $header = true; - } - fclose($fd); - } else { - // Redirect to www.php.net ? - } +$data = fetch_contents($url); +if (is_array($data)) { + exit_with_pretty_error("Search error", "Internal error", "This mirror does not support searches, please report this error to <a href='/contact'>our webmasters</a>"); } $res = unserialize($data);

« previous php.webmaster (#1582) next »