1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#!/usr/bin/perl -w
# NOTA: no "T" switch to upload jpg otherwise Apache error
# ###############
# SCRIPT HEADERS
# ###############
use strict;
use warnings; # demande l'affichage des warnings (erreurs)
use CGI;
use CGI::Carp qw ( fatalsToBrowser ); # à virer en prod ?
use File::Basename;
use DateTime;
# ##########
# INIT
# ##########
my $cgi = new CGI;
my $upload_dir = "/mondomaine/upload";
my $log_file="//mondomaine/upload.log";
my $dt = DateTime->now;
# ##########
# LOGS
# ##########
#open LOG, ">> $log_file";
open LOG, "> $log_file";
print LOG "------------- New entry ($dt) ---------------\n";
print LOG "--- ENV ---\n";
foreach my $key (sort (keys %ENV)) {
print LOG "$key = $ENV{$key}\n";
}
print LOG "upload directory = $upload_dir\n";
print LOG "\n--- parametres ---\n";
foreach my $param ( $cgi->param ) {
print LOG "$param : ", $cgi->param($param), "\n";
}
close LOG;
# ################
# FILE UPLOAD
# ################
my %input;
my $max_size = 30_000;
print $cgi->header();
for my $key ( $cgi->param() ) {
$input{$key} = $cgi->param($key);
}
if ( $input{upload_demo} =~ /\.(exe|asp|php|jsp|cgi|pl|aspx|config|asax|asa|sh|js)$/ ) {
die "Invalid file extension. No executable file types permitted";
}
if ( length($input{upload_demo}) > 0 ) {
# get rid of the leading directories
( my $file_name = $input{upload_demo} ) =~ s/.*\\//;
my $upload_path = "$upload_dir/$file_name";
# open output file
open OUT, ">$upload_path" or die "Error opening $upload_path: $!";
binmode OUT;
my $buffer = '';
my $size = 0;
#In file handle context, upload_file is a file handle
while (my $chars_read = read $input{upload_demo}, $buffer, 4096) {
print OUT $buffer;
$size += $chars_read;
#if size is getting bigger than you want to handle, quit!
if ( $size > $max_size ) {
last;
}
}
close OUT;
if ( -z $upload_path or $size > $max_size ) {
unlink $upload_path;
}
}
# ##########
# HTML
# ##########
print <<END_HTML;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Thanks!</title>
</head>
<body>
<p>Thanks for uploading your file!</p>
</body>
</html>
END_HTML
exit 0; |