-
Notifications
You must be signed in to change notification settings - Fork 2
/
wakaba.pl
executable file
·1866 lines (1487 loc) · 47 KB
/
wakaba.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
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/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use strict;
use CGI;
use DBI;
#
# Import settings
#
use lib '.';
BEGIN { require "config.pl"; }
BEGIN { require "config_defaults.pl"; }
BEGIN { require "strings_en.pl"; } # edit this line to change the language
BEGIN { require "futaba_style.pl"; } # edit this line to change the board style
BEGIN { require "captcha.pl"; }
BEGIN { require "wakautils.pl"; }
#
# Optional modules
#
my ($has_encode);
if(CONVERT_CHARSETS)
{
eval 'use Encode qw(decode encode)';
$has_encode=1 unless($@);
}
#
# Global init
#
my $protocol_re=qr/(?:http|https|ftp|mailto|nntp)/;
my $dbh=DBI->connect(SQL_DBI_SOURCE,SQL_USERNAME,SQL_PASSWORD,{AutoCommit=>1}) or make_error(S_SQLCONF);
return 1 if(caller); # stop here if we're being called externally
my $query=new CGI;
my $task=($query->param("task") or $query->param("action"));
# check for admin table
init_admin_database() if(!table_exists(SQL_ADMIN_TABLE));
# check for proxy table
init_proxy_database() if(!table_exists(SQL_PROXY_TABLE));
if(!table_exists(SQL_TABLE)) # check for comments table
{
init_database();
build_cache();
make_http_forward(HTML_SELF,ALTERNATE_REDIRECT);
}
elsif(!$task)
{
build_cache() unless -e HTML_SELF;
make_http_forward(HTML_SELF,ALTERNATE_REDIRECT);
}
elsif($task eq "post")
{
my $parent=$query->param("parent");
my $name=$query->param("field1");
my $email=$query->param("field2");
my $subject=$query->param("field3");
my $comment=$query->param("field4");
my $file=$query->param("file");
my $password=$query->param("password");
my $nofile=$query->param("nofile");
my $captcha=$query->param("captcha");
my $admin=$query->param("admin");
my $no_captcha=$query->param("no_captcha");
my $no_format=$query->param("no_format");
my $postfix=$query->param("postfix");
post_stuff($parent,$name,$email,$subject,$comment,$file,$file,$password,$nofile,$captcha,$admin,$no_captcha,$no_format,$postfix);
}
elsif($task eq "delete")
{
my $password=$query->param("password");
my $fileonly=$query->param("fileonly");
my $archive=$query->param("archive");
my $admin=$query->param("admin");
my @posts=$query->param("delete");
delete_stuff($password,$fileonly,$archive,$admin,@posts);
}
elsif($task eq "admin")
{
my $password=$query->param("berra"); # lol obfuscation
my $nexttask=$query->param("nexttask");
my $savelogin=$query->param("savelogin");
my $admincookie=$query->cookie("wakaadmin");
do_login($password,$nexttask,$savelogin,$admincookie);
}
elsif($task eq "logout")
{
do_logout();
}
elsif($task eq "mpanel")
{
my $admin=$query->param("admin");
make_admin_post_panel($admin);
}
elsif($task eq "deleteall")
{
my $admin=$query->param("admin");
my $ip=$query->param("ip");
my $mask=$query->param("mask");
delete_all($admin,parse_range($ip,$mask));
}
elsif($task eq "bans")
{
my $admin=$query->param("admin");
make_admin_ban_panel($admin);
}
elsif($task eq "addip")
{
my $admin=$query->param("admin");
my $type=$query->param("type");
my $comment=$query->param("comment");
my $ip=$query->param("ip");
my $mask=$query->param("mask");
add_admin_entry($admin,$type,$comment,parse_range($ip,$mask),'');
}
elsif($task eq "addstring")
{
my $admin=$query->param("admin");
my $type=$query->param("type");
my $string=$query->param("string");
my $comment=$query->param("comment");
add_admin_entry($admin,$type,$comment,0,0,$string);
}
elsif($task eq "removeban")
{
my $admin=$query->param("admin");
my $num=$query->param("num");
remove_admin_entry($admin,$num);
}
elsif($task eq "proxy")
{
my $admin=$query->param("admin");
make_admin_proxy_panel($admin);
}
elsif($task eq "addproxy")
{
my $admin=$query->param("admin");
my $type=$query->param("type");
my $ip=$query->param("ip");
my $timestamp=$query->param("timestamp");
my $date=make_date(time(),DATE_STYLE);
add_proxy_entry($admin,$type,$ip,$timestamp,$date);
}
elsif($task eq "removeproxy")
{
my $admin=$query->param("admin");
my $num=$query->param("num");
remove_proxy_entry($admin,$num);
}
elsif($task eq "spam")
{
my ($admin);
$admin=$query->param("admin");
make_admin_spam_panel($admin);
}
elsif($task eq "updatespam")
{
my $admin=$query->param("admin");
my $spam=$query->param("spam");
update_spam_file($admin,$spam);
}
elsif($task eq "sqldump")
{
my $admin=$query->param("admin");
make_sql_dump($admin);
}
elsif($task eq "sql")
{
my $admin=$query->param("admin");
my $nuke=$query->param("nuke");
my $sql=$query->param("sql");
make_sql_interface($admin,$nuke,$sql);
}
elsif($task eq "mpost")
{
my $admin=$query->param("admin");
make_admin_post($admin);
}
elsif($task eq "rebuild")
{
my $admin=$query->param("admin");
do_rebuild_cache($admin);
}
elsif($task eq "nuke")
{
my $admin=$query->param("admin");
do_nuke_database($admin);
}
$dbh->disconnect();
#
# Cache page creation
#
sub build_cache()
{
my ($sth,$row,@thread);
my $page=0;
# grab all posts, in thread order (ugh, ugly kludge)
$sth=$dbh->prepare("SELECT * FROM ".SQL_TABLE." ORDER BY lasthit DESC,CASE parent WHEN 0 THEN num ELSE parent END ASC,num ASC") or make_error(S_SQLFAIL);
$sth->execute() or make_error(S_SQLFAIL);
$row=get_decoded_hashref($sth);
if(!$row) # no posts on the board!
{
build_cache_page(0,1); # make an empty page 0
}
else
{
my @threads;
my @thread=($row);
while($row=get_decoded_hashref($sth))
{
if(!$$row{parent})
{
push @threads,{posts=>[@thread]};
@thread=($row); # start new thread
}
else
{
push @thread,$row;
}
}
push @threads,{posts=>[@thread]};
my $total=get_page_count(scalar @threads);
my @pagethreads;
while(@pagethreads=splice @threads,0,IMAGES_PER_PAGE)
{
build_cache_page($page,$total,@pagethreads);
$page++;
}
}
# check for and remove old pages
while(-e $page.PAGE_EXT)
{
unlink $page.PAGE_EXT;
$page++;
}
}
sub build_cache_page($$@)
{
my ($page,$total,@threads)=@_;
my ($filename,$tmpname);
if($page==0) { $filename=HTML_SELF; }
else { $filename=$page.PAGE_EXT; }
# do abbrevations and such
foreach my $thread (@threads)
{
# split off the parent post, and count the replies and images
my ($parent,@replies)=@{$$thread{posts}};
my $replies=@replies;
my $images=grep { $$_{image} } @replies;
my $curr_replies=$replies;
my $curr_images=$images;
my $max_replies=REPLIES_PER_THREAD;
my $max_images=(IMAGE_REPLIES_PER_THREAD or $images);
# drop replies until we have few enough replies and images
while($curr_replies>$max_replies or $curr_images>$max_images)
{
my $post=shift @replies;
$curr_images-- if($$post{image});
$curr_replies--;
}
# write the shortened list of replies back
$$thread{posts}=[$parent,@replies];
$$thread{omit}=$replies-$curr_replies;
$$thread{omitimages}=$images-$curr_images;
# abbreviate the remaining posts
foreach my $post (@{$$thread{posts}})
{
my $abbreviation=abbreviate_html($$post{comment},MAX_LINES_SHOWN,APPROX_LINE_LENGTH);
if($abbreviation)
{
$$post{comment}=$abbreviation;
$$post{abbrev}=1;
}
}
}
# make the list of pages
my @pages=map +{ page=>$_ },(0..$total-1);
foreach my $p (@pages)
{
if($$p{page}==0) { $$p{filename}=expand_filename(HTML_SELF) } # first page
else { $$p{filename}=expand_filename($$p{page}.PAGE_EXT) }
if($$p{page}==$page) { $$p{current}=1 } # current page, no link
}
my ($prevpage,$nextpage);
$prevpage=$pages[$page-1]{filename} if($page!=0);
$nextpage=$pages[$page+1]{filename} if($page!=$total-1);
print_page($filename,PAGE_TEMPLATE->(
postform=>(ALLOW_TEXTONLY or ALLOW_IMAGES),
image_inp=>ALLOW_IMAGES,
textonly_inp=>(ALLOW_IMAGES and ALLOW_TEXTONLY),
prevpage=>$prevpage,
nextpage=>$nextpage,
pages=>\@pages,
threads=>\@threads
));
}
sub build_thread_cache($)
{
my ($thread)=@_;
my ($sth,$row,@thread);
my ($filename,$tmpname);
$sth=$dbh->prepare("SELECT * FROM ".SQL_TABLE." WHERE num=? OR parent=? ORDER BY num ASC;") or make_error(S_SQLFAIL);
$sth->execute($thread,$thread) or make_error(S_SQLFAIL);
while($row=get_decoded_hashref($sth)) { push(@thread,$row); }
make_error(S_NOTHREADERR) if($thread[0]{parent});
$filename=RES_DIR.$thread.PAGE_EXT;
print_page($filename,PAGE_TEMPLATE->(
thread=>$thread,
postform=>(ALLOW_TEXT_REPLIES or ALLOW_IMAGE_REPLIES),
image_inp=>ALLOW_IMAGE_REPLIES,
textonly_inp=>0,
dummy=>$thread[$#thread]{num},
threads=>[{posts=>\@thread}])
);
}
sub print_page($$)
{
my ($filename,$contents)=@_;
$contents=encode_string($contents);
# $PerlIO::encoding::fallback=0x0200 if($has_encode);
# binmode PAGE,':encoding('.CHARSET.')' if($has_encode);
if(USE_TEMPFILES)
{
my $tmpname=RES_DIR.'tmp'.int(rand(1000000000));
open (PAGE,">$tmpname") or make_error(S_NOTWRITE);
print PAGE $contents;
close PAGE;
rename $tmpname,$filename;
}
else
{
open (PAGE,">$filename") or make_error(S_NOTWRITE);
print PAGE $contents;
close PAGE;
}
}
sub build_thread_cache_all()
{
my ($sth,$row,@thread);
$sth=$dbh->prepare("SELECT num FROM ".SQL_TABLE." WHERE parent=0;") or make_error(S_SQLFAIL);
$sth->execute() or make_error(S_SQLFAIL);
while($row=$sth->fetchrow_arrayref())
{
build_thread_cache($$row[0]);
}
}
#
# Posting
#
sub post_stuff($$$$$$$$$$$$$$)
{
my ($parent,$name,$email,$subject,$comment,$file,$uploadname,$password,$nofile,$captcha,$admin,$no_captcha,$no_format,$postfix)=@_;
# get a timestamp for future use
my $time=time();
# check that the request came in as a POST, or from the command line
make_error(S_UNJUST) if($ENV{REQUEST_METHOD} and $ENV{REQUEST_METHOD} ne "POST");
if($admin) # check admin password - allow both encrypted and non-encrypted
{
check_password($admin,ADMIN_PASS);
}
else
{
# forbid admin-only features
make_error(S_WRONGPASS) if($no_captcha or $no_format or $postfix);
# check what kind of posting is allowed
if($parent)
{
make_error(S_NOTALLOWED) if($file and !ALLOW_IMAGE_REPLIES);
make_error(S_NOTALLOWED) if(!$file and !ALLOW_TEXT_REPLIES);
}
else
{
make_error(S_NOTALLOWED) if($file and !ALLOW_IMAGES);
make_error(S_NOTALLOWED) if(!$file and !ALLOW_TEXTONLY);
}
}
# check for weird characters
make_error(S_UNUSUAL) if($parent=~/[^0-9]/);
make_error(S_UNUSUAL) if(length($parent)>10);
make_error(S_UNUSUAL) if($name=~/[\n\r]/);
make_error(S_UNUSUAL) if($email=~/[\n\r]/);
make_error(S_UNUSUAL) if($subject=~/[\n\r]/);
# check for excessive amounts of text
make_error(S_TOOLONG) if(length($name)>MAX_FIELD_LENGTH);
make_error(S_TOOLONG) if(length($email)>MAX_FIELD_LENGTH);
make_error(S_TOOLONG) if(length($subject)>MAX_FIELD_LENGTH);
make_error(S_TOOLONG) if(length($comment)>MAX_COMMENT_LENGTH);
# check to make sure the user selected a file, or clicked the checkbox
make_error(S_NOPIC) if(!$parent and !$file and !$nofile and !$admin);
# check for empty reply or empty text-only post
make_error(S_NOTEXT) if($comment=~/^\s*$/ and !$file);
# get file size, and check for limitations.
my $size=get_file_size($file) if($file);
# find IP
my $ip=$ENV{REMOTE_ADDR};
#$host = gethostbyaddr($ip);
my $numip=dot_to_dec($ip);
# set up cookies
my $c_name=$name;
my $c_email=$email;
my $c_password=$password;
# check if IP is whitelisted
my $whitelisted=is_whitelisted($numip);
# process the tripcode - maybe the string should be decoded later
my $trip;
($name,$trip)=process_tripcode($name,TRIPKEY,SECRET,CHARSET);
# check for bans
ban_check($numip,$c_name,$subject,$comment) unless $whitelisted;
# spam check
spam_engine(
query=>$query,
trap_fields=>SPAM_TRAP?["name","link"]:[],
spam_files=>[SPAM_FILES],
charset=>CHARSET,
included_fields=>["field1","field2","field3","field4"],
) unless $whitelisted;
# check captcha
check_captcha($dbh,$captcha,$ip,$parent) if(ENABLE_CAPTCHA and !$no_captcha and !is_trusted($trip));
# proxy check
proxy_check($ip) if (!$whitelisted and ENABLE_PROXY_CHECK);
# check if thread exists, and get lasthit value
my ($parent_res,$lasthit);
if($parent)
{
$parent_res=get_parent_post($parent) or make_error(S_NOTHREADERR);
$lasthit=$$parent_res{lasthit};
}
else
{
$lasthit=$time;
}
# kill the name if anonymous posting is being enforced
if(FORCED_ANON)
{
$name='';
$trip='';
if($email=~/sage/i) { $email='sage'; }
else { $email=''; }
}
# clean up the inputs
$email=clean_string(decode_string($email,CHARSET));
$subject=clean_string(decode_string($subject,CHARSET));
# fix up the email/link
$email="mailto:$email" if $email and $email!~/^$protocol_re:/;
# format comment
$comment=format_comment(clean_string(decode_string($comment,CHARSET))) unless $no_format;
$comment.=$postfix;
# insert default values for empty fields
$parent=0 unless $parent;
$name=make_anonymous($ip,$time) unless $name or $trip;
$subject=S_ANOTITLE unless $subject;
$comment=S_ANOTEXT unless $comment;
# flood protection - must happen after inputs have been cleaned up
flood_check($numip,$time,$comment,$file);
# Manager and deletion stuff - duuuuuh?
# generate date
my $date=make_date($time,DATE_STYLE);
# generate ID code if enabled
$date.=' ID:'.make_id_code($ip,$time,$email) if(DISPLAY_ID);
# copy file, do checksums, make thumbnail, etc
my ($filename,$md5,$width,$height,$thumbnail,$tn_width,$tn_height)=process_file($file,$uploadname,$time) if($file);
# finally, write to the database
my $sth=$dbh->prepare("INSERT INTO ".SQL_TABLE." VALUES(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);") or make_error(S_SQLFAIL);
$sth->execute($parent,$time,$lasthit,$numip,
$date,$name,$trip,$email,$subject,$password,$comment,
$filename,$size,$md5,$width,$height,$thumbnail,$tn_width,$tn_height) or make_error(S_SQLFAIL);
if($parent) # bumping
{
# check for sage, or too many replies
unless($email=~/sage/i or sage_count($parent_res)>MAX_RES)
{
$sth=$dbh->prepare("UPDATE ".SQL_TABLE." SET lasthit=$time WHERE num=? OR parent=?;") or make_error(S_SQLFAIL);
$sth->execute($parent,$parent) or make_error(S_SQLFAIL);
}
}
# remove old threads from the database
trim_database();
# update the cached HTML pages
build_cache();
# update the individual thread cache
if($parent) { build_thread_cache($parent); }
else # must find out what our new thread number is
{
if($filename)
{
$sth=$dbh->prepare("SELECT num FROM ".SQL_TABLE." WHERE image=?;") or make_error(S_SQLFAIL);
$sth->execute($filename) or make_error(S_SQLFAIL);
}
else
{
$sth=$dbh->prepare("SELECT num FROM ".SQL_TABLE." WHERE timestamp=? AND comment=?;") or make_error(S_SQLFAIL);
$sth->execute($time,$comment) or make_error(S_SQLFAIL);
}
my $num=($sth->fetchrow_array())[0];
if($num)
{
build_thread_cache($num);
}
}
# set the name, email and password cookies
make_cookies(name=>$c_name,email=>$c_email,password=>$c_password,
-charset=>CHARSET,-autopath=>COOKIE_PATH); # yum!
# forward back to the main page
make_http_forward(HTML_SELF,ALTERNATE_REDIRECT);
}
sub is_whitelisted($)
{
my ($numip)=@_;
my ($sth);
$sth=$dbh->prepare("SELECT count(*) FROM ".SQL_ADMIN_TABLE." WHERE type='whitelist' AND ? & ival2 = ival1 & ival2;") or make_error(S_SQLFAIL);
$sth->execute($numip) or make_error(S_SQLFAIL);
return 1 if(($sth->fetchrow_array())[0]);
return 0;
}
sub is_trusted($)
{
my ($trip)=@_;
my ($sth);
$sth=$dbh->prepare("SELECT count(*) FROM ".SQL_ADMIN_TABLE." WHERE type='trust' AND sval1 = ?;") or make_error(S_SQLFAIL);
$sth->execute($trip) or make_error(S_SQLFAIL);
return 1 if(($sth->fetchrow_array())[0]);
return 0;
}
sub ban_check($$$$)
{
my ($numip,$name,$subject,$comment)=@_;
my ($sth);
$sth=$dbh->prepare("SELECT count(*) FROM ".SQL_ADMIN_TABLE." WHERE type='ipban' AND ? & ival2 = ival1 & ival2;") or make_error(S_SQLFAIL);
$sth->execute($numip) or make_error(S_SQLFAIL);
make_error(S_BADHOST) if(($sth->fetchrow_array())[0]);
# fucking mysql...
# $sth=$dbh->prepare("SELECT count(*) FROM ".SQL_ADMIN_TABLE." WHERE type='wordban' AND ? LIKE '%' || sval1 || '%';") or make_error(S_SQLFAIL);
# $sth->execute($comment) or make_error(S_SQLFAIL);
#
# make_error(S_STRREF) if(($sth->fetchrow_array())[0]);
$sth=$dbh->prepare("SELECT sval1 FROM ".SQL_ADMIN_TABLE." WHERE type='wordban';") or make_error(S_SQLFAIL);
$sth->execute() or make_error(S_SQLFAIL);
my $row;
while($row=$sth->fetchrow_arrayref())
{
my $regexp=quotemeta $$row[0];
make_error(S_STRREF) if($comment=~/$regexp/);
make_error(S_STRREF) if($name=~/$regexp/);
make_error(S_STRREF) if($subject=~/$regexp/);
}
# etc etc etc
return(0);
}
sub flood_check($$$$)
{
my ($ip,$time,$comment,$file)=@_;
my ($sth,$maxtime);
if($file)
{
# check for to quick file posts
$maxtime=$time-(RENZOKU2);
$sth=$dbh->prepare("SELECT count(*) FROM ".SQL_TABLE." WHERE ip=? AND timestamp>$maxtime;") or make_error(S_SQLFAIL);
$sth->execute($ip) or make_error(S_SQLFAIL);
make_error(S_RENZOKU2) if(($sth->fetchrow_array())[0]);
}
else
{
# check for too quick replies or text-only posts
$maxtime=$time-(RENZOKU);
$sth=$dbh->prepare("SELECT count(*) FROM ".SQL_TABLE." WHERE ip=? AND timestamp>$maxtime;") or make_error(S_SQLFAIL);
$sth->execute($ip) or make_error(S_SQLFAIL);
make_error(S_RENZOKU) if(($sth->fetchrow_array())[0]);
# check for repeated messages
$maxtime=$time-(RENZOKU3);
$sth=$dbh->prepare("SELECT count(*) FROM ".SQL_TABLE." WHERE ip=? AND comment=? AND timestamp>$maxtime;") or make_error(S_SQLFAIL);
$sth->execute($ip,$comment) or make_error(S_SQLFAIL);
make_error(S_RENZOKU3) if(($sth->fetchrow_array())[0]);
}
}
sub proxy_check($)
{
my ($ip)=@_;
my ($sth);
proxy_clean();
# check if IP is from a known banned proxy
$sth=$dbh->prepare("SELECT count(*) FROM ".SQL_PROXY_TABLE." WHERE type='black' AND ip = ?;") or make_error(S_SQLFAIL);
$sth->execute($ip) or make_error(S_SQLFAIL);
make_error(S_BADHOSTPROXY) if(($sth->fetchrow_array())[0]);
# check if IP is from a known non-proxy
$sth=$dbh->prepare("SELECT count(*) FROM ".SQL_PROXY_TABLE." WHERE type='white' AND ip = ?;") or make_error(S_SQLFAIL);
$sth->execute($ip) or make_error(S_SQLFAIL);
my $timestamp=time();
my $date=make_date($timestamp,DATE_STYLE);
if(($sth->fetchrow_array())[0])
{ # known good IP, refresh entry
$sth=$dbh->prepare("UPDATE ".SQL_PROXY_TABLE." SET timestamp=?, date=? WHERE ip=?;") or make_error(S_SQLFAIL);
$sth->execute($timestamp,$date,$ip) or make_error(S_SQLFAIL);
}
else
{ # unknown IP, check for proxy
my $command = PROXY_COMMAND . " " . $ip;
$sth=$dbh->prepare("INSERT INTO ".SQL_PROXY_TABLE." VALUES(null,?,?,?,?);") or make_error(S_SQLFAIL);
if(`$command`)
{
$sth->execute('black',$ip,$timestamp,$date) or make_error(S_SQLFAIL);
make_error(S_PROXY);
}
else
{
$sth->execute('white',$ip,$timestamp,$date) or make_error(S_SQLFAIL);
}
}
}
sub add_proxy_entry($$$$$)
{
my ($admin,$type,$ip,$timestamp,$date)=@_;
my ($sth);
check_password($admin,ADMIN_PASS);
# Verifies IP range is sane. The price for a human-readable db...
unless ($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ && $1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255) {
make_error(S_BADIP);
}
if ($type = 'white') {
$timestamp = $timestamp - PROXY_WHITE_AGE + time();
}
else
{
$timestamp = $timestamp - PROXY_BLACK_AGE + time();
}
# This is to ensure user doesn't put multiple entries for the same IP
$sth=$dbh->prepare("DELETE FROM ".SQL_PROXY_TABLE." WHERE ip=?;") or make_error(S_SQLFAIL);
$sth->execute($ip) or make_error(S_SQLFAIL);
# Add requested entry
$sth=$dbh->prepare("INSERT INTO ".SQL_PROXY_TABLE." VALUES(null,?,?,?,?);") or make_error(S_SQLFAIL);
$sth->execute($type,$ip,$timestamp,$date) or make_error(S_SQLFAIL);
make_http_forward(get_script_name()."?admin=$admin&task=proxy",ALTERNATE_REDIRECT);
}
sub proxy_clean()
{
my ($sth,$timestamp);
if(PROXY_BLACK_AGE == PROXY_WHITE_AGE)
{
$timestamp = time() - PROXY_BLACK_AGE;
$sth=$dbh->prepare("DELETE FROM ".SQL_PROXY_TABLE." WHERE timestamp<?;") or make_error(S_SQLFAIL);
$sth->execute($timestamp) or make_error(S_SQLFAIL);
}
else
{
$timestamp = time() - PROXY_BLACK_AGE;
$sth=$dbh->prepare("DELETE FROM ".SQL_PROXY_TABLE." WHERE type='black' AND timestamp<?;") or make_error(S_SQLFAIL);
$sth->execute($timestamp) or make_error(S_SQLFAIL);
$timestamp = time() - PROXY_WHITE_AGE;
$sth=$dbh->prepare("DELETE FROM ".SQL_PROXY_TABLE." WHERE type='white' AND timestamp<?;") or make_error(S_SQLFAIL);
$sth->execute($timestamp) or make_error(S_SQLFAIL);
}
}
sub remove_proxy_entry($$)
{
my ($admin,$num)=@_;
my ($sth);
check_password($admin,ADMIN_PASS);
$sth=$dbh->prepare("DELETE FROM ".SQL_PROXY_TABLE." WHERE num=?;") or make_error(S_SQLFAIL);
$sth->execute($num) or make_error(S_SQLFAIL);
make_http_forward(get_script_name()."?admin=$admin&task=proxy",ALTERNATE_REDIRECT);
}
sub format_comment($)
{
my ($comment)=@_;
# hide >>1 references from the quoting code
$comment=~s/>>([0-9\-]+)/>gt;$1/g;
my $handler=sub # fix up >>1 references
{
my $line=shift;
$line=~s!>gt;([0-9]+)!
my $res=get_post($1);
if($res) { '<a href="'.get_reply_link($$res{num},$$res{parent}).'" onclick="highlight('.$1.')">>>'.$1.'</a>' }
else { ">>$1"; }
!ge;
return $line;
};
if(ENABLE_WAKABAMARK) { $comment=do_wakabamark($comment,$handler) }
else { $comment="<p>".simple_format($comment,$handler)."</p>" }
# fix <blockquote> styles for old stylesheets
$comment=~s/<blockquote>/<blockquote class="unkfunc">/g;
# restore >>1 references hidden in code blocks
$comment=~s/>gt;/>>/g;
return $comment;
}
sub simple_format($@)
{
my ($comment,$handler)=@_;
return join "<br />",map
{
my $line=$_;
# make URLs into links
$line=~s{(https?://[^\s<>"]*?)((?:\s|<|>|"|\.|\)|\]|!|\?|,|,|")*(?:[\s<>"]|$))}{\<a href="$1"\>$1\</a\>$2}sgi;
# colour quoted sections if working in old-style mode.
$line=~s!^(>.*)$!\<span class="unkfunc"\>$1\</span\>!g unless(ENABLE_WAKABAMARK);
$line=$handler->($line) if($handler);
$line;
} split /\n/,$comment;
}
sub encode_string($)
{
my ($str)=@_;
return $str unless($has_encode);
return encode(CHARSET,$str,0x0400);
}
sub make_anonymous($$)
{
my ($ip,$time)=@_;
return S_ANONAME unless(SILLY_ANONYMOUS);
my $string=$ip;
$string.=",".int($time/86400) if(SILLY_ANONYMOUS=~/day/i);
$string.=",".$ENV{SCRIPT_NAME} if(SILLY_ANONYMOUS=~/board/i);
srand unpack "N",hide_data($string,4,"silly",SECRET);
return cfg_expand("%G% %W%",
W => ["%B%%V%%M%%I%%V%%F%","%B%%V%%M%%E%","%O%%E%","%B%%V%%M%%I%%V%%F%","%B%%V%%M%%E%","%O%%E%","%B%%V%%M%%I%%V%%F%","%B%%V%%M%%E%"],
B => ["B","B","C","D","D","F","F","G","G","H","H","M","N","P","P","S","S","W","Ch","Br","Cr","Dr","Bl","Cl","S"],
I => ["b","d","f","h","k","l","m","n","p","s","t","w","ch","st"],
V => ["a","e","i","o","u"],
M => ["ving","zzle","ndle","ddle","ller","rring","tting","nning","ssle","mmer","bber","bble","nger","nner","sh","ffing","nder","pper","mmle","lly","bling","nkin","dge","ckle","ggle","mble","ckle","rry"],
F => ["t","ck","tch","d","g","n","t","t","ck","tch","dge","re","rk","dge","re","ne","dging"],
O => ["Small","Snod","Bard","Billing","Black","Shake","Tilling","Good","Worthing","Blythe","Green","Duck","Pitt","Grand","Brook","Blather","Bun","Buzz","Clay","Fan","Dart","Grim","Honey","Light","Murd","Nickle","Pick","Pock","Trot","Toot","Turvey"],
E => ["shaw","man","stone","son","ham","gold","banks","foot","worth","way","hall","dock","ford","well","bury","stock","field","lock","dale","water","hood","ridge","ville","spear","forth","will"],
G => ["Albert","Alice","Angus","Archie","Augustus","Barnaby","Basil","Beatrice","Betsy","Caroline","Cedric","Charles","Charlotte","Clara","Cornelius","Cyril","David","Doris","Ebenezer","Edward","Edwin","Eliza","Emma","Ernest","Esther","Eugene","Fanny","Frederick","George","Graham","Hamilton","Hannah","Hedda","Henry","Hugh","Ian","Isabella","Jack","James","Jarvis","Jenny","John","Lillian","Lydia","Martha","Martin","Matilda","Molly","Nathaniel","Nell","Nicholas","Nigel","Oliver","Phineas","Phoebe","Phyllis","Polly","Priscilla","Rebecca","Reuben","Samuel","Sidney","Simon","Sophie","Thomas","Walter","Wesley","William"],
);
}
sub make_id_code($$$)
{
my ($ip,$time,$link)=@_;
return EMAIL_ID if($link and DISPLAY_ID=~/link/i);
return EMAIL_ID if($link=~/sage/i and DISPLAY_ID=~/sage/i);
return resolve_host($ENV{REMOTE_ADDR}) if(DISPLAY_ID=~/host/i);
return $ENV{REMOTE_ADDR} if(DISPLAY_ID=~/ip/i);
my $string="";
$string.=",".int($time/86400) if(DISPLAY_ID=~/day/i);
$string.=",".$ENV{SCRIPT_NAME} if(DISPLAY_ID=~/board/i);
return mask_ip($ENV{REMOTE_ADDR},make_key("mask",SECRET,32).$string) if(DISPLAY_ID=~/mask/i);
return hide_data($ip.$string,6,"id",SECRET,1);
}
sub get_post($)
{
my ($thread)=@_;
my ($sth);
$sth=$dbh->prepare("SELECT * FROM ".SQL_TABLE." WHERE num=?;") or make_error(S_SQLFAIL);
$sth->execute($thread) or make_error(S_SQLFAIL);
return $sth->fetchrow_hashref();
}
sub get_parent_post($)
{
my ($thread)=@_;
my ($sth);
$sth=$dbh->prepare("SELECT * FROM ".SQL_TABLE." WHERE num=? AND parent=0;") or make_error(S_SQLFAIL);
$sth->execute($thread) or make_error(S_SQLFAIL);
return $sth->fetchrow_hashref();
}
sub sage_count($)
{
my ($parent)=@_;
my ($sth);
$sth=$dbh->prepare("SELECT count(*) FROM ".SQL_TABLE." WHERE parent=? AND NOT ( timestamp<? AND ip=? );") or make_error(S_SQLFAIL);
$sth->execute($$parent{num},$$parent{timestamp}+(NOSAGE_WINDOW),$$parent{ip}) or make_error(S_SQLFAIL);
return ($sth->fetchrow_array())[0];
}
sub get_file_size($)
{
my ($file)=@_;
my (@filestats,$size);
@filestats=stat $file;
$size=$filestats[7];
make_error(S_TOOBIG) if($size>MAX_KB*1024);
make_error(S_TOOBIGORNONE) if($size==0); # check for small files, too?
return($size);
}
sub process_file($$$)
{
my ($file,$uploadname,$time)=@_;
my %filetypes=FILETYPES;
# make sure to read file in binary mode on platforms that care about such things
binmode $file;
# analyze file and check that it's in a supported format
my ($ext,$width,$height)=analyze_image($file,$uploadname);
my $known=($width or $filetypes{$ext});
make_error(S_BADFORMAT) unless(ALLOW_UNKNOWN or $known);
make_error(S_BADFORMAT) if(grep { $_ eq $ext } FORBIDDEN_EXTENSIONS);
make_error(S_TOOBIG) if(MAX_IMAGE_WIDTH and $width>MAX_IMAGE_WIDTH);
make_error(S_TOOBIG) if(MAX_IMAGE_HEIGHT and $height>MAX_IMAGE_HEIGHT);
make_error(S_TOOBIG) if(MAX_IMAGE_PIXELS and $width*$height>MAX_IMAGE_PIXELS);
# generate random filename - fudges the microseconds
my $filebase=$time.sprintf("%03d",int(rand(1000)));
my $filename=IMG_DIR.$filebase.'.'.$ext;
my $thumbnail=THUMB_DIR.$filebase."s.jpg";
$filename.=MUNGE_UNKNOWN unless($known);
# do copying and MD5 checksum
my ($md5,$md5ctx,$buffer);
# prepare MD5 checksum if the Digest::MD5 module is available
eval 'use Digest::MD5 qw(md5_hex)';
$md5ctx=Digest::MD5->new unless($@);
# copy file
open (OUTFILE,">>$filename") or make_error(S_NOTWRITE);
binmode OUTFILE;
while (read($file,$buffer,1024)) # should the buffer be larger?
{
print OUTFILE $buffer;
$md5ctx->add($buffer) if($md5ctx);
}
close $file;
close OUTFILE;
if($md5ctx) # if we have Digest::MD5, get the checksum
{
$md5=$md5ctx->hexdigest();
}
else # otherwise, try using the md5sum command
{
my $md5sum=`md5sum $filename`; # filename is always the timestamp name, and thus safe
($md5)=$md5sum=~/^([0-9a-f]+)/ unless($?);