<?php

class RSS_READ {

	/**
	* The remote RSS file to open (eg http://www._null_.com/content/blogrss.php)
	*
	* @var String
	*/
	var $remoteURL;

	/**
	* The string containing the raw XML string
	*
	* @var String
	*/
	var $RSSContent;

	/**
	* The container snippet that will be used when processing
	*
	* @see ProcessRSS
	* @var String
	*/
	var $RSSContainer = "PageRSSContainer";

	/**
	* The snippet that each RSS item will get when processed
	*
	* @see ProcessRSS
	* @var String
	*/
	var $RSSSnippet = "PageRSSItem";

	/**
	* The name of RSS
	*
	* @var String
	*/
	var $RSSName = "";

	/**
	* If set, when processing the snippet it will pass the link and text to this callback function. Users call_user_func_array.
	*
	* @see ProcessRSS
	* @var Mixed
	*/
	var $RSSTemplateCallBack = false;

	/**
	* The main RSS array after the XML is processed
	*
	* @see ProcessXML
	* @var String
	*/
	var $RSSArray = array();

	/**
	* Tracks the current tag that the XML parser is using.
	*
	* @type private
	* @var String
	*/
	var $_currentTag = false;

	/**
	* Tracks when inside the correct RSS tag
	*
	* @type private
	* @var String
	*/
	var $_inItemTag = false;

	/**
	* Running of count of the number of items in the array.
	*
	* @type private
	* @var String
	*/
	var $_currentCount = -1;

	var $templateObj;

	function RSS_READ($remoteURL=false) {

		if ($remoteURL) {
			$this->remoteURL = $remoteURL;
		}
	}

	/**
	* ReturnProcessedRSS
	* Pulls down the URL from remoteURL, parses and process it.
	*
	* @return String
	*/
	function ReturnProcessedRSS() {
		$this->GetRSS();

		return $this->ProcessRSS();
	}

	function GetRSS() {
		if (!$this->remoteURL) {
			return false;
		}

		$this->RSSContent = $this->PostToRemoteFileAndGetResponse($this->remoteURL);

		return $this->RSSContent;
	}

	function ProcessRSS() {
		$count = 0;
		if (!$this->RSSContent) {
			return false;
		}

		$this->unhtmlentities($this->RSSContent);

		$this->ProcessXML();

		if (!$this->RSSArray) {
			return false;
		}

		$output = "";
		foreach ($this->RSSArray as $val) {
			$count++;
			if ($this->RSSTemplateCallBack) {
				$returnArray = call_user_func_array(array($this,"formatHelpCallBack"),array($val['title'],$val['link']));
				$val['title'] = $returnArray[0];
				$val['link'] = $returnArray[1];
			}

			$GLOBALS['RSSTitle'] = $val['title'];
			$GLOBALS['RSSLink'] = $val['link'];

			$output .= $this->templateObj->GetSnippet($this->RSSSnippet);
			if ($count == 10) {
				break;
			}
		}

		if (isset($this->RSSName) && $this->RSSName != '') {
			$cache = new Cache();
			$cache->SetDir('cache');
			$cacheId = $this->RSSName;
			if(!$cache->HasExpired($cacheId)){
				$GLOBALS['RSSBody'] = $cache->ReadCache($cacheId);
				return $this->templateObj->GetSnippet($this->RSSContainer);
			}
			$cache->WriteCache($cacheId, $output);
		}
		$GLOBALS['RSSBody'] = $output;
		return $this->templateObj->GetSnippet($this->RSSContainer);
	}

	function ProcessXML() {
		$xml_parser = xml_parser_create();

		xml_set_object($xml_parser, $this);
		xml_set_element_handler($xml_parser, "_xmlElStart", "_xmlElEnd");
		xml_set_character_data_handler($xml_parser, "_xmlCData");

		if (!xml_parse($xml_parser, $this->RSSContent)) {
			die(sprintf("XML error: %s at line %d",	xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
		}

		xml_parser_free($xml_parser);
	}

	function _xmlElStart($parser, $name, $attrs) {
		$this->_currentTag = $name;

		if ($name == "ITEM") {
			$this->_inItemTag = true;
			$this->_currentCount++;
		}
	}

	function _xmlElEnd($parser, $name) {
		if ($name == "ITEM") {
			$this->_inItemTag = false;
			$this->_currentTag = false;
		}
	}

	function _xmlCData($parser, $data) {

		if (($this->_inItemTag) && (trim($data) != "")) {
			if ($this->_currentTag == "TITLE") {
				$this->RSSArray[$this->_currentCount]['title'] = $data;
			} else if ($this->_currentTag == "LINK") {
				$this->RSSArray[$this->_currentCount]['link'] = $data;
			}
		}
	}

	function PostToRemoteFileAndGetResponse($path) {

		$psplit = explode("?",$path);

		$vars = "";
		if (isset($psplit[1])) {
			$vars = $psplit[1];
		}

		$result = null;

		if (is_callable("curl_exec")) {
			// Use CURL if it's available
			$ch = curl_init($path);

			if (!@curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1)) {
				return false;
			}

			@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

			if (!@curl_setopt($ch, CURLOPT_TIMEOUT, 10)) {
				return false;
			}

			if ($vars != "") {
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
			}

			$result = curl_exec($ch);

			if (!$result) {
				return false;
			}

		} else {
			if (!$result = file_get_contents($path)) {
				return false;
			}
		}

		return $result;
	}

	function formatHelpCallBack($title,$link) {
		$linkCut = substr($link,0,-1);
		$linkID = substr($linkCut,strrpos($linkCut,"/")+1);
		$link = "javascript: LaunchHelp(".$linkID.");";

		return array($title,$link);
	}

	function unhtmlentities($string) {
		$trans_tbl = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
		$trans_tbl = array_flip($trans_tbl);
		$trans_tbl += array('&apos;' => "'");

		return strtr($string, $trans_tbl);
	}
}

?>