#! /usr/local/bin/perl
#
#
#1997.9.14
#ՂƁ@΂@䂢Ɓ@
#@@@@@@@@@Since  1996
#
$|=1;
&init;&jikan;
&readlog;

if ($ENV{'REQUEST_METHOD'} eq "POST") {
        ($id, $dmy) = split(/\t/, @lines[0]);
        $id=sprintf("%03d",++$id);
        &decode;
        &write if($subject && $comment);
}
&getdecode;


&html;
&ended();
exit;

sub init{       #ݒȂ
$url = 'http://www.big.or.jp/~vampire/Junk/upload.cgi';#ݒ肵ȂƓeł܂B
$log_file = 'upload.dat';#p[~bV666 or 606
$title = '䂢́[';
$body='<BODY BGCOLOR="#A1FE9F" TEXT="#000000" LINK="#ff0000" VLINK="#ff0000" ALINK="#FF0000">';
$max = 50;#O̍ős
$host = $ENV{'REMOTE_HOST'};if($host=~/bmc/){$host="ppp753.master.ad.jp";}
$metacode = '<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=x-sjis">';#SJISF
####ANZXgpȂA#B
#$kill_file = './kill.dat';#ANZXp
#open(DB,"$kill_file") || die "Cannot Open Log File $kill_file: $!";
#       @lines2 = <DB>; close(DB);
#foreach $line (@lines2) {
#       next if(length($line)<4);       chop $line;
#       if( $host=~/$line/ ){   print "Status: 204\n\n";        exit;   }
#}
####ANZX܂
#bNt@CgpȂA#B
#$lockfile = './lock/lock-file.lock';
#$retry = 5;
#while (!symlink(".", $lockfile)) {
#if (--$retry <= 0) { print "Status: 204\n\n"; unlink($lockfile); exit; }
#sleep(1);
#}
#bNt@C܂
}#init END

sub ended{      #unlinkexits߂̂́B
#       unlink($lockfile);#bNt@CgpȂA#B
print "Content-type: text/plain\n\n $_[0] \n" if($_[0]);#̍s͍폜łB
exit;
}#ended END

sub write{
$subject=~s/</&lt;/;$comment=~s/</&lt;/;#^O֎~
$subject=~s/\t/ /;$comment=~s/\t/ /;
$subject='???' unless($subject);$comment='?????' unless($comment);
$value="$id\t$type\t$subject\t$comment\t$host\t$ENV{'HTTP_USER_AGENT'}\t$date\t\n";
&writelog;
}

sub writelog{   #$value@linesɉ̂A
        if(@lines > $max-1){#ő𒴂폜܂B
                $del= pop(@lines);
                ($id, $type) = split(/\t/, $del);
                unlink("./image/$id.$type");
        }
        unshift( @lines,$value);
        open(DB,">$log_file") || &ended('$log_file write error');
                eval 'flock(DB,2);';
                seek(DB,0,0);   print DB @lines;
                eval 'flock(DB,8);';
        close(DB);
}#writelog END

sub readlog{    #z@linesɃOǂݍ
        open(DB,"$log_file") || &ended('$log_file open error');
                seek(DB,0,0);  @lines = <DB>;   close(DB);
}#readlog END

sub decode{
return if($ENV{'CONTENT_LENGTH'} > 1050000);#t@C̃TCY̐܂B100k炢܂ŁB
return unless($ENV{'HTTP_REFERER'} =~/$url/);

while(<STDIN>){
        last if($_=~/^\r\n/);
        $bound = $_ if($_=~/^--/);
        $type = 'gif' if($_=~/^Content-Type:.*gif/);
        $type = 'jpg' if($_=~/^Content-Type:.*jpeg/);
        $type = 'txt' if($_=~/^Content-Type:.*text/);
}
$bound=~s/\r\n//;
$type = 'dat' if($type eq '');
open(DB,">./image/$id.$type") || &ended('image dir open error');#imagefBNg777Őݒu܂B
        while(<STDIN>){
                last if($_=~/^$bound/);
                print DB $_;
        }
close (DB);
while(<STDIN>){#蔲fR[h1
                last if($_=~/^$bound/);
                next if($_=~/^\r\n/);
                next if($_=~/^Content/);
                $subject .=$_;  $subject=~s/\r\n//;
}
while(<STDIN>){#蔲fR[h2
                last if($_=~/^$bound/);
                next if($_=~/^\r\n/);
                next if($_=~/^Content/);
                $comment .=$_;  $comment=~s/\r\n//;
}

}#decode END

sub html{
print "Content-type: text/html\n\n";
print <<"_HTML_";
<HTML><HEAD><TITLE>$title</TITLE>$metacode</HEAD>
$body
NetscapełȂ̃p\\\R\ɂKGIF/JPEGt@C𑗂Ă݂ĉˁB<BR>
eLXgt@C邩܂B(98/1/5)<BR>
Netscapeł́ABrowse{^Ńt@C𑗂t@CIł܂B<BR>
]摜͉L̃TCgŊς邱Ƃł܂B<BR>
ӁF܂ɑ傫ȉ摜t@C͓]ł܂B<BR>
(Netscape2.02AIEł̍쓮mFłĂ܂B)<BR>
@\\\]̂߂ɁAuEUzXgt\\Ă܂B<HR>
<FORM METHOD="POST" ENCTYPE="multipart/form-data" ACTION="upload.cgi" >
摜t@C(NetscapeOnly?!):<INPUT TYPE=file  SIZE="10" NAME="filedata"><BR>
^Cg:<INPUT TYPE=text  SIZE="20" NAME="title"><BR>
Rg:<INPUT TYPE=text  SIZE="80" NAME="comment" VALUE="$comment"><BR>
<INPUT TYPE=submit VALUE="摜𑗂"><INPUT TYPE=reset VALUE="Zbg"></FORM>
<HR>
Page:
_HTML_
$total = @lines;
$count = int(($total-1)/$lmax);
for($i=0;$i<=$count;$i++){
        if($i eq $page){ print "[<b>$i</b>] \n";                }else{
                print "[<A HREF=\"./upload.cgi?page=$i&lm=$lmax\">$i</A>] \n";
        }
}#for
print "<HR>\n";
(@lines < $lmax) || (@lines = @lines[$page2 .. $page2+$lmax-1]);
foreach(@lines){
        ($id, $type,$subject,$comment,$host,$brauza,$date) = split(/\t/, $_);
        next unless($id);
        print qq![<A HREF="./image/$id.$type" target="new">$subject</A>]:$comment<font size=2 color="#888888">
($host $date $brauza)</font><BR>\n!;
}
print "<HR><H5 ALIGN=right><A HREF=\"http://www.cup.com/yui/\">䂢́[</A></H5></BODY></HTML>\n";
}#html END

sub jikan{#$dateɎ܂B
        $ENV{'TZ'}   = 'JST-9';$times = time;
        ($sec,$min,$hour,$mday,$month,$year,$wday,$yday,$isdst) = localtime($times);
        $min = "0$min" if ($min < 10);  $month++;
        $youbi = ('','','','','','','y') [$wday];
        $date = "$month/$mday($youbi)$hour:$min";
}#jikan END

sub getdecode{  #ʓIȃfR[hϐւ̑
$buffer = $ENV{'QUERY_STRING'};
@pairs = split(/&/,$buffer);
        foreach $pair (@pairs) {
                ($name, $value) = split(/=/, $pair);
                $value =~ tr/+/ /;
                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
                $FORM{$name} = $value;
        }
$page = $FORM{'page'};   
$page=0 unless($page);
$lmax = $FORM{'lm'};
$lmax=10 unless($lmax);#y[Wɂǂꂾ\邩H
$page2 = $page*$lmax;
}#getdecode END

__END__
