• 一段垃圾代码,自己写的(发送图书馆新书通告) zhumao


    #!/usr/local/bin/perl

    print "content-type: text/html\n\n";
    use CGI;
    use MIME::QuotedPrint;
    use HTML::Entities;
    use Mail::Sendmail;

    $req = new CGI;
    #new version
    #print $req->param("action");
    #print $req->param("fileID");
    #print $req->param("flag");
    &html_css;
    $debug=0;
    ###################update next three lines#######################
    $s_mail = 'test@hotmail.com';
    $mail_subject = 'Monthly Notification (Title recommendation)';
    $mypath = '/cgi-bin';
    #################################################################
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    if($debug == 1)
    {
      print "CurrentLine:<font color=red>";
      print __LINE__;
      print "</font><br>";

      print "\$buffer's length:\t".length($buffer)."\$buffer:$buffer<br>";
    }

    if(length($buffer) > 0)
    {
     @pairs = split(/&/, $buffer);
     foreach $pair (@pairs)
     {
       ($query_name, $query_value) = split(/=/, $pair);
       $query_value =~ tr/+/ /;
       $query_value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
       $query_name =~ tr/+/ /;
       $query_name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

       $DataForm{$query_name} = $query_value;
     }
    }
    $buffer = $ENV{QUERY_STRING};
    @pairs = split(/&/, $buffer);
    foreach $pair (@pairs)
    {
     ($query_name, $query_value) = split(/=/, $pair);
     $query_value =~ tr/+/ /;
     $query_value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
     $query_name =~ tr/+/ /;
     $query_name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

     $FORM{$query_name} = $query_value;
     if($debug == 1)
     {
        print "CurrentLine:<font color=red>";
      print __LINE__;
        print "</font><br>";

      print $query_name."---".$query_value."<br>";
      print "\$FORM{\$query_name} = ".$FORM{$query_name}."<br>";
     }
    }
    &title;
    if($FORM{'action'} eq '')
    {
     $curtime = &mytime('dmy');
     print <<HTML_start;
      <center>
      <TABLE border=0>
      <tr><td>&nbsp;</td></tr>
      <tr><td>
      <li>You need upload one file to library's server.
      <li>Second, click "Send mail". All things have been done!
      <li>Check logfile -- check the error messages.
      <li>Check result --check the mails that you have sent.
      <li>Edit staff list -- update your staff's list
      <li>Back -- back to first page.
      </td></tr>
      <tr><td>&nbsp;</td></tr>
      <tr><td align=right>
      $curtime
      </td></tr>
      </table>
      </center>
    HTML_start
    }
    if($FORM{'action'} eq 'updatestaff' and $FORM{'flag'} ne 'true')
    {
     print <<HTML_updatestaff;
     <center>
     <table border=0>
     <form action="$mypath/index.pl?action=updatestaff&flag=true" method="post">
     <tr><td>Staff code:</td><td><input type="text" name="staffcode" value="" size="20"></td></tr>
     <tr><td>Staff name:</td><td><input type="text" name="staffname" value="" size="20"></td></tr>
     <tr><td>Add staff:</td><td align=left><input type="radio" name="newcmd" checked value="addline"></td></tr>
     <tr><td>Delete one:</td><td align=left><input type="radio" name="newcmd" value="delete"></td></tr>
     <tr><td colspan=2 align=center>
     <input type="submit" value="Continue"></td></tr>
     </form>
     </center>
    HTML_updatestaff
    }
    if($FORM{'action'} eq 'updatestaff' and $FORM{'flag'} eq 'true')
    {
     $staff_file="./staffs/staff.list";
     open(IN,$staff_file) or &error("Can not open file $staff_file!");
     @stafftmplist = <IN>;
     close(IN);
      if($debug == 1)
     {
      print "CurrentLine:<font color=red>";
      print __LINE__;
      print "</font><br>";
      print '$req->param("newcmd")='.$req->param("newcmd")."<br>";
      print '$req->param("staffcode")='.$req->param("staffcode")."<br>";
      print '$req->param("staffname")='.$req->param("staffname")."<br>";
     }
     if($req->param("newcmd") eq 'addline')
     {
      if(length($req->param("staffcode")) == 5 and length($req->param("staffname")) > 0)
      {
       splice(@stafftmplist,-1,0,$req->param("staffcode")."#".$req->param("staffname")."\@ied.edu.hk#\n");
       open(DAT,">$staff_file") || die("Cannot Open File $staff_file");
       print DAT @stafftmplist;
       close(DAT);

       print "<center><font color=blue size=5>Add staff success!</font></center>";
      }
      else
      {
       print "<center><font color=red size=5>Add staff failure. Please check your source.</font></center>";
      }
     }
     if($req->param("newcmd") eq 'delete')
     {
      $index=0;
      $moved_flag = 0;
      foreach $staff_s (@stafftmplist)
      {
       @staff_s_array = split('#',$staff_s);
       if($staff_s_array[0] eq $req->param("staffcode"))
       {
        splice(@stafftmplist,$index,1);
        
        open(DAT,">$staff_file") || die("Cannot Open File $staff_file");
        print DAT @stafftmplist;
        close(DAT);
        print "<center>The staff <font color=red size=5>".$req->param("staffcode")."</font> has been moved!</center>";
        $moved_flag = 1;
       }
       $index++;
      }
      if($moved_flag == 0)
      {
       print "<center><font color=red size=5>Not found the staff</font></center>";
      }
     }
    }
    if($FORM{'action'} eq 'checklog')
    {
     print "<center>";
     opendir(DIR,"./log");
     foreach (readdir(DIR))
     {
      if($_ ne '.' and $_ ne '..')
      {
         print "$_\t<a href=$mypath/index.pl?action=logshow&fileID=$_>Show Content</a>\t<a href=$mypath/index.pl?action=logdelete&fileID=$_>Delete the file</a><br>";
      }
     }
     closedir DIR;
     print "</center>";
    }
    if($FORM{'action'} eq 'logshow')
    {
      open(IN,"./log/$FORM{'fileID'}") or &error("Open file ./log/$FORM{'fileID'} error!<br>");
      while($line = <IN>)
      {
        #$line =~ s/</&lt;/g;
        #$line =~ s/>/&gt;/g;
        #$line =~ s/\n/<BR>/g;
        print $line;
      }
      close(IN);
    }
    if($FORM{'action'} eq 'logdelete')
    {
     unlink("./log/$FORM{'fileID'}") or &error("Delete file ./log/$FORM{'fileID'} error!<br>");
     print "<center>The file $FORM{'fileID'} has been deleted!<br>";
     print "<a href=$mypath/index.pl?action=checklog>Back ..</a></center>";
    }
    if($FORM{'action'} eq 'checktemp')
    {
      print "<center>";
      opendir(DIR,"./tmp");
     print "<table border=0>";
      foreach (readdir(DIR))
      {
        if($_ ne '.' and $_ ne '..')
        {
          print "<tr><td>$_</td><td><a href=$mypath/index.pl?action=tmpshow&fileID=$_>Show Content</a></td><td><a href=$mypath/index.pl?action=tmpdelete&fileID=$_>Delete the file</a></td><td><a href=$mypath/index.pl?action=sendmail&fileID=$_>Send the file</a></td></tr>";
        }
      }
     print "</table>";
      closedir DIR;
      print "</center>";
    }
    if($FORM{'action'} eq 'tmpshow')
    {
      open(IN,"./tmp/$FORM{'fileID'}") or &error("Open file ./tmp/$FORM{'fileID'} error!<br>");
      while($line = <IN>)
      {
       $line =~ s/</&lt;/g;
       $line =~ s/>/&gt;/g;
       $line =~ s/\n/<BR>/g;
       print $line;
      }
      close(IN);
    }
    if($FORM{'action'} eq 'tmpdelete')
    {
     unlink("./tmp/$FORM{'fileID'}") or &error("Delete file ./tmp/$FORM{'fileID'} error!<br>");
     print "<center>The file $query_value[1] has been deleted!<br>";
     print "<a href=$mypath/index.pl?action=checktemp>Back ..</a></center>";
    }

    if($FORM{'action'} eq 'datadelete')
    {
     unlink("./data/$FORM{'fileID'}") or &error("Delete file ./data/$FORM{'fileID'} error!<br>");
     print "<center>The file $FORM{'fileID'} has been deleted!<br>";
     print "<a href=$mypath/index.pl?action=checkdata>Back ..</a></center>";
    }
    if($FORM{'action'} eq 'checkdata')
    {
     print "<center>";
     opendir(DIR,"./data");
     foreach (readdir(DIR))
     {
      if($_ ne '.' and $_ ne '..')
      {
        print "$_\t<a href=$mypath/index.pl?action=datashow&fileID=$_>Show Content</a>\t<a href=$mypath/index.pl?action=datadelete&fileID=$_>Delete the file</a><br>";
      }
     }
     closedir DIR;
     print "</center>";
    }
    if($FORM{'action'} eq 'datashow')
    {
     open(IN,"./data/$FORM{'fileID'}") or &error("Open file ./data/$FORM{'fileID'} error!<br>");
      while($line = <IN>)
     {
      #$line =~ s/</&lt;/g;
      #$line =~ s/>/&gt;/g;
      #$line =~ s/\n/<BR>/g;
      print $line;
     }
     close(IN);
    }

    if($FORM{'action'} eq 'upload' and $FORM{'upload'} ne 'true')
    {
     print <<HTML_upload;
     <html>
     <body>
     <center>
     <form method="POST" action="$mypath/index.pl?action=upload&upload=true" ENCTYPE="multipart/form-data">
     select one file :
     <input type="file" name="FILE1">
     <br>
     <input type="submit" value="Upload!">
     </form>
     </center>
     </body>
     </html>
    HTML_upload
    }
    if($FORM{'action'} eq 'upload' and $FORM{'upload'} eq 'true')
    {
     my $file = $req->param("FILE1");
     my $fileName = $file;
     if ($fileName =~ /([^\/\\]+)$/)
     {
       $fileName="$1";
     }
     $fileName =~ s/\s+//g;
     open (OUTFILE, ">./tmp/$fileName");
     while (my $bytesread = read($file, my $buffer, 1024))
     {
      print OUTFILE $buffer;
     }
     close (OUTFILE);
     print "<center>Upload $fileName success!</center>";
    }
    if($FORM{'action'} eq 'sendmail' and length($FORM{'fileID'}) > 0 )
    {
     $userfile="./staffs/staff.list";
     $sourcefile="./tmp/$FORM{'fileID'}";
     if($debug == 1)
     {
      print "CurrentLine:<font color=red>";
      print __LINE__;
      print "</font><br>";
      print "\$sourcefile=$sourcefile<br>";
     }
     $file_prefix= &mytime('dmyhms');

     open(USER,$userfile) || &error("Could not open file $userfile");
     open(IN,$sourcefile) || &error("Could not open file $sourcefile");
     $testfile=">./data/".$file_prefix.".list";
     $logfile=">./log/".$file_prefix.".log";
     
     open(OUT,$testfile) || &error("Could not open file $testfile");
     open(LOG,$logfile) || &error("Could not open file $logfile");
     
     @userlists=<USER>;
     $i=0;
     $staff_index=0;
     while($line=<IN>)
     {
        $lists[$i]=$line;
      if($debug == 1)
      {
        print "CurrentLine:<font color=red>";
        print __LINE__;
        print "</font><br>";
       print "$line"."'s length:".length($line)."<br>";
      }
        if(length($line) == 1 && $i > 3)
        {
          ($location,$classnum,$bookname,@staffs)=&check(@lists);

          $pages="<tr><td>".$location."<br>".$classnum."<br>".$bookname."<br>";
          foreach $staff_tmp1 (@staffs)
          {
        $staff_tmp1 = substr($staff_tmp1,0,5);
           $mail_list[$staff_index]= $staff_tmp1."#####".$pages . $staff_tmp1 . ".</td></tr>\n";
           $staff_index++;
          }
         
          $location='';
          $classnum='';
          $bookname='';
          $index=0;
          $counter=@staffs;
          while($index<=$counter)
          {
            shift(@staffs);
            $index++;
          }
          $index=0;
          $counter=@lists;
          while($index<=$counter)
          {
            shift(@lists);
            $index++;
          }
          $i=0;
        }
        $i++;
     }
      $pages_title='<table border=0><tr><td align=center><strong>';
      $pages_title.='Monthly Notification (Title recommendation)';
      $pages_title.='</strong></td></tr><tr><td>&nbsp;</td></tr>';
      $pages_title.='<tr><td>The library materials that you have recommended are now available on shelves.</td></tr>';
      $pages_title.='<tr><td>Thank you for your recommendation.</td></tr>';
      $pages_title.='<tr><td>&nbsp;</td><tr><td align=right>Library&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>';
      $pages_title.='<tr><td align=right>';
      $pages_title.=&mytime('dmy');
      $pages_title.='</td></tr>';
      $pages_title.='<tr><td>&nbsp;</td></tr>';
      $pages_title.='<tr><td>&nbsp;</td></tr>';
      $pages_title.='<tr><td>&nbsp;</td></tr>';
      $pages_title.='<tr><td>&nbsp;</td></tr>';

     $k=0;
     foreach $j(@userlists)
     {
      ($code) = split("#",$j);
      $found_flag=0;
      foreach $i(@mail_list)
      {
       if(substr($i,0,5) eq $code)
       {
        $new_maillist[$k]=$i;
        $found_flag=1;
        $k++;
       }
      }
      if($found_flag == 0)
      {
       print LOG $code."Not found!<br>";
      }
      else
      {
       $found_flag = 0;
      }
     }
     $tmp='';
     $i=0;
     $new_maillist[@new_maillist]="#################";
     foreach (@new_maillist)
     {
      $first_str=substr($new_maillist[$i],0,5);
      $second_str=substr($new_maillist[$i+1],0,5);
      $code=$first_str;
      chomp($first_str);
      chomp($second_str);
      if($first_str eq $second_str)
      {
       $tmp.=substr($new_maillist[$i],10);
      }
      else
      {
       if($new_maillist[$i] ne '#################')
       {
        $tmp = $tmp.substr($new_maillist[$i],10);
        $newpages=$pages_title . $tmp;
        &mynewmail($code,$newpages);
        $tmp='';
        $code='';
       }
      }
      $i++;
     }
     print "<center><font size=5 color=blue>Mails have been sent. OK!</font></center>";
     close(USER);
     close(IN);
     close(OUT);
     close(LOG);
    }
    #################function##################
    sub mytime
    {
      my($flag)=@_;
      my($mydate)='';
      my(@weekDay)=("Mon","Tue","Wed","Thu","Fri","Sat","Sun");
      my(@Month)=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
      ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = localtime(time());
      $year+=1900;
      $mon=$Month[$mon];
      $wday=$weekDay[$wday+1];
     if($hour < 10)
     {
      $hour="0".$hour;
     }
     if($sec < 10)
     {
      $sec="0".$sec;
     }
     if($min < 10)
     {
      $min="0".$min;
     }
      if($flag eq 'dmy')
      {
              $mydate="$day $mon $year";
      }
      if($flag eq 'dmyhms')
      {
              $mydate="$day-$mon-$year-$hour-$min-$sec";
      }
      return($mydate);
    }

    sub title
    {
    print <<HTML_start;
            <center>
            <table border=0>
            <tr>
            <td><a href="$mypath/index.pl?action=upload">Upload</a></td>
            <td><a href="$mypath/index.pl?action=checktemp">Sendmail</a></td>
            <td><a href="$mypath/index.pl?action=checklog">Logfile</a></td>
            <td><a href="$mypath/index.pl?action=checkdata">Resultfile</a></td>
            <td><a href="$mypath/index.pl?action=updatestaff">Update staff</a></td>
            <td><a href="$mypath/index.pl">Back to main </a></td>
            </tr>
            </table>
            </center>
            <hr width=60%>
    HTML_start
    }

    sub check
    {
      my(@lists)=@_;
      my($index)=0;
      while($index<=@lists)
      {
        if($lists[$index] =~ /LOCATION/)
        {
                last;
        }
        $index++;
      }
      $location=$lists[$index];
      $index++;
      $classnum=$lists[$index];
      $index++;
      $bookname=$lists[$index];
      $index++;
      until($lists[$index] =~ /[A-Z][A-Z]\d{3}/)
      {
        if(length($lists[$index]) ==1)
        {
                last;
        }
        $bookname.=$lists[$index];
        $index++;
      }
      $i=0;
      while($lists[$index] =~ /[A-Z][A-Z]\d{3}/)
      {
          $staffs[$i]=$lists[$index];
          $i++;
          $index++;
      }
      return($location,$classnum,$bookname,@staffs);
    }

    sub error
    {
      my($message) = @_;

      print <<End_of_Error;
     Content-type: text/html
     Status: 500 CGI Error
     
     <HTML>
     <HEAD><TITLE>CGI Error</TITLE></HEAD>
     <BODY>
     <H1>Oops! Error</H1>
     <HR>
     $message
     <HR>
     </BODY>
     </HTML>
    End_of_Error
    }
    sub html_css
    {
     print <<HTML_css;
     <style>
     BODY {FONT-SIZE: 14px; BACKGROUND: #e4eaef; COLOR: #000000; FONT-FAMILY: Arial, Helvetica, Verdana, Sans}
     A:link {COLOR: #0033cc; TEXT-DECORATION: underline}
     A:visited { TEXT-DECORATION: underline}
     A:hover {COLOR: #f00000; TEXT-DECORATION: underline}
     A:active {TEXT-DECORATION: underline}
     TD {FONT-SIZE: 12px}
     </style>
    HTML_css
    }
    sub mynewmail
    {
      my($staff,$tmppages) = @_;
      my($user_found)= 0;
      foreach $user(@userlists)
      {
          if(substr($user,0,5) eq $staff)
          {
            ($code,$email,$test)=split("#",$user);

        if($debug == 1)
        {
         $pages =~ s/\n/<br>/g;
            print OUT $pages."E-mail:$email<br><hr>";
        }
        else
        {
              %mail = (from => $s_mail,to => $email,subject => $mail_subject);
          $mail{'content-type'} = 'text/html; charset="big5"';

            $mail{body} = <<END_OF_BODY;
         <html>$tmppages</html>
    END_OF_BODY
          sendmail(%mail) || &error("Error: $Mail::Sendmail::error\n");
              print  OUT $tmppages."E-mail:$email<br><hr>";
            }
            $user_found = 1;
          }
      }
      if($user_found == 0)
      {
      if($debug ==1)
      {
            print "Error:".$staff."\tNOT FOUND!<br>";
      }
      else
      {
       print LOG "Error:".$staff."\tNOT FOUND!\n";
      }
      }
    }
    #日志处理部分有问题,不能显示错误和正常的日志,但是运行的结果是对的.

  • 相关阅读:
    字符串(url)拼接变量
    elementUI table数据显示效果(二)
    异常(转)
    PHP 的异常处理、错误的抛出及错误回调函数 (转)
    详细解读PHP类的封装 (转)
    什么是抽象类
    什么是类,什么是对象,类和对象之间的关系
    魔术方法
    类的声名
    self
  • 原文地址:https://www.cnblogs.com/zhumao/p/195731.html
Copyright © 2020-2023  润新知