/
mdsite.php5
217 lines (166 loc) · 5.91 KB
/
mdsite.php5
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
<?php
/************
MDsite - A PHP-based minimalistic website engine to display webpage content based on the markdown syntax, with automatic menu generation from the file tree.
Copyright (C) 2015 - Alessandro Pellegrini <alessandro@pellegrini.tk>
In brief:
- files starting with "_" are ignored
- Any file named like "10_Name_of_file" is rendered as "Name of file", so numbers at the beginning can be used for ordering the menu
- Explicit files can be excluded (on a global basis, no control on files in single folders, except for the '_' trick) using the $ignored_files array below
Note that (for simplicity) either "index.php" and/or "index.md" should be in the in the ignored files array, as this is the file which is processed by default
when pointing to a directory in the tree.
****************** DO NOT EDIT BELOW THIS LINE *********************/
require_once("config.php5");
require_once("Parsedown.php5");
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
mb_language('uni');
mb_regex_encoding('UTF-8');
ob_start('mb_output_handler');
function get_menu_level($page) {
global $default_content, $main_folder;
$page = str_replace($main_folder, "", $page);
return substr_count($page, '/');
}
function get_actual_content($page) {
global $main_folder, $error_404_content;
if(file_exists($page . ".md")) {
return $page . ".md";
} else if(file_exists($page . ".php")) {
return $page . ".php";
} else if(is_dir($page)) {
return get_actual_content($page . "/index");
}
return $main_folder . '/' . $error_404_content;
}
function apply_menu_icon($entry) {
global $menu_icons, $default_menu_icon;
$icon = isset($menu_icons[$entry]) ? $menu_icons[$entry] : $default_menu_icon;
return "<i class=\"$icon\"></i> " . $entry;
}
function sanitize_entry($entry) {
// Remove extension
$entry = pathinfo($entry, PATHINFO_FILENAME);
// Remove initial number for ordering entries
if(is_numeric(substr($entry, 0, 1))) {
$entry = substr($entry, strpos($entry, "_") + 1);
}
// Replace _ with spaces
$entry = str_replace("_", " ", $entry);
return $entry;
}
function should_hide($directory, $page, $call_level) {
global $default_content, $main_folder;
$directory = str_replace($main_folder, "", $directory );
$ret = false;
if($call_level == 0)
return false;
if($call_level > get_menu_level($page) + 1)
$ret = true;
$path_parts = explode('/', $directory);
foreach($path_parts as $part) {
if($part != "")
if(!strstr($page, $part))
$ret = true;
}
return $ret;
}
function php_file_tree($directory, $return_link, $whole, $extensions = array(), $ignored_files = array()) {
// Generates a valid XHTML list of all directories, sub-directories, and files in $directory
// Remove trailing slash
if( substr($directory, -1) == "/" )
$directory = substr($directory, 0, strlen($directory) - 1);
$code = php_file_tree_dir($directory, $return_link, $whole, $extensions, $ignored_files);
return $code;
}
function php_file_tree_dir($directory, $return_link, $whole, $extensions = array(), $ignored_files = array(), $call_level = 0) {
global $main_folder, $default_content;
$php_file_tree = '';
$page = (isset($_GET['p']) ? $_GET['p'] : $default_content);
// Show only entries related to current path (one level more)
if( $whole == false && should_hide($directory, $page, $call_level) )
return "";
// Get and sort directories/files
if( function_exists("scandir") )
$file = scandir($directory);
else
$file = php4_scandir($directory);
natcasesort($file);
// Purge unwanted / unneeded elements from the scanned array
foreach( array_keys($file) as $key ) {
// Filter unwanted extensions
if( !empty($extensions) ) {
if( !is_dir("$directory/$file[$key]") ) {
$ext = substr($file[$key], strrpos($file[$key], ".") + 1);
if( !in_array($ext, $extensions) ) {
unset($file[$key]);
continue;
}
}
}
// Filter unwanted files (array)
if( !empty($ignored_files) ) {
if(in_array($file[$key], $ignored_files)) {
unset($file[$key]);
continue;
}
}
// Filter unwanted files (filename)
if(substr($file[$key], 0, 1) == "_") {
unset($file[$key]);
continue;
}
}
if(count($file) > 0) {
$php_file_tree = "<ul>";
foreach( $file as $this_file ) {
if( $this_file[0] != ".") {
$link = str_replace("[link]", "$directory/" . urlencode( pathinfo($this_file, PATHINFO_FILENAME) ), $return_link);
$link = str_replace($main_folder, "", $link);
$menu_name = apply_menu_icon(htmlspecialchars(sanitize_entry($this_file)));
if( is_dir("$directory/$this_file") ) {
// Directory
$php_file_tree .= "<li><a href=\"$link\">" . $menu_name . "</a>";
$php_file_tree .= php_file_tree_dir("$directory/$this_file", $return_link, $whole, $extensions, $ignored_files, $call_level+1);
$php_file_tree .= "</li>";
} else {
// File
$php_file_tree .= "<li><a href=\"$link\">" . $menu_name . "</a></li>";
}
}
}
$php_file_tree .= "</ul>";
}
return $php_file_tree;
}
// For PHP4 compatibility
function php4_scandir($dir) {
$dh = opendir($dir);
while( false !== ($filename = readdir($dh)) ) {
$files[] = $filename;
}
sort($files);
return($files);
}
function menu($whole = false) {
global $main_folder, $return_link, $allowed_files, $ignored_files;
echo php_file_tree($main_folder, $return_link, $whole, $allowed_files, $ignored_files);
}
function content() {
global $error_404_content, $main_folder, $default_content;
$page = (isset($_GET['p']) ? $_GET['p'] : $default_content);
$page = get_actual_content($main_folder.'/'.$page);
$ext = substr($page, strrpos($page, '.', -1), strlen($page));
if($ext == ".php") {
include($page);
} else if($ext == ".md") {
echo Parsedown::instance()->text(file_get_contents(htmlspecialchars($page)));
}
}
function page_name() {
global $default_content;
mb_http_output("UTF-8");
$page = (isset($_GET['p']) ? $_GET['p'] : $default_content);
echo sanitize_entry(end(explode('/', $page)));
}
?>