1
0
Fork 0

Adds rss capabilities

This commit is contained in:
sloum 2024-01-18 09:09:45 -08:00
parent 0494d26b0c
commit 398b5840ed
5 changed files with 62 additions and 10 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
posts/*
media/*
blog_pass_hash.txt
rssFeed.xml

View File

@ -19,16 +19,16 @@ function title_from_filename( $fn ) {
return $fn;
}
function make_date_from_stamp( $ts ) {
function make_date_from_stamp( $ts, $ds=DATE_STYLE ) {
// Expects "time" from split_filename
$date = date( DATE_STYLE, (int)$ts );
$date = date( $ds, (int)$ts );
return $date;
}
function split_filename( $fn ) {
function split_filename( $fn, $ds=DATE_STYLE ) {
$parts = explode( "_", $fn, 2);
$out = [];
$out["time"] = make_date_from_stamp( $parts[0] );
$out["time"] = make_date_from_stamp( $parts[0], $ds );
$out["title"] = title_from_filename( $parts[1] );
$out["raw"] = urlencode( $fn );
$out["encoded"] = $fn;

View File

@ -16,6 +16,7 @@
<?php endif; ?>
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/admin-bar.css">
<link rel="alternate" type="application/rss+xml" title="<?php echo SITE_NAME; ?> RSS Feed" href="/rssFeed.xml">
<?php echo INDEX_HEAD ?: ""; ?>
</head>
<body class="index">
@ -49,9 +50,3 @@
<?php endif; ?>
</body>
</html>
</body>
</html>

View File

@ -26,6 +26,8 @@
$success = file_put_contents( "./posts/" . $ts . "_" . urlencode( $title ) . ".md", $body );
if ( $success ) {
// Yay! Redirect to admin
include_once( 'rss_gen.php' );
build_feed();
header("Location: admin.php?success=1");
die();
} else {

54
rss_gen.php Normal file
View File

@ -0,0 +1,54 @@
<?php
// Bounce if not logged in
$logged_in = $_COOKIE["checkin"] ?? null;
if ( !$logged_in || $logged_in != "waiting for expiry" ) {
header("Location: blog_log.php");
die();
}
include_once( 'common.php' );
include_once( 'config.php' );
function build_feed() {
$files = array_values( array_diff( scandir( "./posts" ), array('..', '.')));
if ( !count( $files ) ) {
return;
}
$siteName = SITE_NAME;
$siteLang = SITE_LANG;
$siteRoot = $root = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
$buildDate = date( DATE_RSS );
$header = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>$siteName</title>
<link>$siteRoot</link>
<description>$siteName RSS Feed</description>
<lastBuildDate>$buildDate</lastBuildDate>
<language>$siteLang</language>
XML;
$item = <<<'XML'
<item>
<title>%s</title>
<guid>%s</guid>
<link>%s</link>
<pubDate>%s</pubDate>
</item>
XML;
foreach ( array_reverse( $files ) as $f ) {
$fn = split_filename( $f, DATE_RSS );
$root = $siteRoot . 'post.php?f=';
$t = title_from_filename( $f );
$header .= sprintf( $item, $fn['title'], $fn['encoded'], $root . $fn['encoded'], $fn['time'] );
}
$header .= " </channel>\n</rss>";
file_put_contents( "./rssFeed.xml", $header );
}