<?php
/*
InsertRDF, a PHP script to read RDFs,  Copyright (C) 2002 Ian Monroe
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; with version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

//      InsertRDF Documentation
//      InsertRDF($url, $cache_file, $description = true, $max = 10, $cache_time = 3600)
//
//      This is file has a function, InsertRDF, to insert a RDF feed into your website
//
//      Just copy the URL of the RDF and paste it as the first agrument and then
//      create an unique file name for the second agrument. The other arguments are
//      optional.
//
//      A third agrument of TRUE can be added if you want to see descriptions of the news
//      stories, something some RDF news sources provide. It trys to show descriptions by default.
//      
//      $max is the maximum number of items to be displayed. It is 10 by default.
//      $cache_time is the amount of time in seconds that has to pass before it downloads a
//      new version instead of using the cache. Its an hour be default. You probably shouldn't
//      make this any lower, as I've read it could get your IP banned.
//      
//      Examples:
//      PHP for Freshmeat.net:
//      <?
//      include("rdf.php");
//      InsertRDF("http://freshmeat.net/backend/fm.rdf", "/tmp/freshmeat.net.cache");
//      ?>
//      PHP for BBC World (with news descriptions):
//      <?
//      include("rdf.php");
//      InsertRDF("http://www.newsisfree.com/HPE/xml/feeds/60/60.xml", "/tmp/bbcworld.cache", TRUE);
//      ?>
//      Some of the code (such as the stuff deciding whether or not to download
//      a new version) comes from the freshmeat.php3 located at http://swamp.chl.chalmers.se/backends/
//
//      The closest thing this script has to a homepage would be http://ian.webhop.org/projects.php
*/

//3 general purpose functions
//from php.net:
function decodeHTML($string) {
  
$string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES)));
  
$string = preg_replace("/&#([0-9]+);/me", "chr('\\1')", $string);
  return
$string;
}
             
function
FindLength($AString) //from some comments from the docs at http://www.php.net
{
foreach (
count_chars($AString) as $my_value) $length=$length+$my_value;
return
$length;
}
//I'm used to REBOL text parsing. PHP probably has better ways of doing this.
function ParseIt($begin, $end, $haystack, $offset = 0)
{
$StartPos = strpos($haystack, $begin, $offset);
$EndPos = strpos($haystack, $end, $offset);
$ParsedLength = $EndPos - $StartPos;
//$returnthis = substr($haystack, $StartPos, $ParsedLength);
$offset=$EndPos + FindLength($end);
return
substr($haystack, $StartPos, $ParsedLength);
}
//You change the default values here
function InsertRDF($url, $cache_file, $description = true, $max = 10, $cache_time = 3600)
{
$cache_time_rnd =       300 - rand(0, 600);
$time           =       split(" ", microtime());

if ( (!(
file_exists($cache_file))) || ((filectime($cache_file) + $cache_time - $time[1]) +
$cache_time_rnd < 0) || (!(filesize($cache_file))) )
{
//Put file in string
$rdf = join ('', file ($url));
$fcache = fopen($cache_file, "w");
fputs($fcache, "<ul>");
if(
substr_count($rdf, "<item") < $max)
  {
   
$max = substr_count($rdf, "<item");
  }
  
$max = $max - substr_count($rdf, "<items>");
$ItemArray = Array();
//get items
$offset = 0;
ParseIt("<items>", "</items>", $rdf, &$offset); //to move the offset to skip <items>
for($i=1; $i<=$max; $i++)
  {
   
$ItemArray[$i] = ParseIt("<item", "</item>", $rdf, &$offset);  
  }
$TitleArray = Array();
$LinkArray = Array();
$DescriptionArray = Array();
$nobug = true;            //Get around a bug in some newsisfree.com RDFs, not really needed anymore...
if(strpos($rdf, "</link>"))
{
  
$nobug = true;  
}
else
{
  
$nobug = false;
}
for(
$i=1; $i<=$max; $i++)
  {
   
$TitleArray[$i] = strip_tags(ParseIt("<title>", "</title>", $ItemArray[$i]));
   if(
$nobug)
   {
   
$LinkArray[$i] = strip_tags(ParseIt("<link>", "</link>", $ItemArray[$i]));
   }
   else
   {
   
$LinkArray[$i] = strip_tags(ParseIt("<link>", "<description>", $ItemArray[$i]));
   }
   if (
$TitleArray[$i] == "Customize this feed") //skips the newsisfree advertisement
   
{
       continue;
   }
   
$decodedTitle = decodeHTML($TitleArray[$i]);
   
$DescriptionArray[$i] = strip_tags(ParseIt("<description>", "</description>", $ItemArray[$i]));
   
$decodedDesc = decodeHTML($DescriptionArray[$i]);
   
fputs($fcache, "<li><a href=\"$LinkArray[$i]\">$decodedTitle</a> ");
   if(
$description) fputs($fcache, "$decodedDesc");
//   if($description) fputs($fcache, $DescriptionArray[$i]);
   
fputs($fcache,"</li>\n");
  }
//fputs($fcache, print_r($DescriptionArray));

fputs($fcache, "\n</ul>\n");
fclose($fcache);
}
if (
file_exists($cache_file)) {
    include(
$cache_file);
    }
}
?>