Created
August 20, 2019 16:30
-
-
Save JeffreyATW/45188334304713b845ed5a97c6e6d55c to your computer and use it in GitHub Desktop.
RSS feed based off of http://scribbles.clockworkcaracal.com/posts/2018-07-06-simple-php-webcomic-site/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Change these to suit your site! | |
$homepage = "http://example.com"; // The home page of your comic | |
$comic_dir = "comics"; // The folder where comic image files are kept | |
$news_dir = "news"; // The folder where post/news HTML files are kept | |
$digits = 3; // The number of digits in each comic ID. e.g. 3 for 001 | |
// Checks whether a page exists for a given ID in the directory | |
// If it does, returns the file name | |
function page_exists($idnum, $target_dir) { | |
global $digits; | |
if (strlen($idnum) < $digits) $idnum = str_pad($idnum, $digits, '0', STR_PAD_LEFT); | |
if (filetype($target_dir) == 'dir') { | |
$flist = glob("$target_dir/$idnum.*"); | |
if (count($flist) > 0) return $flist[0]; | |
else return false; | |
} | |
else return false; | |
} | |
function display_news($page_id) { | |
global $news_dir; | |
if ($news_file = page_exists($page_id, $news_dir)) include($news_file); | |
} | |
function add_item($item, $key) { | |
global $comic_dir, $homepage; | |
if (substr(mime_content_type("$comic_dir/$item"),0,5) == 'image') { | |
$item_id = explode(".", $item)[0]; | |
echo "<item> | |
<guid>$homepage/index.php?id=$item_id</guid> | |
<title>$item_id</title> | |
<link>$homepage/index.php?id=$item_id</link> | |
<description><![CDATA["; | |
display_news($item_id); | |
echo "]]></description> | |
</item>\n"; | |
} | |
} | |
function comics() { | |
global $comic_dir; | |
if (filetype($comic_dir) == 'dir') { | |
$file_list = array_slice(scandir($comic_dir,1), 0, 20); | |
array_walk($file_list, 'add_item'); | |
} else { | |
echo filetype($comic_dir); | |
} | |
} | |
header( "Content-type: text/xml"); | |
echo "<?xml version='1.0' encoding='UTF-8'?> | |
<rss version='2.0' xmlns:atom=\"http://www.w3.org/2005/Atom\"> | |
<channel> | |
<title>The Garden of Everything</title> | |
<link>$homepage</link> | |
<atom:link href=\"$homepage/"; | |
echo basename($_SERVER['PHP_SELF']); | |
echo "\" rel=\"self\" type=\"application/rss+xml\" /> | |
<description /> | |
<language>en-us</language>\n"; | |
comics(); | |
echo "</channel></rss>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment