Added an RSS news feed for blog posts

This commit is contained in:
Markus Koch 2017-05-27 11:45:08 +02:00
parent 655fdf20e6
commit 673771dd27
5 changed files with 125 additions and 14 deletions

View File

@ -69,6 +69,7 @@
global $pageTitle; global $pageTitle;
global $postUrl; global $postUrl;
global $htmlHead; global $htmlHead;
global $CMS_RSS;
$counters['figure'] = 1; $counters['figure'] = 1;
$counters['video'] = 1; $counters['video'] = 1;
@ -95,12 +96,12 @@
$blogentry->tags = explode(',', $blogentry->tags); $blogentry->tags = explode(',', $blogentry->tags);
sort($blogentry->tags); sort($blogentry->tags);
$blogentry->permalink = "?p=blog&b=" . str_replace("/", POSTS_DIR_SEP, $blogentry->file); $blogentry->permalink = getServerUrl() . "/?p=blog&b=" . str_replace("/", POSTS_DIR_SEP, $blogentry->file);
$blogentry->isPublic = ($blogentry->status == "released"); $blogentry->isPublic = ($blogentry->status == "released");
} }
$renderIt = $CMS_INDEXING || ($blogentry->status == "released") || ($blogentry->status == "unlisted"); $renderIt = $CMS_INDEXING || ($blogentry->status == "released") || ($blogentry->status == "unlisted");
if ($renderIt) { if ($renderIt && !isset($CMS_RSS)) {
if (isset($postUrl) && !$CMS_INDEXING) { // Set meta information if (isset($postUrl) && !$CMS_INDEXING) { // Set meta information
$pageTitle = $blogentry->title; $pageTitle = $blogentry->title;
$htmlHead .= '<meta name="description" content="' . $blogentry->description . '"> $htmlHead .= '<meta name="description" content="' . $blogentry->description . '">
@ -212,7 +213,8 @@
} }
echo "<blog>"; if (!isset($CMS_RSS))
echo "<blog>";
if (isset($postUrl)) { // Display post if (isset($postUrl)) { // Display post
$CMS_SINGLE_POST = true; $CMS_SINGLE_POST = true;
echo '<div class="post">'; echo '<div class="post">';
@ -227,21 +229,44 @@
else { else {
//echo "<h1>Blog posts</h1>"; //echo "<h1>Blog posts</h1>";
if (isset($tagSearch)) { if (isset($tagSearch)) {
echo "<h1 class=\"bloginfoheader\">Archive for '$tagSearch'</h1>"; echo "<h1 class=\"bloginfoheader\"><table border=0 style='width: 100%;'><tr><td>Archive for '$tagSearch'</td>";
if (RSS_ENABLED) {
echo '<td style="text-align:right;padding:6px; padding-bottom: 0px; "><a href="/rss.php?t=' . $tagSearch . '" title="RSS Feed for \'' . $tagSearch . '\'"><img src="img/rss.svg" style="width: 32px;"></a></td>';
}
echo '</tr></table></h1>';
$pageTitle = "Archive for '$tagSearch'"; $pageTitle = "Archive for '$tagSearch'";
} }
if (isset($CMS_RSS) && RSS_SHOW_FULL) {
$CMS_SINGLE_POST = true;
}
$i = 0; $i = 0;
$morePosts = false; $morePosts = false;
foreach($posts as $k=>$v) { foreach($posts as $k=>$v) {
if ($v->isPublic) { if ($v->isPublic) {
if (!isset($tagSearch) || in_array($tagSearch, $v->tags)) { if (!isset($tagSearch) || in_array($tagSearch, $v->tags)) {
if ($i++ >= $postsSkip) { if ($i++ >= $postsSkip) {
//echo "<a href=\"$v->permalink\">$v->title by $v->author</a><br>"; if (isset($CMS_RSS)) {
echo '<div class="post">'; if (!isset($latestDisplayedPost)) {
require $v->includeFile; $latestDisplayedPost = $v->ctime;
renderFooter(); }
echo "</div>"; echo '<item>
echo '<hr class="postSeparator">'; <title>' . $v->title . '</title>
<description><![CDATA[';
require $v->includeFile;
renderFooter();
echo ']]></description>
<link><![CDATA[' . $v->permalink . ']]></link>
<guid isPermaLink="true"><![CDATA[' . $v->permalink . ']]></guid>
<pubDate>' . date($RSS_DATE_FORMAT, $v->ctime) . '</pubDate>
<dc:creator>' . $v->author . '</dc:creator>
</item>';
} else {
echo '<div class="post">';
require $v->includeFile;
renderFooter();
echo "</div>";
echo '<hr class="postSeparator">';
}
if (($i % POSTS_PAGE_SIZE) == 0) { if (($i % POSTS_PAGE_SIZE) == 0) {
$morePosts = true; $morePosts = true;
break; break;
@ -259,5 +284,6 @@
if ($morePosts || $postsSkip > 0) if ($morePosts || $postsSkip > 0)
echo "</tr></table></p>"; echo "</tr></table></p>";
} }
echo "</blog>"; if (!isset($CMS_RSS))
echo "</blog>";
?> ?>

30
img/rss.svg Normal file
View File

@ -0,0 +1,30 @@
<svg viewBox="342 512 305 305" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="box-shadow: 0px 0px 4px #142532;">
<style type="text/css">
.rsslogo {
stroke: #142532;
fill: none;
fill-opacity: 1;
stroke-width: 20;
}
.rsslogo-ellipse {
fill: #142532;
}
</style>
<g>
<ellipse class="rsslogo rsslogo-ellipse" cx="450" cy="710" rx="30" ry="30"/>
</g>
<line class="rsslogo" x1="400" y1="580" x2="504" y2="580"/>
<line class="rsslogo" x1="580" y1="656" x2="580" y2="760"/>
<line class="rsslogo" x1="497" y1="577" x2="583" y2="663"/>
<line class="rsslogo" x1="400" y1="620" x2="484" y2="620"/>
<line class="rsslogo" x1="540" y1="676" x2="540" y2="760"/>
<line class="rsslogo" x1="477" y1="617" x2="543" y2="683"/>
<line class="rsslogo" x1="360" y1="530" x2="630" y2="530"/>
<line class="rsslogo" x1="360" y1="530" x2="360" y2="800"/>
<line class="rsslogo" x1="630" y1="530" x2="630" y2="800"/>
<line class="rsslogo" x1="360" y1="800" x2="630" y2="800"/>
<line class="rsslogo" x1="623" y1="527" x2="633" y2="537"/>
<line class="rsslogo" x1="357" y1="793" x2="367" y2="803"/>
<line class="rsslogo" x1="623" y1="803" x2="633" y2="793"/>
<line class="rsslogo" x1="357" y1="537" x2="367" y2="527"/>
</svg>

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -71,9 +71,14 @@
<nav> <nav>
<div id="navContainer"> <div id="navContainer">
<ul class="pagetitle"><li <?php echo $homesel; ?> style="float: left;"><a class="pagetitle" href="/"><?php echo PAGE_NAME; ?></a></li></ul> <ul class="pagetitle"><li <?php echo $homesel; ?> style="float: left;"><a class="pagetitle" href="/"><?php echo PAGE_NAME; ?></a></li></ul>
<ul> <ul>
<?php mkMenu(); ?> <?php mkMenu(); ?>
</ul> <?php if (RSS_ENABLED) { ?>
<li style="float: right; margin: 10px;">
<a href="/rss.php" title="RSS Feed" style="margin: 0px; padding: 0px; border: none;"><img src="img/rss.svg" width="32px" height="32px"></a>
</li>
<?php } ?>
</ul>
</div> </div>
</nav> </nav>
<div id="contentContainer"> <div id="contentContainer">

40
rss.php Normal file
View File

@ -0,0 +1,40 @@
<?php
$CMS_ACTIVE = true;
$CMS_RSS = true;
require_once "shared.php";
if (RSS_ENABLED) {
header('Content-Type: application/rss+xml; charset=UTF-8');
ob_start(function($output) {
global $tagSearch;
global $latestDisplayedPost;
$RSS_DATE_FORMAT = "D, d M Y H:i:s O";
if (!isset($latestDisplayedPost)) {
$latestDisplayedPost = time();
}
if ($tagSearch != "") {
$tagSearchTitle = " - Archive for '" . $tagSearch . "'";
} else {
$tagSearchTitle = "";
}
$output = '<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/css" href="' . getServerUrl() . '/style.css" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>' . PAGE_NAME . $tagSearchTitle . '</title>
<description><![CDATA[' . "Blog of " . PAGE_AUTHOR . $tagSearchTitle . ']]></description>
<link>' . getServerUrl() . '</link>
<lastBuildDate>' . date($RSS_DATE_FORMAT, $latestDisplayedPost) . '</lastBuildDate>
<pubDate>' . date($RSS_DATE_FORMAT, $latestDisplayedPost) . '</pubDate>
<ttl>' . RSS_REFRESH_INTERVAL . '</ttl>
<atom:link href="' . getServerUrl() . "/rss.php" . '" rel="self" type="application/rss+xml" />
' . $output;
return $output;
});
require "blog.php";
echo '</channel></rss>';
}
?>

View File

@ -12,6 +12,10 @@
define('POSTS_MAX_FILENAME', 30); // Maximum length of a post name (including suffix) define('POSTS_MAX_FILENAME', 30); // Maximum length of a post name (including suffix)
define('POSTS_TAG_SEARCH', 30); // Maximum length for the tag search argument define('POSTS_TAG_SEARCH', 30); // Maximum length for the tag search argument
define('RSS_ENABLED', true); // Enable the RSS news feed
define('RSS_REFRESH_INTERVAL', 14400); // Advertised update interval for the RSS feed
define('RSS_SHOW_FULL', false); // Show extended section in RSS feed
$menuItems = array( $menuItems = array(
array("label" => "Page A", "link" => "?p=00-template", "highlight" => "related-subpages,00-template"), array("label" => "Page A", "link" => "?p=00-template", "highlight" => "related-subpages,00-template"),
array("label" => "Page B", "link" => "?p=404", "highlight" => "404"), array("label" => "Page B", "link" => "?p=404", "highlight" => "404"),
@ -135,4 +139,10 @@
return $counters['sources']++; return $counters['sources']++;
} }
} }
function getServerUrl() {
$temp = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$temp .= $_SERVER['HTTP_HOST'];
return $temp;
}
?> ?>