#!/usr/bin/perl
###########################################################################
# List of Updates:
#   $year += 1900; close<HANDLE> -> close(HANDLE);
#   $pcode : Time dependent passcode to avoid automatic input (2008/1/28)
###########################################################################

$Error = "";
$post_path="index.html";
$dot="<img src=ball2.gif width=14 height=14>";
$HEADER="Content-type: text/html\n\n";

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
if ($year<1900) {$year += 1900;}
$month=($mon+1);
$date="$year³â$month¿ù$mdayÀÏ";
$time="$hour½Ã$minºÐ";

# pcode : Time dependent passcode to avoid automatic input (2008/1/28)
# p*43189669*832429 = p (mod n) where n=60*60*24*365=2^7*3^3*5^3*73
$pcode = time() % (60*60*24*365); # 1 (Year)  = 31536000 (Second)


# ---------------------------------------------------------------------------
if (!defined($ENV{'CONTENT_LENGTH'})) {
$pcode = $pcode*43189669%(60*60*24*365) + 40000000; # 400000000~71535999
print $HEADER; print <<EOF;
<head><title>ÃßÃµ Package ÀÛ¼ºÇÏ±â</title></head>
<body bgcolor=#ffffff>

<center><font color="darkblue"> <B>
ÃßÃµÇÏ´Â Scientific Software Package¸¦ Àû¾î ÁÖ¼¼¿ä. 
</B></font></center>
<br>
<font><b> ±ÛÀ» ¾²½ÅÈÄ¿¡ <font color="red">Send </font>¹öÆ°À»
´©¸£¸é µË´Ï´Ù.</b></font><br>

<form method=post action=post.cgi>
<table>
  <tr><td><B>Package Á¾·ù</B> <td>
     <input type=radio name="class" value="General-Purpose">General-Purpose
     <input type=radio name="class" value="Documentation">Document
     <input type=radio name="class" value="Misc">Misc
  <br>
     <input type=radio name="class" value="Sci-Comp-Pkg" checked>Sci-Comp-Pkg :
     <A href=http://gams.nist.gov/serve.cgi/Class/ target=x>GAMS_Class</A>
     <input type=text name="gams" value="" size=1>
  <tr><td><B>Package ÀÌ¸§</B>
         <td> <input type=text name="title" size=30>
  <tr><td><b>Package URL</b>
         <td><input type=text value="http://www." name=home size=60>
  <tr><td><b>Package¿¡ ´ëÇÑ<br>°£´ÜÇÑ ¼³¸í</b><p>&nbsp
         <td><textarea name="desc" cols=60 rows=4></textarea>
  <tr><td><b>Package Status</b> <td>
     <input type=radio name="status" value="Commercial">Commercial
     <input type=radio name="status" value="Public-Src" checked>Public-Src
     <input type=radio name="status" value="Src+Binary">Src+Binary
     <input type=radio name="status" value="Binary_only">Binary_only
  <tr><td><b>Package ±¸¼º<br>°ü·Ã Remark</b><p>&nbsp
         <td><textarea name="install" cols=60 rows=4></textarea>
  <tr><td><b>µî·ÏÀÎÁõÄÚµå</b>\n<td><input type=text name=passcode size=8>
  (ÀÌÈ­¿©´ë ¿ÜºÎÁ¢¼ÓÀÚ´Â <b>$pcode</b>¸¦ ÀÔ·ÂÇÏ½Ê½Ã¿À.)
  <tr><td><td><input type=submit value=Send>
  <input type=reset value=Clear>
</table>
</form>
<p>
</body>

EOF
exit;
}
# ---------------------------------------------------------------------------


&ReadParse;

($class)=($in{'class'}=~/^\s*(\S.+\S)\s*$/);
if ($class eq "Sci-Comp-Pkg") {$class = $class . ":" . substr($in{'gams'},0,1);}
($title)=($in{'title'}=~/^\s*(\S.+\S)\s*$/); $Title=uc($title);
($home)=($in{'home'}=~/^\s*(\S+)\s*$/);
$desc=$in{'desc'}; $desc =~ tr/\001-\020/ /;
$status=$in{'status'};
$install=$in{'install'}; $install =~ tr/\001-\020/ /;

$Error = "";
$Nclass="Package Á¾·ù¸¦"; $Ntitle="Package ÀÌ¸§À»"; $Nhome="Package URLÀ»";
if ($class eq "") { $Error .= "<h3>$Nclass ¾²Áö ¾ÊÀ¸¼Ì½À´Ï´Ù.</h3>\n"; }
if ($title eq "") { $Error .= "<h3>$Ntitle ¾²Áö ¾ÊÀ¸¼Ì½À´Ï´Ù.</h3>\n"; }

@URL = split(/\//,$home);
if ($home eq "" || $home eq "http://" || $#URL < 2 || $URL[3] eq "~") {
                   $Error .= "<h3>$Nhome ¾²Áö ¾ÊÀ¸¼Ì½À´Ï´Ù.</h3>\n";
} else {
   @HOST = split(/[.]/,$URL[2]);
   if ( $#HOST<1 || ($#HOST==1 && length($HOST[1])!=3)
        || (gethostbyname($URL[2]))[0] eq "" ) {
       $Error .= "<h3>Site Host NAME($URL[2])À» È®ÀÎÇÒ ¼ö ¾ø½À´Ï´Ù.</h3>";
   }
}

$passcode = ($in{'passcode'}-40000000) * 832429 % (60*60*24*365);
if ($host_ip !~ /203.255/ and $host_ip !~/192.168/ and
   $pcode+($passcode<=$pcode?0:60*60*24*365) > $passcode+3600) {
   $Error .= "<h3>µî·ÏÀÎÁõ¹øÈ£¸¦ È®ÀÎÇØ ÁÖ½Ê½Ã¿À.</h3>\n";
}


if ( $Error ne "" ) {
   print "$HEADER $Error";
   print "Back Å°¸¦ ´­·¯ ´Ù½Ã ÀÔ·ÂÇÏ½Ê½Ã¿À.\n";
   exit;
}

open(File,"$post_path") || die "$post_path ¿­±â ½ÇÆÐ: $!\n";
@lines=<File>;
close File;

open(New,">$post_path") || die "$post_path ¿­±â ½ÇÆÐ: $!\n";
$found=0;
for ($i=0; $i<=$#lines; $i++) {
   $Tfound = ($lines[$i] =~ /<dt>.*<B>(.*)<\/B>/) ? uc($1) : "" ;

   if ($lines[$i] =~ /<!--begin:$class-->/) {
      print New "$lines[$i]";
      $found=1;
   } elsif ($found==1 && $Tfound eq $Title) {
      print New "<dt>$dot <font color=darkblue><a href=$home ",
           "target=_top><B>$title</B></a></font> $desc",
           "<dd>$status: $install\n";
      $found=0;
   } elsif ($found==1 && ($Tfound gt $Title || $lines[$i]=~/<\/dl>/)) {
      print New "<dt>$dot <font color=darkblue><a href=$home ",
           "target=_top><B>$title</B></a></font> $desc",
           "<dd>$status: $install\n";
      print New "$lines[$i]";
      $found=0;
   } else {
      print New "$lines[$i]";
   }
}
close(New);

open(Re,"$post_path") || die "È­ÀÏ ¿­±â ½ÇÆÐ\n";
@all=<Re>;
close(Re);
print "Content-Type: text/html\n\n";
print "<hr>\n"; 
print @all;

sub ReadParse{
   $host_ip = $ENV{'REMOTE_ADDR'};
   $host_name = $ENV{'REMOTE_HOST'};
   read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
   @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;
      $in{$name}=$value;
   }
}
