krz/php-blog
clone: git clone https://gitbay.org/krz/php-blog.git
main: index.php · raw
1<?php
2
3// Set global variables for this specific website
4// Domains must match the URLs in the _data files
5$websiteProtocol = 'https://';
6$shortDomain = 'blog.example.com';
7$fullDomain = $websiteProtocol . $shortDomain;
8
9// Trim the leading slashes & split the path on slashes
10$path = ltrim($_SERVER['REQUEST_URI'], '/');
11$elements = explode('/', $path);
12
13// Show home if there are no parameters or paths
14if (empty($elements[0])) {
15 showHomepage();
16} // Else, identify the first item in the URL so we can identify which function to use
17else {
18 switch (array_shift($elements)) {
19 case 'post':
20 showPost($elements);
21 break;
22 case 'submit-comment':
23 include_once('_functions/submitComment.php');
24 submitComment();
25 break;
26 case 'comments':
27 showComments();
28 break;
29 case 'category':
30 header('Location: ' . $fullDomain . '/categories/');
31 die();
32 case 'categories':
33 showCategory();
34 break;
35 case 'rss':
36 case 'rss.xml':
37 showRSS();
38 break;
39 case 'robots.txt':
40 showRobots();
41 break;
42 default:
43 header('HTTP/1.1 404 Not Found');
44 }
45}
46
47function showHomepage()
48{
49 // Add Articles
50 $contentCol = '<h1>My Blog</h1>';
51
52 // Get metadata on all posts from the metadata.json file
53 include_once('_functions/loadJSON.php');
54 $contentCol .= loadHomepageJSON('_data/metadata.json');
55
56 // Create a template
57 include_once('_classes/Template.php');
58 $template = new Template(
59 $GLOBALS['fullDomain'],
60 'Explore the thoughts of...',
61 'Blog | YourName',
62 '',
63 $contentCol,
64 ''
65 );
66
67 // Echo the HTML to the user
68 $template->echoTemplate();
69}
70
71function showPost($params)
72{
73 // URL Parameter
74 $query = $params[0];
75
76 // Get metadata on this post from the metadata.json file
77 include_once('_functions/loadJSON.php');
78 $headerData = loadPostJSON('_data/metadata.json', $query);
79
80 // Apply metadata to post header
81 $header = '<header><h1 class="blog-post-title">' . $headerData->title . '</h1><div class="blog-post-metadata"><time class="d-inline" datetime="' . date_format(date_create($headerData->created), "Y-m-d") . '">' . date_format(date_create($headerData->created), "Y-m-d") . '</time><span style="margin:0 0.5rem;">::</span><a href="' . $GLOBALS['fullDomain'] . '/categories/#' . $headerData->tag . '">#' . $headerData->tag . '</a></div></header>';
82
83 // Get post .md file that matches the .html file requested in the URL
84 include_once('_functions/parseMarkdown.php');
85 $fileName = './posts/' . $headerData->id . '-' . str_replace('.html', '.md', $query);
86 $securedHTML = parseMarkdown($fileName);
87
88 // Echo Results
89 $contentCol = '<article class="blog-post">' . $header . '<section class="blog-post-body">' . $securedHTML . '</section></article>';
90
91 // Create a comment section
92 $postURL = $GLOBALS['fullDomain'] . '/post/' . $query;
93 $commentForm = '<form action="/submit-comment/" method="POST"><h3>Leave a Comment</h3><section hidden><label class="form-label" for="postURL">Post URL</label><input class="form-control" id="postURL" name="postURL" type="text" value="' . $postURL . '"></section><section><label class="form-label" for="userName">Display Name</label><input class="form-control" id="userName" name="userName" placeholder="John Doe" type="text"></section><section><label class="form-label" for="userContent">Your Comment</label><textarea class="form-control" id="userContent" name="userContent" rows="3" placeholder="# Feel free to use Markdown" aria-describedby="commentHelp" required></textarea><div id="commentHelp" class="form-text">Comments are saved as Markdown and cannot be edited or deleted.</div></section><button type="submit">Submit</button></form>';
94
95 // Load saved comments
96 $userComments = '<section id="comments" class="comments"><h3>Comments</h3>';
97 $userComments .= loadCommentJSON('_data/comments.json', $query);
98 $userComments .= '</section>';
99
100 // Combine comment form and previous user comments
101 $commentSection = $commentForm . $userComments;
102
103 // Create a template
104 include_once('_classes/Template.php');
105 $template = new Template(
106 $GLOBALS['fullDomain'] . '/post/' . $query,
107 $headerData->description . ' Read more at ' . $GLOBALS['fullDomain'] . '!',
108 $headerData->title . ' | Blog',
109 '<link rel="stylesheet" href="/static/prism.css"><script src="/static/prism.js"></script>',
110 $contentCol,
111 $commentSection
112 );
113
114 // Echo the HTML to the user
115 $template->echoTemplate();
116}
117
118function showComments()
119{
120 // Add title
121 $commentHeader = '<h1>Recent Comments</h1>';
122
123 // Load saved comments
124 include_once('_functions/parseMarkdown.php');
125 include_once('_functions/loadJSON.php');
126 $userComments = '<section id="comments" class="comments"><h3>Comments Across All Blog Posts</h3>';
127 $userComments .= loadCommentJSON('_data/comments.json');
128 $userComments .= '</section>';
129
130 // Combine comment form and previous user comments
131 $commentSection = $commentHeader . $userComments;
132
133 // Create a template
134 include_once('_classes/Template.php');
135 $template = new Template(
136 $GLOBALS['fullDomain'] . '/comments/',
137 'Read through some recent comments for blog posts at ' . $GLOBALS['fullDomain'] . '.',
138 'Recent Comments | Blog',
139 '<link rel="stylesheet" href="/static/prism.css"><script src="/static/prism.js"></script><meta name="robots" content="noindex,nofollow">',
140 '',
141 $commentSection
142 );
143
144 // Echo the HTML to the user
145 $template->echoTemplate();
146}
147
148function showCategory()
149{
150 // Open the article list
151 $contentCol = '<h1>Categories</h1>';
152
153 // Get metadata on all posts from the metadata.json file
154 include_once('_functions/loadJSON.php');
155 $contentCol .= loadCategoryJSON('_data/metadata.json');
156
157 // Create a template
158 include_once('_classes/Template.php');
159 $template = new Template(
160 $GLOBALS['fullDomain'] . '/categories/',
161 'Browse the categories for blog posts at ' . $GLOBALS['fullDomain'] . '.',
162 'Categories | Blog',
163 '<meta name="robots" content="noindex,nofollow">',
164 $contentCol,
165 ''
166 );
167
168 // Echo the HTML to the user
169 $template->echoTemplate();
170}
171
172function showRSS()
173{
174 // Loop through the metadata file and display any article that is published
175 include_once('_functions/loadRSS.php');
176 $rssContents = loadRSS('_data/metadata.json');
177
178 // Echo the RSS XML
179 header('Content-type: text/xml');
180 echo $rssContents;
181 die();
182}
183
184function showRobots()
185{
186 header('Content-type: text/plain');
187 echo 'User-agent: * Disallow:';
188 die();
189}