[?php

function deleteNodes($xpath, $query){
	$items = $xpath->evaluate($query);
	for ($i = 0; $i < $items->length; $i++) {
           $items->item($i)->parentNode->removeChild($items->item($i));
    }
}

function getPage($url){
        $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
        curl_setopt($curl, CURLOPT_AUTOREFERER, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($curl, CURLOPT_TIMEOUT, 2 );                

        $html = curl_exec( $curl );
        curl_close( $curl );
		return $html;
}

function processKorrespondentFeed($url){
		$html = getPage($url);

        //$html = @mb_convert_encoding($html, 'utf-8', 'windows-1251');   

        
        $dom = new DOMDocument("1", "utf-8");
        @$dom->loadXML($html);
		$dom->encoding = "utf-8";
        $xpath = new DOMXPath($dom);
       
        deleteNodes($xpath, "/rss/channel/item/category");
		deleteNodes($xpath, "/rss/channel/item/comments");
		deleteNodes($xpath, "/rss/channel/item/source");
		deleteNodes($xpath, "/rss/channel/item/description");

		$items = $xpath->evaluate("/rss/channel/item/link");
		for ($i = 0; $i < $items->length; $i++) {
			$itemUrl = $items->item($i)->nodeValue . "/print";
			$itemText = getPage($itemUrl);
			$itemText = strip_tags_attributes(@mb_convert_encoding($itemText, 'utf-8', 'windows-1251'));   
			$node = $dom->createElement("description");
			$nodeContent = $dom->createCDATASection($itemText);
			$items->item($i)->parentNode->appendChild($node);
			$node->appendChild($nodeContent);
		}


		return $dom->saveXML();
	}
	
function strip_tags_attributes($sSource, $aAllowedTags = array(), $aDisabledAttributes = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavaible', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragdrop', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterupdate', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmoveout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload'))
    {
        if (empty($aDisabledAttributes)) return strip_tags($sSource, implode('', $aAllowedTags));

        return preg_replace('/\s(' . implode('|', $aDisabledAttributes) . ').*?([\s\>])/', '\\2', preg_replace('/<(.*?)>/ie', "'<' . preg_replace(array('/javascript:[^\"\']*/i', '/(" . implode('|', $aDisabledAttributes) . ")[ \\t\\n]*=[ \\t\\n]*[\"\'][^\"\']*[\"\']/i', '/\s+/'), array('', '', ' '), stripslashes('\\1')) . '>'", strip_tags($sSource, implode('', $aAllowedTags))) );
    }	
?]