forked from wesl-ee/BBDiary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BBDiary.php
executable file
·364 lines (342 loc) · 9.28 KB
/
BBDiary.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
<?php
/* BBDiary: A girl's diary on one page
* Prettyboy-yumi */
/* CONFIG_BBDIARY_HOMEPAGE
* A URL to this diary */
define("CONFIG_BBDIARY_HOMEPAGE", "https://sweetsbeats.herokuapp.com/");
/* CONFIG_DIARY_FSPATH
* The directory you will be serving */
define("CONFIG_DIARY_FSPATH", "/app/posts/");
/* CONFIG_APACHE2_XSENDFILE
* Enable this if you are running Apache with XSendfile */
define("CONFIG_APACHE2_XSENDFILE", false);
/* CONFIG_NGINX_XSENDFILE
* Enable this if you are running nginx with XSendfile */
define("CONFIG_NGINX_XSENDFILE", false);
/* CONFIG_LIGHTTPD_XSENDFILE
* Enable this if you are running lighttpd with XSendfile */
define("CONFIG_LIGHTTPD_XSENDFILE", false);
/* CONFIG_CLEAN_URL
* Enable only if you have followed the Clean URL steps laid
* out in the README */
define("CONFIG_CLEAN_URL", false);
/*******************************************************
* You probably don't need to touch anything below here *
* unless you are contributing *
* of course ~Prettyboy-yumi *
*******************************************************/
// Current working directory
$relpath = urldecode($_GET['path']) ?: '/';
$abspath = realpath(CONFIG_DIARY_FSPATH . $relpath);
if (is_dir($abspath)) $abspath .= '/';
// Error handling
if (!$abspath) {
$errresponse = "$_SERVER[SERVER_PROTOCOL] 404 Not Found";
header($errresponse);
}
else if (strpos($abspath, CONFIG_DIARY_FSPATH) === false) {
$errresponse = "$_SERVER[SERVER_PROTOCOL] 403 Forbidden";
header($errresponse);
}
else if (!is_readable($abspath)) {
$errresponse = "$_SERVER[SERVER_PROTOCOL] 403 Forbidden";
header($errresponse);
}
else if (is_file($abspath)) {
$mtype = mime_content_type($abspath);
if (explode('/', $mtype)[0] != 'text') {
$fsize = filesize($abspath);
header("Content-Type: $mtype");
header("Content-Length: $fsize");
if (CONFIG_LIGHTTPD_XSENDFILE)
header("X-LIGHTTPD-send-file: $abspath");
else if (CONFIG_APACHE2_XSENDFILE)
header("X-Sendfile: $abspath");
else if (CONFIG_NGINX_XSENDFILE)
header("X-Accel-Redirect: $abspath");
else
readfile($abspath);
die;
}
} ?>
<!DOCTYPE HTML>
<HTML>
<head>
<title>Dev blog</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- BBDiary Favicon -->
<link href="data:image/ico;base64,AAABAAEAEBAQAAAAAAAoAQAAFgAAACgAAAAQA
AAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAAAAAAAD/ScwAAAAAAP8AAAD///8AAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAERERERERERE
SIiIiIiIiITMjIyMjIyIxEyMjMyMyIjETIyMjIyMiMRMjIyMjIyMjMiMjMyMzIyMSIiIiIi
IiIRAAAAAAAAABEzMzADMzMAETMAMwMwAzARMzMzAzMzMBEzADMDMAMwETMzMAMzMwARAAA
AAAAAABEREREREREREAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" rel=icon type="image/x-icon">
<style>
/* Main Layout */
body { background-color: aliceblue; color: black;
/* Starry background */
background-image: url("background.jpg");
background-repeat: repeat;
background-attachment: fixed;
background-size: 100%;
font-family:"ヒラギノ角ゴ Pro W3",
"Hiragino Kaku Gothic Pro",
Osaka,
"メイリオ",
Meiryo,
"MS Pゴシック",
"MS PGothic",
sans-serif;
}
h1 { font-family: "Palatino Linotype",
"Book Antiqua",
Palatino,
serif;
font-weight: lighter; }
h1 a, h2 a, h3 a { text-decoration: none; }
/* Reader */
main { max-width: 40em;
margin: auto;
padding: 10px;
background: white;
border: 3px double;
border-radius: 5px; }
ol, ul { display: flex;
flex-wrap: wrap;
justify-content: center;
list-style-type: none;
white-space: nowrap;
list-style-type: none;
padding: 0; }
li { margin: 15px; }
li a { padding-left: 5px;
padding-right: 5px;
transition: box-shadow 0.5s, color 1s;
border: 1px solid;
text-decoration: none;
cursor: pointer; }
.hoverbox { transition: box-shadow 0.5s, color 1s; }
.hoverbox:hover { box-shadow: -3px 3px 10px; }
article { text-align: justify; }
header { text-align: center; }
footer { text-align: center; margin: auto; }
/* Link styling */
a, a:visited { outline: none; color: inherit; }
a:hover { text-decoration: none; }
/* Navigation bar */
nav a { border-top: 1px solid;
border-bottom: 1px solid;
border-right: 1px solid;
padding-right: 8px;
padding-left: 8px;
text-decoration: none; }
nav a:first-child { border-left: 1px solid; }
.about { max-width: 20em;
display:flex;
justify-content:center;
align-items: center;
}
.aboutContainer{ padding-left: 5px;
display:flex;
justify-content:center;
align-items:flex-end;}
.aboutIMG { /*padding-right: 8em;*/
border-radius:10px;
border:2px solid;
}
.aboutText {padding-left:8px;
font-size: 19px;
/*text-decoration: none;*/
font-weight: 100}
.aboutTT {padding-left:10px;
font-size: 18px;
/*text-decoration: none;*/
font-weight: 100;
line-height: 2;}
</style>
</head>
<body><main>
<header>
<h1><a href="<?php print CONFIG_BBDIARY_HOMEPAGE?>">Hello! </a>
<div class="about">
<div class="aboutContainer">
<image src="pfp.png" width="128"height="128" class="aboutIMG">
</image>
</div>
<div>
<div class="aboutText"> Hello, I'm sweets; I make video games and write software.
I grew up playing on N64/Gamecube, and I'm heavily inspired by Harvest Moon, Animal Crossing, Zelda, etc.
</div>
<div class="aboutTT"> Things I love: Beer, my cats, cold weather, Linux</div>
</div>
</div>
</h1>
<nav><?php
$chunks = explode('/', $relpath);
if (empty($chunks[count($chunks)-1]))
// Ignore the empty item for directories
// (trailing slash)
array_pop($chunks);
foreach ($chunks as $n => $chunk) {
if (!is_file(CONFIG_DIARY_FSPATH
. urldecode($chunkedpath) . $chunk)
|| (count($chunks) - $n - 1))
$chunk .= '/';
$chunkedpath .= urlencode($chunk);
$chunkedpath = str_replace("%2F", "/", $chunkedpath);
if ($chunkedpath == '/')
$href = CONFIG_BBDIARY_HOMEPAGE;
else if (CONFIG_CLEAN_URL)
$href = $chunkedpath;
else
$href = "?path=$chunkedpath";
print <<<HOVERBOX
<a class=hoverbox href="$href">$chunk</a>
HOVERBOX;
}
?></nav>
</header>
<hr>
<?php
if ($errresponse) {
print <<<ERRRESP
<article>Error: $errresponse</article>
ERRRESP;
}
else if (is_file($abspath)) {
$text = file_get_contents($abspath);
$text = bbbbbbb($text);
$text = nl22br($text);
print <<<RESP
<article>$text</article>
RESP;
} else if (is_dir($abspath)) {
$contents = array_diff(
scandir($abspath), array('.', '..')
);
print <<<UL
<ul>
UL;
foreach ($contents as $item) {
$href = urlencode($relpath) . urlencode($item);
$href = str_replace("%2F", "/", $href);
if (CONFIG_CLEAN_URL)
$href = $href;
else
$href = "?path=$href";
if (is_file($abspath.$item)) {
print <<<FHREF
<li><a class=hoverbox href="$href">$item</a></li>
FHREF;
} else {
print <<<DHREF
<li><a class=hoverbox href="$href/">$item</a></li>
DHREF;
}
}
print <<<EOUL
</ul>
EOUL;
}
?></main>
</body>
</HTML>
<?php
/*
* Like strpos but does not loop over the
* entire string when given an offset
*/
function indexOf($string, $substring, $offset = 0)
{
$stringlen = strlen($string);
$sublen = strlen($substring);
for ($i = $offset, $j = 0; $i < $stringlen; $i++) {
if ($string{$i} == $substring{$j}) {
if (!(++$j - $sublen))
return $i - $sublen + 1;
} else $j = 0;
}
return -1;
}
/*
* Magical BBCode parse
*/
function bbbbbbb($string)
{
$opened = []; $contents = [];
$offset = 0;
while (($a = indexOf($string, "[", $offset)) >= 0
&& ($b = indexOf($string, "]", $offset)) > $a) {
// Push the parsed contents to an array
$contents[] = substr($string, $offset, $a - $offset);
$tag = substr($string, $a, $b + 1 - $a);
$contents[] = $tag;
// Since we finished scanning the part of the string
// that is behind the last seen bracket, advance
// the scanning offset
$offset = $b + 1;
// Strip brackets
$inside = substr($tag, 1, strlen($tag) - 2);
// Is this a closing or opening tag?
if ($inside{0} == "/") {
$tag = substr($inside, 1);
if (!count($opened[$tag]))
continue;
$opening_tag = array_pop($opened[$tag]);
$from = $opening_tag["index"];
$param = $opening_tag["param"];
$to = count($contents) - 1;
switch($tag) {
case 'b':
$open = "<strong>";
$close = "</strong>";
break;
case 'i':
$open = "<em>";
$close = "</em>";
break;
case 'color':
$color = htmlspecialchars($param);
$open = "<span style='color:$color'>";
$close = "</span>";
break;
case 'quote':
$open = "<blockquote>";
$close = "</blockquote>";
break;
case 'url':
$url = htmlspecialchars($param);
$open = "<a href='$url'>";
$close = "</a>";
break;
case 'code':
$open = "<pre>";
$close = "</pre>";
break;
default: continue;
}
$contents[$from] = $open;
$contents[$to] = $close;
} else {
if (($c = indexOf($inside, "=")) > 0) {
$tag = substr($inside, 0, $c);
$param = substr($inside, $c + 1);
} else {
$tag = $inside;
unset($param);
}
$opened[$tag][] = [
"index" => count($contents) - 1,
"param" => $param
];
}
}
$contents[] = substr($string, $offset);
return join($contents);
}
function nl22br($string)
{
return str_replace("\n\n", "<br/><br/>", $string);
}
?>