<?
	echo "<"."?xml version=\"1.0\" encoding=\"windows-1251\"?".">\n";
	echo "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\n";
	echo "\t<channel>\n";
	echo "<atom:link href=\"http://mehanika.ru/rss/news.xml\" rel=\"self\" type=\"application/rss+xml\" />\n";
	echo "\t<title>Новости компании &quot;Механика&quot;</title>\n";
	echo "\t<link>http://mehanika.ru/</link>\n";
	echo "\t<description>Территория Вашего двигателя</description>\n";
	echo "\t<generator>mikle[at]mehanika.ru</generator>\n";
	echo "\t<pubDate>".date("D, d M Y H:i:s +0300")."</pubDate>\n";
	echo "\t<image>\n\t\t<url>http://mehanika.ru/img/mehanika_logo.gif</url>\n\t\t<link>http://mehanika.ru</link>\n\t\t<title>Компания &quot;Механика&quot;</title>\n\t</image>\n";


	if(file_exists($_SERVER[DOCUMENT_ROOT]."/_news.xml")) {
		$fp=fopen($_SERVER[DOCUMENT_ROOT]."/_news.xml","r");
		$buf="";
		while(!feof($fp)) {
			$buf.=str_replace("\r","",fgets($fp,4096));
		}
		fclose($fp);
		preg_match_all("/<item>(.*?)<\/item>/ims",$buf,$items);
		for($i=0;$i<count($items[1]);$i++) {
			$title=getTag("title",$items[1][$i]);
			$link=getTag("link",$items[1][$i]);
			$text=getTag("text",$items[1][$i]);
			$date=getTag("date",$items[1][$i]);
			if($title) {
				if($link) {
					if(strtolower(substr($link,0,4))!="http")
						$link="http://mehanika.ru".$link;
				} else
					$link="http://mehanika.ru/";
				echo "\t\t<item>\n";
				echo "\t\t\t<title>".my_decode(htmlspecialchars(my_encode($title)))."</title>\n";
				echo "\t\t\t<link>".my_decode(htmlspecialchars(my_encode($link)))."</link>\n";
				echo "\t\t\t<guid>".my_decode(htmlspecialchars(my_encode($link)))."</guid>\n";
				echo "\t\t\t<description>".my_decode(htmlspecialchars(my_encode($text)))."</description>\n";
				if($date) {
					$tmp=mktime(10,0,0,substr($date,3,2),substr($date,0,2),substr($date,6,5));
					$date=date("D, d M Y H:i:s +0300",$tmp);
					echo "\t\t\t<pubDate>".$date."</pubDate>\n";
				}
				echo "\t\t</item>\n";
			}
		}
	}

	echo "\t</channel>\n";
	echo "</rss>\n";

	$fp=fopen("news.log","a+");
	fwrite($fp,date("d M Y H:i:s")."\n");
	fclose($fp);


function getTag($s,$v) {
	preg_match("/<$s>(.*?)<\/$s>/ims",$v,$tmp);
	return  $tmp[1];
}

function my_encode($s) {
	return preg_replace("/&([^;]+);/i","~\\1;",$s);
}

function my_decode($s) {
	$s=str_replace("~mdash;","--",$s);	//из-за Firefox!
	$s=str_replace("~rarr;","&gt;",$s);
	$s=str_replace("~laquo;","&quot;",$s);
	$s=str_replace("~raquo;","&quot;",$s);
	return preg_replace("/~([^;]+);/i","&\\1;",$s);
}

?>
