-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathheadless-cms.php
More file actions
317 lines (206 loc) · 8.54 KB
/
headless-cms.php
File metadata and controls
317 lines (206 loc) · 8.54 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
* Called to process every request to the server
*/
function handle_request() {
$page = get_page();
// If request comes from the client side router, then the whole page does not need to be sent
if(isset($_GET["csr"]) && $_GET["csr"] == 'true') {
header('Content-type: application/json');
echo json_encode($page);
exit;
}
return $page;
}
function get_page() {
// Gets the path of the webpage file on the local system
$requested_path = get_requested_path();
// Ensure there is a trailing slash
if(!substr($requested_path, -1) !== '/') {
$requested_path = $requested_path . '/';
}
// Absolute path to root of webpage file
$dir_path = __DIR__ . '/webpages' . $requested_path;
if(!does_page_exist($dir_path)) {
return handle_error(404);
}
$page = get_parsed_page($dir_path);
if($page == false) {
return handle_error(500);
}
return $page;
}
function get_parsed_page($dir_path) {
try {
// If there is a template.php file, run it to get the webpage content
if(is_file(($dir_path . 'template.php'))) {
// Attempt to get a cached file if it exists
$cached_page = get_cached_page($dir_path);
// If cache miss, generate the page
if($cached_page === false) {
ob_start();
// Run the script
include $dir_path . 'template.php';
// Get the output and store it as a string
$template_content = ob_get_clean();
$page = new Page($dir_path, $template_content);
if(isset($page->settings["cache-for"]) && is_numeric($page->settings["cache-for"])) {
save_cached_page($dir_path, $template_content, time() + intval($page->settings["cache-for"]));
}
return new Page($dir_path, $template_content);
} else {
return $cached_page;
}
} else {
// Get the page.html content
return new Page($dir_path, file_get_contents($dir_path . 'page.html'));
}
} catch (\Throwable $th) {
return false;
}
}
function does_page_exist($dir_path) {
if(is_file(($dir_path . 'page.html')) || is_file($dir_path . 'template.php')) {
return true;
}
return false;
}
function get_requested_path() {
$requested_file_name = parse_url($_SERVER['REQUEST_URI'])["path"];
return $requested_file_name;
}
function handle_error($error_code) {
// Set the correct HTTP response code
http_response_code($error_code);
$error_dir_path = __DIR__ . '/errors' . '/' . $error_code . '/' ;
// Is there an error page
if(does_page_exist($error_dir_path)) {
$page = get_parsed_page($error_dir_path);
// Then something went wrong with reading the file
if($page == false) exit;
return $page;
}
return new Page($error_dir_path, "<p style='text-align:center;>'Error {$error_code}</p>");
}
class Page {
public $content;
public $settings;
private $dir_path;
function __construct($dir_path, $raw_page_content) {
$this->dir_path = $dir_path;
list($hasSettings, $page_parts) = $this->parse_page_content($raw_page_content);
if($hasSettings && count($page_parts) == 1) {
// If the page has only settings and no content
$this->content = '';
$this->settings = $this->parse_raw_settings_block($page_parts[0]);
} elseif(!$hasSettings && count($page_parts) === 1) {
// If the page has no settings and just content
$this->content = $page_parts[0];
$this->settings = null;
} else {
// If the page has both settings and content
$this->content = $page_parts[1];
$this->settings = $this->parse_raw_settings_block($page_parts[0]);
}
// Path to possible styles.css file
$styles_path = $this->dir_path . 'styles.css';
// If there is a styles.css file, then include the styles in the page
if(is_file($styles_path)) {
$this->content = "<style>\n\n" . file_get_contents($styles_path) . "\n</style>\n\n" . $this->content;
}
}
private function parse_page_content($page_content) {
// Does the page have page settings
$hasSettings = preg_match('/^.[=]+([\s]+)?$/m', $page_content) > 0;
// Split the file on a line of equals characters (The separator between setting and the page content)
// From now on, this line will be referred to as a 'splitter' line.
$split = preg_split('/^.[=]+([\s]+)?$/m', $page_content);
return array($hasSettings, $split);
}
private function parse_raw_settings_block($raw_settings_block) {
$raw_settings_block = preg_replace("/<!--(.*?)-->/", "", $raw_settings_block);
$temp_settings = array();
// Iterate over each new line
foreach(preg_split("/((\r?\n)|(\r\n?))/", $raw_settings_block) as $line) {
// Split the line on the first ':' character
$parts = explode(':', $line);
if(count($parts) == 1) {
// Then set as key-only setting
$keyName = strtolower(trim($parts[0]));
// Then key name is invalid
if($keyName === '') continue;
$temp_settings[$keyName] = true;
continue;
}
if(count($parts) !== 2) {
// Then is malformed settings line
continue;
}
// Extract the name (key) of the setting and its respective value
$key = strtolower(trim($parts[0]));
$value = trim($parts[1]);
$temp_settings[$key] = $value;
}
return $temp_settings;
}
function get_property($property_name) {
// Check this setting exists
if(isset($this->settings[$property_name])) {
switch ($property_name) {
case 'title':
return "<title>{$this->settings[$property_name]}</title><meta property='og:title' content='{$this->settings[$property_name]}' />";
case 'description':
return "<meta name='description' content='{$this->settings[$property_name]}'><meta name='og:description' content='{$this->settings[$property_name]}'>";
case 'og-image':
return "<meta property='og:image' content='{$this->settings[$property_name]}' />";
case 'og-url':
return "<meta property='og:url' content='{$this->settings[$property_name]}' />";
case 'og-type':
return "<meta property='og:type' content='{$this->settings[$property_name]}' />";
case 'favicon':
return "<link rel='shortcut icon' type='image' href='{$this->settings[$property_name]}' />";
}
return $this->settings[$property_name];
}
// Page setting is NOT set
switch ($property_name) {
case 'favicon':
return "<link rel='shortcut icon' type='image' href='/resources/favicon.png' />";
}
return '';
}
}
/**
* Returns the contents of an in-date cached template.php page if it exists
*/
function get_cached_page($dir_path) {
$GET_hash = hash("sha256", $_SERVER["QUERY_STRING"]);
$file_path = $dir_path . "template.cached." . $GET_hash;
if(is_file($file_path)) {
$cache_content = file_get_contents($file_path);
// Check the TTL against the generated time. There will always be a settings block as pages are only cached
// if the cache-for setting is set
$page = new Page($dir_path, $cache_content);
// Test if current time is before expiry
if(time() < intval($page->settings["cache-expires"])) {
return $page;
} else {
// Delete invalid cache file
unlink($file_path);
return false;
}
}
// If file does not exist
return false;
}
/**
* Saves the executed template page
*/
function save_cached_page($dir_path, $page_content, $cache_invalid_at) {
$GET_hash = hash("sha256", $_SERVER["QUERY_STRING"]);
$file_path = $dir_path . "template.cached." . $GET_hash;
$page_content = "generated: " . time() . "\n" . "cache-expires: " . $cache_invalid_at . "\n\n" . $page_content;
// Save the cached file
file_put_contents($file_path, $page_content);
}
?>