<?php
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/header.php");
$GLOBALS['APPLICATION']->RestartBuffer(); 
$xmlfile[] = "iblock_rss_4.xml";
$xmlfile[] = "iblock_rss_5.xml";
$xmlfile[] = "iblock_rss_6.xml";
$xmlfile[] = "iblock_rss_7.xml";
$xmlfile[] = "iblock_rss_8.xml";
$xmlfile[] = "iblock_rss_11.xml";
$xmlfile[] = "iblock_rss_12.xml";
$xmlfile[] = "iblock_rss_13.xml";
$xmlfile[] = "iblock_rss_14.xml";
$xmlfile[] = "iblock_rss_15.xml";
$xmlfile[] = "iblock_rss_16.xml";
$xmlfile[] = "iblock_rss_20.xml";

$linkstr = array();
$file1 = $_SERVER["DOCUMENT_ROOT"]."/upload/iblock_rss_3.xml";
$fp = fopen($file1,"r");
$data = fread($fp,filesize($file1));
fclose($fp);
$xmlmain = simplexml_load_string($data);
$code = array();
$xmlmain->channel->title = "Карта сайта";
foreach($xmlmain->channel->item as $key=>$item) {
	$link = explode("/",(String)$item->link);
	$code[(String)$item->link] = $link[(count($link)-2)];
}
	
foreach($code as $key=>$item) {
	$items[] = $item;
}
$arFilter = Array("IBLOCK_TYPE" => "portal", 'CODE'=>$items);
$db_list = CIBlockElement::GetList(Array(), $arFilter, false,false,array("CODE","DATE_CREATE"));
while($ar_result = $db_list->GetNext())
{
	$code[$ar_result['CODE']] = 'http://www.apiworld.ru/'.strtotime($ar_result['DATE_CREATE']).'.html';
}
foreach($xmlmain->channel->item as $key=>$item) {
	$link = explode("/",(String)$item->link);
	$link = $code[$link[(count($link)-2)]];
	$linkstr[]=array("link"=>$link,"date"=>(String)$item->pubDate);
}

foreach($xmlfile as $xmlitem) {
	$file1 = $_SERVER["DOCUMENT_ROOT"]."/upload/".$xmlitem;
	$fp = fopen($file1,"r");
	$data = fread($fp,filesize($file1));
	fclose($fp);
	$xml = simplexml_load_string($data);
	$code = array();
	foreach($xml->channel->item as $key=>$item) {
		$link = explode("/",(String)$item->link);
		$code[(String)$item->link] = $link[(count($link)-2)];
	}
	
	foreach($code as $key=>$item) {
		$items[] = $item;
	}
	$arFilter = Array("IBLOCK_TYPE" => "portal", 'CODE'=>$items);
	$db_list = CIBlockElement::GetList(Array(), $arFilter, false,false,array("CODE","DATE_CREATE"));
	while($ar_result = $db_list->GetNext())
	{
		$code[$ar_result['CODE']] = 'http://www.apiworld.ru/'.strtotime($ar_result['DATE_CREATE']).'.html';
	}
	foreach($xml->channel->item as $key=>$item) {
		$cat = (String)$item->category;
		$link = explode("/",(String)$item->link);
		//var_dump($link[(count($link)-2)],(int)$link[(count($link)-2)]);
		if(substr($cat,0,22)!='Фотогалерея') {
			$link = $code[$link[(count($link)-2)]];
			if($link=="") {
				$linkstr[]=array("link"=>(String)$item->link,"date"=>(String)$item->pubDate);
			}else $linkstr[]=array("link"=>$link,"date"=>(String)$item->pubDate);
		}else {
			$link1 = $link[(count($link)-3)];
			unset($link[(count($link)-2)]);
			//var_dump($link1);
			$linkstr[$link1]=array("link"=>implode("/",$link),"date"=>(String)$item->pubDate);
		}
	}
}
//exit();
//var_dump($linkstr);

#echo $xmlmain->asXML();
//die();

$str  ='<?xml version="1.0" encoding="UTF-8"?>';
$str .= "\n".'<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"';
$str .= "\n".'         xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

$template .= "\n".'	<url>'."\n";
$template .= '		<loc>{link}</loc>'."\n";
$template .= '		<lastmod>{date}</lastmod>'."\n";
$template .= '		<priority>0.8</priority>'."\n";
$template .= '	</url>';

$i=0;
foreach($linkstr as $item) {
	if($item["link"]!="") {
		$template1 = str_replace(array('{link}','{date}'),array($item["link"],date("Y-m-d",strtotime($item["date"]))),$template);
		$str .= $template1;
		$i++;
	}
}
$str .= "\n".'</urlset> ';

$fp = fopen("rss.xml","w+");
fwrite($fp,$str);
fclose($fp);

$file1 = $_SERVER["DOCUMENT_ROOT"]."/upload/rss.xml";
$fp = fopen($file1,"r");
$data = fread($fp,filesize($file1));
fclose($fp);

header("Content-Type: text/xml");
header("Expires: Thu, 19 Feb 1998 13:24:18 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Cache-Control: post-check=0,pre-check=0");
header("Cache-Control: max-age=0");
header("Pragma: no-cache");

echo $data;
die();

?>