#!/usr/bin/perl
use CGI::Fast;
use CGI::Carp qw(fatalsToBrowser);
use strict;
use warnings;
my $socket = '/var/www/run/sockets/upload.sock';
# max file upload size MB
$CGI::POST_MAX = 1024 * 1024 * 500;
$ENV{FCGI_SOCKET_PATH} = $socket;
$ENV{FCGI_SOCKET_PERM} = 0775;
while (my $q = CGI::Fast->new) {
print qq(Content-Type: text/html; charset=utf-8\n\n);
print qq(\n);
print qq(\n);
my $head = &head_default;
print qq(
\n$head\n\n);
my $body;
if ( $q->param && $q->request_method() eq 'POST') {
$body = &upload($q);
} else {
$body = &body_default;
}
print qq(\n$body\n\n);
print qq(\n);
}
exit(0);
sub head_default {
my $css = &css;
my $head = <<"EOH";
Hello, World
$css
EOH
return($head);
}
sub css {
my $css=<
/* LABEL:has(~ DETAILS[open]) { display: none; }
}
/*]]>*/
EOC
return($css);
}
sub body_default {
my $body;
$body .= <Upload a File
EOB
return($body);
}
sub upload {
my ($q) = (@_);
if (!$q->param('fileToUpload')) {
return(0);
}
my $file = $q->param('fileToUpload');
$file =~ s/\s+/ /g;
$file =~ s/[^\w\ \_\.\-]+//g;
if (! $file) {
return(0);
}
my $upload_dir = "/var/www/uploads";
my $upload = $q->upload('fileToUpload');
open ( my $ufile, ">", "$upload_dir/$file" )
or die "$!";
binmode($ufile);
while ( my $data = <$upload> ) {
print $ufile $data;
}
close ($ufile);
my $body = <
The file $file was uploaded successfully.
Press the back button or close this tab.
EOB
return($body);
}