forked from PVNFU-28/picochan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpico
executable file
·1158 lines (982 loc) · 47.7 KB
/
pico
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/local/bin/haserl -u16384
<% -- Picochan CGI/HTML Frontend
-- HAPAS ARE SUPERIOR TO WHITES
local cgi = require("picoaux.cgi");
local pico = require("picoengine");
local html = {};
html.table = {};
html.list = {};
html.container = {};
html.form = {};
--
-- INITIALIZATION
--
local sitename = pico.global.get("sitename");
pico.account.register_login(COOKIE["session_key"]);
cgi.initialize();
local function printf(...)
cgi.outputbuf[#cgi.outputbuf + 1] = string.format(...);
end
local function thumbsize(w, h, mw, mh)
return math.min(w, mw, math.floor(w / h * mh + 0.5)), math.min(h, mh, math.floor(h / w * mw + 0.5));
end
--
-- HTML FUNCTIONS
--
function html.begin(...)
local title = string.format(...);
title = title and (title .. " - ") or "";
printf("<!DOCTYPE html>\n");
printf("<html>");
printf( "<head>");
printf( "<title>%s%s</title>", title, sitename);
printf( "<link rel='stylesheet' type='text/css' href='/static/picochan.css' />");
printf( "<link rel='shortcut icon' type='image/png' href='/static/favicon.png' />");
printf( "<meta charset='utf-8' />");
printf( "<meta name='viewport' content='width=device-width, initial-scale=1.0' />");
printf( "</head>");
printf( "<body>");
printf( "<nav id='topbar'><ul>");
printf( "<li class='system'><a href='/pico/' accesskey='`'>main</a></li>");
printf( "<li class='system'><a href='/pico/System/mod' accesskey='1'>mod</a></li>");
printf( "<li class='system'><a href='/pico/System/log' accesskey='2'>log</a></li>");
printf( "<li class='system'><a href='/pico/System/stats' accesskey='3'>stats</a></li>");
printf( "<li class='system'><a href='/pico/Recent' accesskey='4'>recent</a></li>");
printf( "<li class='system'><a href='/pico/Overboard' accesskey='5'>overboard</a></li>");
local boards = pico.board.list();
for i = 1, #boards do
printf("<li class='board'><a href='/pico/%s' title='%s'>/%s/</a></li>",
boards[i]["Name"], boards[i]["Title"], boards[i]["Name"]);
end
if pico.account.current then
printf("<span id='logged-in-notification'>Logged in as <b>%s</b> <a href='/pico/System/mod/logout'>[Logout]</a></span>", pico.account.current["Name"]);
end
printf( "</ul>");
printf( "<a class='invisible' href='' accesskey='r'></a>");
printf( "<a class='invisible' href='#postform' accesskey='p'></a>");
printf( "</nav>");
end
function html.finish()
printf("<!-- %d ms generation time -->", os.clock() * 1000);
printf("</body></html>");
end
function html.error(title, ...)
cgi.outputbuf = {};
html.begin("error");
html.redheader(title);
html.container.begin();
printf(...);
html.container.finish();
html.finish();
cgi.finalize();
end
function html.redheader(...)
printf("<h1 class='redheader'>%s</h1>", string.format(...));
end
function html.announce()
printf("<div id='announce'>%s</div>", pico.global.get("announce"));
end
function html.container.begin(width)
printf("<div class='container %s'>", width or "narrow");
end
function html.container.finish()
printf("</div>");
end
function html.container.barheader(...)
printf("<h2 class='barheader'>%s</h2>", string.format(...));
end
function html.list.begin(class)
printf("<".."%sl>", (class == "ordered") and "o" or "u");
end
function html.list.finish(class)
printf("</%sl>", (class == "ordered") and "o" or "u");
end
function html.list.entry(...)
printf("<li>%s</li>", string.format(...));
end
function html.table.begin(...)
printf("<table><tr>");
for i = 1, select("#", ...) do
printf("<th>%s</th>", select(i, ...));
end
printf("</tr>");
end
function html.table.entry(...)
printf("<tr>");
for i = 1, select("#", ...) do
printf("<td>%s</td>", select(i, ...));
end
printf("</tr>");
end
function html.table.finish()
printf("</table>");
end
function html.date(timestamp, reldisplay)
local difftime = os.time() - timestamp;
local unit, multiple;
local decimal = false;
local reltime;
if difftime >= (60 * 60 * 24 * 365) then
unit = "year";
multiple = difftime / (60 * 60 * 24 * 365);
decimal = true;
elseif difftime >= (60 * 60 * 24 * 30) then
unit = "month";
multiple = difftime / (60 * 60 * 24 * 30);
decimal = true;
elseif difftime >= (60 * 60 * 24 * 7) then
unit = "week";
multiple = difftime / (60 * 60 * 24 * 7);
elseif difftime >= (60 * 60 * 24) then
unit = "day";
multiple = difftime / (60 * 60 * 24);
elseif difftime >= (60 * 60) then
unit = "hour";
multiple = difftime / (60 * 60);
elseif difftime >= (60) then
unit = "minute";
multiple = difftime / (60);
else
unit = "second";
multiple = difftime;
end
if decimal then
reltime = string.format("%.1f %s%s ago", multiple, unit, multiple == 1 and "" or "s");
else
multiple = math.floor(multiple);
reltime = string.format("%d %s%s ago", multiple, unit, multiple == 1 and "" or "s");
end
return string.format("<time datetime='%s' title='%s'>%s</time>", os.date("!%F %T", timestamp),
reldisplay and os.date("!%F %T %Z %z", timestamp) or reltime,
reldisplay and reltime or os.date("!%F %T", timestamp));
end
function html.striphtml(s)
s = tostring(s);
local ret = s
:gsub("&", "&")
:gsub("<", "<")
:gsub(">", ">")
:gsub("'", "'")
:gsub("\"", """);
return ret;
end
function html.unstriphtml(s)
s = tostring(s);
local ret = s
:gsub(""", "\"")
:gsub("'", "'")
:gsub(">", ">")
:gsub("<", "<")
:gsub("&", "&");
return ret;
end
function html.picofmt(post_tbl, disable_refs)
if post_tbl["Email"] and post_tbl["Email"]:match("nofo") then
return html.striphtml(post_tbl["Comment"]);
end
local function handle_refs(number)
local ref_post_tbl = pico.post.tbl(post_tbl["Board"], number, true);
if not ref_post_tbl then
return string.format("<s><a class='reference'>>>%d</a></s>", number);
else
return string.format("<a class='reference' href='/pico/%s/%d#%d'>>>%d</a>",
ref_post_tbl["Board"], ref_post_tbl["Parent"] or number, number, number);
end
end
local function handle_xbrefs(board, number)
if not tonumber(number) then
return string.format("<a class='reference' href='/pico/%s'>>>>/%s/</a>%s", board, board, number);
end
local ref_post_tbl = pico.post.tbl(board, number, true);
if not ref_post_tbl then
return string.format("<s><a class='reference'>>>>/%s/%s</a></s>", board, number or "");
else
return string.format("<a class='reference' href='/pico/%s/%d#%d'>>>>/%s/%d</a>",
board, ref_post_tbl["Parent"] or number, number, board, number);
end
end
local function handle_url(previous, url)
local balance_tbl = {
["("] = ")",
[";"] = ">",
["{"] = "}",
["["] = "]"
};
url = html.unstriphtml(url);
local append = "";
if balance_tbl[previous] then
append = url:match("%" .. balance_tbl[previous] .. "[%.%?,!]?$") or "";
url = url:gsub("%" .. balance_tbl[previous] .. "[%.%?,!]?$", "");
end
url = html.striphtml(url);
return string.format("%s<a href='%s'>%s</a>%s", previous, url, url, append);
end
local s = "\n" .. html.striphtml(post_tbl["Comment"]) .. "\n";
if not disable_refs then
s = s:gsub(">>>/([%d%l]-)/(%d+)", handle_xbrefs);
s = s:gsub(">>>/([%d%l]-)/([%s!,%.:;%?])", handle_xbrefs);
s = s:gsub(">>(%d+)", handle_refs);
end
s = s:gsub("(.)(https?://[a-zA-Z0-9%.%%%-%+%(%)_/=%?&;:,#~@]-[a-zA-Z0-9%.%%%-%+%(%)/%?&;:,#@])[^a-zA-Z0-9%.%%%-%+%(%)_/=%?&;:,#~@]", handle_url);
s = s:gsub("'''([^\r\n]-)'''", "<b>%1</b>");
s = s:gsub("''([^\r\n]-)''", "<i>%1</i>");
s = s:gsub("~~([^\r\n]-)~~", "<s>%1</s>");
s = s:gsub("__([^\r\n]-)__", "<u>%1</u>");
s = s:gsub("==([^%s])==", "<span class='redtext'>%1</span>");
s = s:gsub("==([^%s][^\r\n]-[^%s])==", "<span class='redtext'>%1</span>");
s = s:gsub("%*%*([^\r\n]-)%*%*", "<span class='spoiler'>%1</span>");
s = s:gsub("%(%(%([^\r\n]-%)%)%)", "<span class='kiketext'>%1</span>");
s = s:gsub("([\r\n])(>.-)([\r\n])", "%1<span class='greentext'>%2</span>%3");
s = s:gsub("([\r\n])(<.-)([\r\n])", "%1<span class='pinktext'>%2</span>%3");
s = s:gsub("^[\r\n]+", "");
s = s:gsub("[\r\n]+$", "");
return s;
end
function html.formatfilesize(size)
if size > (1024 * 1024) then
return string.format("%.2f MiB", (size / 1024 / 1024));
elseif size > 1024 then
return string.format("%.2f KiB", (size / 1024));
else
return string.format("%d B", size);
end
end
function html.modlinks(post_tbl)
local board = post_tbl["Board"];
local number = post_tbl["Number"];
if (not pico.account.current)
or (pico.account.current["Board"]
and pico.account.current["Board"] ~= board) then
return;
end
printf("<span class='mod-links'>");
printf("<a href='/pico/System/mod/post/delete/%s/%d'>[D]</a>", board, number);
if not post_tbl["Parent"] then
printf("<a href='/pico/System/mod/post/move/%s/%d'>[M]</a>", board, number);
printf("<a href='/pico/System/mod/post/sticky/%s/%d'>[S]</a>", board, number);
printf("<a href='/pico/System/mod/post/lock/%s/%d'>[L]</a>", board, number);
printf("<a href='/pico/System/mod/post/autosage/%s/%d'>[A]</a>", board, number);
printf("<a href='/pico/System/mod/post/cycle/%s/%d'>[C]</a>", board, number);
end
printf("</span>");
end
function html.threadflags(post_tbl)
if (post_tbl["Sticky"] == 1 or post_tbl["Lock"] == 1
or post_tbl["Autosage"] == 1 or post_tbl["Cycle"] == 1) then
printf("<span class='thread-flags'>");
printf("%s%s%s%s",
post_tbl["Sticky"] == 1 and "📌" or "",
post_tbl["Lock"] == 1 and "🔒" or "",
post_tbl["Autosage"] == 1 and "⚓" or "",
post_tbl["Cycle"] == 1 and "♻️" or "");
printf("</span>");
end
end
function html.renderpostfiles(post_tbl)
local file_tbl = post_tbl["Files"];
if file_tbl then
for i = 1, #file_tbl do
local file = file_tbl[i];
local filename = file["Name"];
local extension = pico.file.extension(filename);
local class = pico.file.class(extension);
printf("<div class='post-file%s'>", #file_tbl == 1 and "-single" or "");
printf("<div class='post-file-info'>");
printf("<a href='/media/%s' title='Open file in new tab' target='_blank'>%s...%s</a><br />%s%s",
filename, filename:sub(1, #file_tbl == 1 and 64 or 24), extension,
html.formatfilesize(file["Size"]), file["Width"] and (" " .. file["Width"] .. "x" .. file["Height"]) or "");
printf(" <a href='/media/%s' title='Download file' download>(dl)</a>", filename);
if pico.account.current and ((not pico.account.current["Board"])
or (pico.account.current["Board"] == post_tbl["Board"])) then
printf(" <span class='mod-links'>");
printf("<a href='/pico/System/mod/post/unlink/%s/%d/%s' title='Unlink File'>[U]</a>",
post_tbl["Board"], post_tbl["Number"], filename);
if not pico.account.current["Board"] then
printf("<a href='/pico/System/mod/file/delete/%s' title='Delete File'>[F]</a>",
filename);
end
printf("</span>");
end
printf("</div>");
if extension == "svg" then
printf("<a href='/media/%s'><img class='post-file-thumbnail' src='/media/thumb/%s' alt='[SVG]' /></a>", filename, filename);
elseif class == "image" then
printf("<style>input[type='checkbox']:checked + img.post-file-thumbnail[src='/media/thumb/%s'] + div.post-file-fullsize::after " ..
"{background-image: url('/media/%s'); width: calc(90vh * (%d/%d)); height: calc(90vw * (%d/%d));}</style>",
filename, filename, file["Width"] or 0, file["Height"] or 0, file["Height"] or 0, file["Width"] or 0);
printf("<label>");
printf("<input type='checkbox' hidden />");
printf("<img class='post-file-thumbnail' src='/media/thumb/%s' width='%d' height='%d' />",
filename, thumbsize(file["Width"] or 0, file["Height"] or 0, 200, 200));
printf("<div class='post-file-fullsize'></div>");
printf("</label>");
elseif extension == "pdf" then
printf("<a href='/media/%s'><img class='post-file-thumbnail' src='/media/thumb/%s' width='%d' height='%d' alt='[PDF]' /></a>",
filename, filename, thumbsize(file["Width"] or 200, file["Height"] or 200, 200, 200));
elseif extension == "epub" then
printf("<a href='/media/%s'><img class='post-file-thumbnail' src='/static/epub.png' width=100 height=70 alt='[EPUB]' /></a>", filename);
elseif extension == "txt" then
printf("<a href='/media/%s' target='_blank'><img class='post-file-thumbnail' src='/static/txt.png' width=100 height=70 alt='[TXT]' /></a>", filename);
elseif class == "video" then
printf("<video class='post-video' controls loop preload='none' src='/media/%s' poster='/media/thumb/%s'></video>", filename, filename);
elseif class == "audio" then
printf("<audio class='post-audio' controls loop preload='none' src='/media/%s'></audio>", filename);
end
printf("</div>");
end
end
end
function html.renderpost(post_tbl, include_board_in_id)
printf("<div id='%s%d' class='post-container'>",
include_board_in_id and post_tbl["Board"] .. "-" or "", post_tbl["Number"]);
printf("<div class='post%s'>", post_tbl["Parent"] and "" or " thread");
printf("<div class='post-header'>");
if post_tbl["Subject"] ~= "" then
printf("<span class='post-subject'>%s</span>", html.striphtml(post_tbl["Subject"]));
end
printf("<span class='post-name'>");
if post_tbl["Email"] ~= "" then
printf("<a class='post-email' href='mailto:%s'>%s</a>",
html.striphtml(post_tbl["Email"]), html.striphtml(post_tbl["Name"]));
else
printf("%s", html.striphtml(post_tbl["Name"]));
end
printf("</span>");
printf("<span class='post-date'>%s</span>", html.date(post_tbl["Date"]));
printf("<span class='post-number'><a href='/pico/%s/%d#%d'>No.</a><a href='/pico/%s/%d#postform'>%d</a></span>",
post_tbl["Board"], post_tbl["Parent"] or post_tbl["Number"], post_tbl["Number"],
post_tbl["Board"], post_tbl["Parent"] or post_tbl["Number"], post_tbl["Number"]);
html.threadflags(post_tbl);
html.modlinks(post_tbl);
local reflist = pico.post.refs(post_tbl["Board"], post_tbl["Number"]);
for i = 1, #reflist do
printf("<a class='referrer' href='/pico/%s/%d#%d'>>>%d</a> ",
post_tbl["Board"], post_tbl["Parent"] or post_tbl["Number"], reflist[i], reflist[i]);
end
printf("</div>");
html.renderpostfiles(post_tbl);
printf("<div class='post-comment'>%s</div>", html.picofmt(post_tbl));
printf("</div></div>");
end
function html.rendercatalog(catalog_tbl)
printf("<div class='catalog-container'>");
for i = 1, #catalog_tbl do
local post_tbl = catalog_tbl[i];
local board = post_tbl["Board"];
local number = post_tbl["Number"];
printf("<div class='catalog-thread'>");
printf("<a class='catalog-thread-link' href='/pico/%s/%d'>", board, number);
if post_tbl["File"] then
local extension = pico.file.extension(post_tbl["File"]);
local class = pico.file.class(extension);
if class == "image" or class == "video" or extension == "pdf" then
if post_tbl["FileWidth"] and post_tbl["FileHeight"] then
printf("<img alt='***' src='/media/icon/%s' width=%d height=%d />",
post_tbl["File"], thumbsize(post_tbl["FileWidth"], post_tbl["FileHeight"], 100, 70));
else
printf("<img alt='***' src='/media/icon/%s' />", post_tbl["File"]);
end
elseif class == "audio" then
printf("<img alt='***' src='/static/audio.png' width=100 height=70 />");
elseif extension == "epub" then
printf("<img alt='***' src='/static/epub.png' width=100 height=70 />");
elseif extension == "txt" then
printf("<img alt='***' src='/static/txt.png' width=100 height=70 />");
end
else
printf("***");
end
printf("</a>");
printf("<div class='catalog-thread-info'>");
printf("<a href='/pico/%s'>/%s/</a> R:%d ", board, board, post_tbl["ReplyCount"]);
html.threadflags(post_tbl);
printf("</div>");
printf("<div class='catalog-thread-lastbumpdate'>Bump: %s</div>", html.date(post_tbl["LastBumpDate"], true));
printf("<div class='catalog-thread-subject'>%s</div>", html.striphtml(post_tbl["Subject"]));
printf("<div class='catalog-thread-comment'>%s</div>", html.picofmt(post_tbl, true));
printf("</div>");
end
printf("</div>");
end
function html.brc(title, redheader)
html.begin(title);
html.redheader(redheader);
html.container.begin();
end
function html.cfinish()
html.container.finish();
html.finish();
end
function html.form.postform(board_tbl, parent)
printf("<fieldset><form id='postform' action='/pico/System/post' method='POST' enctype='multipart/form-data'>");
printf( "<input name='board' value='%s' type='hidden' />", board_tbl["Name"]);
if parent ~= nil then
printf("<input name='parent' value='%d' type='hidden' />", parent);
end
printf( "<a class='close-button' href='##' accesskey='w'>[X]</a>");
printf( "<label for='name'>Name</label><input id='name' name='name' type='text' maxlength=64 placeholder='%s' /><br />", pico.global.get("defaultpostname"));
printf( "<label for='email'>Email</label><input id='email' name='email' type='text' maxlength=64 /><br />");
printf( "<label for='subject'>Subject</label><input id='subject' name='subject' type='text' maxlength=64 autocomplete=off />");
printf( "<input type='submit' value='Post' accesskey='s' /><br />");
printf( "<label for='comment'>Comment</label><textarea id='comment' name='comment' form='postform' rows=5 cols=35 maxlength=%d></textarea><br />", board_tbl["PostMaxLength"]);
for i = 1, board_tbl["PostMaxFiles"] do
printf("<label for='file%d'>File %d</label><input id='file%d' name='file%d' type='file' />%s", i, i, i, i, i ~= board_tbl["PostMaxFiles"] and "<br />" or "");
end
if parent == nil and board_tbl["ThreadCaptcha"] == 1
or parent ~= nil and board_tbl["PostCaptcha"] == 1 then
local captchaid, captchab64 = pico.captcha.create();
printf("<input name='captchaid' value='%s' type='hidden' />", captchaid);
printf("<br /><label for='captcha'>Captcha</label><input id='captcha' name='captcha' type='text' pattern='[a-zA-Z]{6}' maxlength=6 autocomplete=off required /><br />");
printf("<img id='captcha-image' src='data:image/jpeg;base64,%s' />", captchab64);
end
printf("</form></fieldset>");
end
function html.form.mod_login()
printf("<fieldset><form method='POST'>");
printf( "<label for='username'>Username</label><input id='username' name='username' type='text' required autofocus /><br />");
printf( "<label for='password'>Password</label><input id='password' name='password' type='password' required /><br />");
printf( "<label for='submit'>Submit</label><input id='submit' type='submit' value='Continue' />");
printf("</form></fieldset>");
end
function html.form.board_create()
printf("<fieldset><form method='POST'>");
printf( "<label for='name'>Name</label><input id='name' name='name' type='text' required autofocus /><br />");
printf( "<label for='title'>Title</label><input id='title' name='title' type='text' required /><br />");
printf( "<label for='subtitle'>Subtitle</label><input id='subtitle' name='subtitle' type='text' /><br />");
printf( "<label for='submit'>Submit</label><input id='submit' type='submit' value='Create' />");
printf("</form></fieldset>");
end
function html.form.board_delete()
printf("<fieldset><form method='POST'>");
printf( "<label for='name'>Name</label><input id='name' name='name' type='text' required autofocus /><br />");
printf( "<label for='reason'>Reason</label><input id='reason' name='reason' type='text' /><br />");
printf( "<label for='submit'>Submit</label><input id='submit' type='submit' value='Delete' />");
printf("</form></fieldset>");
end
function html.form.board_config_select()
printf("<fieldset><form method='POST'>");
printf( "<label for='Name'>Name</label><input id='Name' name='Name' type='text' required autofocus /><br />");
printf( "<label for='submit'>Submit</label><input id='submit' type='submit' value='Continue' />");
printf("</form></fieldset>");
end
function html.form.board_config(board)
local board_tbl = pico.board.tbl(board);
printf("<fieldset><form method='POST'>");
printf( "<input type='hidden' name='Name' value='%s' />", board_tbl["Name"]);
printf( "<label for='Title'>Title</label><input id='Title' name='Title' type='text' value='%s' maxlength=32 required /><br />", html.striphtml(board_tbl["Title"]));
printf( "<label for='Subtitle'>Subtitle</label><input id='Subtitle' name='Subtitle' type='text' value='%s' maxlength=64 /><br />", html.striphtml(board_tbl["Subtitle"]));
printf( "<label for='Lock'>Lock</label><input id='Lock' name='Lock' type='checkbox' value=1 %s/><br />", board_tbl["Lock"] == 1 and "checked " or "");
printf( "<label for='DisplayOverboard'>DisplayOverboard</label><input id='DisplayOverboard' name='DisplayOverboard' type='checkbox' value=1 %s/><br />", board_tbl["DisplayOverboard"] == 1 and "checked " or "");
printf( "<label for='PostMaxFiles'>PostMaxFiles</label><input id='PostMaxFiles' name='PostMaxFiles' type='number' value='%d' min=0 max=5 required /><br />", board_tbl["PostMaxFiles"]);
printf( "<label for='ThreadMinLength'>ThreadMinLength</label><input id='ThreadMinLength' name='ThreadMinLength' type='number' value='%d' required /><br />", board_tbl["ThreadMinLength"]);
printf( "<label for='PostMaxLength'>PostMaxLength</label><input id='PostMaxLength' name='PostMaxLength' type='number' value='%d' required /><br />", board_tbl["PostMaxLength"]);
printf( "<label for='PostMaxNewlines'>PostMaxNewlines</label><input id='PostMaxNewlines' name='PostMaxNewlines' type='number' value='%d' required /><br />", board_tbl["PostMaxNewlines"]);
printf( "<label for='PostMaxDblNewlines'>PostMaxDblNewlines</label><input id='PostMaxDblNewlines' name='PostMaxDblNewlines' type='number' value='%d' required /><br />", board_tbl["PostMaxDblNewlines"]);
printf( "<label for='TPHLimit'>TPHLimit</label><input id='TPHLimit' name='TPHLimit' type='number' value='%d' required /><br />", board_tbl["TPHLimit"]);
printf( "<label for='PPHLimit'>PPHLimit</label><input id='PPHLimit' name='PPHLimit' type='number' value='%d' required /><br />", board_tbl["PPHLimit"]);
printf( "<label for='ThreadCaptcha'>ThreadCaptcha</label><input id='ThreadCaptcha' name='ThreadCaptcha' type='checkbox' value=1 %s/><br />", board_tbl["ThreadCaptcha"] == 1 and "checked " or "");
printf( "<label for='PostCaptcha'>PostCaptcha</label><input id='PostCaptcha' name='PostCaptcha' type='checkbox' value=1 %s/><br />", board_tbl["PostCaptcha"] == 1 and "checked " or "");
printf( "<label for='CaptchaTriggerTPH'>CaptchaTriggerTPH</label><input id='CaptchaTriggerTPH' name='CaptchaTriggerTPH' type='number' value='%d' required /><br />", board_tbl["CaptchaTriggerTPH"]);
printf( "<label for='CaptchaTriggerPPH'>CaptchaTriggerPPH</label><input id='CaptchaTriggerPPH' name='CaptchaTriggerPPH' type='number' value='%d' required /><br />", board_tbl["CaptchaTriggerPPH"]);
printf( "<label for='BumpLimit'>BumpLimit</label><input id='BumpLimit' name='BumpLimit' type='number' value='%d' min=0 max=1000 required /><br />", board_tbl["BumpLimit"]);
printf( "<label for='PostLimit'>PostLimit</label><input id='PostLimit' name='PostLimit' type='number' value='%d' min=0 max=1000 required /><br />", board_tbl["PostLimit"]);
printf( "<label for='ThreadLimit'>ThreadLimit</label><input id='ThreadLimit' name='ThreadLimit' type='number' value='%d' min=0 max=1000 required /><br />", board_tbl["ThreadLimit"]);
printf( "<label for='submit'>Submit</label><input id='submit' type='submit' value='Configure' />");
printf("</form></fieldset>");
end
function html.form.account_create()
printf("<fieldset><form id='account-create' method='POST'>");
printf("<label for='name'>Name</label><input id='name' name='name' type='text' required autofocus /><br />");
printf("<label for='password'>Password</label><input id='password' name='password' type='password' pattern='.{6,128}' maxlength=128 required /><br />");
printf("<label for='type'>Type</label><select form='account-create' id='type' name='type'>");
printf( "<option value='admin'>Administrator</option>");
printf( "<option value='bo'>Board Owner</option>");
printf( "<option value='gvol'>Global Volunteer</option>");
printf( "<option value='lvol' selected>Local Volunteer</option>");
printf("</select><br />");
printf("<label for='board'>Board</label><input id='board' name='board' type='text' /><br />");
printf("<label for='submit'>Submit</label><input id='submit' type='submit' value='Create' />");
printf("</form></fieldset>");
end
function html.form.account_delete()
printf("<fieldset><form method='POST'>");
printf("<label for='name'>Name</label><input id='name' name='name' type='text' required autofocus /><br />");
printf("<label for='reason'>Reason</label><input id='reason' name='reason' type='text' required /><br />");
printf("<label for='submit'>Submit</label><input id='submit' type='submit' value='Delete' />");
printf("</form></fieldset>");
end
function html.form.account_config()
printf("<fieldset><form method='POST'>");
printf("<label for='name'>Account</label><input id='name' name='name' type='text' value='%s' required /><br />", pico.account.current["Name"]);
printf("<label for='password'>Password</label><input id='password' name='password' type='password' required /><br />");
printf("<label for='submit'>Submit</label><input id='submit' type='submit' value='Change Password' />");
printf("</form></fieldset>");
end
function html.form.globalconfig(varname)
printf("<fieldset><form id='globalconfig' method='POST'>");
printf("<input type='hidden' name='name' value='%s' />", varname);
printf("<label for='value'>%s</label>", varname);
if varname == "frontpage" or varname == "announce" then
printf("<textarea id='value' name='value' form='globalconfig' cols=40 rows=12 autofocus>%s</textarea>",
html.striphtml(pico.global.get(varname)) or "");
else
printf("<input id='value' name='value' value='%s' type='text' autofocus />",
html.striphtml(pico.global.get(varname)) or "");
end
printf("<br /><label for='submit'>Submit</label><input id='submit' type='submit' value='Set' />");
printf("</form></fieldset>");
end
function html.form.mod_action_reason()
printf("<fieldset><form method='POST'>");
printf("<label for='reason'>Reason</label><input id='reason' name='reason' type='text' required autofocus />");
printf("<input type='submit' value='Continue' />");
printf("</form></fieldset>");
end
function html.form.mod_move_thread()
printf("<fieldset><form method='POST'>");
printf("<label for='destination'>Destination</label><input id='destination' name='destination' type='text' required autofocus /><br />");
printf("<label for='reason'>Reason</label><input id='reason' name='reason' type='text' required /><br />");
printf("<input type='submit' value='Continue' />");
printf("</form></fieldset>");
end
function html.form.mod_multidelete()
printf("<fieldset><form method='POST'>");
printf("<label for='board'>Board</label><input id='board' name='board' type='text' required autofocus /><br />");
printf("<label for='ispec'>Include</label><input id='ispec' name='ispec' type='text' required /><br />");
printf("<label for='espec'>Exclude</label><input id='espec' name='espec' type='text' /><br />");
printf("<label for='reason'>Reason</label><input id='reason' name='reason' type='text' required /><br />");
printf("<input type='submit' value='Continue' />");
printf("</form></fieldset>");
end
function html.form.mod_pattdelete()
printf("<fieldset><form method='POST'>");
printf("<label for='pattern'>Pattern</label><input id='pattern' name='pattern' type='text' required autofocus /><br />");
printf("<label for='reason'>Reason</label><input id='reason' name='reason' type='text' required /><br />");
printf("<input type='submit' value='Continue' />");
printf("</form></fieldset>");
end
--
-- PAGE DEFINITIONS
--
cgi.headers["Content-Type"] = "text/html; charset=utf-8";
cgi.headers["Cache-Control"] = "no-cache";
cgi.headers["Content-Security-Policy"] = "default-src 'none'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; media-src 'self'; prefetch-src 'self';";
cgi.headers["Referrer-Policy"] = "no-referrer";
cgi.headers["X-DNS-Prefetch-Control"] = "off";
cgi.headers["X-Frame-Options"] = "deny";
if cgi.pathinfo[1] == nil or cgi.pathinfo[1] == "" then
html.begin("welcome");
html.redheader("Welcome to %s", sitename);
html.container.begin();
printf("%s", pico.global.get("frontpage") or "");
html.container.finish();
html.finish();
elseif cgi.pathinfo[1] == "System" then
if cgi.pathinfo[2] == "mod" then
if not pico.account.current and cgi.pathinfo[3] ~= "login" then
cgi.headers["Status"] = "303 See Other";
cgi.headers["Location"] = "/pico/System/mod/login";
cgi.finalize();
end
if cgi.pathinfo[3] == nil or cgi.pathinfo[3] == "" then
html.brc("dashboard", "Moderation Dashboard");
printf("You are logged in as <b>%s</b>. Your account type is <b>%s</b>.",
pico.account.current["Name"], pico.account.current["Type"]);
html.container.barheader("Global");
html.list.begin("unordered");
html.list.entry("<a href='/pico/System/mod/global/announce'>Change global announcement</a>");
html.list.entry("<a href='/pico/System/mod/global/sitename'>Change site name</a>");
html.list.entry("<a href='/pico/System/mod/global/frontpage'>Change front-page content</a>");
html.list.entry("<a href='/pico/System/mod/global/defaultpostname'>Change default post name</a>");
html.list.entry("<a href='/pico/System/mod/global/indexpagesize'>Change index page size</a>");
html.list.entry("<a href='/pico/System/mod/global/indexwindowsize'>Change index window size</a>");
html.list.entry("<a href='/pico/System/mod/global/recentpagesize'>Change recent posts page size</a>");
html.list.entry("<a href='/pico/System/mod/global/logpagesize'>Change mod log page size</a>");
html.list.finish();
html.container.barheader("Miscellaneous Tools");
html.list.begin("unordered");
html.list.entry("<a href='/pico/System/mod/tools/multidelete'>Multi-delete by range</a>");
html.list.entry("<a href='/pico/System/mod/tools/pattdelete'>Pattern delete</a>");
html.list.finish();
html.container.barheader("Accounts");
html.list.begin("unordered");
html.list.entry("<a href='/pico/System/mod/account/create'>Create an account</a>");
html.list.entry("<a href='/pico/System/mod/account/delete'>Delete an account</a>");
html.list.entry("<a href='/pico/System/mod/account/config'>Configure an account</a>");
html.list.finish();
html.container.barheader("Boards");
html.list.begin("unordered");
html.list.entry("<a href='/pico/System/mod/board/create'>Create a board</a>");
html.list.entry("<a href='/pico/System/mod/board/delete'>Delete a board</a>");
html.list.entry("<a href='/pico/System/mod/board/config'>Configure a board</a>");
html.list.finish();
html.cfinish();
elseif cgi.pathinfo[3] == "login" then
html.brc("login", "Moderator Login");
if POST["username"] and POST["password"] then
local session_key, errmsg = pico.account.login(POST["username"], POST["password"]);
if not session_key then
printf("Cannot log in: %s", errmsg);
else
cgi.headers["Set-Cookie"] = "session_key=" .. session_key .. "; HttpOnly; Path=/pico; SameSite=Strict";
cgi.headers["Status"] = "303 See Other";
cgi.headers["Location"] = "/pico/System/mod";
cgi.finalize();
end
end
html.form.mod_login();
html.cfinish();
elseif cgi.pathinfo[3] == "logout" then
pico.account.logout(COOKIE["session_key"]);
cgi.headers["Set-Cookie"] = "session_key=; HttpOnly; Path=/pico; Expires=Thursday, 1 Jan 1970 00:00:00 GMT; SameSite=Strict";
cgi.headers["Status"] = "303 See Other";
cgi.headers["Location"] = "/pico/Overboard";
cgi.finalize();
elseif cgi.pathinfo[3] == "global" then
html.brc("change global configuration", "Change global configuration");
if POST["name"] then
local result, msg;
if POST["value"] == "" then
result, msg = pico.global.set(POST["name"], nil);
else
result, msg = pico.global.set(POST["name"], POST["value"]);
end
printf("%s: %s", result and "Variable set" or "Cannot set variable", msg);
end
html.form.globalconfig(cgi.pathinfo[4]);
html.cfinish();
elseif cgi.pathinfo[3] == "account" then
if cgi.pathinfo[4] == "create" then
html.brc("create account", "Create account");
if POST["name"] ~= nil and POST["name"] ~= "" then
printf("%s", select(2, pico.account.create(POST["name"], POST["password"], POST["type"], POST["board"])));
end
html.form.account_create();
html.cfinish();
elseif cgi.pathinfo[4] == "delete" then
html.brc("delete account", "Delete account");
if POST["name"] and POST["reason"] then
local status, msg = pico.account.delete(POST["name"], POST["reason"]);
printf("%s%s", (not status) and "Cannot delete account: " or "", msg);
end
html.form.account_delete();
html.cfinish();
elseif cgi.pathinfo[4] == "config" then
html.brc("configure account", "Configure account");
if POST["name"] and POST["password"] then
printf("%s", select(2, pico.account.changepass(POST["name"], POST["password"])));
end
html.form.account_config();
html.cfinish();
end
elseif cgi.pathinfo[3] == "board" then
if cgi.pathinfo[4] == "create" then
html.brc("create board", "Create board");
if POST["name"] and POST["title"] and POST["subtitle"] then
local status, msg = pico.board.create(POST["name"], POST["title"], POST["subtitle"]);
printf("%s%s", (not status) and "Cannot create board: " or "", msg);
end
html.form.board_create();
html.cfinish();
elseif cgi.pathinfo[4] == "delete" then
html.brc("delete board", "Delete board");
if POST["name"] and POST["reason"] then
local status, msg = pico.board.delete(POST["name"], POST["reason"]);
printf("%s%s", (not status) and "Cannot delete board: " or "", msg);
end
html.form.board_delete();
html.cfinish();
elseif cgi.pathinfo[4] == "config" then
html.brc("configure board", "Configure board");
if POST["Name"] == nil or POST["Name"] == "" then
html.form.board_config_select();
elseif not pico.board.exists(POST["Name"]) then
printf("Cannot configure board: Board does not exist");
html.form.board_config_select();
else
if POST["Title"] then
local status, msg = pico.board.configure(POST);
printf("%s%s", (not status) and "Cannot configure board: " or "", msg);
end
html.form.board_config(POST["Name"]);
end
html.cfinish();
end
elseif cgi.pathinfo[3] == "post" then
if not (cgi.pathinfo[4] == "delete"
or cgi.pathinfo[4] == "unlink"
or cgi.pathinfo[4] == "move"
or cgi.pathinfo[4] == "sticky"
or cgi.pathinfo[4] == "lock"
or cgi.pathinfo[4] == "autosage"
or cgi.pathinfo[4] == "cycle") then
html.error("Invalid action", "Action is invalid");
end
html.begin("%s post", cgi.pathinfo[4]);
html.redheader("Modify or Delete a Post");
html.container.begin();
local board_tbl = pico.board.tbl(cgi.pathinfo[5]);
local post_tbl = pico.post.tbl(cgi.pathinfo[5], cgi.pathinfo[6]);
if not post_tbl then
html.error("Action failed", "Cannot find post /%s/%d", cgi.pathinfo[5], cgi.pathinfo[6]);
end
if POST["reason"] and POST["reason"] ~= "" then
local result, msg;
if cgi.pathinfo[4] == "delete" then
result, msg = pico.post.delete(cgi.pathinfo[5], cgi.pathinfo[6], POST["reason"]);
elseif cgi.pathinfo[4] == "unlink" then
result, msg = pico.post.unlink(cgi.pathinfo[5], cgi.pathinfo[6], cgi.pathinfo[7], POST["reason"]);
elseif cgi.pathinfo[4] == "move" then
result, msg = pico.post.movethread(cgi.pathinfo[5], cgi.pathinfo[6], POST["destination"], POST["reason"]);
else
result, msg = pico.post.toggle(cgi.pathinfo[4], cgi.pathinfo[5], cgi.pathinfo[6], POST["reason"]);
end
if not result then
html.error("Action failed", "Backend returned error: %s", msg);
else
cgi.headers["Status"] = "303 See Other";
if cgi.pathinfo[4] == "move" then
cgi.headers["Location"] = "/pico/" .. POST["destination"];
else
cgi.headers["Location"] =
post_tbl["Parent"] and ("/pico/" .. board_tbl["Name"] .. "/" .. post_tbl["Parent"])
or ("/pico/" .. board_tbl["Name"] .. "/" .. post_tbl["Number"]);
end
cgi.finalize();
end;
end
printf("You are about to <b>%s</b>%s the following post:", cgi.pathinfo[4],
cgi.pathinfo[4] == "unlink" and " " .. cgi.pathinfo[7] .. " from" or "");
html.renderpost(post_tbl);
if cgi.pathinfo[4] == "move" then
html.form.mod_move_thread();
else
html.form.mod_action_reason();
end
html.cfinish();
elseif cgi.pathinfo[3] == "tools" then
if cgi.pathinfo[4] == "multidelete" then
html.brc("multidelete", "Multidelete");
if POST["board"] then
local result, msg = pico.post.multidelete(POST["board"], POST["ispec"], POST["espec"], POST["reason"]);
printf("%s", msg);
end
html.form.mod_multidelete();
html.cfinish();
elseif cgi.pathinfo[4] == "pattdelete" then
html.brc("pattern delete", "Pattern delete");
if POST["pattern"] then
local result, msg = pico.post.pattdelete(POST["pattern"], POST["reason"]);
printf("%s", msg);
end
html.form.mod_pattdelete();
html.cfinish();
end
elseif cgi.pathinfo[3] == "file" then
if cgi.pathinfo[4] == "delete" then
html.brc("delete file", "Delete file");
if POST["reason"] and POST["reason"] ~= "" then
local result, msg = pico.file.delete(cgi.pathinfo[5], POST["reason"]);
if not result then
html.error("Action failed", "Backend returned error: %s", msg);
else
cgi.headers["Status"] = "303 See Other";
cgi.headers["Location"] = "/pico/Overboard";
cgi.finalize();
end
end
printf("You are about to <b>delete</b> the file %s from <i>all boards</i>.", cgi.pathinfo[5]);
html.form.mod_action_reason();
html.cfinish();
end
end
elseif cgi.pathinfo[2] == "log" then
html.begin("logs");
html.redheader("Moderation Logs");
html.container.begin("wide");
local page = tonumber(cgi.pathinfo[3]) or 1;
page = (page < 1) and 1 or page;
printf("<div class='page-switcher'>");
printf("<a class='page-switcher-prev' href='/pico/System/log/%d'>[Prev]</a>", page - 1);
printf("<a class='page-switcher-next' href='/pico/System/log/%d'>[Next]</a>", page + 1);
printf("</div>");
html.table.begin("Account", "Board", "Date", "Description");
local log_tbl = pico.log.retrieve(page);
for i = 1, #log_tbl do
local entry = log_tbl[i];
html.table.entry(entry["Account"] == "SYSTEM" and "<i>SYSTEM</i>" or entry["Account"],
entry["Board"] == "GLOBAL" and "<i>GLOBAL</i>" or string.format("<a href='/pico/%s'>/%s/</a>", entry["Board"], entry["Board"]),
html.date(entry["Date"]),
html.striphtml(entry["Description"]));
end
html.table.finish();
printf("<div class='page-switcher'>");
printf("<a class='page-switcher-prev' href='/pico/System/log/%d'>[Prev]</a>", page - 1);
printf("<a class='page-switcher-next' href='/pico/System/log/%d'>[Next]</a>", page + 1);
printf("</div>");
html.cfinish();
elseif cgi.pathinfo[2] == "stats" then
html.begin("stats");
html.redheader("Posting Statistics");
html.container.begin("wide");
html.table.begin("Board", "TPW (7d)", "TPD (1d)", "PPD (7d)", "PPD (1d)", "PPH (1h)", "Total Posts");
local g_tpw7d = 0;
local g_tpd1d = 0;
local g_ppd7d = 0;
local g_ppd1d = 0;
local g_pph1h = 0;
local g_total = 0;
local board_list_tbl = pico.board.list();
for i = 1, #board_list_tbl do
local board = board_list_tbl[i]["Name"];
local tpw7d = pico.board.stats.threadrate(board, 24 * 7, 1);
local tpd1d = pico.board.stats.threadrate(board, 24, 1);
local ppd7d = pico.board.stats.postrate(board, 24, 7);
local ppd1d = pico.board.stats.postrate(board, 24, 1);
local pph1h = pico.board.stats.postrate(board, 1, 1);
local total = pico.board.stats.totalposts(board);
g_tpw7d = g_tpw7d + tpw7d;
g_tpd1d = g_tpd1d + tpd1d;
g_ppd7d = g_ppd7d + ppd7d;
g_ppd1d = g_ppd1d + ppd1d;
g_pph1h = g_pph1h + pph1h;
g_total = g_total + total;
html.table.entry(string.format("<a href='/pico/%s' title='%s'>/%s/</a>", board, board_list_tbl[i]["Title"], board),
tpw7d, tpd1d, ppd7d, ppd1d, pph1h, total);
end
html.table.entry("<i>GLOBAL</i>", g_tpw7d, g_tpd1d, g_ppd7d, g_ppd1d, g_pph1h, g_total);
html.table.finish();
html.cfinish();
elseif cgi.pathinfo[2] == "post" then
local file_hashes = {};