#!/usr/bin/perl -w 

#**************************************
# Name: CGI Perl - File Upload
# Description:Shows you the key of how to upload a binary file using perl/cgi.
#
# Inputs:You will need to make a form with the following properties:
#   <form  enctype="multipart/form-data" method=post action="http://www.1stmuse.com/cgi-bin/upload.cgi">
#   <input type=file name=filex>
#    and of course, a submit button
#**************************************
    
###	NOTE: if you want to to do textfile uploading, just remove all the binmode() functions and that's it!
use CGI;

# $root_ = "/data/20/1/128/63/1943063/user/2120061/htdocs";
$root_ = "$ENV{'DOCUMENT_ROOT'}";

$upload_path = "$root_/hermes/";
$js_path ="http://novalis-production.com/hermes/js";

###	Set Maximum Upload size to 2MB (prevent from uploading extremely large files or other such malicious stuff)
$CGI::POST_MAX = 2048;




###    IMPORTANT: Set Standard Input to Binary Mode -- this is critical for binary data, like images
binmode(STDIN);
@input_stream = <STDIN>;

###	pick up from from the array: @input_stream the $upload_file_data
$upload_file_data = @input_stream[1];

###	pick up from from the $upload_file_data the correct $file_name
###     this works well if the image file was upoaded from MS Windows systems
($a0, $variable_name, $file_path) = split(/\;/, $upload_file_data);
$original_path = $file_path;
$file_path =~ s/\"//g;  # - substitute quotation charater to nil
$file_path =~ s/\\/\;/g;  # - substitute backslash to semicolon
$file_path =~ s/\//\;/g;  # - substitute slash to semicolon
@path_elements = split (/\;/, $file_path);
### file name is the last of all path_elements
$file_name_index = $#path_elements;
$file_name = @path_elements[$file_name_index];
$file_name =~ s/filename=//g;  # - substitute "filename=" string to nil
$file_name =~ s/\=//g;  # - substitute "=" string to nil
$file_name =~ s/[\|\£\$\%\&\)\(\?\^\*\§\=\'\@\",\-\s+]/_/g; # - substitute whitespace(s) & other strange characters to underscore
$file_name =~s/..$//;   # - trimming last two characters 
$file_name =~s/^_//;   # - trimming leading underscore
$file_name =~s/\_+/_/g;   # - trimming redundant underscore
$file_slika = $file_name;


###	This is just for a single file upload; you can improve the code to include boundary separations and stuff like that..
###	These two lines delete the boundary and file info which comes with the formfeed and leaves only the binary data
###	First line  : removes initial 4 elements from the array: @input_stream.
###	Second line : removes last element from the array: @input_stream.
splice(@input_stream,0,4);
splice(@input_stream,$#input_stream ,1);

$in = join("",@input_stream );
$in = substr($in,0,length($in) - 2);;
###	This next sniplet assumes you are uploading a JPEG file and saves it as $file_name in the $upload_path folder
###	You can also further improve the script by retreiving the filename from the 'spliced' lines
$image_file = $upload_path . $file_name;
### if you want to have a time_STAMP prefix 
### $time_stamp   = time();
### $time_stamp  .= "_";
### $image_file = $upload_path .$time_stamp .$file_name;
#### change :ValueTransfer('$time_stamp$file_name')

open(ff,">$image_file");
###	Set file output to binary mode (since you're assuming to write a binary file)
binmode(ff);
print ff $in;
close(ff);


###	Print HTML file header
print "content-type: text/html\n\n";
print<<HTML;
<html>
<HEAD>
   <TITLE>Upload</TITLE>
  </HEAD>
<body bgcolor="#ffffff" onLoad="ValueTransfer('$file_name');window.close()";>
  <script language="javascript" src="$js_path/global.js"></script>	
	<script language="javascript" src="$js_path/style.js"></script>
<script language="javascript">

function ValueTransfer(f) {
	self.opener.document.myForm.ICONOGRAFIA_file_web.value = f;
	self.opener.document.myForm.ICONOGRAFIA_gallery.value += ',' + f;
	}

</script>
	
FILE UPLOAD ...
HTML
###	end of HTML file header

## response
print "terminato con successo. <br> Uploaded Size: ".length($in)." bytes "."<br>file_path: ".$original_path."<br>file_name: ".$file_name;

print<<HTML;

<form name=Transfer><br> Uploaded file:
<input name="file_name" id="file_name" type="text" size="24" maxlength="24" value="$file_name" readonly
       onChange="ValueTransfer(this.value)" onfocus="ValueTransfer(this.value)"        >
<br> 	 
<INPUT onclick="ValueTransfer('$file_name');window.close()"; TYPE="button" VALUE="Confirm file name"> 
</body></html>
HTML

# end of perl script
