diff options
Diffstat (limited to '_functions')
-rw-r--r-- | _functions/loadJSON.php | 111 | ||||
-rw-r--r-- | _functions/loadRSS.php | 51 | ||||
-rw-r--r-- | _functions/parseMarkdown.php | 21 | ||||
-rw-r--r-- | _functions/submitComment.php | 25 |
4 files changed, 208 insertions, 0 deletions
diff --git a/_functions/loadJSON.php b/_functions/loadJSON.php new file mode 100644 index 0000000..c8540dc --- /dev/null +++ b/_functions/loadJSON.php @@ -0,0 +1,111 @@ +<?php + +function loadHomepageJSON(string $fileName): string +{ + // Get the file contents and decode JSON + $fileContents = file_get_contents($fileName); + $data = json_decode($fileContents); + + // Loop through the JSON object + $stack = []; + $metadata = ''; + foreach ($data as $postObject) { + // Only load published posts + if ($postObject->published == 'Yes') { + // Add relevant metadata to the output template + $year = date_format(date_create($postObject->created), "Y"); + if (!in_array($year, $stack)) { + // If any posts have been created in a list, close the existing <ul> and open a new <ul> after the heading + if (!empty($stack)) { + $metadata .= '</ul>'; + } + $metadata .= '<h2 class="text-center">' . $year . '</h2><ul class="blog-posts">'; + array_push($stack, $year); + } + $metadata .= '<li><time datetime=' . date_format(date_create($postObject->created), "Y-m-d") . '>' . date_format(date_create($postObject->created), "Y-m-d") . '</time><a href=' . $postObject->link . ' aria-label="Read ' . $postObject->title . ' blog post">' . $postObject->title . '</a></li>'; + } + } + $metadata .= '</ul>'; + return $metadata; +} + +function loadCategoryJSON(string $fileName): string +{ + // Get the file contents and decode JSON + $fileContents = file_get_contents($fileName); + $data = json_decode($fileContents); + + // Sort the JSON data by the `tag`, then by `create`. + // I had to sort a -> b to get an "A-Z" sort for `tag`, + // but b -> a to get a "first-last" sort for `create`. + uasort($data, function ($a, $b) { + return strcmp($a->tag, $b->tag) ?: strcmp($b->created, $a->created); + }); + + // Loop through the JSON object + $stack = []; + $metadata = ''; + foreach ($data as $postObject) { + // Only load published posts + if ($postObject->published == 'Yes') { + // Add relevant metadata to the output template + $category = str_replace('-', ' ', $postObject->tag); + if (!in_array($category, $stack)) { + // If any posts have been created in a list, close the existing <ul> and open a new <ul> after the heading + if (!empty($stack)) { + $metadata .= '</ul>'; + } + $metadata .= '<h2 id=' . $category . ' class="text-center">' . ucwords($category) . '</h2><ul class="blog-posts">'; + array_push($stack, $category); + } + + $metadata .= '<li><time datetime=' . date_format(date_create($postObject->created), "Y-m-d") . '>' . date_format(date_create($postObject->created), "Y-m-d") . '</time><a href=' . $postObject->link . ' aria-label="Read ' . $postObject->title . ' blog post">' . $postObject->title . '</a></li>'; + } + } + $metadata .= '</ul>'; + return $metadata; +} + +function loadPostJSON(string $fileName, string $query) +{ + // Get the file contents and decode JSON + $fileContents = file_get_contents($fileName); + $data = json_decode($fileContents); + + // Loop through the JSON object + foreach ($data as $postObject) { + // If this function is called to get a specific post's metadata, return the whole object + if ($postObject->link == $GLOBALS['fullDomain'] . "/post/" . $query) { + return $postObject; + } + } +} + +function loadCommentJSON(string $fileName, string $query = null): string +{ + // Set up an empty comment section + $commentSection = ''; + + // Load the file and decode the JSON + $fileContents = file_get_contents($fileName); + $data = json_decode($fileContents); + + // Sort comments by date - latest comments will appear at the top + uasort($data, function ($a, $b) { + return strcmp($b->timestamp, $a->timestamp); + }); + + // Loop through all comments add to returned string if it matches criteria + foreach ($data as $commentObject) { + if (($query !== null) && ($commentObject->postURL == $GLOBALS['fullDomain'] . "/post/" . $query)) { + $securedHTML = parseMarkdown(null, $commentObject->comment); + $commentSection .= '<div class="user-comment"><div class="row"><label>Timestamp:</label><p>' . $commentObject->timestamp . '</p></div><div class="row"><label>Name:</label><p>' . $commentObject->username . '</p></div><div class="row markdown"><label>Comment:</label><div class="comment-markdown">' . $securedHTML . '</div></div></div>'; + } else if ($query == null) { + $securedHTML = parseMarkdown(null, $commentObject->comment); + $pageURL = str_replace($GLOBALS['fullDomain'] . '/post/', '', $commentObject->postURL); + $postObject = loadPostJSON('_data/metadata.json', $pageURL); + $commentSection .= '<div class="user-comment"><div class="row"><label>Post:</label><a href="' . $commentObject->postURL . '#comments">' . $postObject->title . '</a></div><div class="row"><label>Timestamp:</label><p>' . $commentObject->timestamp . '</p></div><div class="row"><label>Name:</label><p>' . $commentObject->username . '</p></div><div class="row markdown"><label>Comment:</label><div class="comment-markdown">' . $securedHTML . '</div></div></div>'; + } + } + return $commentSection; +}
\ No newline at end of file diff --git a/_functions/loadRSS.php b/_functions/loadRSS.php new file mode 100644 index 0000000..1deb969 --- /dev/null +++ b/_functions/loadRSS.php @@ -0,0 +1,51 @@ +<?php + +function loadRSS(string $fileName): string +{ + // Get the file contents and decode JSON + $fileContents = file_get_contents($fileName); + $data = json_decode($fileContents); + + // Set-up RSS variables + $rssCounter = 0; + $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/" + ><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>'; + + // Loop through the JSON object + foreach ($data as $postObject) { + // Only load published posts + if ($postObject->published == 'Yes') { + // Parse the markdown to HTML + include_once('_functions/parseMarkdown.php'); + $fileLink = str_replace($GLOBALS['fullDomain'] . '/post/', '', $postObject->link); + $fileName = 'posts/' . $postObject->id . '-' . str_replace('.html', '.md', $fileLink); + $securedHTML = parseMarkdown($fileName); + + if ($rssCounter == 0) { + $rssContents .= '<lastBuildDate>' . date_format(date_create($postObject->created), 'D, d M Y H:i:s T') . '</lastBuildDate>'; + $rssCounter = $rssCounter + 1; + } + + $rssContents .= + '<item><title>' . + str_replace(['&', '<', '>'], ['&', '<', '>'], $postObject->title) . + '</title><author>' . + str_replace(['&', '<', '>'], ['&', '<', '>'], $postObject->author) . + '</author><dc:creator>' . + str_replace(['&', '<', '>'], ['&', '<', '>'], $postObject->author) . + '</dc:creator><link>' . + str_replace(['&', '<', '>'], ['&', '<', '>'], $postObject->link) . + '</link><pubDate>' . + date_format(date_create($postObject->created), 'D, d M Y H:i:s T') . + '</pubDate><guid>' . + str_replace(['&', '<', '>'], ['&', '<', '>'], $postObject->link) . + '</guid><description><![CDATA[' . + $postObject->description . + ']]></description><content:encoded><![CDATA[' . + $securedHTML . + ']]></content:encoded></item>'; + } + } + $rssContents .= '</channel></rss>'; + return $rssContents; +}
\ No newline at end of file diff --git a/_functions/parseMarkdown.php b/_functions/parseMarkdown.php new file mode 100644 index 0000000..6ca15d8 --- /dev/null +++ b/_functions/parseMarkdown.php @@ -0,0 +1,21 @@ +<?php + +function parseMarkdown(string $fileName = null, string $fileContents = null) +{ + if ($fileName != null) { + // Get file contents + $fileContents = file_get_contents('/var/www/' . $GLOBALS['shortDomain'] . '/' . $fileName, FILE_USE_INCLUDE_PATH); + } + + // Parse the markdown to HTML + include_once('_classes/Parsedown.php'); + $md = Parsedown::instance()->text($fileContents); + $html = new DOMDocument(); + $html->loadHTML($md); + $html_links = $html->getElementsByTagName('a'); + foreach ($html_links as $html_ink) { + $html_ink->setAttribute('rel', 'noopener,noreferrer'); + $html_ink->setAttribute('target', '_blank'); + } + return $html->saveHTML(); +}
\ No newline at end of file diff --git a/_functions/submitComment.php b/_functions/submitComment.php new file mode 100644 index 0000000..d96d39a --- /dev/null +++ b/_functions/submitComment.php @@ -0,0 +1,25 @@ +<?php + +function submitComment() +{ + // Get the content sent from the comment form + $comment = htmlentities($_POST['userContent']); + $postURL = $_POST['postURL']; + + // Set default values if blank + if (isset($_POST['userName']) && trim($_POST['userName']) !== "") { + $username = $_POST['userName']; + } else { + $username = null; + } + + // Create a 'Comment' object + include_once('_classes/Comment.php'); + $userComment = new Comment($comment, $postURL, $username); + + // Append comment to JSON file + $userComment->saveComment('_data/comments.json'); + + // Send the user back + header('Location: ' . $postURL . '#comments'); +}
\ No newline at end of file |