~cyborg/php-blog

php-blog/index.php -rw-r--r-- 6.7 KiB
ce6c1357Christian Cleberg add test site link 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php

// Set global variables for this specific website
// Domains must match the URLs in the _data files
$websiteProtocol = 'https://';
$shortDomain = 'blog.example.com';
$fullDomain = $websiteProtocol . $shortDomain;

// Trim the leading slashes & split the path on slashes
$path = ltrim($_SERVER['REQUEST_URI'], '/');
$elements = explode('/', $path);

// Show home if there are no parameters or paths
if (empty($elements[0])) {
    showHomepage();
} // Else, identify the first item in the URL so we can identify which function to use
else {
    switch (array_shift($elements)) {
        case 'post':
            showPost($elements);
            break;
        case 'submit-comment':
            include_once('_functions/submitComment.php');
            submitComment();
            break;
        case 'comments':
            showComments();
            break;
        case 'category':
            header('Location: ' . $fullDomain . '/categories/');
            die();
        case 'categories':
            showCategory();
            break;
        case 'rss':
        case 'rss.xml':
            showRSS();
            break;
        case 'robots.txt':
            showRobots();
            break;
        default:
            header('HTTP/1.1 404 Not Found');
    }
}

function showHomepage()
{
    // Add Articles
    $contentCol = '<h1>My Blog</h1>';

    // Get metadata on all posts from the metadata.json file
    include_once('_functions/loadJSON.php');
    $contentCol .= loadHomepageJSON('_data/metadata.json');

    // Create a template
    include_once('_classes/Template.php');
    $template = new Template(
        $GLOBALS['fullDomain'],
        'Explore the thoughts of...',
        'Blog | YourName',
        '',
        $contentCol,
        ''
    );

    // Echo the HTML to the user
    $template->echoTemplate();
}

function showPost($params)
{
    // URL Parameter
    $query = $params[0];

    // Get metadata on this post from the metadata.json file
    include_once('_functions/loadJSON.php');
    $headerData = loadPostJSON('_data/metadata.json', $query);

    // Apply metadata to post header
    $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>';

    // Get post .md file that matches the .html file requested in the URL
    include_once('_functions/parseMarkdown.php');
    $fileName = './posts/' . $headerData->id . '-' . str_replace('.html', '.md', $query);
    $securedHTML = parseMarkdown($fileName);

    // Echo Results
    $contentCol = '<article class="blog-post">' . $header . '<section class="blog-post-body">' . $securedHTML . '</section></article>';

    // Create a comment section
    $postURL = $GLOBALS['fullDomain'] . '/post/' . $query;
    $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>';

    // Load saved comments
    $userComments = '<section id="comments" class="comments"><h3>Comments</h3>';
    $userComments .= loadCommentJSON('_data/comments.json', $query);
    $userComments .= '</section>';

    // Combine comment form and previous user comments
    $commentSection = $commentForm . $userComments;

    // Create a template
    include_once('_classes/Template.php');
    $template = new Template(
        $GLOBALS['fullDomain'] . '/post/' . $query,
        $headerData->description . ' Read more at ' . $GLOBALS['fullDomain'] . '!',
        $headerData->title . ' | Blog',
        '<link rel="stylesheet" href="/static/prism.css"><script src="/static/prism.js"></script>',
        $contentCol,
        $commentSection
    );

    // Echo the HTML to the user
    $template->echoTemplate();
}

function showComments()
{
    // Add title
    $commentHeader = '<h1>Recent Comments</h1>';

    // Load saved comments
    include_once('_functions/parseMarkdown.php');
    include_once('_functions/loadJSON.php');
    $userComments = '<section id="comments" class="comments"><h3>Comments Across All Blog Posts</h3>';
    $userComments .= loadCommentJSON('_data/comments.json');
    $userComments .= '</section>';

    // Combine comment form and previous user comments
    $commentSection = $commentHeader . $userComments;

    // Create a template
    include_once('_classes/Template.php');
    $template = new Template(
        $GLOBALS['fullDomain'] . '/comments/',
        'Read through some recent comments for blog posts at ' . $GLOBALS['fullDomain'] . '.',
        'Recent Comments | Blog',
        '<link rel="stylesheet" href="/static/prism.css"><script src="/static/prism.js"></script><meta name="robots" content="noindex,nofollow">',
        '',
        $commentSection
    );

    // Echo the HTML to the user
    $template->echoTemplate();
}

function showCategory()
{
    // Open the article list
    $contentCol = '<h1>Categories</h1>';

    // Get metadata on all posts from the metadata.json file
    include_once('_functions/loadJSON.php');
    $contentCol .= loadCategoryJSON('_data/metadata.json');

    // Create a template
    include_once('_classes/Template.php');
    $template = new Template(
        $GLOBALS['fullDomain'] . '/categories/',
        'Browse the categories for blog posts at ' . $GLOBALS['fullDomain'] . '.',
        'Categories | Blog',
        '<meta name="robots" content="noindex,nofollow">',
        $contentCol,
        ''
    );

    // Echo the HTML to the user
    $template->echoTemplate();
}

function showRSS()
{
    // Loop through the metadata file and display any article that is published
    include_once('_functions/loadRSS.php');
    $rssContents = loadRSS('_data/metadata.json');

    // Echo the RSS XML
    header('Content-type: text/xml');
    echo $rssContents;
    die();
}

function showRobots()
{
    header('Content-type: text/plain');
    echo 'User-agent: * Disallow:';
    die();
}