<?php
function GetContent( $url )
{
$server = strtolower( preg_replace ("~^(?:http://|//)?(?:[a-z0-9\-_]+\.)*([a-z0-9\-_]+\.[a-z0-9\-_]+).*?$~is", "$1", $url ) );
$host = strtolower( preg_replace ("~^(?:http://|//)?((?:[a-z0-9\-_]+\.)+[a-z0-9\-_]+).*?$~is", "$1", $url ) );
$ruri = preg_replace ("~^(?:http://)?(?:[a-z0-9\-_]+\.)+[a-z0-9\-_]+(.*)$~is", "$1", $url );
$socket = @fsockopen( $server, 80, $errno, $errstr, 5 ) or $socket = false;
if (!$socket)
return @file_get_contents ($url);
$request = "GET " . $ruri . " HTTP/1.0\r\n";
$request .= "Host: " . $host . "\r\n";
$request .= "Accept: text/html, application/xml;q=0.9, */*;q=0.1\r\n";
$request .= "Accept-Charset: windows-1251, utf-8;q=0.6, *;q=0.1\r\n";
$request .= "Accept-Encoding: deflate, identity, *;q=0\r\n";
$request .= "Accept-Language: ru\r\n";
$request .= "Connection: close\r\n";
$request .= "Keep-Alive: 300\r\n";
$request .= "Expires: Thu, 01 Jan 1970 00:00:01 GMT\r\n";
$request .= "Cache-Control: no-store, no-cache, must-revalidate\r\n";
$request .= "Pragma: no-cache\r\n";
$request .= "Cookie: income=1\r\n";
//$request .= "Referer: http://" . $ref . "/\r\n";
$request .= "User-Agent: Mozilla/5.0 (compatible; MSIE 6.0; Windows 98)\r\n";
$request .= "\r\n";
fputs($socket, $request);
$fcont = "";
while ($line = @fgets ($socket, 1024))
$fcont .= $line;
fclose($socket);
$pos = strpos ( $fcont, "\r\n\r\n" );
if ($pos !== false)
$fcont = substr ( $fcont, $pos + 4 );
if (!$fcont)
$fcont = @file_get_contents ($url);
return $fcont;
}
function GetAdvert()
{
$m_host = $_SERVER['HTTP_HOST'];
$m_ruri = $_SERVER['REQUEST_URI'];
$dbfile = dirname(__FILE__)."/advert.db";
if (isset($_GET['cleardbfile'])) @unlink ($dbfile);
$ftime = (file_exists($dbfile) ? filemtime( $dbfile ):0);
$elapsed = time() - $ftime > 3600;
//Если данные устарели, обновляем
if ($elapsed)
{
$fcont = @GetContent("http://linksbase.ru/links/".$m_host) or $fcont = "";
//@file_put_contents( $dbfile, $fcont );
$fptr = fopen($dbfile, "w");
@fwrite($fptr, $fcont);
@fclose($fptr);
}
else
$fcont = @file_get_contents( $dbfile );
$links = "<!--linksbase-->";
$uricell = "#uri:#always#lnk:#";
$pos = strpos ( $fcont, $uricell );
if ($pos !== false)
{
$pos += strlen ( $uricell );
$end = strpos ( $fcont, "#uri:#", $pos );
$end = ($end !== false) ? $end : strlen ($fcont);
$links = substr ( $fcont, $pos, $end-$pos );
}
$uricell = "#uri:#".$m_ruri."#lnk:#";
$pos = strpos ( $fcont, $uricell );
if ($pos !== false)
{
$pos += strlen ( $uricell );
$end = strpos ( $fcont, "#uri:#", $pos );
$end = ($end !== false) ? $end : strlen ($fcont);
$links = substr ( $fcont, $pos, $end-$pos );
}
//Выводим текст
return $links;
}
$smarty->assign("GetAdvert",GetAdvert());
?>
Попробуйте так. Но честно говоря скрипт не совсем понятный...