#!/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
#       
#   This code is copyrighted and gives no warranties.
#**************************************
    
###	NOTE: if you want to to do textfile uploading, just remove all the binmode() functions and that's it!
use CGI;

use Encode;
use utf8;

$root_ = "$ENV{'DOCUMENT_ROOT'}";

$upload_path = "$root_/wienerberger/";
$main_path ="http://loyalty.hr";

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

###	Print HTML file header
print "content-type: text/html\n\n";
print<<HTML;
<html>
<HEAD>
   <TITLE>Upload</TITLE>
  </HEAD>
<body bgcolor="#ffffff">

<script language="javascript">
// Browser Detection 
function BrowserCheck() {
	var b = navigator.appName;	
	if (b=="Netscape") this.b = "ns";
	else if (b=="Microsoft Internet Explorer") this.b = "ie";
	else this.b = b;
	this.version = navigator.appVersion;
	//if (this.version.indexOf('Win')>0) this.os = "PC";
	//else if (this.version.indexOf('Mac')>0) this.os = "Mac";	
	this.WinOs = (this.version.indexOf('Win')>0);
	this.v = parseInt(this.version);
	this.ns = (this.b=="ns" && this.v>=4);
	this.ns4 = (this.b=="ns" && this.v==4);
	this.ns5 = (this.b=="ns" && this.v==5);
	this.ie = (this.b=="ie" && this.v>=4);
	this.ie4 = (this.version.indexOf('MSIE 4')>0);
	this.ie5 = (this.version.indexOf('MSIE 5')>0);
	this.ie6 = (this.version.indexOf('MSIE 6')>0);
	this.min = (this.ns||this.ie);
}
var browser = new BrowserCheck();


if (browser.WinOs){
	if (browser.b == "ns"){
  	document.write('<link rel="stylesheet" href="$main_path/js/styles_ns.css">');
	} else{
 		document.write('<link rel="stylesheet" href="$main_path/js/styles_ie.css">');
	}
} else {
	if (browser.b == "ns"){
	  document.write('<link rel="stylesheet" href="$main_path/js/mac_styles_ns.css">');
	} else {
	  document.write('<link rel="stylesheet" href="$main_path/js/mac_styles_ie.css">');
	}
};

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


###    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);

$file_name ="new_cards.csv";

###	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) );
###	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
$uploaded_file = $upload_path . $file_name;
### if you want to have a time_STAMP prefix 
### $time_stamp   = time();
### $time_stamp  .= "_";
### $uploaded_file = $upload_path .$time_stamp .$file_name;
#### change :ValueTransfer('$time_stamp$file_name')

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

## 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" type="text" size="24" maxlength="24" value="$file_name" readonly        >
<br> 	 
<INPUT onclick="window.close()"; TYPE="button" VALUE="Confirm file name"> 
</body></html>
HTML

# end of perl script
