#!/usr/bin/perl
require "wiz_resources";
use CGI qw(param);

header("Uploader","","upload");
  
$maxsize=(1*1024*1024);

if ($user ne "") {
  $action = param("action");
  if (($action eq "header") || ($action eq "upload")) {
    upload_proc();
  } elsif ($action eq "delete_header"){
    delete_header();
  } elsif ($action eq "delete"){
    delete_file();
  } else {
    upload_forms();
  }
} else {
  headline("Error: Access denied","red","","You should not use this page unless you have logged into the system.  Please <a href=\"$default_cgi/login\">log in</a> and try again.");
}

footer();

## end

sub delete_header {
  my $verify=param("verify");

  if ($verify eq "true") {
    for ($loop=0; $loop<6; $loop++){      
      $ext="gif"  if ($loop == 0);   
      $ext="jpeg" if ($loop == 1);   
      $ext="jpg"  if ($loop == 2);   
      $ext="jpe"  if ($loop == 3);   
      $ext="png"  if ($loop == 4);   
      $ext="swf"  if ($loop == 5);   
      $ext="svg"  if ($loop == 6);
      unlink "${homesite}uploads/header-logo.$ext" if (-e "${homesite}uploads/header-logo.$ext");
    }
    headline("Header Deleted","","","The header image has been deleted.  Please update all of your web pages to make sure they take into account this change.");
    set_change_date();
    print "<p>You can now <a href=\"$ENV{SCRIPT_NAME}\">go back to the upload page</a> or <a href=\"$default_cgi/edit\">continue editing</a>";

  } else {
    headline("Verification: delete header","","","Are you sure you want to delete the header?  This will mean that the logo at the top of all of your pages will no longer be available.");
        print "<p>\n<a href=\"$ENV{REQUEST_URI}&verify=true\"><font color=\"#ff0000\">Continue: override safety check and delete</font></a> or <a href=\"$ENV{SCRIPT_NAME}\"><font color=\"#00CC00\">Stop: go back to upload page</font></a>";
  }
}

sub delete_file {
  my $verify=param("verify");
  my $delete_file=param("delete_file");
  if ($delete_file =~ m~\.\./~) {
    headline("Error: bad input detected","red","","Do not try to delete files outside the upload directory.  That would be bad.");
  } else {
    if ($verify eq "true") {
      unlink "${homesite}uploads/$delete_file" if (-e "${homesite}uploads/$delete_file");
      headline("File Deleted","","","The file '<span style=\"background: #ffcc99;\">$delete_file</span>' has been deleted.  Please update all of your web pages to make sure they take into account this change.");
      set_change_date();
      print "<p>You can now <a href=\"$ENV{SCRIPT_NAME}\">go back to the upload page</a> or <a href=\"$default_cgi/edit\">continue editing</a>";
    } else {
      headline("Verification: delete file","","","Are you sure you want to delete the file '<span style=\"background: #ffcc99;\">$delete_file</span>'?  This will mean that any links to this file, or any pages containing this image, will no longer work.");
          print "<p>\n<a href=\"$ENV{SCRIPT_NAME}?action=delete&delete_file=$delete_file&verify=true\"><font color=\"#ff0000\">Continue: override safety check and delete</font></a> or <a href=\"$ENV{SCRIPT_NAME}\"><font color=\"#00CC00\">Stop: go back to upload page</font></a>";
    }
  }
}

sub upload_proc {
  $err=0;
  unless ($ENV{'REQUEST_METHOD'} =~ m~^POST$~i) {
    headline("Error: method not allowed","red","","The data was sent using a  request method of <i>$ENV{'REQUEST_METHOD'}</i> rather than <i>POST</i>.  File uploading requires a POST method.");
    $err++;
  }
  
  unless ($ENV{'CONTENT_TYPE'} =~ m~multipart/form-data~i) {
    headline("Error: invalid content type","red","","The data was sent with a content type of <i>$ENV{'CONTENT_TYPE'}</i>; this has to be <i>multipart/form-data</i> for file uploads to work.");
    $err++;
  }
  
  $content_length=$ENV{'CONTENT_LENGTH'};
  if ($content_length > $maxsize) {
    headline("Error: too big","red","","The data you sent was <span style=\"background: #ffcc99;\">$content_length bytes</span> in size, which is too big.  The maximum size for file uploads on this server is <span style=\"background: #ffff99;\">$maxsize bytes</span>");
    $err++; 
  }
  
  if ($content_length == 0) {
    headline("Error: nothing sent","red","","No data appears to have been sent.  Please try again.");
    $err++; 
  }
  
  my $query = new CGI;
  my $file = $query->param('upfile');
  print "File: $file<br>\n";
  
  if ($file eq "") {
    headline("Error: No file specified","red","","No file name was given.  Please try again.");
    $err++; 
  }
  
  if ($err == 0) {
    $buffered=0;
    unless (-d "${homesite}uploads/") {
      mkdir "${homesite}uploads/",0777;
      system "chmod 777 ${homesite}uploads/";
    }
    $filename=$file;
    $filename=$1 if ($filename =~ m~\\([^\\]+?)$~i);
    ##$outputfile="$serverroot/uploads/".$uniqueid;
    $outputfile="${homesite}uploads/$filename";

    if ($action eq "header") {
      $ext="gif";
      if ($filename =~ m~\.([a-z]{3,4})$~i) {
        $ext=$1;
      }
      $filename="${homesite}uploads/header-logo.$ext";
      for ($loop=0; $loop<6; $loop++){      
        $ext="gif"  if ($loop == 0);   
        $ext="jpeg" if ($loop == 1);   
        $ext="jpg"  if ($loop == 2);   
        $ext="jpe"  if ($loop == 3);   
        $ext="png"  if ($loop == 4);   
        $ext="swf"  if ($loop == 5);   
        $ext="svg"  if ($loop == 6);
        unlink "${homesite}uploads/header-logo.$ext" if (-e "${homesite}uploads/header-logo.$ext");    
      }
      $outputfile=$filename;
    }

    open(OUT, ">$outputfile");
    while (read($file, $buffer, 1024)) {
      $buffered+=length($buffer);
      print OUT $buffer;
    }
    close OUT;


#    if (($buffered < ($content_length/1.1)) || ($buffered < 10)) {
#      headline("Error: lengths didn't match","red","","The file size ($content_length) did not match the amount of data received ($buffered).");
#      unlink $outputfile;
#      print "<p>Unfortunately it was not possible to upload the file from your browser.  Please try to rectify the error, or upgrade your browser to a newer version.</p>\n";
#      print "<p>You can now <a href=\"$ENV{SCRIPT_NAME}\">go back to the upload page</a> or <a href=\"$default_cgi/edit\">continue editing</a>";

    if ($buffered < 10) {
      headline("Error: lengths didn't match","red","","The file size ($content_length) did not match the amount of data received ($buffered).");
      unlink $outputfile;
      print "<p>Unfortunately it was not possible to upload the file from your browser.  Please try to rectify the error, or upgrade your browser to a newer version.</p>\n";
      print "<p>You can now <a href=\"$ENV{SCRIPT_NAME}\">go back to the upload page</a> or <a href=\"$default_cgi/edit\">continue editing</a>";
    } else {
      $outputfile=$1 if ($outputfile =~ m~(/uploads/.*)$~i);
      if ($action eq "header") {
        headline("Header uploaded","","","");
      } else {
        headline("File uploaded","","","");
      }
      print "<b><span style=\"background: #ffff99;\">$file</span></b> was uploaded ";
      if ($action ne "header"){
        print "as <b><span style=\"background: #ffcc99;\">$outputfile</span></b>";
      } else {
        print "successfully as a header image";
      }
      print ".<p>\n";
      if ($action eq "header") {
        print "If you have just uploaded a header graphic for the first time don't forget that for your header is displayed on the top of you web pages you must go to the main edit window and <b>refresh each of your web pages</b>.<p>\n";
        set_change_date();
      }
      print "You can now <a href=\"$ENV{SCRIPT_NAME}\">carry on managing your uploaded files</a> or <a href=\"$default_cgi/edit\">go back to the main edit page</a>.\n";
    }
  } else {
    print "<p>Unfortunately it was not possible to upload the file from your browser.  Please try to rectify the error, or upgrade your browser to a newer version.</p>\n";
    print "<p>You can now <a href=\"$ENV{SCRIPT_NAME}\">go back to the upload page</a> or <a href=\"$default_cgi/edit\">continue editing</a>";
  }
}

sub upload_forms {
print "";
headline("Header","","","");

print <<EOF;  
You can upload a header graphic such as your company logo or just a nice header image welcoming viewers to your site.
<p>
This gets included at the top of <b>every</b> page on your site, so try not to make it too big.  GIF images work best for simple logos, JPEGs for more detailed work.  Once you have uploaded a header, use the refresh function on the main edit window so your pages are updated to include the graphic.

<script language="javascript">
<!-- 
document.write("<a href=\\"javascript:void(0)\\" onclick=\\"javascript:window.open('/info/graphicshelp.html','myWindow','toolbar=no,status=no,location=no,menubar=no,width=300,height=400,screenx=0,screeny=0,top=0,left=0,scrollbars=yes,resizable=yes');\\">");
// --></script><noscript><a href="/info/graphicshelp.html" target="_blank"></noscript>More...</a>
<p align="center">
EOF

my $delete_flag=0;
for ($loop=0; $loop<6; $loop++){      
  $ext="gif"  if ($loop == 0);   
  $ext="jpeg" if ($loop == 1);   
  $ext="jpg"  if ($loop == 2);   
  $ext="jpe"  if ($loop == 3);   
  $ext="png"  if ($loop == 4);   
  $ext="swf"  if ($loop == 5);   
  $ext="svg"  if ($loop == 6);
  $delete_flag++ if (-e "${homesite}uploads/header-logo.$ext");    
}
if ($delete_flag) {
  print <<EOF;
<table border="0" cellpadding="0" cellspacing="10">
<tr valign="top"><td>
EOF
}
print <<EOF;
<!-- add header -->
<form method="POST" enctype="multipart/form-data" action="$ENV{SCRIPT_NAME}">
<input type="hidden" name="action" value="header">
<table border="1" cellpadding="5" cellspacing="0">
<tr><td align="center" bgcolor="#ff6600">
<font color="#ffffff"><b><big>Upload Header</big></b></font></td></tr>
<tr><td align="center">File:<br /><input type="file" name="upfile"></td></tr>
<tr><td align="center" bgcolor="#ff6600"><input type="submit" value="Send!"></td></tr></table>
</form>
<!-- / add header -->
EOF

if ($delete_flag) {
  print <<EOF;
</td><td>

<!-- delete header -->
<table border="1" cellpadding="5" cellspacing="0">
<tr><td align="center" bgcolor="#ff6600">
<font color="#ffffff"><b><big>Remove Header</big></b></font></td></tr>
<tr><td align="center">
<a href="$ENV{SCRIPT_NAME}?action=delete_header">Click to delete image
<br /><img src="$image_dir/tools/delete.gif" width="20" height="20" alt="Remove header" hspace="5" border="0" style="border: 0px" /></a>
</td></tr></table>
<!-- / delete header -->

</td></tr></table>
</p>

EOF
}

print "<p><hr><p>\n\n";
headline("Files","","","");

print <<EOF;  
You can also upload other files for inclusion in your website.
<p>
Certain
<script language="javascript">
<!-- 
document.write("<a href=\\"javascript:void(0)\\" onclick=\\"javascript:window.open('/info/images.html','myWindow','toolbar=no,status=no,location=no,menubar=no,width=300,height=400,screenx=0,screeny=0,top=0,left=0,scrollbars=yes,resizable=yes');\\">");
// --></script><noscript><a href="/info/images.html" target="_blank"></noscript>types of images</a> can be displayed in the page, and any other file type can be linked to from a paragraph.  So, for instance, you can upload a Word document, create a new paragraph on one of your pages that describes the file, and then link to it so people can download it.
<p align="center">
EOF

my $file_flag=0;
if (-d "${homesite}uploads/") {
  opendir DIR, "${homesite}uploads/";
  while ($dirfile = readdir(DIR)) {
    if (($dirfile ne ".") && ($dirfile ne "..") && (!($dirfile =~ m~^header-logo\.~))) {
      $file_flag++;
    }
  }
  rewinddir DIR;
  closedir DIR;
}

if ($file_flag) {
  print <<EOF;
<table border="0" cellpadding="0" cellspacing="10">
<tr valign="top"><td>
EOF
}

print <<EOF;
<!-- add file -->
<form method="POST" enctype="multipart/form-data" action="$ENV{SCRIPT_NAME}">
<input type="hidden" name="action" value="upload">
<table border="1" cellpadding="5" cellspacing="0">
<tr><td align="center" bgcolor="#ff6600">
<font color="#ffffff"><b><big>Upload File</big></b></font></td></tr>
<tr><td align="center">File:<br /><input type="file" name="upfile"></td></tr>
<tr><td align="center" bgcolor="#ff6600"><input type="submit" value="Send!"></td></tr></table>
</form>
<!-- / add file -->
EOF

if ($file_flag) {
  print <<EOF;
</td><td>

<!-- delete file -->
<form method="POST" enctype="multipart/form-data" action="$ENV{SCRIPT_NAME}">
<input type="hidden" name="action" value="delete">
<table border="1" cellpadding="5" cellspacing="0">
<tr><td align="center" bgcolor="#ff6600">
<font color="#ffffff"><b><big>Delete File</big></b></font></td></tr>
<tr><td align="center">File:<br />


EOF

print "<select name=\"delete_file\"";
print " size=\"5\"" if ($file_flag > 4);
print ">\n";
if (-d "${homesite}uploads/") {
  opendir DIR, "${homesite}uploads/";
  while ($dirfile = readdir(DIR)) {
    if (($dirfile ne ".") && ($dirfile ne "..") && (!($dirfile =~ m~^header-logo\.~))) {
      print "<option value=\"$dirfile\">$dirfile</option>\n";
    }
  }
  closedir DIR;
}

print <<EOF;
</select>

</td></tr>
<tr><td align="center" bgcolor="#ff6600"><input type="submit" value="Delete!"></td></tr></table>
</form>
<!-- / delete header -->

</td></tr></table>
</p>

EOF
}

#endproc
}
