krz/php-blog
A PHP blogging system with built-in static comments.
clone: git clone https://gitbay.org/krz/php-blog.git
main: _functions/loadRSS.php · raw
1<?php
2
3function loadRSS(string $fileName): string
4{
5 // Get the file contents and decode JSON
6 $fileContents = file_get_contents($fileName);
7 $data = json_decode($fileContents);
8
9 // Set-up RSS variables
10 $rssCounter = 0;
11 $rssContents = '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
12 ><channel><atom:link href="' . $GLOBALS['fullDomain'] . '/rss.xml" rel="self" type="application/rss+xml" /><title>Blog</title><link>' . $GLOBALS['fullDomain'] . '</link><description>Lorem ipsum dolor sit amet...</description><copyright>Copyright 20xx - 20xx, My Name</copyright><language>en-us</language><docs>https://cyber.harvard.edu/rss/index.html</docs><lastBuildDate>Mon, 04 Jan 2021 00:00:00 CST</lastBuildDate><ttl>60</ttl><image><url></url><title>Blog</title><link>'.$GLOBALS['fullDomain'].'</link></image>';
13
14 // Loop through the JSON object
15 foreach ($data as $postObject) {
16 // Only load published posts
17 if ($postObject->published == 'Yes') {
18 // Parse the markdown to HTML
19 include_once('_functions/parseMarkdown.php');
20 $fileLink = str_replace($GLOBALS['fullDomain'] . '/post/', '', $postObject->link);
21 $fileName = 'posts/' . $postObject->id . '-' . str_replace('.html', '.md', $fileLink);
22 $securedHTML = parseMarkdown($fileName);
23
24 if ($rssCounter == 0) {
25 $rssContents .= '<lastBuildDate>' . date_format(date_create($postObject->created), 'D, d M Y H:i:s T') . '</lastBuildDate>';
26 $rssCounter = $rssCounter + 1;
27 }
28
29 $rssContents .=
30 '<item><title>' .
31 str_replace(['&', '<', '>'], ['&', '<', '>'], $postObject->title) .
32 '</title><author>' .
33 str_replace(['&', '<', '>'], ['&', '<', '>'], $postObject->author) .
34 '</author><dc:creator>' .
35 str_replace(['&', '<', '>'], ['&', '<', '>'], $postObject->author) .
36 '</dc:creator><link>' .
37 str_replace(['&', '<', '>'], ['&', '<', '>'], $postObject->link) .
38 '</link><pubDate>' .
39 date_format(date_create($postObject->created), 'D, d M Y H:i:s T') .
40 '</pubDate><guid>' .
41 str_replace(['&', '<', '>'], ['&', '<', '>'], $postObject->link) .
42 '</guid><description><![CDATA[' .
43 $postObject->description .
44 ']]></description><content:encoded><![CDATA[' .
45 $securedHTML .
46 ']]></content:encoded></item>';
47 }
48 }
49 $rssContents .= '</channel></rss>';
50 return $rssContents;
51}