Location: PHPKode > scripts > Syndication Classes > SC_RSS.class.php
<?php

/**
* SC_RSS.class.php
*
* Copyright (c) 2005, James Logsdon
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author James Logsdon
* @version 1.1
* @package SC_XML
*/

require_once "SC_XML.class.php";

/**
 * This class handles RSS output
 *
 * @author James Logsdon
 * @package XML
 * @version 1.1
 */
class SC_RSS extends SC_XML
{

	/**
	 * The version of the feed (.90|.91|.92|1.0|2.0)
	 * @access protected
	 */
	var $version;

	/**
	 * Valid tags
	 * @access private
	 */
	var $tags = array (
		'0.9' => array (
			'rdf:RDF' => array (
				'channel' => array (
					'title'       => true,
					'description' => true,
					'link'        => true
				),
				'image' => array (
					'title' => true,
					'url'   => true,
					'link'  => true
				),
				'item' => array (
					'title' => true,
					'link'  => true
				),
				'textinput' => array (
					'title'       => true,
					'description' => true,
					'name'        => true,
					'link'        => true
				)
			)
		),
		'2' => array (
			'rss' => array (
				'channel' => array (
					'title'          => true,
					'link'           => true,
					'description'    => true,
					'language'       => true,
					'copyright'      => true,
					'managingEditor' => true,
					'webMaster'      => true,
					'pubDate'        => true,
					'lastBuildDate'  => true,
					'category'       => true,
					'generator'      => true,
					'docs'           => true,
					'cloud'          => true,
					'ttl'            => true,
					'image'          => array (
						'title'  => true,
						'link'   => true,
						'url'    => true,
						'width'  => true,
						'height' => true,
					),
					'item'           => array (
						'title'       => true,
						'link'        => true,
						'description' => true,
						'author'      => true,
						'category'    => true,
						'comments'    => true,
						'enclosure'   => true,
						'guid'        => true,
						'pubDate'     => true,
						'source'      => true
					),
					'rating'         => true,
					'textInput'      => array (
						'title'       => true,
						'name'        => true,
						'link'        => true,
						'description' => true,
					),
					'skipHours'      => true,
					'skipDays'       => true
				)
			)
		)
	);

	/**
	 * When you call the class, an array is expected with information on the
	 * Channel
	 *
	 * The array must have three elements: title, link, and description. These
	 * are required for a valid RSS file.
	 *
	 * The only task this function (the constructor) performs is initializing
	 * the document.
	 *
	 * @param array $info  Consists of three elements: title, link, and description
	 * @param float $version  The version of the feed
	 * @return void
	 */
	function SC_RSS ( $info = array (), $version = 2.0 )
	{
		// Start the feed
		parent::SC_XML();
		$this->_isFeed = true;

		if ( $version != 2.0 AND
			$version != .90 )
		{
			$this->Error ( 'Only RSS 2.0 and .90 are supported at this time.' );
		}

		$this->version = $version;

		if ( !isset ( $info['title'] ) OR
			!isset ( $info['link'] ) OR
			!isset ( $info['description'] ) )
		{
			$this->Error ( 'You must provide a Title, Link, and Description' );
		}

		if ( $this->version == .90 )
		{
			$this->openTag ( 'rdf:RDF', array (
				"\n" . 'xmlns:rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
				"\n" . 'xmlns' => 'http://my.netscape.com/rdf/simple/0.9/'
			) );
		}
		else
		{
			$this->openTag ( 'rss', array ( 'version' => $this->version ) );
		}
		$this->openTag ( 'channel' );

		// Add the tags
		foreach ( $info as $tag => $val )
		{
			$this->openTag ( $tag, null, false, false );
			/* Check for image and textInput tags */
			/* Added by Andreas Rohr */
			if ( in_array ( $tag, array( 'image', 'textInput' ) ) )
			{
				foreach ( $val as $subtag => $subval )
				{
					$this->openTag ( $subtag, null, false, false );
					$this->addValue ( $subval );
					$this->closeTag ( false );
				}
			}
			else
			{
				$this->addValue ( $val );
			}
			$this->closeTag ( false );
		}


		// In RSS .90, the Channel tag is closed after channel info is added
		if ( $this->version == .90 )
		{
			$this->closeTag ( );
		}
	}

	/**
	 * Add an item to the feed
	 *
	 * You must provide either a title or a description.
	 *
	 * If an array value is an array, key 1 is made into an attribute string
	 * while key 0 is the value itself. The attributes must be in an array
	 * formatted like this:
	 *
	 * array ( attributeName => attributeValue );
	 *
	 * If giving a GUID and you wish for it to be a permaLink, you can pass
	 * a value of true for the second key instead of an array. Example:
	 *
	 * array ( 'guid' => array ( 'UniqueID', true ) );
	 *
	 * @param array $info  The item information
	 * @return void
	 */
	function addItem ( $info = array() )
	{
		if ( !isset ( $info['title'] ) OR
			!isset ( $info['description'] ) )
		{
			$this->Error ( 'You must provide either a description or a title' );
		}

		$this->openTag ( 'item' );
		foreach ( $info as $tag=>$val )
		{
			/* GUID invalidation fix */
			/* Added by Andreas Rohr */
			if ( is_array ( $val ) AND $tag == 'guid' )
			{
				$this->openTag ( $tag, array ( 'isPermaLink' => ( ( $val[1] === true )?( 'true' ):( 'false' ) ) ), false, false );
				$this->addValue ( $val[0] );
			}
			else if ( is_array ( $val ) )
			{
				$this->openTag ( $tag, $val[1], false, false );
				$this->addValue ( $val[0] );
			}
			else
			{
				$this->openTag ( $tag, null, false, false );
				$this->addValue ( $val );
			}
			$this->closeTag ( false );
		}
		$this->closeTag ( );
	}

	/**
	 * Return a version string
	 *
	 * @return string  The version (RSS version)
	 */
	function version()
	{
		return 'RSS ' . $this->version;
	}

	/**
	 * Close the channel and rss tags and return the feed
	 *
	 * @return string  The feed contents
	 */
	function Go()
	{
		// If the document is not malformed, this will close the channel and rss tags
		$this->closeTag ( );
		$this->closeTag ( );
		return parent::Go();
	}

}

?>
Return current item: Syndication Classes