-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blogfu.php
285 lines (223 loc) · 8.22 KB
/
Blogfu.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
// -----------------------------------------------------------------------------------
// ____ _ _____
// | __ )| | ___ __ _| ___| _
// | _ \| |/ _ \ / _` | |_ | | | |
// | |_) | | (_) | (_| | _|| |_| |
// |____/|_|\___/ \__, |_| \__,_|
// |___/
//
// Absurdly simple flat file blog engine written in PHP
// -----------------------------------------------------------------------------------
// VERSION 1.0.0
// -----------------------------------------------------------------------------------
// USAGE:
//
// $B = new Blogfu(array();
//
// if ($B->uriExists()) {
// if ($B->isValidRequest()) {
// $entry = $B->getEntry();
// echo $entry->title;
// echo $entry->body;
// }
// else {
// echo '404';
// }
// }
// else {
//
// foreach ($B->getTitles() as $row) {
// echo $row->date;
// echo $row->title;
// }
// }
//
// -----------------------------------------------------------------------------------
// Author : Rick Ellis https://github.com/rickellis/Blogfu
// License : MIT
// -----------------------------------------------------------------------------------
class Blogfu {
var $uriRequest = '';
var $marker = array();
var $options = array(
'showErrors' => false,
'baseDir' => 'content',
'tocFile' => '_toc.json',
'fileExt' => '.md'
);
// --------------------------------------------------------------------
// Constructor. Sets the options and validates them.
function Blogfu($options = array()) {
if (is_array($options) and count($options) > 0) {
foreach ($options as $key => $val) {
$this->options[$key] = $val;
}
}
if ($this->_validateOptions()) {
$this->_getUriRequest();
}
}
// --------------------------------------------------------------------
// Returns true/false if the URI contains a segment
function uriExists() {
return ($this->uriRequest !== '') ? true : false;
}
// --------------------------------------------------------------------
// Returns true if the URI corresponds to a blog post file
function isValidRequest() {
if ($this->uriExists() === false) {
return false;
}
if (! file_exists($this->_getRequestPath())) {
$this->_printError('The URI does not correspond to a valid file name.');
return false;
}
return true;
}
// --------------------------------------------------------------------
// Get the blog entry based on the URI request. Returns an object
function getEntry() {
$titles = $this->_getDecodedTitles();
if (! isset($titles[$this->uriRequest])) {
$this->_printError('The URI does not correspond to an item in the TOC file');
return false;
}
$blog = new stdClass;
$blog->filename = $this->uriRequest;
foreach ($titles[$this->uriRequest] as $key => $val) {
$blog->$key = $val;
}
$blog->body = file_get_contents($this->_getRequestPath());
return $blog;
}
// --------------------------------------------------------------------
// Returns an array with all the info in the TOC file
function getTitles() {
$i = 1;
foreach ($this->_getDecodedTitles() as $key => $val) {
$row = new stdClass;
$row->filename = $key;
foreach ($val as $k => $v) {
$row->$k = $v;
}
$result[$i] = $row;
$i++;
}
return $result;
}
// --------------------------------------------------------------------
// Load the TOC Jason file and decode it
private function _getDecodedTitles() {
$titles = json_decode(file_get_contents($this->_getTocPath()), true);
if (json_last_error() !== JSON_ERROR_NONE) {
$this->_printError('JSON ERROR: '.json_last_error_msg());
return array();
}
return $titles;
}
// --------------------------------------------------------------------
// Returns the full path to the TOC file
private function _getTocPath() {
return $this->_getOption('baseDir') . $this->_getOption('tocFile');
}
// --------------------------------------------------------------------
// Returns the full path to the requestted blog post
private function _getRequestPath() {
return $this->_getOption('baseDir'). $this->uriRequest . $this->_getOption('fileExt');
}
// --------------------------------------------------------------------
// Validates all the options passed to the constructor
private function _validateOptions() {
if (! is_bool($this->_getOption('showErrors'))) {
$this->_setOption('showErrors', true);
$this->_printError('The showErrors option must be a boolean value (true/false)');
return false;
}
// Validate the baseDir
$path = $this->_getOption('baseDir');
// Resolve the absolute path if possible
if (realpath($path) !== FALSE)
{
$path = realpath($path).'/';
}
// Add trailing slash if missing
$path = rtrim($path, '/').'/';
// Does the directory exist?
if (! is_dir($path)) {
$this->_printError('The specified path is not valid: ' . $this->_getOption('baseDir'));
return false;
}
$this->_setOption('baseDir', $path);
// Validate table of contents file
if (! file_exists($this->_getTocPath())) {
$this->_printError('The table of contents file does not exits: ' . $this->_getTocPath());
return false;
}
// Validate file extension
$ext = $this->_getOption('fileExt');
if (substr($ext, 0, 1) != '.') {
$ext = '.'.$ext;
$this->_setOption('fileExt', $ext);
}
return true;
}
// --------------------------------------------------------------------
// Fetches the URI segment
private function _getUriRequest(){
if (! isset($_SERVER['PATH_INFO'])) {
return;
}
$uri = $_SERVER['PATH_INFO'];
if ($uri == '/') {
return;
}
if (strpos($uri, '/') !== false) {
$uri = explode('/', $uri)[1];
}
$this->uriRequest = $uri;
}
// --------------------------------------------------------------------
// Helper function to retrieve an option from the $options array
private function _getOption($which) {
if (! isset($this->options[$which])) {
$this->_printError('Invalid option: ' . $which);
return false;
}
return $this->options[$which];
}
// --------------------------------------------------------------------
// Sets an option in the $options array
private function _setOption($which, $value) {
$this->options[$which] = $value;
}
// --------------------------------------------------------------------
// Echos an error if showErrors is set to true
private function _printError($str) {
if ($this->options['showErrors'] === true) {
echo $str;
}
}
// --------------------------------------------------------------------
// Set a benchmark marker
function mark($name)
{
$this->marker[$name] = microtime();
}
// --------------------------------------------------------------------
// Calculates the time difference between two marked points.
function elapsedTime($point1 = 'start', $point2 = 'end', $decimals = 4)
{
if ( ! isset($this->marker[$point1]))
{
return '';
}
if ( ! isset($this->marker[$point2]))
{
$this->marker[$point2] = microtime();
}
list($sm, $ss) = explode(' ', $this->marker[$point1]);
list($em, $es) = explode(' ', $this->marker[$point2]);
return number_format(($em + $es) - ($sm + $ss), $decimals);
}
}