#!/usr/bin/perl -w
#
# test.cgi -- A CGI script for FileUpload module demonstration.
#	      Make sure FileUpload.pm has been installed before
#	      you start to test this script.
#	      This script reads input from "./form.html" file,
#	      it expects there is a file being uploaded with
#	      input field name, ufile.
#
# Simon Tneoh Chee-Boon tneohcb@pc.jaring.my
# 
# Copyright (c) 2000-2002 Simon Tneoh Chee-Boon. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
use strict;
use vars qw($cgi $fu $sizewritten);

# Autoflush.
$| = 1;

# Print the HTTP header line.
# I am not using CGI->header here because I want to check if CGI
# version 2.46 or above is installed.
print "Content-Type: text/html\n\n";

# Make sure CGI version 2.46 or above is installed.
eval("use CGI 2.46 qw(-private_tempfiles :standard);");
(print("Error: $@"), exit(-1)) if $@;

# Make sure FileUpload is installed.
eval("use FileUpload 0.07;");
(print("Error: $@"), exit(-1)) if $@;

# Create CGI object.
$cgi = new CGI;

# Has any files been uploaded?
if (! $cgi->param('ufile')) {
  print <<__END__ERROR__;
<html>
<head><title>Error</title></head>
<body bgcolor='white'>
It seems like you have not selected any files yet.
Please try again.
</body>
</html>
__END__ERROR__
  exit(-1);
}

# Create the FileUpload object.
$fu = new FileUpload($cgi->param('ufile'));

if (! $fu) {
  print <<__END__ERROR__;
<html>
<head><title>Error</title></head>
<body bgcolor='white'>
Failed to create FileUpload object.
Please try again.
</body>
</html>
__END__ERROR__
}

# Print results.
print <<__END__HTML__;
<html>
<head>
<title>Upload Result</title>
</head>
<body bgcolor='white'>
<h2>Upload Result</h2>
[ <a href='./'>Back To Index</a> | 
<a href='./form.html'>Back To Demonstration Page</a> ]
<hr>
<blockquote>
<table border='1' width='80%' cellpadding='5'>
<tr>
  <th>Action</th>
  <th>Command</th>
  <th>Returns</th>
</tr>
<tr>
  <td align='left'>Original filename</td>
  <td><font face='Courier'>\$fu-&gt;orig_filename</font></td>
  <td><font face='Courier'>${\$fu->orig_filename}</font></td>
</tr>
<tr>
  <td align='left'>Original filepath</td>
  <td><font face='Courier'>\$fu-&gt;orig_filepath</font></td>
  <td><font face='Courier'>${\$fu->orig_filepath}</font></td>
</tr>
<tr>
  <td align='left'>Extensions denied</td>
  <td><font face='Courier'>\$fu-&gt;deny_ext</font></td>
  <td><font face='Courier'><font color='blue'>${\sprintf("%s", join("</font>,<font color='blue'>", $fu->deny_ext))}</font></font></td>
</tr>
<tr>
  <td align='left'>Add extensions denied</td>
  <td><font face='Courier'>\$fu-&gt;deny_ext('cgi', 'shtml')</font></td>
  <td><font face='Courier'><font color='blue'>${\sprintf("%s", join("</font>,<font color='blue'>", $fu->deny_ext('cgi', 'shtml')))}</font></font></td>
</tr>
<tr>
  <td align='left'>Extensions denied</td>
  <td><font face='Courier'>\$fu-&gt;deny_ext</font></td>
  <td><font face='Courier'><font color='blue'>${\sprintf("%s", join("</font>,<font color='blue'>", $fu->deny_ext))}</font></font></td>
</tr>
<tr>
  <td align='left'>Characters allowed</td>
  <td><font face='Courier'>\$fu-&gt;allow_char</font></td>
  <td><font face='Courier'><font color='blue'>${\sprintf("%s", join("</font>,<font color='blue'>", $fu->allow_char))}</font></font></td>
</tr>
<tr>
  <td align='left'>Save the file<br>
    (<font size='-2' color='red'>For security problem, we only demo save_as() 
    here.</font>)</td>
  <td><font face='Courier'>\$fu-&gt;save_as("./test.html")</font></td>
  <td><font face='Courier'>
__END__HTML__
# Try to save the uploaded file.
$sizewritten = -1;
if (!($sizewritten = $fu->save_as("./test.html"))) {
  print "Fail to save file: $!";
}
else {
  print $sizewritten."bytes written<br>\n";
}
print <<__END__HTML1__;
  </font></td>
</tr>
<tr>
  <td align='left'>Add characters allowed</td>
  <td><font face='Courier'>\$fu-&gt;allow_char('#\/', '\,')</font></td>
  <td><font face='Courier'><font color='blue'>${\sprintf("%s", join("</font>,<font color='blue'>", $fu->allow_char('#\/', '\,')))}</font></font></td>
</tr>
<tr>
  <td align='left'>Characters allowed</td>
  <td><font face='Courier'>\$fu-&gt;allow_char</font></td>
  <td><font face='Courier'><font color='blue'>${\sprintf("%s", join("</font>,<font color='blue'>", $fu->allow_char))}</font></font></td>
</tr>
<tr>
  <td align='left'>Current FILEHANDLE position</td>
  <td><font face='Courier'>\$fu-&gt;get_pos</font></td>
  <td><font face='Courier'>${\$fu->get_pos}</font></td>
</tr>
<tr>
  <td align='left'>Set FILEHANDLE position</td>
  <td><font face='Courier'>\$fu-&gt;set_pos(255)</font></td>
  <td><font face='Courier'>${\$fu->set_pos(255)}</font></td>
</tr>
<tr>
  <td align='left'>Read a line</td>
  <td><font face='Courier'>\$fu-&gt;get_line</font></td>
  <td><font face='Courier'>${\$fu->get_line}</font></td>
</tr>
<tr>
  <td align='left'>Current FILEHANDLE position</td>
  <td><font face='Courier'>\$fu-&gt;get_pos</font></td>
  <td><font face='Courier'>${\$fu->get_pos}</font></td>
</tr>
<tr>
  <td align='left'>File size</td>
  <td><font face='Courier'>\$fu-&gt;size</font></td>
  <td><font face='Courier'>${\$fu->size}</font></td>
</tr>
<tr>
  <td align='left'>Save the file<br>
    (<font size='-2' color='red'>For security problem, we only demo save_as() 
    here.</font>)</td>
  <td><font face='Courier'>\$fu-&gt;save_as("./test.html")</font></td>
  <td><font face='Courier'>
__END__HTML1__
# Try to save the uploaded file.
$sizewritten = -1;
if (!($sizewritten = $fu->save_as("./test.html"))) {
  print "Fail to save file: $!";
}
else {
  print $sizewritten."bytes written<br>\n";
}
print <<__END__HTML2__;
  </font></td>
</tr>
<tr>
  <td align='left'>Check the uploaded file</td>
  <td colspan='2'><a href='./test.html'>./test.html</a>
    (<font color='red'>Notice: Because this script always save the uploaded
    file to "./test.html", so you might see file that's uploaded by 
    others.</font>)
  </td>
</tr>
</table>

</blockquote>
<hr>
[ <a href='./'>Back To Index</a> | 
<a href='./form.html'>Back To Demonstration Page</a> ]
</body>
</html>
__END__HTML2__

# Done.
exit(0);
__END__

