-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path.mdhandler.php
77 lines (61 loc) · 2.29 KB
/
.mdhandler.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
<?php
/**
* Zookeeper Online
*
* @author Jim Mason <jmason@ibinx.com>
* @copyright Copyright (C) 1997-2023 Jim Mason <jmason@ibinx.com>
* @link https://zookeeper.ibinx.com/
* @license GPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* version 3, along with this program. If not, see
* http://www.gnu.org/licenses/
*
*/
require_once __DIR__."/vendor/autoload.php";
use Erusev\Parsedown\Parsedown;
use ZK\Engine\Engine;
$stylesheet = "css/zoostyle.css";
$target = realpath(__DIR__.$_GET['asset']);
if(strncmp($target, __DIR__.DIRECTORY_SEPARATOR, strlen(__DIR__)+1) ||
!file_exists($target) || substr($target, -3) != '.md' ) {
http_response_code(404);
return;
}
$mtime = filemtime($target);
header("ETag: \"${mtime}\""); // RFC 2616 requires ETag in 304 response
if(isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
strstr($_SERVER['HTTP_IF_NONE_MATCH'], "\"${mtime}\"") !== false ||
isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $mtime) {
http_response_code(304); // Not Modified
return;
}
header("Content-Type: text/html");
header("Last-Modified: ".gmdate('D, d M Y H:i:s', $mtime)." GMT");
// for HEAD requests, there is nothing more to do
if($_SERVER['REQUEST_METHOD'] == "HEAD")
return;
$depth = substr_count($_GET['asset'], '/', 1);
$stylesheet = str_repeat("../", $depth) . Engine::decorate($stylesheet);
ob_start("ob_gzhandler");
echo "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n";
echo "<link rel=\"stylesheet\" href=\"$stylesheet\">\n";
echo "<title>".basename($_SERVER['REQUEST_URI'])."</title>\n";
echo "</head>\n<body>\n<div class='box'>\n";
ob_start(function($buffer) {
return (new Parsedown())->toHtml($buffer);
});
require_once($target);
ob_end_flush(); // markdown
echo "\n</div>\n</body>\n</html>\n";
ob_end_flush(); // ob_gzhandler