<?php
	require_once( $GLOBALS['site_root'].'/includes/models/RSSItem.inc.php' );
	\Pacal\php\globals\PacalGlobals::loadClass( 'globals/PacalXMLParser' );

	class RSS {
		protected $_version = 2.0;
		protected $_title = '';
		protected $_link = '';
		protected $_description = '';
		protected $_items = array();
			


		public function __construct() {
			$this->_version = 2.0;
			$this->_items = array();
		}

		public function setVersion( $v ) { $this->_version = $v; }
		public function getVersion() { return $this->_version; }
		public function setTitle( $v ) { $this->_title = $v; }
		public function setLink( $v ) { $this->_link = $v; }
		public function setDescription( $v ) { $this->_description = $v; }

		public function clearItems() {
			$this->_items = array();
		}

		public function addItem( RSSItem $obj ) {
			$this->_items[] = $obj;
		}
		public function getItems() { return $this->_items; }

		public function render() {
			$current_locale = setlocale( LC_TIME , 0 );
			setlocale( LC_TIME , 'C' );
			$retval = '';
			$retval.='<?xml version="1.0" encoding="UTF-8"?>'."\n";
			$retval.=sprintf( '<rss version="%.1f">' , $this->_version )."\n";
			$retval.='<channel>';
			if (!empty( $this->_title )) $retval.=sprintf( '<title>%s</title>' , $this->_title );
			if (!empty( $this->_description )) $retval.=sprintf( '<description>%s</description>' , $this->_description );
			if (!empty( $this->_link )) $retval.=sprintf( '<link>%s</link>' , $this->_link );

			// items
			if (!empty( $this->_items )) foreach ($this->_items as $item) $retval.=$item->render();
			$retval.='</channel>';
			$retval.='</rss>';
			setlocale( LC_TIME , $current_locale );
			return $retval;
		}

		public function loadFromString( $string ) {
			\Pacal\php\globals\getPL()->logMessage( 'loading parser...' );
			$xmlp = new RSSParser();
			$xmlp->setRSS( $this );
			$xmlp->processHtmlTags( $string );
		}

	}

	class RSSParser extends \Pacal\php\globals\PacalXMLParser {
		protected $rss = null;
		
		protected $_in_channel = false;
		protected $_in_item = false;
		protected $_data = '';
		protected $_rss_attrs = array();
		protected $_current_item_tag = '';
		protected $_item_attrs = array();
		protected $_item_img = '';

		public function __construct() {
			parent::__construct();
		}
		public function setRSS( $parent ) { $this->rss = $parent; }

		protected function clearData() { $this->_data = ''; }
		protected function addData( $d ) { $this->_data.=html_entity_decode( $d , ENT_COMPAT , 'UTF-8' ); }

		public function parseData( $data ) {
			// \Pacal\php\globals\getPL()->logMessage( 'data:' . $data );
			$this->addData( $data );
		}

		protected function clearItemAttribs() {
			$this->_item_attrs = array();
			$this->_item_img = '';
		}

		protected function saveItemAttrib() {
			$this->_item_attrs[ $this->_current_item_tag ] = trim( $this->_data );
		}

		protected function closeItem() {
			$rssitem = new RSSItem( $this->rss );
			if (isset( $this->_item_attrs['title'] )) $rssitem->setTitle( $this->_item_attrs['title'] );
			if (isset( $this->_item_attrs['description'] )) $rssitem->setDescription( $this->_item_attrs['description'] );
			if (isset( $this->_item_attrs['link'] )) $rssitem->setUrl( $this->_item_attrs['link'] );
			if (isset( $this->_item_attrs['guid'] )) $rssitem->setGuid( $this->_item_attrs['guid'] );
			if (isset( $this->_item_attrs['author'] )) $rssitem->setAuthor( $this->_item_attrs['author'] );
			if (isset( $this->_item_attrs['pubDate'] )) {
				$dt = DateTime::createFromFormat( DATE_RSS , $this->_item_attrs['pubDate'] );
				if ($dt) {
					$rssitem->setPublicationDate( $dt->getTimestamp() );
				} else {
					$rssitem->setPublicationDate( time() );
				}
				// \Pacal\php\globals\getPL()->logMessage( 'dt:' . print_r( $dt , true ) );
			}
			if (!empty( $this->_item_img )) {
				$rssitem->setImage( $this->_item_img );
			} else if (isset( $this->_item_attrs['leadImage'] )) {
				$rssitem->setImage( $this->_item_attrs['leadImage'] );
			} else if (isset( $this->_item_attrs['dc:image'] )) {
				$rssitem->setImage( $this->_item_attrs['dc:image'] );
			}
			$this->rss->addItem( $rssitem );
		}

		protected function parseItemTag( $tag ) {
			switch ($tag['tagtype']) {
			case 'title':
			case 'description':
			case 'link':
			case 'guid':
			case 'author':
			case 'pubDate':
			case 'leadImage':
			case 'dc:image':
				$this->_current_item_tag = $tag['tagtype'];
				$this->clearData();
				break;
			case 'content:encoded':
				$this->_current_item_tag = 'description';
				$this->clearData();
				break;
			case '/title':
			case '/description':
			case '/content:encoded':
			case '/link':
			case '/guid':
			case '/author':
			case '/pubDate':
			case '/leadImage':
			case '/dc:image':
				$this->saveItemAttrib();
				break;
			case 'enclosure':
				$this->_item_img = $this->stripQuotesFromAttr( $tag['url'] );
				break;
			case '/item':
				$this->closeItem();
				$this->_in_item = false;
			}
		}

		protected function parseChannelTag( $tag ) {
			if ($this->_in_item) {
				$this->parseItemTag( $tag );
				return;
			}
			switch ($tag['tagtype']) {
			case '/channel':
				$this->_in_channel = false;
				break;
			case 'item':
				$this->_in_item = true;
				$this->clearItemAttribs();
			}

		}

		public function parseHtmlTag( $tag ) {
			if ($this->_in_channel) {
				$this->parseChannelTag( $tag );
			} else {
				switch ($tag['tagtype']) {
				case 'channel':
					$this->_in_channel = true;
					break;
				}
			}
			// \Pacal\php\globals\getPL()->logMessage( 'tag:' . \Pacal\php\globals\Pacal_da( $tag , false , true ) );
			return $tag;
		}

	}

?>
