-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
218 lines (204 loc) · 7.6 KB
/
index.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/*
// Author: Ryan Shehee
// Date: 2019-09-29 17:50:00
// Version: 0.7
//
// Description: This is a simple weblog that is
// clean, simple, easy, and portable!
//
// License: GNU GENERAL PUBLIC LICENSE, Version 3
*/
/*
// Start benchmark
*/
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
/*
// Disable caching
*/
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.
/*
// Define variables: title of blog, timezone, and logfile if desired
*/
$configFilename = 'config.php';
if(file_exists($configFilename)) {
require_once($configFilename);
}
/*
// Set timezone
*/
date_default_timezone_set($timezone);
/*
// Write log entry
*/
$geolocArray = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR']));
if(is_writable($logFilename) && $_SERVER['REMOTE_ADDR'] != $adminIP) {
file_put_contents($logFilename, date('Y-m-d H:i:s').' from '.$_SERVER['REMOTE_ADDR']." (".$geolocArray['geoplugin_city'].", ".$geolocArray['geoplugin_region']." : ".$geolocArray['geoplugin_latitude'].", ".$geolocArray['geoplugin_longitude'].")".PHP_EOL, FILE_APPEND);
}
/*
// Function for making the filename timestamp
*/
function makeTimestamp(&$markdownArray) {
$filename = $markdownArray['filename'];
$filenameArray = explode(".", $filename);
$filenameArray = explode("/", $filenameArray[0]);
$filenameArray = explode("_", $filenameArray[1]);
$filenameArray[1] = str_replace("-", ":", $filenameArray[1]);
if($filenameArray[1]) {
$timestamp = implode(" ", $filenameArray);
$timestamp = strtotime($timestamp);
} else {
$timestamp = $markdownArray['filemtime'];
}
return $timestamp;
}
/*
// Function for formatting sections
*/
function markdownToHTML(&$markdownArray) {
if(file_exists($markdownArray['filename'])) {
$timestamp = makeTimestamp($markdownArray);
$year = date('Y', $timestamp);
$month = date('F', $timestamp);
$date = date('jS', $timestamp);
$day = date('l', $timestamp);
$hour = date('g', $timestamp);
$minute = date('i', $timestamp);
$second = date('s', $timestamp);
$meridiem = date('A T', $timestamp);
$contents = file_get_contents($markdownArray['filename']);
$filesize = filesize($markdownArray['filename']);
$htmlString .= !empty($markdownArray['tag']) ? "<".$markdownArray['tag'] : "<section";
$htmlString .= !empty($markdownArray['class']) ? ' class="'.$markdownArray['class'].'" ' : NULL;
$htmlString .= ' id="' . (!empty($markdownArray['id']) ? $markdownArray['id'] : date('c', $timestamp) ).'">';
$Parsedown = new Parsedown();
$htmlString .= $Parsedown->text($contents);
$htmlString .= '<footer>';
$htmlString .= '<span class="datetime">';
$htmlString .= '<span class="day">'.$day.'</span><span class="comma">, </span>';
$htmlString .= '<span class="month">'.$month.'</span><span class="space"> </span>';
$htmlString .= '<span class="date">'.$date.'</span><span class="space"> </span>';
$htmlString .= '<span class="year">'.$year.'</span><span class="space"> </span>';
$htmlString .= '<span class="hour">'.$hour.'</span><span class="colon">:</span>';
$htmlString .= '<span class="minute">'.$minute.'</span><span class="colon">:</span>';
$htmlString .= '<span class="second">'.$second.'</span><span class="space"> </span>';
$htmlString .= '<span class="meridiem">'.$meridiem.'</span>';
$htmlString .= '</span>';
$htmlString .= '<span class="filesize">'.$filesize.' bytes</span>';
$htmlString .= '</footer>';
$htmlString .= "</". (!empty($markdownArray['tag']) ? $markdownArray['tag'] : "section") .">";
return $htmlString;
}
}
/*
// Read all markdown posts and convert them into HTML
*/
$parsedownFilename = 'Parsedown/Parsedown.php';
if(file_exists($parsedownFilename)) {
require_once($parsedownFilename);
}
/*
// Start with Welcome message
*/
if(file_exists($welcomeFilename)) {
$markdownArray = array(
'filename' => $welcomeFilename,
'tag' => 'article',
'id' => 'welcome',
'filemtime' => filemtime($welcomeFilename)
);
$htmlString .= markdownToHTML($markdownArray);
}
/*
// Then add FAQ
*/
if(file_exists($faqFilename)) {
$markdownArray = array(
'filename' => $faqFilename,
'tag' => 'section',
'id' => 'faq',
'filemtime' => filemtime($faqFilename)
);
$htmlString .= markdownToHTML($markdownArray);
}
/*
// Finally convert any posts
*/
$dir = glob("md/*.md");
$count = 0;
$total = count($dir);
if(is_array($dir)) {
foreach($dir as $postFilename) {
$markdownArray = array(
'filename' => $postFilename,
'tag' => 'section',
'id' => NULL,
'filemtime' => filemtime($postFilename)
);
$count++;
if($count == $total) {
$markdownArray['id'] = 'latest';
}
$htmlString .= markdownToHTML($markdownArray);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title." - Updated ".date('D, M jS Y g:i:s A T', $markdownArray['filemtime']); ?></title>
<link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet">
<style>
* { margin:0; padding:0; font-family:'Roboto',sans-serif; }
html, body { font-size:12pt; height: 100%; width:100%; }
html { background-color:#FFF; }
body { background-color:#FFF; color:#000; }
body > header, body > footer { background-color:#FFF; display:block; outline:1px solid #000; padding:0.5rem 10%; position:fixed; width:80%; z-index:1; }
body > header { top:0; }
body > footer { bottom:0; font-size:0.8rem; }
header h1 { float:left; }
body > header p, body >header nav { text-align:right; }
article, section { margin:auto; padding:0.5rem 0; width:80%; }
article { margin-top:5rem; }
section { border-top:1px solid #CCC; }
article footer, section footer { font-size:0.8rem; padding:0.5rem 0; width:100%; }
h1, h2, h3, h4, h5, h6, p { padding:0.5rem 0; }
ol, ul { padding:0.5rem 2rem; }
img { display:block; margin:0 auto; max-width:100%; }
.filesize { color:#CCC; float:right; }
a:link { color:#00F; border-bottom:1px solid #00F; border-radius:1rem; text-decoration:none; padding:0 0.5rem; } /* unvisited link */
a:visited { color:#00B; } /* visited link */
a:hover { color:#008; border-top:1px solid #008; border-bottom:none; } /* mouse over link */
a:active { color:#F00; border-top:1px solid #F00; border-bottom:none; } /* selected link */
</style>
</head>
<body>
<header>
<h1><?php echo $title; ?></h1>
<p class="datetime"><?php echo date('l, F jS Y g:i:s A T'); ?></p>
<nav class="right"><a href="#welcome">Welcome</a> <a href="#faq">FAQ</a> <a href="#latest">Latest</a></nav>
</header>
<?php echo $htmlString; ?>
<br>
<br>
<br>
<br>
<footer><p>Updated <?php echo date('l, F jS Y g:i:s A T', $markdownArray['filemtime']); ?>. Page generated in
<?php
/*
// End benchmark
*/
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
echo $total_time;
?> seconds. <a href="https://github.com/shehee/Simple_Weblog">Simple Weblog</a> is licensed under the <a href="https://www.gnu.org/licenses/gpl-3.0.en.html">GNU GPLv3</a>. <a href="https://parsedown.org/">Parsedown</a> is licensed under the <a href="https://github.com/erusev/parsedown/blob/master/LICENSE.txt">The MIT License</a>.</p></footer>
</body>
</html>