// ************************************************************************************
// Program File Name : instantfeedreader.php
// Programmer : Lim Hock Leong Benjamin
// Purpose:
// Instant Feed Reader Code
// Heavily modified from the base source codes found at zend.com
//
// Version Stable Build 1.6
// Last Updated 08 Jun 2005
// Date Completed : 15 Jul 2004
//
// Please see the instantfeedreader.config.inc.php for the Revision History
//
// Copyright 2005 Lim Hock Leong Benjamin
// ************************************************************************************
@require("instantfeedreader.config.inc.php");
global $sFilename, $iDisplay, $sCacheLocation;
if ( trim($sFilename) == "" )
{
$sFilename = $_GET["feed"];
}
$iDisplayCount = 0;
$iDebug = 0;
$_item = array();
$_image = array();
$_channel = array();
$_sFlag = "";
$_sCurrentTag = "";
$_sPreviousTag = "";
function initItemArray()
{
global $_item, $iDebug;
if ( $iDebug == 1 )
echo "initItemArray\n";
$_item = array ("TITLE"=>"", "LINK"=>"", "DESCRIPTION"=>"");
}
function initImageArray()
{
global $_image, $iDebug;
if ( $iDebug == 1 )
echo "initImageArray\n";
$_image = array ("URL"=>"", "TITLE"=>"", "LINK"=>"", "WIDTH"=>"", "HEIGHT"=>"", "GUID"=>"");
}
function initChannelArray()
{
global $_channel, $iDebug;
if ( $iDebug == 1 )
echo "initChannelArray\n";
$_channel = array ("TITLE"=>"", "LINK"=>"", "DESCRIPTION"=>"", "LANGUAGE"=>"", "COPYRIGHT"=>"", "DOCS"=>"");
}
function startElement($parser, $name)
{
global $_sFlag, $_sCurrentTag, $iDebug;
if ( $iDebug == 1 )
echo "startElement : $name\n";
switch ($name)
{
case "ITEM":
initItemArray();
$_sFlag = "ITEM";
break;
case "CHANNEL":
initChannelArray();
$_sFlag = "CHANNEL";
break;
case "IMAGE":
initImageArray();
$_sFlag = "IMAGE";
break;
default:
$_sCurrentTag = $name;
break;
}
}
function endElement($parser, $name)
{
global $_item, $_channel, $_image, $_sFlag, $iDebug, $iDisplay, $iDisplayCount, $iLinksOpenInNewWindow, $iShowDescription, $sStartLineTag, $sStartTitleTag, $sEndTitleTag, $sStartDescriptionTag, $sEndDescriptionTag, $sEndLineTag, $iOutputType, $iShowPubDate, $sStartPubDateTag, $sEndPubDateTag, $sTitleAnchorClass, $iMaxDescriptionChars, $iMaxTitleChars, $iStripAwayHTMLTags, $iConvertToHTMLEntities;
if ( $iDebug == 1 )
echo "endElement : $name : $_sFlag\n";
switch ($name)
{
case "ITEM":
$sLink = "";
$sTitle = "";
$sDescription = "";
$sPubDate = "";
$sLink = $_item["LINK"];
if ( trim($sLink) == "" )
{
// some RSS/XML uses GUID instead!
$sLink = $_item["GUID"];
}
if ( $iStripAwayHTMLTags == 1 )
{
$sTempTitle = strip_tags($_item["TITLE"]);
$sTempDescription = strip_tags($_item["DESCRIPTION"]);
}
else
{
$sTempTitle = $_item["TITLE"];
$sTempDescription = $_item["DESCRIPTION"];
}
if ( $iMaxTitleChars > 0 )
{
if ( $iConvertToHTMLEntities == 1)
{
$sTitle = htmlentities(substr($sTempTitle,0,$iMaxTitleChars));
}
else
{
$sTitle = substr($sTempTitle,0,$iMaxTitleChars);
}
}
else
{
if ( $iConvertToHTMLEntities == 1)
{
$sTitle = htmlentities($sTempTitle);
}
else
{
$sTitle = $sTempTitle;
}
}
if ( $iMaxDescriptionChars > 0 )
{
if ( $iConvertToHTMLEntities == 1)
{
$sDescription = htmlentities(substr($sTempDescription,0,$iMaxDescriptionChars));
}
else
{
$sDescription = substr($sTempDescription,0,$iMaxDescriptionChars);
}
}
else
{
if ( $iConvertToHTMLEntities == 1)
{
$sDescription = htmlentities($sTempDescription);
}
else
{
$sDescription = $sTempDescription;
}
}
if ( $iShowPubDate == 1)
{
if ( $iStripAwayHTMLTags == 1 )
{
$sPubDate = strip_tags($_item["PUBDATE"]);
if ( $sPubDate == "" )
{
$sPubDate = strip_tags($_item['DC']['DATE']);
}
}
else
{
$sPubDate = $_item["PUBDATE"];
if ( $sPubDate == "" )
{
$sPubDate = $_item['DC']['DATE'];
}
}
}
$sLinksOpenInNewWindow = "";
if ( $iLinksOpenInNewWindow == 1 )
{
$sLinksOpenInNewWindow = "target=_blank";
}
if ( $sTitleAnchorClass == "" )
{
$sTitleAnchorClass = "";
}
else
{
$sTitleAnchorClass = "class='$sTitleAnchorClass'";
}
if ( $iDisplay == 0 )
{
if ( $iShowDescription == 1 )
{
if ( $iOutputType == 1 )
{
echo "document.write(\"$sStartLineTag$sStartTitleTag$sTitle$sStartPubDateTag$sPubDate$sEndPubDateTag$sEndTitleTag$sStartDescriptionTag$sDescription$sEndDescriptionTag$sEndLineTag\");\n";
}
else
{
echo "$sStartLineTag$sStartTitleTag$sTitle$sStartPubDateTag$sPubDate$sEndPubDateTag$sEndTitleTag$sStartDescriptionTag$sDescription$sEndDescriptionTag$sEndLineTag\n";
}
}
else
{
if ( $iOutputType == 1 )
{
echo "document.write(\"$sStartLineTag$sStartTitleTag$sTitle$sStartPubDateTag$sPubDate$sEndPubDateTag$sEndTitleTag$sEndLineTag\");\n";
}
else
{
echo "$sStartLineTag$sStartTitleTag$sTitle$sStartPubDateTag$sPubDate$sEndPubDateTag$sEndTitleTag$sEndLineTag\n";
}
}
}
else
{
if ( $iDisplayCount < $iDisplay )
{
if ( $iShowDescription == 1 )
{
if ( $iOutputType == 1 )
{
echo "document.write(\"$sStartLineTag$sStartTitleTag$sTitle$sStartPubDateTag$sPubDate$sEndPubDateTag$sEndTitleTag$sStartDescriptionTag$sDescription$sEndDescriptionTag$sEndLineTag\");\n";
}
else
{
echo "$sStartLineTag$sStartTitleTag$sTitle$sStartPubDateTag$sPubDate$sEndPubDateTag$sEndTitleTag$sStartDescriptionTag$sDescription$sEndDescriptionTag$sEndLineTag\n";
}
}
else
{
if ( $iOutputType == 1 )
{
echo "document.write(\"$sStartLineTag$sStartTitleTag$sTitle$sStartPubDateTag$sPubDate$sEndPubDateTag$sEndTitleTag$sEndLineTag\");\n";
}
else
{
echo "$sStartLineTag$sStartTitleTag$sTitle$sStartPubDateTag$sPubDate$sEndPubDateTag$sEndTitleTag$sEndLineTag\n";
}
}
$iDisplayCount++;
}
}
initItemArray();
break;
}
}
function parseData($parser, $text)
{
global $_item, $_channel, $_image, $_sFlag, $_sCurrentTag, $iDebug;
if ( $iDebug == 1 )
echo "parseData : $_sFlag : $text : ";
// initialise the previous tag if this routine is first run.
if ( $_sPreviousTag == "" )
{
$_sPreviousTag = $_sCurrentTag;
}
// is the data just whitespace? if so, we don't want it!
$crap = trim($text);
if ($crap)
{
// get rid of leading whitespace
$text = ltrim($text);
switch ($_sFlag)
{
case "ITEM":
if ( $_sPreviousTag == $_sCurrentTag )
{
$_item[$_sCurrentTag] .= $text;
}
else
{
$_item[$_sCurrentTag] = $text;
}
$temp = $_item[$_sCurrentTag];
if ( $iDebug == 1 )
echo "$temp \n";
break;
case "IMAGE":
if ( $_sPreviousTag == $_sCurrentTag )
{
$_image[$_sCurrentTag] .= $text;
}
else
{
$_image[$_sCurrentTag] = $text;
}
$temp = $_image[$_sCurrentTag];
if ( $iDebug == 1 )
echo "$temp \n";
break;
case "CHANNEL":
if ( $_sPreviousTag == $_sCurrentTag )
{
$_channel[$_sCurrentTag] .= $text;
}
else
{
$_channel[$_sCurrentTag] = $text;
}
$temp = $_channel[$_sCurrentTag];
if ( $iDebug == 1 )
echo "$temp \n";
break;
default:
if ( $iDebug == 1 )
echo "\n";
break;
}
}
else
{
if ( $iDebug == 1 )
echo "\n";
}
$_sPreviousTag = $_sCurrentTag;
}
function parseRDF($file)
{
global $iDebug, $sCustomErrorMessage;
$xml_parser = xml_parser_create();
initItemArray();
initImageArray();
initChannelArray();
if ( $iDebug == 1 )
echo "parseRDF\n";
// Set up event handlers
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "parseData");
// Open up the file
$fp = fopen ($file, "r") or die ($sCustomErrorMessage);
while ($data = fread ($fp, 4096))
{
if (!xml_parse($xml_parser, $data, feof($fp)))
{
die (sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code ($xml_parser)), xml_get_current_line_number($xml_parser)));
}
}
fclose($fp);
xml_parser_free($xml_parser);
}
function getCacheFileName($file)
{
global $iDebug,$sCacheLocation;
// is file in the cache?
$saSpecialTokens = array("/", ":", "?", "&", "=", ".", "-", "#", "~", "@", "$", "%", "^", "*", "(", ")", "+", "|", "\\", "{", "}", "[", "]");
$sCacheFile = str_replace($saSpecialTokens, "_", $file);
return $sCacheFile;
}
function cached($file)
{
global $iDebug,$sCacheLocation,$iTimeInSecondsToCache;
// is file in the cache?
$sCacheFile = getCacheFileName($file);
if ( $iDebug == 1 )
echo $sCacheFile;
if (is_readable($sCacheLocation . "/" . $sCacheFile))
{
if ( $iDebug == 1 )
echo "Cache Readable";
// Is it still alive?
$Diff = time() - filectime($sCacheLocation . "/" . $sCacheFile);
if ($iTimeInSecondsToCache <= 0 )
{
$iTimeInSecondsToCache = 1200;
}
if ( $iDebug == 1 )
echo $Diff;
if ($Diff > $iTimeInSecondsToCache) // old stuff!
{
if ( $iDebug == 1 )
echo "Cache is old!";
unlink($sCacheLocation . "/" . $sCacheFile);
return 1;
}
else
{
if ( $iDebug == 1 )
echo "Cache is still current!";
return 0;
}
}
else // not in cache!
{
if ( $iDebug == 1 )
echo "Cache Unreadable";
return 1;
}
}
function retrieveRDFfromCache($file)
{
global $iDebug,$sCacheLocation,$iTimeInSecondsToCache;
// is file in the cache?
$sCacheFile = getCacheFileName($file);
if ( $iDebug == 1 )
echo $sCacheFile;
parseRDF($sCacheLocation . "/" . $sCacheFile);
}
function cacheRDF($file)
{
global $iDebug,$sCacheLocation;
$sCacheFile = getCacheFileName($file);
copy($file, $sCacheLocation . "/" . $sCacheFile);
}
if ( $sCacheLocation == "" ) // no caching
{
parseRDF($sFilename);
}
else
{
if ( cached($sFilename) == 1 ) // not cached
{
cacheRDF($sFilename);
retrieveRDFfromCache($sFilename);
}
else // cached
{
retrieveRDFfromCache($sFilename);
}
}
?>