forked from phpcoinn/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
163 lines (134 loc) · 4.35 KB
/
index.php
File metadata and controls
163 lines (134 loc) · 4.35 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
<?php
// PHPCoin Docs Viewer
set_time_limit(5);
error_reporting(E_ALL);
ini_set('display_errors', 1);
define('PHPCOIN_DOCS_VIEWER_VERSION', '0.0.5');
require_once dirname(__DIR__) . '/apps.inc.php';
require_once './Parsedown.php';
register_shutdown_function('shutdown_handler');
function shutdown_handler() {
// print '<p>DEBUG: shutdown_handler @ ' . time() . '</p>';
$error = error_get_last();
if (! empty($error)) {
print '<pre>' . print_r($error, true) . '</pre>';
}
// print '<hr /><br><br><br>';
}
class ParsedownExt extends Parsedown {
private $docPath;
private $baseDir;
private $realBaseDir;
public function __construct($docPath, $baseDir)
{
$this->docPath = $docPath;
$this->baseDir = $baseDir;
$this->realBaseDir = realpath($this->baseDir);
}
protected function inlineLink($Excerpt)
{
$link = parent::inlineLink($Excerpt);
$href = $link['element']['attributes']['href'];
// Don't rewrite external links, mailto links, or anchors
if (preg_match('/^(https?:\/\/|mailto:|#)/', $href)) {
return $link;
}
// Don't rewrite links to non-markdown files
$allowedExtensions = ['md', 'png', 'pdf'];
$extension = pathinfo($href, PATHINFO_EXTENSION);
if ($extension && !in_array(strtolower($extension), $allowedExtensions)) {
return $link;
}
$currentDocDir = $this->baseDir . ($this->docPath ? $this->docPath . '/' : '');
$file = $currentDocDir . $href;
$realFile = realpath($file);
if ($realFile === false || strpos($realFile, $this->realBaseDir) !== 0) {
// This is an invalid link, pointing outside the docs directory.
// Let's make it a dead link and style it to indicate it's broken.
$link['element']['attributes']['href'] = '#';
if (isset($link['element']['attributes']['class'])) {
$link['element']['attributes']['class'] .= ' broken-link';
} else {
$link['element']['attributes']['class'] = 'broken-link';
}
$link['element']['attributes']['title'] = 'Invalid link (points outside of documentation)';
return $link;
}
if ($realFile == $this->realBaseDir) {
$newDoc = '';
} else {
$newDoc = substr($realFile, strlen($this->realBaseDir) + 1);
if (is_dir($realFile)) {
$newDoc .= '/';
}
}
$link['element']['attributes']['href'] = "/apps/docs/?doc=".$newDoc;
return $link;
}
}
$docsDir = dirname(dirname(dirname(__DIR__)));
$baseDir = $docsDir.'/docs/';
if(!empty($_GET['doc'])) {
$link = $_GET['doc'];
$file = $baseDir . $link;
if (is_dir($file)) {
if (substr($link, -1) !== '/') {
$link .= '/';
}
$file = $baseDir . $link . 'README.md';
}
} else {
$link = '';
$file = $baseDir . 'README.md';
}
// Security: Prevent path traversal
$realFile = realpath($file);
$realBaseDir = realpath($baseDir);
if ($realFile === false || strpos($realFile, $realBaseDir) !== 0) {
define("PAGE", "Docs - Not Found");
define("APP_NAME", "Docs");
http_response_code(404);
require_once __DIR__. '/../common/include/top.php';
echo "<h1>404 Docs Not Found</h1>";
require_once __DIR__ . '/../common/include/bottom.php';
exit;
} else {
$file = $realFile;
}
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if ($extension === 'png') {
header('Content-Type: image/png');
readfile($file);
exit;
} elseif ($extension === 'pdf') {
header('Content-Type: application/pdf');
readfile($file);
exit;
}
$relativePath = str_replace($baseDir, '', $file);
$docPath = dirname($relativePath);
if ($docPath == ".") {
$docPath = "";
}
$pd = new ParsedownExt($docPath, $baseDir);
$pd->setSafeMode(true);
$text = file_get_contents($file);
define("PAGE", "Docs");
define("APP_NAME", "Docs");
define("HEAD_CSS", "/apps/docs/docs.css");
?>
<?php
require_once __DIR__. '/../common/include/top.php';
?>
<?php echo $pd->text($text); ?>
<div class="container-fluid">
<hr>
<div class="row">
<div class="col-sm-6">
PHPCoin Docs Viewer v<?php echo PHPCOIN_DOCS_VIEWER_VERSION ?>
</div>
</div>
</div>
<?php
require_once __DIR__ . '/../common/include/bottom.php';
?>