This repository has been archived by the owner on Jul 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbs.pl
executable file
·718 lines (589 loc) · 20.1 KB
/
bbs.pl
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
#!/usr/bin/env perl
BEGIN{
use strict;
require 'config.pl';
}
use PerlHP;
<%
use DBI;
use PerlHP::Utils;
use Digest::SHA;
use lib '.';
my $has_encode=0;
eval 'use Encode qw(decode)';
$has_encode=1 unless $@;
my $dbh;
open_db();
my $threads=get_threads();
my %langs;
BEGIN {
our $lang;
our $c_lang;
$lang=$c_lang unless $lang;
%langs=LANGS;
if(!$lang){
my @accept_langs=split(/[^a-z-]+/,$ENV{HTTP_ACCEPT_LANGUAGE});
for(@accept_langs){
if($langs{$_}){
$lang=$_;
last;
}
}
}
$lang=LANG_DEFAULT unless $langs{$lang};
cookie('c_lang',$lang,time()+315360000);
require "strings_$lang.pl";
}
our $task;
if($task eq 'post'){
die S_HAX if $ENV{REQUEST_METHOD} ne 'POST';
our $thread;
our $field1;
our $field2;
our $comment;
our $title;
our $name;
our $link;
our $file;
die S_SPAM if $name or $link;
$thread=0 unless $thread;
make_post($thread,$field1,$field2,$comment,$title,$file);
}else{
my $thread=0;
if($ENV{PATH_INFO}=~/^\/+([0-9]+)\/?/){
$thread=$1;
}
my $sth;
if($thread){
$sth=$dbh->prepare('SELECT UNIX_TIMESTAMP(lastmod) FROM threads WHERE id=?;')
or die S_DBERR;
$sth->execute($thread) or die S_DBERR;
}else{
$sth=$dbh->prepare('SELECT UNIX_TIMESTAMP(lastmod) FROM threads ORDER BY '.
'lastmod DESC LIMIT 1;') or die S_DBERR;
$sth->execute() or die S_DBERR;
}
my($modified)=$sth->fetchrow_array();
my $ifmod=parse_http_date($ENV{HTTP_IF_MODIFIED_SINCE});
if($modified <= $ifmod){
header('Status: 304 Not modified');
}else{
header('Content-Type: text/html; charset=utf-8');
header('Date: '.make_date(time(),'http'));
header('Last-Modified: '.make_date($modified,'http'));
%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="<%= S_LANG %>" xml:lang="<%= S_LANG %>">
<head>
<title><%= TITLE %></title>
<link rel="stylesheet" type="text/css" href="<%= expand_filename(STYLESHEET) %>" />
<link rel="shortcut icon" type="image/x-ico" href="<%= expand_filename(FAVICON) %>" />
<link rel="Alternate" media="handheld" type="application/xhtml+xml" href="<%= expand_filename('mobile.pl') %>" />
<link rel="Alternate" media="handheld" type="text/vnd.wap.wml" href="<%= expand_filename('wml.pl') %>" />
<script type="text/javascript" src="<%= expand_filename('bbs.js') %>"></script>
</head>
<body onload="init()">
<% if($ENV{HTTP_ACCEPT}=~/application\/vnd\.wap\.xhtml\+xml/){ %>
<p><a href="<%= expand_filename('mobile.pl') %>"><%= S_MOBILE %></a></p>
<% }elsif($ENV{HTTP_ACCEPT}=~/text\/vnd\.wap\.wml/){ %>
<p><a href="<%= expand_filename('wml.pl') %>"><%= S_MOBILE %></a></p>
<%
}
if($ENV{PATH_INFO}=~/\/subback\/?.*/){
%>
<div id="navigation">
<a href="<%= $ENV{SCRIPT_NAME} %>"><%= S_RETURN %></a>
</div>
<div id="allthreads">
<% full_thread_list() %>
</div>
<% }elsif($ENV{PATH_INFO}=~/^\/+([0-9]+)\/+([lr0-9,\-]+)\/?.*/){ %>
<div id="navigation">
<a href="<%= $ENV{SCRIPT_NAME} %>"><%= S_RETURN %></a>
|
<a href="<%= $ENV{SCRIPT_NAME} %>/<%= $1 %>"><%= S_ENTIRE %></a>
|
<a href="<%= $ENV{SCRIPT_NAME} %>/<%= $1 %>/-100"><%= S_FIRST100 %></a>
|
<a href="<%= $ENV{SCRIPT_NAME} %>/<%= $1 %>/l50"><%= S_LAST50 %></a>
</div>
<% show_thread_posts($1,$2) %>
<% }elsif($ENV{PATH_INFO}=~/^\/+([0-9]+)\/?.*/){ %>
<div id="navigation">
<a href="<%= $ENV{SCRIPT_NAME} %>"><%= S_RETURN %></a>
|
<a href="<%= $ENV{SCRIPT_NAME} %>/<%= $1 %>/-100"><%= S_FIRST100 %></a>
|
<a href="<%= $ENV{SCRIPT_NAME} %>/<%= $1 %>/l50"><%= S_LAST50 %></a>
</div>
<% show_thread($1) %>
<%
}else{
if(scalar(keys %langs)>1){
%>
<div id="navigation">
<%
my @langlinks;
for(keys %langs){
push @langlinks,"<a href=\"$ENV{SCRIPT_NAME}?lang=$_\">$langs{$_}</a>";
}
%>
<%= join(' | ',@langlinks) %>
</div>
<% } %>
<div id="threadlistbox">
<p id="threadlist">
<% main_page_thread_list() %>
</p>
<p id="threadlistnav">
<a href="#threadform"><%= S_NEWTHR %></a>
|
<a href="<%= $ENV{SCRIPT_NAME} %>/subback"><%= S_ALLTHR %></a>
</p>
</div>
<div id="threads">
<% main_page_threads() %>
</div>
<form id="threadform" class="postform" method="post"
action="<%= $ENV{SCRIPT_NAME} %>" enctype="multipart/form-data">
<p>
<input type="hidden" name="task" value="post" />
<span style="display:none">
<label for="name">Spam: </label>
<input type="text" name="name" id="name" />
<label for="link">Spam: </label>
<input type="text" name="link" id="link" /><br />
</span>
<label for="field1"><%= S_NAME %></label>
<input type="text" name="field1" id="field1" />
<label for="field2"><%= S_LINK %></label>
<input type="text" name="field2" id="field2" />
<br />
<label for="title"><%= S_TITLE %></label>
<input type="text" name="title" id="title" />
<input type="submit" value="<%= S_CREATETHR %>" /><br />
<label for="comment"><%= S_COMMENT %></label><br />
<textarea name="comment" rows="6" cols="60" id="comment"></textarea><br />
<label for="file"><%= S_IMAGE %></label>
<input type="file" name="file" id="file" />
</p>
</form>
<% } %>
<p class="footer">
- <% printf(S_POWERED,'<a href="https://github.com/hotaru2k3/perlhp">modified</a> <a href="http://wakaba.c3.cx/perlhp/">PerlHP</a>',
SQL_DB_LINK) %>
-
</p>
</body>
</html>
<%
}
}
close_db();
sub main_page_thread_list(){
my $i=0;
my $j=0;
my $thread;
for $thread (@$threads){
last if $j>40;
++$j;
my($id,$title,$lasthit,$postcount,$permasaged,$deleted)=@$thread;
my $href;
$href='#t-'.($i+1) if $i<10;
$href="$ENV{SCRIPT_NAME}/$id" if $i>9;
++$i;
%>
<a href="<%= $ENV{SCRIPT_NAME}%>/<%= $id %>"><%= $i %></a>:<a
href="<%= $href %>"><%= $title %>
(<%= $postcount %>)</a><%= $i<@$threads?',':'' %>
<%
}
}
sub main_page_threads(){
my $thread;
return if !@$threads;
my $c=0;
for $thread (@$threads){
if(++$c<10){
my($id,$title,$lasthit,$postcount,$permasaged,$deleted)=@$thread;
%>
<div class="<%= $permasaged?'permasaged':'' %>thread" id="t-<%= $c %>">
<div class="navarrows">
<a href="#threadlistbox">⇑</a>
<a href="#t-<%= $c==1?(10>@$threads?scalar @$threads:10):$c-1 %>">↑</a>
<a href="#t-<%= (($c==10||$c==@$threads)?1:$c+1) %>">↓</a>
</div>
<p class="threadtitle">
<a href="<%= $ENV{SCRIPT_NAME} %>/<%= $id %>">
<%= $title %></a>
(<%= $postcount %><%= $permasaged?', '.S_PERMASAGED:'' %>)
</p>
<%
my $sth=$dbh->prepare('SELECT * FROM posts WHERE thread=? AND NUM = 1;') or
die S_DBERR;
$sth->execute($id) or die S_DBERR;
show_post($sth->fetchrow_array());
$sth=$dbh->prepare('SELECT * FROM posts WHERE thread=? AND num !=1 ORDER '.
'BY num DESC LIMIT 9;') or die S_DBERR;
$sth->execute($id) or die S_DBERR;
my $posts=$sth->fetchall_arrayref();
my $post;
for $post (reverse @$posts){
show_post(@$post);
}
reply_form($id,$postcount);
%>
</div>
<%
}
}
}
sub full_thread_list(){
my $thread;
for $thread (@$threads){
my($id,$title,$lasthit,$postcount,$permasaged,$deleted)=@$thread;
%>
<p>
<a href="<%= $ENV{SCRIPT_NAME} %>/<%= $id %>">
<%= $title %> (<%= $postcount %><%= $permasaged?', '.S_PERMASAGED:'' %>)
</a>
</p>
<%
}
}
sub show_thread($){
my($thread)=@_;
my $sth=$dbh->prepare('SELECT * FROM threads WHERE id=? AND deleted=0;') or
die S_DBERR;
$sth->execute($thread) or die S_DBERR;
my($id,$title,$lasthit,$postcount,$permasaged,$deleted)=$sth->fetchrow_array();
die S_NOTHR if $deleted or !$title;
$sth=$dbh->prepare('SELECT * FROM posts WHERE thread=?;') or die S_DBERR;
$sth->execute($thread) or die S_DBERR;
my $posts=$sth->fetchall_arrayref();
%>
<div class="<%= $permasaged?'permasaged':'' %>thread">
<p class="threadtitle">
<%= $title %> (<%= $postcount %><%= $permasaged?', '.S_PERMASAGED:'' %>)
</p>
<%
my $post;
for $post (@$posts){
show_post(@$post);
}
reply_form($thread,$postcount);
%>
</div>
<%
}
sub show_thread_posts($$){
my($thread,$specs)=@_;
my $sth=$dbh->prepare('SELECT * FROM threads WHERE id=? AND deleted=0;') or
die S_DBERR;
$sth->execute($thread) or die S_DBERR;
my($id,$title,$lasthit,$postcount,$permasaged,$deleted)=
$sth->fetchrow_array();
die S_NOTHR if $deleted or !$title;
%>
<div class="<%= $permasaged?'permasaged':'' %>thread">
<p class="threadtitle">
<%= $title %> (<%= $postcount %><%= $permasaged?', '.S_PERMASAGED:'' %>)
</p>
<%
my @specs=split(/,/,$specs);
my @posts;
my $spec;
for $spec (@specs){
if($spec=~/^[0-9]{1,4}$/ and $spec<=$postcount){
push(@posts,$spec);
}elsif($spec=~/^([0-9]{0,4})\-([0-9]{0,4})$/){
if(!$1 and !$2){
push(@posts,1..$postcount);
}elsif($1 and !$2 and $1<$postcount){
push(@posts,($1||1)..$postcount);
}elsif($2 and !$1 and $2<1001){
push(@posts,1..(($postcount<$2)?$postcount:$2));
}elsif($1<$2 and $2<1001){
push(@posts,($1||1)..(($postcount<$2)?$postcount:$2));
}elsif($1>$2 and $1<1001){
push(@posts,reverse(($2||1)..(($postcount<$1)?$postcount:$1)));
}elsif($1==$2 and $1<=$postcount and $1){
push(@posts,$1);
}
}elsif($spec=~/^l([0-9]{0,4})$/){
my $start=1+$postcount-$1;
$start=1 if $start<1;
push(@posts,$start..$postcount);
}elsif($spec=~/^r([0-9]{0,4})$/){
push(@posts,map{int(rand($postcount)+1)}1..($1||1));
}
}
@posts=(1..$postcount) unless @posts;
die 'too many posts' if scalar(@posts)>2000;
$sth=$dbh->prepare('SELECT * FROM posts WHERE thread=?;') or die S_DBERR;
$sth->execute($thread) or die S_DBERR;
my $all_posts=$sth->fetchall_arrayref();
my $post;
for $post (@posts){
show_post(@{$$all_posts[$post-1]});
}
reply_form($thread,$postcount);
%>
</div>
<%
}
sub show_post($$$$$$$$$$$){
my($num,$thread,$time,$name,$trip,$link,$comment,$ip,$file,$filename,$deleted)=@_;
if($deleted){
%>
<div class="post">
<p class="deleted"><%= S_DELETEDPOST %></p>
</div>
<% }else{ %>
<div class="post">
<p class="postheader">
<span class="postnum"><%= $num %></span>
<%= S_NAME %>
<% if($link){ %>
<a href="<%= $link %>">
<% } %>
<span class="postername"><%= $name %></span>
<span class="postertrip"><%= $trip %></span>
<% if($link){ %>
</a>
<% } %>
: <span class="posttime"><%= $time %></span>
<%= S_ID %><span class="id"><%= $link=~/sage/?'Heaven':make_id($thread.$ip) %></span>
</p>
<div class="postbody">
<%
if($file){
my @fileparts=split /\./,$file;
my $ext=pop @fileparts;
my $file=join '.',@fileparts;
%>
<div class="image">
<a href="<%= expand_filename("src/$file.$ext") %>">
<% if($ext =~ /^(?:jpg|gif|png|svg)$/){ %>
<img src="<%= expand_filename("thumb/$file.gif") %>" alt="<%= $filename %>" title="<%= $filename %>" />
<% } else { %>
<span class="nothumb"><%= S_NOTHUMB %>:<br /><%= $filename %></span>
<% } %>
</a>
</div>
<%
}
%>
<div class="commenttext">
<%= $comment %>
</div>
</div>
</div>
<%
}
}
sub reply_form($$){
my($thread,$postcount)=@_;
if($postcount>999){
%>
<p class="threadclosed"><%= S_CLOSED %></p>
<p class="threadnav">
<a href="<%= $ENV{SCRIPT_NAME} %><%= $thread %>"><%= S_ENTIRE %></a>
<a href="<%= $ENV{SCRIPT_NAME} %>/<%= $thread %>/-100"><%= S_FIRST100 %></a>
<a href="<%= $ENV{SCRIPT_NAME} %>/<%= $thread %>/l50"><%= S_LAST50 %></a>
</p>
<% }else{ %>
<form class="postform" action="<%= $ENV{SCRIPT_NAME} %>" method="post"
enctype="multipart/form-data">
<p>
<input type="hidden" name="task" value="post" />
<input type="hidden" name="thread" value="<%= $thread %>" />
<span style="display:none;">
<label for="name-<%= $thread %>">Spam: </label>
<input type="text" name="name" id="name-<%= $thread %>" />
<label for="link-<%= $thread %>">Spam: </label>
<input type="text" name="link" id="link-<%= $thread %>" /><br />
</span>
<label for="field1-<%= $thread %>"><%= S_NAME %></label>
<input type="text" name="field1" id="field1-<%= $thread %>" />
<label for="field2-<%= $thread %>"><%= S_LINK %></label>
<input type="text" name="field2" id="field2-<%= $thread %>" />
<input type="submit" value="<%= S_REPLY %>" /><br />
<label for="comment-<%= $thread %>"><%= S_COMMENT %></label><br />
<textarea name="comment" rows="6" cols="60" id="comment-<%= $thread %>"></textarea><br />
<label for="file-<%= $thread %>"><%= S_IMAGE %></label>
<input type="file" name="file" id="file-<%= $thread %>" />
</p>
<p class="threadnav">
<a href="<%= $ENV{SCRIPT_NAME} %>/<%= $thread %>"><%= S_ENTIRE %></a>
<a href="<%= $ENV{SCRIPT_NAME} %>/<%= $thread %>/-100"><%= S_FIRST100 %></a>
<a href="<%= $ENV{SCRIPT_NAME} %>/<%= $thread %>/l50"><%= S_LAST50 %></a>
</p>
</form>
<%
}
}
sub get_threads(){
my $sth=$dbh->prepare('SELECT * FROM threads WHERE deleted=0 ORDER BY '.
'lasthit DESC;') or die S_DBERR;
$sth->execute() or die S_DBERR;
return $sth->fetchall_arrayref();
}
sub make_post($$$$;$$){
my($thread,$name,$link,$comment,$title,$file)=@_;
my($trip,$filename);
die S_HAX unless $thread=~/^[0-9]*$/;
$name=substr($name,0,128);
$link=substr($link,0,512);
$comment=substr($comment,0,8192);
$title=substr($title,0,256);
($name,$trip)=process_tripcode($name,'!',SECRET,'utf8');
$name=ANON_NAME if !$name and !$trip;
#$link=clean_string(decode_string($link,'utf8'));
$link=my_clean_string($link);
$name=gethostbyaddr inet_aton($ip),AF_INET or $ip if $link=~/fusianasan/;
$link="mailto:$link" if($link and $link!~/^$PerlHP::Utils::protocol_re:/);
#$comment=~s/^(>>[0-9\-,]+)/ $1/gm;
#$comment=do_wakabamark(undef,0,$comment);
$comment=format_post(my_clean_string($comment),$thread);
die S_NOTEXT unless $comment;
die S_SPAM if spam_check($link."\n".$comment,SPAM_FILE);
if($file){
#$filename=clean_string(decode_string($file,'utf8'));
$filename=my_clean_string($file);
my $fh=upload('file');
binmode($fh);
my $buffer;
my $size=-s $fh;
die S_TOOBIG if $size>MAX_FILE_SIZE;
my($ext,$height,$width)=analyze_image($fh);
if(!$width){
$ext='invalid';
if($filename=~/\.([^\.])$/){
$ext=$1;
}
if($ext=~/^(jpg|gif|png|svg)$/){
$ext.='.invalid';
}
}
seek($fh,0,0);
my $sha=Digest::SHA->new(256);
read $fh,$buffer,$size;
$sha->add($buffer);
$file=$sha->hexdigest;
seek($fh,0,0);
open OUTFILE,"+>src/${file}.${ext}" or die S_NOWRITE;
binmode OUTFILE;
print OUTFILE $buffer or die S_NOWRITE;
close $fh;
close OUTFILE;
if($ext =~ /^(?:jpg|gif|png|svg)$/){
my $tn_height=100;
my $tn_width=int($width*100/$height);
if($ext eq 'svg'){
`svg2png -w $tn_width -h $tn_height src/${file}.${ext} -|convert -size ${tn_width}x${tn_height} -geometry ${tn_width}x${tn_height} - thumb/${file}.gif`;
`convert -size ${tn_width}x${tn_height} -geometry ${tn_width}x${tn_height} src/${file}.${ext} thumb/${file}.gif` if $?;
}else{
`convert -size ${tn_width}x${tn_height} -geometry ${tn_width}x${tn_height} src/${file}.${ext} thumb/${file}.gif`;
}
}
$file=$file.'.'.$ext;
}
my $sth;
if($thread==0){
#$title=clean_string(decode_string($title,'utf8'));
$title=my_clean_string($title);
die S_NOTITLE unless $title;
$sth=$dbh->prepare('INSERT INTO threads VALUES(null,?,?,?,0,0);') or die
S_DBERR;
$sth->execute($title,time,0) or die S_DBERR;
$sth=$dbh->prepare('SELECT id FROM threads WHERE id IS NULL;') or
die S_DBERR;
$sth->execute() or die S_DBERR;
($thread)=$sth->fetchrow_array();
}
$sth=$dbh->prepare('SELECT * FROM threads WHERE id=?;') or die S_DBERR;
$sth->execute($thread) or die S_DBERR;
my($thread,$title,$lasthit,$postcount,$permasaged,$deleted)=
$sth->fetchrow_array();
#my $tl="$ENV{SCRIPT_NAME}/$thread";
#$comment=~s/(>>((?:[0-9\-]|,)+))/<a href="$tl\/$2" rel="nofollow">$1<\/a>$3/g;
++$postcount;
my $time=scalar(gmtime(time));
$sth=$dbh->prepare('INSERT INTO posts VALUES(?,?,?,?,?,?,?,?,?,?,0);') or
die S_DBERR;
$sth->execute($postcount,$thread,$time,$name,$trip,$link,$comment,
$ENV{REMOTE_ADDR},$file,$filename) or die S_DBERR.' '.$sth->errstr;
$lasthit=time unless $link=~/sage/ or $permasaged;
$sth=$dbh->prepare('UPDATE threads SET lasthit=?,postcount=? WHERE id=?;') or
die S_DBERR;
$sth->execute($lasthit,$postcount,$thread) or die S_DBERR;
close_db();
header('Status: 301 Go West');
header("Location: $ENV{SCRIPT_NAME} ");
}
sub open_db(){
$dbh=DBI->connect(SQL_DBI_SOURCE,SQL_USERNAME,SQL_PASSWORD,{AutoCommit=>1})
or die S_SQLCONF;
my $sth=$dbh->prepare('CREATE TABLE IF NOT EXISTS threads(id BIGINT UNSIGNED'.
' UNIQUE PRIMARY KEY AUTO_INCREMENT NOT NULL,title TEXT,lasthit INTEGER '.
'UNSIGNED NOT NULL,postcount SMALLINT UNSIGNED,permasaged BOOL,deleted BOOL'.
',lastmod TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,'.
'INDEX threadindex (id));') or die S_DBERR;
$sth->execute() or die S_DBERR;
$sth=$dbh->prepare('CREATE TABLE IF NOT EXISTS posts(num SMALLINT UNSIGNED '.
'NOT NULL,thread BIGINT UNSIGNED NOT NULL REFERENCES threads(id),time TEXT,'.
'name TEXT,trip TEXT,link TEXT,comment TEXT,ip TEXT,file TEXT,filename TEXT'.
',deleted BOOL,INDEX postindex (thread,num));') or die S_DBERR;
$sth->execute() or die S_DBERR;
}
sub close_db(){
$dbh->disconnect();
}
sub make_id($){
my($text)=@_;
return encode_base64(rc4(null_string(6),$text.SECRET),"");
}
sub expand_filename($){
my ($filename)=@_;
return $filename if($filename=~m!^/!);
return $filename if($filename=~m!^\w+:!);
my ($self_path)=$ENV{SCRIPT_NAME}=~m!^(.*/)[^/]+$!;
return $self_path.$filename;
}
sub my_clean_string($){
my($str)=@_;
$str=decode('utf8',$str) if $has_encode;
$str=~s/&/&/g;
$str=~s/</</g;
$str=~s/>/>/g;
$str=~s/"/"/g;
$str=~s/'/'/g;
$str=~s/[\x00-\x08\x0b\x0c\x0e-\x1f\x80-\x84]//g; # control chars
$str=~s/[\x{d800}-\x{dfff}]//g; # surrogate code points
$str=~s/[\x{202a}-\x{202e}]//g; # text direction
$str=~s/[\x{fdd0}-\x{fdef}\x{fffe}\x{ffff}\x{1fffe}\x{1ffff}\x{2fffe}\x{2ffff}\x{3fffe}\x{3ffff}\x{4fffe}\x{4ffff}\x{5fffe}\x{5ffff}\x{6fffe}\x{6ffff}\x{7fffe}\x{7ffff}\x{8fffe}\x{8ffff}\x{9fffe}\x{9ffff}\x{afffe}\x{affff}\x{bfffe}\x{bffff}\x{cfffe}\x{cffff}\x{dfffe}\x{dffff}\x{efffe}\x{effff}\x{ffffe}\x{fffff}]//g; # non-characters
$str=join('',map{$_<0x10fffe?$_:''}split(//,$str));
return $str;
}
sub format_post($$){
my $protocol_re=qr/(?:http|https|ftp|mailto|nntp)/;
my($text,$thread)=@_;
$text=~s/\r\n/\n/sg; # fix newlines
$text=~s/\r/\n/sg; # fix newlines
$text=~s{($protocol_re:[^\s<>"]*?)((?:\s|<|>|"|\.|\)|\]|!|\?|,|,|")*(?:[\s<>"]|$))}{<a href="$1" rel="nofollow">${1}</a>$2}sg; # hyperlink urls
$text=~s/>>([0-9,\-]+)/<a href="$ENV{SCRIPT_NAME}\/$thread\/$1">>>$1<\/a>/g; # >> links
$text=~s/^(>.*)$/<blockquote><p>$1<\/p><\/blockquote>/mg; # blockquotes
$text=~s/^\x{3000}(.*)$/<p lang="ja">$1<\/p>/mg; # sjis art
$text=~s/^ (.*)$/<code>$1<\/code>/mg; # code sections
$text=~s/^spoiler:(.*)$/<div class="spoiler"><input type="button" class="spoilerbutton" value="spoiler" onclick="this.parentNode.getElementsByTagName('div')[0].style.display='block';this.style.display='none'" \/><div class="spoilertext">$1<\/div><\/div>/mg; # spoilers
$text=~s/<\/p><\/blockquote>\n<blockquote><p>/\n/sg; # fixup multiline quotes
$text=~s/<\/p>\n<p lang="ja">/\n/sg; # fixup multiline sjis art
$text=~s/<\/code>\n<code>/\n/sg; # fixup multiline code sections
$text=~s/<\/div><\/div>\n<div class="spoiler"><input type="button" class="spoilerbutton" value="spoiler" onclick="this.parentNode.getElementsByTagName('div')[0].style.display='block';this.style.display='none'" \/><div class="spoilertext">/\n/sg; # fixup multiline spoilers
$text=~s/(<\/(?:blockquote|p)>)\n/$1/sg; # remove newlines after blockquotes
$text=~s/\n(<blockquote)/$1/sg; # remove newlines before blockquotes
$text=~s/\n/<br \/>/sg; # convert newlines to <br />
return $text;
}
%>