-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.php
111 lines (107 loc) · 4.52 KB
/
blog.php
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
<!DOCTYPE html>
<html lang='en'>
<?php
# Error handler, a carry over from the olden days
function iAmError($n) {
return "<center><h3>An Error Occured: $n</h3></center>";
}
# A small helper to remove . and .. from scandir
function scandir2($n){
return array_slice(scandir($n), 2);
}
# Parsedown for post rendering
include "dependencies/Parsedown.php";
$Parsedown = new Parsedown();
# GET method for post number
$post = isset($_GET['post']) ? $_GET['post'] : '';
# POSTS STORED IN
$dir = 'blog';
# CHANGE THIS TO CHANGE THE NUMBER OF LINES RENDERED
$RENDER_LINES = 3;
?>
<head>
<title>blog</title>
<meta charset='utf-8'>
<style>
table {
margin-top: 100px;
margin-right: auto;
margin-left: auto;
}
</style>
</head>
<body>
<table width="800">
<tr>
<td>
<div>
<?php
if(empty($post)) {
# Include scandir2 to exclude . and .. folder paths
$posts = scandir2($dir);
if(!empty($posts)){
sort($posts);
#To flip the order of the files so that our newest files are first
$posts = array_reverse($posts);
foreach($posts as $post) {
$files = scandir2("$dir/$post");
foreach($files as $file){
if (explode(".", $file)[1] == "md") {
# Limits the number of lines that we render
$handler = fopen("$dir/$post/$file", "r");
$title = trim(substr(fgets($handler), 2));
$md = "##[$title](blog.php?post=$post)\n";
$md .= fgets($handler); // Blank Line
$md .= fgets($handler); // Date
$md .= fgets($handler); // Blank Line
for ( $x = 0; $x < $RENDER_LINES; $x++ ) {
$md .= fgets($handler);
}
if(!feof($handler))
$md .= "\n[Read more...](blog.php?post=$post)";
fclose($handler);
echo $Parsedown->text($md);
echo "<br />";
break;
}
}
}
} else {
echo iAmError('There are currently no posts.');
}
} else {
$posts = scandir2("$dir");
if (in_array($post, $posts)){
$files = scandir2("$dir/$post");
foreach($files as $file){
if (explode(".", $file)[1] == "md") {
$handler = fopen("$dir/$post/$file", "r");
$md = "#[" . trim(substr(fgets($handler), 2)) . "](blog.php?post=$post)\n";
while(!feof($handler))
$md .= fgets($handler);
fclose($handler);
echo $Parsedown->text($md);
echo "<br /><a href='blog.php'>Back</a>";
break;
}
}
} else {
echo iAmError('Blog post not found.');
}
}
?>
</div>
</td>
</tr>
<tr>
<td>
<div style="text-align: center;">
<p>
<a href='.'>index</a>
</p>
</div>
</td>
</tr>
</table>
</body>
</html>