home *** CD-ROM | disk | FTP | other *** search
Wrap
#!/usr/bin/perl # Template : <..> # Function Name : init # Description : initialize parameters needed by this program # Inputs : none # Outputs : none # Return value : 1 if ok, 0 if error. # Calls : # Notes : # Created on Mon Jul 22 14:21:09 PDT 1996 by Steve Hsueh sub init { $thisurl = $ENV{'SERVER_URL'}.$ENV{'SCRIPT_NAME'}; $upload_dir = '/usr/home/shpank/public_html/incoming/'; # location for uploaded files $authorurl = 'webmaster@cavalcade-whimsey.com'; # for the mail-to tag $ACCESS_DENIED = 'access denied'; 1; } # end of init # Function Name : print_header # Description : print http header Content-Type # Inputs : none # Outputs : write message to stdout # Return value : 1 if ok, 0 if error. # Calls : print # Notes : none # Created on Mon Jul 22 12:13:36 PDT 1996 by Steve Hsueh sub print_header { print "Content-Type: text/html\n\n"; 1; } # end of print_header # Template : <..> # Function Name : urldecode # Description : decode url-encoded contents # Inputs : $input # Outputs : %GLOBAL # Return value : 1 if ok, 0 if error. # Calls : # Notes : # Creadted on Sun Aug 25 11:30:34 PDT 1996 by Steve Hsueh sub urldecode { local($in) = @_; local($i, @input_list); @input_list = split(/&/,$in); foreach $i (@input_list) { $i =~ s/\+/ /g; # Convert plus's to spaces # Convert %XX from hex numbers to alphanumeric $i =~ s/%(..)/pack("c",hex($1))/ge; # Split into key and value. $loc = index($i,"="); $key = substr($i,0,$loc); $val = substr($i,$loc+1); $GLOBAL{$key} = $val; } 1; } # end of urldecode # Template : <..> # Function Name : read_net_input # Description : read input from web # Inputs : none # Outputs : %GLOBAL # Return value : 1 if ok, 0 if error. # Calls : sysread(), print(), foreach() # Notes : # if GET, read input from environment variable. if POST, read input from # stdin. Parse the content if it is ??, save the content and boundary if it is # multipart/form-data. # Created on Mon Jul 22 12:18:15 PDT 1996 by Steve Hsueh sub read_net_input { local ($i, $loc, $key, $val, $input); local($f,$header, $header_body, $len, $buf); if ($ENV{'REQUEST_METHOD'} eq "GET") { $input = $ENV{'QUERY_STRING'}; } elsif ($ENV{'REQUEST_METHOD'} eq "POST") { # Need to read TILL we got all bytes, bug fixed in v00.02 $len = 0; $input = ''; while( $len != $ENV{'CONTENT_LENGTH'} ) { $buf = ''; $len += sysread(STDIN, $buf, $ENV{'CONTENT_LENGTH'}); $input .= $buf; } } # conform to RFC1867 for upload specific if( $ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/ ) { $boundary = '--'.$1; # please refer to RFC1867 @list = split(/$boundary/, $input); $header_body = $list[1]; $header_body =~ /\r\n\r\n|\n\n/; # separate header and body $header = $`; # front part $body = $'; # rear part $body =~ s/\r\n$//; # the last \r\n was put in by Netscape $GLOBAL{'FILE_CONTENT'} = $body; # open(FD,">input.txt"); print FD $input; close(FD); # for tracking # parse header $header =~ /filename=\"(.+)\"/; $GLOBAL{'FILE_NAME'} = $1; $GLOBAL{'FILE_NAME'} =~ s/\"//g; # remove "s $GLOBAL{'FILE_NAME'} =~ s/\s//g; # make sure no space(include \n, \r..) in the file name # parse trailer for( $i=2; $list[$i]; $i++) { $list[$i] =~ s/^.+name=$//; $list[$i] =~ /\"(\w+)\"/; $GLOBAL{$1} = $'; } return 1; } urldecode($input); 1; } # end of read_net_input # Template : <..> # Function Name : read_file # Description : read content of a file into buffer # Inputs : file name # Outputs : file content # Return value : $content if ok, 0 if error. # Calls : open, read, close # Notes : none # Created on Mon Jul 22 12:29:04 PDT 1996 by Steve Hsueh sub read_file { local($fname) = @_; local($content); open(FILE, "<$fname") || return ''; while(<FILE>) { $content .= $_; } close(FILE); $content; } # end of read_file # Template : <..> # Function Name : read_dir # Description : read the content of a directory # Inputs : $target_dir # Outputs : $dir_content # Return value : $dir_content if ok, 0 if error. # Calls : opendir(), closedir(), readdir() # Notes : $dir_content contain all files in the dir except # for ./, ../, and hidden files. All files are \n seperated. # Creadted on Sat Aug 24 13:25:00 PDT 1996 by Steve Hsueh sub read_dir { local($target_dir) = @_ ; local($filename, $dir_content, $open_failed); return 0 if( !$target_dir ); opendir(DIR, $target_dir) || do { $open_failed = $!; }; $target_dir =~ s/^\.\///; # remove ./ $target_dir =~ /(.+)\/(.+)\/$/; # find out upper level $GLOBAL{'UP_LEVEL'} = $1; # save upper level as a global if( $target_dir ) { $dir_content = "..back\n\n\n"; } if( !$open_failed ) { while($filename = readdir(DIR)) { if( $filename =~ /^\.|^\#|~$/ ) { next; } # skip hidden files $dir_content .= "$target_dir$filename\n"; } closedir(DIR); } else { $dir_content .= "$ACCESS_DENIED\n"; } $dir_content; } # end of read_dir # Template : <..> # Function Name : format_html_output # Description : format the output to be html # Inputs : $dir_content # Outputs : formated dir content # Return value : $formated_content if ok, 0 if error. # Calls : # Notes : this is specifically for file download # Creadted on Sat Aug 24 14:14:18 PDT 1996 by Steve Hsueh sub format_html_output { local($content) = @_; local(@filelist, $formated_content, $up_level); return 0 if (!$content); @filelist = sort(split(/\n/, $content)); $formated_content = "<UL>\n"; foreach $f (@filelist) { if( $f =~ /^$ACCESS_DENIED$/ ) { $formated_content .= "<li><font color=\#ff0000>$ACCESS_DENIED</font>"; last; } if( $f eq '..back' ) { $up_level = $GLOBAL{'UP_LEVEL'}; $formated_content = "<img src=\"/images/back.gif\"> <a href=".$thisurl.'/'.$up_level.">$f</a><br>\n<UL>\n"; next; } if( !$f ) { next; } if( -d $f ) { $f = "$f/"; } $formated_content .= '<li><a href='.$thisurl.'/'.$f.">$f</a><br>\n"; } $formated_content .= "</UL>\n"; $formated_content; } # end of format_html_output # Template : <..> # Function Name : show_dir_content # Description : show the content of a directory # Inputs : $dir # Outputs : the content of that directory # Return value : exit after show content, no return # Calls : # Notes : if no param passed in , it defaults to current dir # the order of input elements in the upload form is important, it effect how # the browser(Netscape) sends the request. # Creadted on Sat Aug 24 14:37:45 PDT 1996 by Steve Hsueh sub show_dir_content { local($dir) = @_; local($files, $f_files); $dir = './' if (!$dir); # default to cgi dir $files = read_dir($dir); $f_files = format_html_output($files); print_header(); print " <HTML> <HEAD><TITLE>File UpLoad</TITLE></HEAD> <BODY BGCOLOR=\"\#000000\" TEXT=\"\#0000ff\" LINK=\"\#FFFF99\" VLINK=\"\#FFFF99\" ALINK=\"\#FF8000\"> <center><img src=\"http://206.156.15.206/picts/cavalcade.gif\"></center> <p> <center><table width=500><tr><td><font size=-1> The following form will allow you to upload files to my site. Please only upload graphics, .txt files, or .tab files. I log all entries to this site, and any and all warez will <b>NOT BE TOLERATED!</b> So please be kind, and I thank you for sending me your files. <p> <FORM METHOD=\"POST\" ENCTYPE=multipart/form-data > <b>File Name:</b><br> <INPUT TYPE=\"file\" NAME=\"file\" SIZE=35 > <p> <INPUT TYPE=HIDDEN NAME=UPLOAD_DIR VALUE=DEFAULT> <INPUT TYPE=submit NAME=UPLOAD VALUE=UPLOAD > <input type=\"reset\" value=\"Clear\"> <INPUT TYPE=HIDDEN NAME=CURRENT_DIR VALUE=\"$dir\"> </FORM> <HR> Comments, questions or problems? mail to <a HREF=\"mailto:$authorurl\">$authorurl</a><br> </font> </td></tr></table></center> </BODY> </HTML> "; exit; } # end of show_dir_content # Template : <..> # Function Name : show_file_not_found # Description : show the file not found message # Inputs : nonte # Outputs : file not found # Return value : exit after show content, no return # Calls : # Notes : none # Creadted on Sat Aug 24 14:37:45 PDT 1996 by Steve Hsueh sub show_file_not_found { print_header(); print "<TITLE>Not Found</TITLE><H1>Not Found</H1> The requested object does not exist on this server. The link you followed is either outdated, inaccurate, or the server has been instructed not to let you have it. Connection closed by foreign host.\n"; exit; } # show_file_not_found # Template : <..> # Function Name : start_download # Description : start to download a file # Inputs : file # Outputs : write to sdtout # Return value : 1 if ok, 0 if error. # Calls : # Notes : need to check if the input is a dir or a file # if dir then call show_dir_content() else start download # Creadted on Sat Aug 24 14:40:12 PDT 1996 by Steve Hsueh sub start_download { local($target_file) = @_; local($file_name); $target_file =~ s/^\/|^\\|\s//; if( -d $target_file ) { show_dir_content("./$target_file"); } # check if file exists , though this is not likely to happen if ( ! -e "./$target_file") { show_file_not_found(); } # get file name $file_name = $target_file; $file_name =~ s/.+\/([^\/]+)$/$1/; # for PC system $file_name =~ s/.+\\([^\\]+)$/$1/; # for Unix system # start download print "Content-Type: application/x-unknown\n"; print "Content-Disposition: attachment; fillename=$file_name\n\n"; print read_file($target_file); 1; } # end of start_download # Template : <..> # Function Name : show_upload_failed # Description : show upload failed page # Inputs : $reason # Outputs : html page # Return value : no return # Calls : print_header, print() # Notes : # Creadted on Sun Aug 25 13:41:24 PDT 1996 by Steve Hsueh sub show_upload_failed { local($reason) = @_; print_header(); print "<TITLE>Upload Failed</TITLE><H1>Upload Failed</H1> The requested object was not uploaded to the server. <br> Reason : $reason. The server may have decided not let you write to the directory specified. Please contact the <a href=\"mailto:webmaster\@cavalcade-whimsey.com\">webmaster</a> for this problem. Connection closed by foreign host.\n"; exit; } # end of show_upload_failed # Template : <..> # Function Name : show_upload_success # Description : display a upload success html page # Inputs : uploaded_file # Outputs : html page # Return value : no return # Calls : print_header, print() # Notes : the file names is global. we also need to display file size for verification # Creadted on Sun Aug 25 13:42:50 PDT 1996 by Steve Hsueh sub show_upload_success { local($uploaded_file) = @_; local(@status_list) ; # @status_list = stat($uploaded_file); $file_stats = `ls -la $uploaded_file`; @status_list = split(/\s+/, $file_stats); # bug fix in v00.01 print_header(); #foreach $s ( @status_list ) { print "==$s== <br>\n"; } print " <HTML> <HEAD><TITLE>File UpLoaded</TITLE> </HEAD> <BODY BGCOLOR=\"\#000000\" TEXT=\"\#0000ff\" LINK=\"\#FFFF99\" VLINK=\"\#FFFF99\" ALINK=\"\#FF8000\"> <center><img src=\"http://206.156.15.206/picts/cavalcade.gif\"></center> <p> <center><table width=500> <tr><td><font size=-1> Thank you for your recent submission. Whenever you get some new pictures or guitar tablature, and you think they should be here, than please feel free to send it to me. Thanks again. <p> Here is the information on what you uploaded. <p> Remote File Name : <FONT COLOR=\#FFFFFF> $GLOBAL{'FILE_NAME'} </FONT> <br> File Name : $filename <br> Location : My Upload Directory <br> File Size : $status_list[4] <br> Local Time: $status_list[5] $status_list[6] $status_list[7] <br> <p> I thank you again for uploading these files. If you would like to upload more, just click <a href=\"http://206.156.15.206/shpank/udload.cgi\">here</a>. <p> Peace! <p> <center><a href=\"mailto:shpank\@beachin.net\">Shpank</a></center> </font> </td></tr></table></center> </BODY> </HTML> "; exit; } # end of show_upload_success # Template : <..> # Function Name : handle_upload # Description : handle file upload from browser # Inputs : none # Outputs : read from stdin and save the file to current directory # Return value : 1 if ok, 0 if error. # Calls : # Notes : # Creadted on Sat Aug 24 18:24:30 PDT 1996 by Steve Hsueh sub handle_upload { if( !$GLOBAL{'FILE_NAME'} ) { show_file_not_found(); } # grep the file name , there is always a / in front of the file name #$GLOBAL{'FILE_NAME'} =~ /.+\\([^\\]+)$|([^\/]+)$/; $filename = $GLOBAL{'FILE_NAME'}; $filename =~ s/.+\\([^\\]+)$|.+\/([^\/]+)$/\1/; if( $GLOBAL{'UPLOAD_DIR'} =~ /CURRENT/ ) { # change upload dir to current $GLOBAL{'CURRENT_DIR'} =~ s/\s//g; $upload_dir = $GLOBAL{'CURRENT_DIR'}; } $write_file = $upload_dir.$filename; open(ULFD,">$write_file") || show_upload_failed("$write_file $!"); print ULFD $GLOBAL{'FILE_CONTENT'}; close(ULFD); show_upload_success($write_file); 1; } # end of handle_upload # Template : <..> # Function Name : main # Description : main function of the script # Inputs : none # Outputs : none # Return value : 1 if ok, 0 if error. # Calls : # Notes : simulate a C style modularity # Creadted on Sat Aug 24 12:47:18 PDT 1996 by Steve Hsueh sub main { init(); read_net_input(); # print_header(); # while( ($n, $v) = each(%ENV)) { print "$n = $v <br>"; } print "<br>"; # while( ($n, $v) = each(%GLOBAL)) { print "$n = $v <br>"; } print "<br>"; if( $GLOBAL{'UPLOAD'} ) { handle_upload(); } elsif( !$ENV{'PATH_INFO'} || $ENV{'PATH_INFO'} eq '/' ) { show_dir_content(); } else { start_download( $ENV{'PATH_INFO'} ); } 1; # for fun } # end of main # program starts from here main();