Rss Get > Rss Get.php

Questions? Need Help? Want to share? » PHP Forum
Script Name:
Rss Get

Download:
rss-get.zip

Category:
Web Fetching

Archive Content:

Content:

Rss Get.php:


<!---

  =========================
  RSS_Get by Aaron Dunlap
  http://www.aarondunlap.com
  =========================
  aaron 'at' aarondunlap.com
  =========================
  Version 1.481 (05/04/05)

-->
<?php

  
#==========================
  # The following line declares how long to wait before reloading the RSS feed. 
  # Keep in mind,  many sites discourage people from pulling its feed more often than 30 minutes.
  # For example,  Slashdot.org may ban your server if it tries to load the rss feed too often.
  
  # You may change the below line.
  #==========================

  
$minutes 30//How often to update the image. 0 indicates that image updates every time it is called (heavy strain on bandwidth & server)
 
  #==========================
  # The next line is what directory to dump the htm files generated by this script.
  # It should be relative to the location of this script,  and it *MUST* have a trailing slash if it's a seperate dir.
  # If you want the files to be dumped in the same dir as this script,  set it to ""

  # You may change the next line
  #==========================
  
  
$filename "rss/";
  
  
#==========================
  # The next 4 lines are default settings for the script to use,  if they are not set prior to including this script
  # You may change the next 4 lines.
  #==========================

  
$default_url "http://www.aarondunlap.com/rss.php?mode=hl"//URL for the RSS/XML feed you're subscribing to.
  
$default_displayname "|| aarondunlap.com ||"//Title to appear before headlines. Should be feed-specific
  
$default_number 5//How many headlines to display. If you set it higher than the ammount of headlines,  it will stop there
  
$default_target "_self"//Target for headline links. Options: "_self" and "_blank"

  #==========================
  # The next line is whether or not the script should auto-update. 
  # If enabled,  the script will check to see if a newer version of this script is available and will 
  # automatically download and install it. Your current version of the script will be backed up  in your $filename 
  # directory.

  # If you have manually edited this script,  you should disable this. Otherwise,  it is recomended that you enable it.
  # Also note,  your customized settings (default_url,  default_displayname,  filename,  etc) WILL be kept during
  # auto-updates. Alterations to the actual function code (beneath) would be overwritten in an update.

  # During updates,  your existing script is backed up into the $filename directory as "backup_[date]_(time).php"

  # You may change the next line
  #==========================
  
  
$autoupdate TRUE;
  
  
#==========================
  # The settings below are for advanced headline truncation.
  
  # $default_trunc is how many characters to cut off at,  
  #                the default is FALSE,  which wont truncate
  #                at all. 
  
  #                You may use $trunc when calling this script
  #                to define the truncation for specific feeds
  
  # $default_delim is what character to cut off the headline at
  #                for example,  if your headlines contain useless
  #                data at the end,  like "(via Reuters)",  setting
  #                the delimiter to "(" will stop that from showing.
  
  #                You may use $delim when calling this script
  #                to define the truncation for specific feeds
  
  #===========================

  
$default_trunc FALSE;
  
$default_delim FALSE;

  
#XXXXXXXXXXXXXXXXXXXXXXXXXX
  # Everything below is functional code.

  # You should not change it unless you know what you're doing,
  # and even if you do know what you're doing,  are you sure you're
  # awake enough to do it? Maybe take a nap,  rethink things,  try again.
  #XXXXXXXXXXXXXXXXXXXXXXXXXX

  
$version 1.481;

  
//If these variables aren't declared already,  use defaults.
  
if (!isset($url)) { $url $default_url; }
  if (!isset(
$displayname)) { $displayname $default_displayname; }
  if (!isset(
$number)) { $number $default_number; }
  if (!isset(
$target)) { $target $default_target; }
  if (!isset(
$trunc)) { $trunc $default_trunc; }
  if (!isset(
$delim)) { $delim $default_delim; }
  

  
//In-URL definitions. Cannot be used for including,  but it works great for testing.
  
if (isset($_GET['url'])) { $url $_GET['url'];}
  if (isset(
$_GET['number'])) { $number $_GET['number'];}
  if (isset(
$_GET['displayname'])) { $displayname $_GET['displayname'];}
  if (isset(
$_GET['rssHeadline'])) { $rssHeadline $_GET['rssHeadline'];}
  
$basefile $filename
  
$versionfile $filename."updatelog.htm"//File for update attempt log
  
$filename .= md5($url).".htm"//Prepare filename for htm output


  #==========================
  # Script updating. Checks to see if an update attempt has been made in 24 hours,  then goes ahead.
  #==========================
  
if (((is_file($versionfile)) && (((time()-filemtime($versionfile)) >= (24 60 60))) || (!is_file($versionfile))) && ($autoupdate == TRUE)) { 
    
$out ""
    
    
//If updatelog.htm is too big,  clear it out.
    
if (filesize($versionfile) > 102400)  { unlink($versionfile); $out .= "<i>[".date("m/d/y H:i:s")."]</i> <b>Cleared update log because it was too big (>100kB)</b><hr>"; }
    
    
$fd fopen ($versionfile ,  "a+");
    
$out .= "<i>[".date("m/d/y H:i:s")."]</i> Attempting to acquire latest version number: ";
    
fwrite ($fd ,  $out);
      require 
'http://www.aarondunlap.com/code/rss_get_version.php';

    if ((isset(
$latestversion)) && (isset($versionURL))) { 
        
$out "<b>Acquired!</b><br>\n <i>[".date("m/d/y H:i:s")."]</i> Comparing version numbers: ";
        
fwrite ($fd ,  $out);
        if (
$version $latestversion) {
            
$out "<b>Your version ($version) is out-of-date. Updating to version $latestversion.</b><br>\n <i>[".date("m/d/y H:i:s")."]</i> Loading updated script: ";
            
fwrite ($fd ,  $out);    

            
//Reads the new file from my server
            
if (@$newversfile fopen($versionURL, "r")) {

                while (!
feof($newversfile)) {
                     
$newvers .= fgets($newversfile);
                }

                
//Transfers currently existing settings onto the update.
                
$newvers str_replace("http://www.aarondunlap.com/rss.php?mode=hl", $default_url, $newvers);
                
$newvers str_replace("|| aarondunlap.com ||", $default_displayname, $newvers);
                
$newvers str_replace('$default_number = 5;', '$default_number = '.$default_number.';', $newvers);
                
$newvers str_replace('$autoupdate = TRUE;', '$autoupdate = '.$autoupdate.';', $newvers);
                
$newvers str_replace('$filename = "rss/";', '$filename = "'.$basefile.'";', $newvers);
                
$newvers str_replace('$minutes = 30;', '$minutes = '.$minutes.';', $newvers);
                
$newvers str_replace('$default_target = "_self";', '$default_target = '.$default_target.';', $newvers);
            

                
$out "<b>Opened</b><br>\n <i>[".date("m/d/y H:i:s")."]</i> Checking write-access in directory: ";
                
fwrite ($fd ,  $out);

                
//Checks to make sure we can write in this directory
                
if (is_writable(getcwd())) {
                    
$out "<b>Writable!</b><br>\n <i>[".date("m/d/y H:i:s")."]</i> Writing new version to temporary file: ";
                    
fwrite ($fd ,  $out);

                    
$outfile fopen("rss_get_temp.txt", "w+");
                    
fwrite($outfile,  $newvers);
                    
fclose($outfile);
                    
$out "<b>Written.</b><br>\n<i>[".date("m/d/y H:i:s")."]</i> Backing up outdated file in $basefile directory:";
                    
fwrite ($fd ,  $out);
                    

                    
//Backs up the current script (this one) into the rss folder                     
                    
if (!copy("rss_get.php", $basefile."backup_".date("m-d-y_(H.i)").".php")) {
                        
$out "<b>Failed.</b><hr>\n";
                        
fwrite ($fd ,  $out);
                        
fclose($fd);
                    } else {
                        
$out "<b>Moved.</b><br>\n<i>[".date("m/d/y H:i:s")."]</i> Replacing script with new version:";
                        
fwrite ($fd ,  $out);

                        
//Renames the temp file as this file,  effectively replacing it.
                        
if (@!rename("rss_get_temp.txt", "rss_get.php")) {
  
/*Dude,  look at these if blocks!*/            $out "<b>Failed.</b><hr>\n";
                            
fwrite ($fd ,  $out);
                            
fclose($fd);
                        } else {
                            
chmod("rss_get.php", 774);
                            
$out "<b>Replaced</b><br>\n<i>[".date("m/d/y H:i:s")."]</i><b> RSS_GET updating complete. </b>\n<br><i>[".date("m/d/y H:i:s")."]</i><b>Version note from author:</b>".$message."<hr>\n";
                            
fwrite ($fd ,  $out);
                            
fclose($fd);
                        } 
    
                    }
    
                } else {
                    
$out "<b>Not Writable.</b><hr>\n";
                    
fwrite ($fd ,  $out);
                    
fclose($fd);                
                }
                
            } else { 
                
$out "<b>Failed.</b><hr>\n";
                
fwrite ($fd ,  $out);
                
fclose($fd);
            }
        } else {
            
$out "<b>Your version is current</b><hr>\n";
            
fwrite ($fd ,  $out);    
        }
        
    } else {     
        
$out "<b>Failed.</b><hr>\n";
        
fwrite ($fd ,  $out);
        
fclose($fd);
     }
 } 
// That's a lot of elses! Alright,  we're done with the update thing.
  



  #==========================
  # Check the modify time of the htm file for this feed,  and see if it's too new to reload the feed.
  # If the file is too new,  it loads the htm file. This stops the script from constantly pulling the feed.
  #==========================

  
if (($minutes 0) && (is_file($filename)) && (((time()-filemtime($filename)) < ($minutes 60)))) { 
      include 
$filename;
    
$time floor((time()-filemtime($filename)) / 60); //See how many "minutes ago" the file was made.
    
echo "<br><i><span class=\"updated\">Updated $time minutes ago.</span></i>"//Include "minutes ago" after output.
       
  
} elseif (@fopen($url, "r")) { //Makes sure the file can actually be accessed.

        #==========================
      # If we're down here,  it means that the feed needs to be reloaded.
      #==========================

      
$rssHandle fopen($url, "r") ; // Open the rss file for reading 

      
while (!feof($rssHandle)) {
          
$rssData .= fgets($rssHandle);
      }

      
#==========================
      # Feed parsing
      #==========================
      
$tag "item ";
      
$rssData =  preg_replace("/<" $tag "(.|\s)*?>/", "<item>", $rssData);
      
$rssData chop($rssData); // Strip any whitespace from the end of the string    
      
$rssData ereg_replace("[\r, \n]",  "",  $rssData); //Clear line breaks
      
$rssData strstr($rssData, "<item>"); //Remove everything before <item>.

      #==========================
      # Strip specific tags and their content from the feed,  to lighten the strain on the processor.

      # Currently,  only the <description></description> tags are stripped,  we don't need them and sometimes
      # they are REALLY long,  getting rid of them now makes it easier to parse later.
      #==========================      
      
$tags_and_content_to_strip = Array("description");

      foreach (
$tags_and_content_to_strip as $tag) {
           
$rssData preg_replace("/<" $tag ">(.|\s)*?<\/" $tag ">/", "", $rssData);
      }

      
$rssData str_replace("<item>", "",  $rssData); //Remove <item> itself
      
$rssData urldecode($rssData); //Replace any silly %20-type characters with their readable replacement.
      
$rssData str_replace(strstr("</channel>", $rssData), "", $rssData);
      
$rssArray explode("</item>", $rssData); //Creates an Array from all the headlines
  
        
$title = array();
      
$link = array();

      
#==========================
      # This loop creates an array for links and another for titles.
      #==========================  
      
$x 0;
      while(
$x $number) {
          
$link[$x] = strstr($rssArray[$x], "<link>"); //Remove everything before <link>
        
$link[$x] = ereg_replace("<link>", "", $link[$x]); 
    
         
$link[$x] = str_replace(strstr($link[$x], "</link>"), "", $link[$x]);
         
$link[$x] = trim($link[$x]);

        
$title[$x] = strstr($rssArray[$x], "<title>");
          
$title[$x] = ereg_replace("<title>", "", $title[$x]); // Remove the leading <title> tags from the selected headline
          
$title[$x] = str_replace(strstr($title[$x], "</title>"), "", $title[$x]); //  Remove </title> and anything after it
          
$title[$x] = trim($title[$x]);
        
        if (
$trunc != FALSE) { $title[$x] = str_replace(substr($title[$x], $trunc), "", $title[$x]); }
        if (
$delim != FALSE) { $title[$x] = str_replace(strstr($title[$x], $delim), "", $title[$x]); }
        
            if (
$title[$x] == "") { $number $x; break; } //If there are no more headlines,  reset $number to the max.
        
$x++;
      }

      
#==========================
      # Writing the file
      #==========================
      
$fp fopen($filename,  "w+"); 
      
$x=0;  
      
fwrite($fp, "<b><span class=\"displayname\">$displayname</span></b> \n"); //Write the displayname to the file

      
while ($x $number) { //This loop writes each line individualy.
          
fwrite($fp, "<br>\n-<a class=\"headlinellink\" target=\"$target\" href=\"$link[$x]\">$title[$x]</a>");
        
$x++;
      }
     
fclose($fp);
     include 
$filename;
     echo 
"<br><i><span class=\"updated\">Live.</span></i>";

 } else { 
    
#==========================
    # Error handling:
    # (rss url given is unreadable)
    #==========================
    
echo "<b>Could not connect to $url. </b>"

}

?>



Other Web Fetching Scripts:

WebMaster Resources Home

©RingsWorld.com