-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
447 lines (362 loc) · 18.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
require_once("./_docs.config.php");
if ($debug)
{
ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
error_reporting(E_ALL);
}
foreach ($phpDependencies as $dependency)
require_once($phpDependencyUrl . $dependency);
ini_set("open_basedir", $contentBaseUri);
if (!isset($parser))
{
if (empty($parserClassName))
throw new RuntimeException("No Parser was found.");
$parser = new $parserClassName();
//Log::debug("Created parser from provided class name.");
}
// Tries to get the contents of the file and parse them using Parsedown.
// Returns the parsed content as a giant string, or null if there was an error.
function getFileContents(string $filePath): ?string
{
global $parser;
try
{
$rawFile = file_get_contents($filePath);
if (empty($rawFile))
return "";
$parsedFile = $parser->text($rawFile);
$parsedFile = preg_replace("<@(post|get|delete|put)=((\/[a-zA-Z0-9_]*)+)>i", "<div class='bar-group'>
<div class='bar-group-addon'>
<strong>$1</strong>
</div>
<div>
$2
</div>
</div>", $parsedFile);
return $parsedFile;
}
catch (\Exception $e)
{
throw new Exception("Failed to fetch and parse requested file content: {$filePath}.");
}
return null;
}
// Scans one single level in $directory
function scanDirectory(string $directory = "."): array|false
{
global $ignoredFiles;
if (file_exists($directory))
return array_diff(scandir($directory), $ignoredFiles);
else
return false;
}
// https://stackoverflow.com/questions/34190464/php-scandir-recursively mateoruda
// Scans all the way into each subdirectory of $dir and so on.
function scanDirectoryRecursive(string $dir): array|false
{
global $ignoredFiles;
if (!file_exists($dir))
return false;
$result = [];
foreach (array_diff(scandir($dir), $ignoredFiles) as $filename)
{
if ($filename[0] === ".")
continue;
$filePath = $dir . "/" . $filename;
if (is_dir($filePath))
{
$result[] = $filePath;
foreach (scanDirectoryRecursive($filePath) as $childFilename)
$result[] = $filename . "/" . $childFilename;
}
else
$result[] = $filename;
}
return $result;
}
// htaccess rules sends page variable
// this should ALWAYS be set, so maybe check later and don't have all this in a big block. guard clause style.
if (isset($_GET["page"]))
{
$requestUri = $_GET["page"];
//Log::debug("------ Requested page: " . ($requestUri ?: "it's empty"));
// webserver/public_html/wiki-content/api/calendar/event
// will be replaced with the real local file uri when it is found
$localFileUri = rtrim("$contentBaseUri/$requestUri", "/"); // remove / from end of uri
//Log::debug("First localFileUri: {$localFileUri}");
// if the exact file exists, it should probably be a directory
// we don't want people using .md in the url, so if a filename
// is requested with it, we treat it as invalid.
if (file_exists($localFileUri))
{
//Log::debug("Exact file exists.");
// if the file is a directory, check for an index
if (is_dir($localFileUri))
{
//Log::debug("Exact file is a directory.");
if (file_exists("{$localFileUri}/index.md"))
{
$localFileUri = "{$localFileUri}/index.md";
$localDirectoryUri = pathinfo($localFileUri, PATHINFO_DIRNAME);
//Log::debug("Exact file (which is a directory) contains index.md, choosing.");
}
else // localFileUri exists but is a directory and does not contain an index
{
$localDirectoryUri = $localFileUri;
$localFileUri = null;
//Log::debug("Exact file (which is a directory) does not contain an index, and therefore does not exist.");
}
}
else
{
//Log::debug("Requested file {$localFileUri} exists and is NOT a directory. Ignoring request for file + .md");
$localDirectoryUri = $requestUri;
$localFileUri = null;
}
}
else if (file_exists("{$localFileUri}.md"))
{
//Log::debug("File + .md exists, it is a regular article.");
// just extra sanity checking to make sure the .md is not a directory, you never know
if (!is_dir("{$localFileUri}.md"))
{
//Log::debug("File + .md is not a directory, choosing.");
// set directory to use the current file uri, then add .md to the file uri
$localFileUri = "{$localFileUri}.md";
$localDirectoryUri = pathinfo($localFileUri, PATHINFO_DIRNAME);
}
else
{
//Log::error("Requested file {$localFileUri}.md exists and IS a directory. This is not allowed.");
$localDirectoryUri = $requestUri;
$localFileUri = null;
}
}
// exact file and file.md don't exist
else
{
//Log::debug("Neither exact file nor file + .md existed.");
$localDirectoryUri = $requestUri;
$localFileUri = null;
}
$localDirectoryUri = rtrim($localDirectoryUri, "/");
//Log::debug("Chosen localFileUri: {$localFileUri}");
//Log::debug("Chosen localDirectoryUri: {$localDirectoryUri}");
// API/Object/Hook -> API, Object, Hook
$directoryTree = explode("/", $requestUri);
$currentPageHref = $baseUri;
$currentPageTitle = "Home";
$previousPageHref = "";
$previousPageTitle = "";
$navbarLinks = "<li><a href='{$baseUri}' class='nav-link fas fa-home' aria-label='Home'><span class='sr-only'>Home</span></a></li>" . PHP_EOL;
// adds an LI for each navbar page
for ($i = 0; $i < count($directoryTree); $i++)
{
if (empty($directoryTree[$i]))
continue;
$previousPageHref = $currentPageHref;
$previousPageTitle = $currentPageTitle;
$currentPageHref .= "/{$directoryTree[$i]}";
$currentPageTitle = htmlspecialchars(ucwords(str_replace("_", " ", $directoryTree[$i])));
$navbarLinks .= "<li><a class='nav-link' href='{$currentPageHref}'>{$currentPageTitle}</a></li>" . PHP_EOL;
}
// creates the sidebar
// names of all files in this filepath
// names of all directories, plus the files & directories in THOSE directories, that are in this filepath
function buildSidebarLinks(string $filepath): string
{
global $baseUri;
global $contentBaseUri;
global $ignoreHiddenFiles;
//Log::debug("Scanning {$filepath} for files and directories (this, plus 1 layer).");
$containers = "";
$pageLinks = "";
// webUriBase is required for links to the items, not to access the item.
$webUriBase = $baseUri;
// by removing any portion from before the content directory
// we can create a web url using the filepath from inside the content
// directory. so it will work regardless of where the content is
// e.g. it can be outside of public_html.
if (str_starts_with($filepath, $contentBaseUri))
$webUriBase .= str_replace($contentBaseUri, "", $filepath);
else if (str_starts_with($filepath, $contentBaseUri))
$webUriBase .= str_replace($contentBaseUri, "", $filepath);
//Log::debug("webUriBase: {$webUriBase}");
if ($files = scanDirectory($filepath))
{
//Log::debug("Found " . count($files) . " items.");
foreach ($files as $file)
{
// TODO: I don't think is is necessary because . is usually in the $ignoredFiles list.
if ($ignoreHiddenFiles and
str_starts_with($file, ".")
)
{
//Log::debug("Ignoring hidden file.");
continue;
}
$fileInfo = pathinfo("{$filepath}/{$file}");
$fname = htmlentities(ucwords(str_replace("_", " ", $fileInfo["filename"])));
$fhref = "{$webUriBase}/{$fileInfo["filename"]}";
if (is_dir("{$filepath}/{$file}"))
{
//Log::debug("Found additional directory: {$filepath}/{$file}.");
if ($files_ = scanDirectory("{$filepath}/{$file}"))
{
//Log::debug("Found additional " . count($files_) . " in {$filepath}/{$file}.");
// TODO: if filepath is empty, display it as a regular page
$containers .= "<div><h1><a href='{$fhref}' class='nav-link'>{$fname}</a></h1><ul>";
// cheap and easy "sorting" by type, directories on top, links below
$directories = "";
$links = "";
foreach ($files_ as $file_)
{
if ($ignoreHiddenFiles and
str_starts_with($file_, ".")
)
{
//Log::debug("Ignoring hidden file.");
continue;
}
$fileInfo_ = pathinfo("{$filepath}/{$file}/{$file_}");
$fname_ = htmlentities(ucwords(str_replace("_", " ", $fileInfo_["filename"])));
$fhref_ = "{$webUriBase}/{$file}/{$fileInfo_["filename"]}";
if (is_dir("{$filepath}/{$file}/{$file_}"))
$directories .= "<li><a href='{$fhref_}' class='nav-link'><i class='fas fa-folder-open'></i> {$fname_}</a></li>";
else
$links .= "<li><a href='{$fhref_}' class='nav-link'>{$fname_}</a></li>";
}
$containers .= $directories;
$containers .= $links;
$containers .= "</ul></div>";
}
else
{
// TODO: this is where directories that have no children (index.md is not included as a child) are placed.
// it needs to look prettier.
$containers .= "<div><h1><a href='{$fhref}' class='nav-link'>{$fname}</a></h1></div>";
//Log::debug("Directory {$filepath}/{$file} contains no children.");
}
}
else
$pageLinks .= "<li><a href='{$fhref}' class='nav-link'>{$fname}</a></li>" . PHP_EOL;
}
}
if (!empty($pageLinks))
$containers .= "<div class='lonely-links'><h1><i>Current Page</i></h1><ul class='lonely-links'>{$pageLinks}</ul></div>";
/* this didn't make it in because there's no text-muted class for docs.
else
$containers .= "<span class='text-muted'>It's so lonely here...</span>";
*/
return $containers;
}
$links = buildSidebarLinks($localDirectoryUri);
if (is_null($localFileUri))
$file = "<div class='file-empty'><i class='fas fa-clock-rotate-left'></i><p>Future home of something cool.<p><small>This document hasn't been written yet.</small></div>";
else
$file = getFileContents($localFileUri);
if (isset($_GET["ajax"]))
{
exit(json_encode([ "status" => 200,
"title" => !empty($currentPageTitle) ? "{$currentPageTitle} • Kerris Haus Docs" : "Kerris Haus Docs",
"currentPageHref" => $currentPageHref,
"navbar" => $navbarLinks,
"webPreviousDirectory" => $previousPageHref,
"webPreviousDirectoryName" => $previousPageTitle,
"links" => $links,
"content" => $file
]));
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="author" content="Kerris Haus">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="content-language" content="en">
<meta name="theme-color" content="#14161D">
<link rel="stylesheet" type="text/css" href="https://kerrishaus.com/assets/fontawesome/6.1.1/css/all.min.css" />
<link rel="stylesheet" type="text/css" href="https://docs.kerrishaus.com/assets/styles.css" />
<link rel="stylesheet" type="text/css" href="https://docs.kerrishaus.com/assets/person.css" />
<link rel="stylesheet" type="text/css" href="https://docs.kerrishaus.com/assets/bar-group.css" />
<link rel="stylesheet" type="text/css" href="https://docs.kerrishaus.com/assets/table.css" />
<link rel="stylesheet" type="text/css" href="https://docs.kerrishaus.com/assets/markdown.css" />
<link rel="stylesheet" type="text/css" href="https://docs.kerrishaus.com/assets/table_of_contents.css" />
<link rel="stylesheet" type="text/css" href="https://docs.kerrishaus.com/assets/code.css" />
<link rel="stylesheet" type="text/css" href="https://kerrishaus.com/assets/styles/footer.css" />
<noscript>
<link rel="stylesheet" type="text/css" href="https://docs.kerrishaus.com/assets/noscript.css" />
</noscript>
<script src="https://kerrishaus.com/assets/scripts/jquery-3.7.0.min.js"></script>
<script src="https://docs.kerrishaus.com/assets/wiki.js"></script>
<?php foreach ($cssIncludes as $cssInclude): ?>
<link rel="stylesheet" type="text/css" href="<?= $cssInclude ?>" />
<?php endforeach; ?>
<?php foreach ($javascriptIncludes as $jsInclude): ?>
<script src="<?= $jsInclude ?>"></script>
<?php endforeach; ?>
<?php if (!empty($currentPageTitle)): ?>
<title>
<?= htmlentities($currentPageTitle) ?> - Kerris Haus Docs
</title>
<?php else: ?>
<title>Kerris Haus Docs</title>
<?php endif; ?>
</head>
<body>
<div class="wiki-container">
<div class="sidebar" id="sidebar">
<div class="navbar-brand">
<a href="https://kerrishaus.com/" class="navbar-brand">
<center>
<img src="https://kerrishaus.com/assets/logo/text-small.png" alt="Kerris Haus">
</center>
</a>
<div id="sidebar-close-button">
<i class="fas fa-times" aria-label="Close sidebar"></i>
</div>
</div>
<br/>
<div class="searchbar">
<form class="form">
<input class="form-control" type="text" placeholder="press / to quick search" id="searchbar" autocomplete="off" />
</form>
</div>
<?php if (!empty($previousPageHref)): ?>
<a href="<?= $previousPageHref; ?>" id="webPreviousDirectory" class="nav-link"><i class="fas fa-arrow-left" aria-hidden="true"></i> <?= $previousPageTitle; ?></a>
<?php endif; ?>
<div class="links">
<!--
<div>
<h1>API</h1>
<ul>
<li><a href="https://docs.kerrishaus.com/API/portal" class="nav-link">Portal</a></li>
<li><a href="https://docs.kerrishaus.com/API/games" class="nav-link">Games</a></li>
</ul>
</div>
-->
<?= $links ?>
</div>
</div>
<main class="wiki-content-container">
<header>
<div id="sidebar-toggle-button">
<i class="fas fa-bars" aria-label="Toggle sidebar"></i>
</div>
<nav>
<ul id="navbar">
<?= $navbarLinks ?>
</ul>
</nav>
</header>
<div class="wiki-content">
<?= $file ?>
</div>
</main>
</div>
<?= file_get_contents("https://kerrishaus.com/inc/templates/_footer.php"); ?>
</body>
</html>