Random Sequence

PHP 5 script to generate an RSS 2 app-cast from the contents of a folder.

Produces feeds suitable for the Sparkle framework to auto-update your Cocoa Application.

PHP RSS Appcast Generator

How to Use

  1. Place this script in the folder you want to make an appcast of
  2. Point your feed reader to http://<folder URL>/appcast.php

Configuration (Optional)

  1. Change $title & $description variables as appropriate to your folder
  2. Add any additional file types to the $fileTypes array

Download

appcast.php (2 KB)

<?php
// ----------------------------------------------------------- //
// Script to generate an RSS appcast from folder contents
// Version 1.0.1
// 
// (cc) Random Sequence 2007, Some Rights Reserved
// 
// Licenced under a creative commons Attribution ShareAlike licence
// http://creativecommons.org/licenses/by-sa/3.0/               
// ----------------------------------------------------------- //   

// REQUIRES PHP 5 or greater
// Tested with APACHE 1 & 2 on Mac OS X, Debian Linux   

// -------------------- BEGIN CONFIG ------------------------- //

$title = "Downloads";           // Used as feed title in feed readers
$description = "File List";     // Used as feed description in feed readers

// these are the types of files to list in the appcast & their MIME Types. Use lower case.
$fileTypes = array( "zip"=>"application/zip",
                    "tgz"=>"application/x-gtar",
                    "tar"=>"application/x-tar",
                    "dmg"=>"application/octet-stream"
                    );

// -------------------- END OF CONFIG ------------------------ //

$appcastHeader = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<rss version=\"2.0\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:sparkle=\"http://www.andymatuschak.org/xml-namespaces/sparkle\">
<channel>
    <title>*title*</title>
    <link>*link*</link>
    <description>*description*</description>
    <language>en</language>
";
$appcastTemplate = "
        <item>
            <guid  isPermaLink=\"false\">*guid*</guid>
            <title>*title*</title>
            <description>*description*</description>
            <pubDate>*pubdate*</pubDate>
            <enclosure
                sparkle:version=\"*version*\"             
                type=\"*type*\"
                url=\"*url*\"
                length=\"*length*\" />
        </item>";

$appcastFooter = "
</channel>
</rss>";

$files = scandir(getcwd());
$etag = sha1(implode("/",$files));

// support for conditional fetch
if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
    header('HTTP/1.1 304 Not Modified');
    exit;       
}

$appcast = $appcastHeader;

$link = "http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"];

$appcast = str_replace("*link*",$link,$appcast);
$appcast = str_replace("*title*",$title,$appcast);
$appcast = str_replace("*description*",$description,$appcast);

foreach ($files as $file) {
    preg_match("/.*\.([a-z09]{1,3})$/i",$file,$matches);
    if (isset($matches[1]) && isset($fileTypes[strtolower($matches[1])]) !== false) {
        $appcastFile = $appcastTemplate;

        $folderUrl = "http://".$_SERVER["HTTP_HOST"].substr($_SERVER["PHP_SELF"],0,strrpos($_SERVER["PHP_SELF"],"/"))."/";

        $guid = $folderUrl.sha1($file);         
        $title = $file;
        $description = preg_replace("/^(.*?)([0-9]+[a-z])\.([a-z09]{1,3})$/i","$1",$file);
        $pubdate = date("D, d M Y H:i:s",filectime($file)); 
        $type = $fileTypes[strtolower($matches[1])];            
        $url = $folderUrl.$file;
        $length = filesize($file);      
        $version = preg_replace("/^(.*?)([0-9]+[a-z])\.([a-z09]{1,3})$/i","$2",$file);

        $appcastFile = str_replace("*guid*",$guid,$appcastFile);    
        $appcastFile = str_replace("*title*",$title,$appcastFile);
        $appcastFile = str_replace("*description*",$description,$appcastFile);
        $appcastFile = str_replace("*version*",$version,$appcastFile);
        $appcastFile = str_replace("*pubdate*",$pubdate,$appcastFile);
        $appcastFile = str_replace("*type*",$type,$appcastFile);
        $appcastFile = str_replace("*url*",$url,$appcastFile);                                  
        $appcastFile = str_replace("*length*",$length,$appcastFile);                                    

        $appcast .= $appcastFile;   
    }
}

$appcast .= $appcastFooter; 

header("Content-type: application/xml; charset=UTF-8");     
header("ETag: $etag");

echo $appcast;
exit;
?>

Sponsored Links: