#native_company# #native_desc#
#native_cta#

Simple RSS reader

By Danil van den Akker
on October 9, 2004

Version: 0.1

Type: Full Script

Category: Other

License: GNU General Public License

Description: With this script, it becomes easy to display an RSS-feed on your homepage. No external libraries are needed (except PHP). To display an RSS in a page, just add the following line: <script src=”/directoryAtYourServer/rss.php?rssFeed=www.servername.com/somefeed.xml”></script> Use “&showDate=false” after the url to hide the publication date. Use “&description=true” after the url to display the complete description.

<?php

/*
		Please keep this header.
		Author: D.S. van den Akker
		Build at: 09 october 2004
*/

$showDate = getParam("showDate");
$showDescription = getParam("showDescription");
$maxAantal = getParam("maxAantal");
$url = getParam("rssFeed");
$server = substr($url, 0, strpos($url, '/'));
$path = substr($url, strpos($url, '/'), strlen($url));
$nieuws = readRssFeed($server, $path);

header("Content-type: text/javascript");

echo "document.write("";
echo "<img src="./blok.gif">&nbsp;<span class="mainHeader">";
echo "&nbsp;<span class="mainHeader">";
echo getTag("title", $nieuws);
echo "</span><br>";

$newsItem = getNextNewsItem();
$i = 0;
while ($newsItem != "" && ($maxAantal == "" || $i < $maxAantal))
{
	echo "-&nbsp;<a href='" . getTag("link", $newsItem) . "'>";
	echo getTag("title", $newsItem);
	echo "</a>";
	if ($showDate != "false")
	{
		echo "&nbsp;";
		echo stripDate(getTag("pubDate", $newsItem));
	}
	if ($showDescription == "true")
	{
		echo "&nbsp;";
		echo getTag("description", $newsItem);
	}
	echo "<BR>";
	$newsItem = getNextNewsItem();
	$i++;
}

echo "");";

/************************************** Helper functions **************************************/
function stripDate($fullDate)
{
	$string = $fullDate;
	$pattern = "/([^,]*), ([^ ]*) ([^ ]*) ([^ ]*)(.*)/i";
	$replacement = "$1, $2 $3 $4";
	return preg_replace($pattern, $replacement, $string);
}

function getNextNewsItem()
{
	global $nieuws;

	$begin = strpos($nieuws, "<item>");
	if ($begin == 0)
	{
		$begin = strpos($nieuws, "<item ");
		if ($begin == 0)
		{
			return "";
		}
	}
	$begin += 6;
	$aantChar = strpos($nieuws, "</item>") - $begin;
	$nieuwsitem = substr($nieuws, $begin, $aantChar);
	$nrToSkip = $begin + 6 + $aantChar + 7;
	$nieuws = substr($nieuws, $nrToSkip, strlen($nieuws) - $nrToSkip);
	$testArray = array();
	return $nieuwsitem;
}

function getTag($tagname, $newsItem)
{
	$begin = strpos($newsItem, "<".$tagname.">") + strlen($tagname)+2;
	$aantChar = strpos($newsItem, "</".$tagname.">") - $begin;
	$subitem = substr($newsItem, $begin, $aantChar);
	if ($subitem == "")
	{
		echo "WARNING!!! Empty newsitem: " . $newsItem;
	}
	return $subitem;
}

function readRssFeed($server, $path)
{
	error_reporting(E_ALL);
	
	/* Get the port for the WWW service. */
	$service_port = getservbyname('www', 'tcp');
	
	/* Get the IP address for the target host. */
	$address = gethostbyname($server);
	
	/* Create a TCP/IP socket. */
	$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
	if ($socket < 0) {
	    echo "socket_create() failed: reason: " . socket_strerror($socket) . "n";
	} 
	
	$result = socket_connect($socket, $address, $service_port);
	if ($result < 0) {
	    echo "socket_connect() failed.nReason: ($result) " . socket_strerror($result) . "n";
	}
	
	$in = "GET " . $path . " HTTP/1.1rn";
	$in .= "Host: " . $server . "rn";
	$in .= "Connection: Closernrn";
	$out = '';
	
	socket_write($socket, $in, strlen($in));
	
	$foundTCPBody = false;
	$returnValue = "";
	while ($out = socket_read($socket, 2048)) 
	{
		{
	    $returnValue .= $out;
	  }
	}
	
	socket_close($socket);
	return $returnValue;
}

function getParam($paramName)
{
	global $HTTP_GET_VARS, $HTTP_POST_VARS;

	if (isset($HTTP_GET_VARS[$paramName]))
		return $HTTP_GET_VARS[$paramName];

	if (isset($HTTP_POST_VARS[$paramName]))
		return $HTTP_POST_VARS[$paramName];

	return '';
}

?>