$max_upload = 1024 * 1024 * 1024 * 1; $file_location='/var/www/vhosts/somedomain.com/httpsdocs/content'; use CGI; use DBI; use Fcntl qw(:DEFAULT :flock); use File::Temp qw/ tempfile tempdir /; # This subroutine handles exiting with an error message. $error_file = "$file_location/tmp_err"; sub UploadFailure { $mes = shift; # Try to open error file to output message to $err_ok = open (ERRFILE,">", $error_file); if($err_ok) { print ERRFILE "$mes\n"; print ERRFILE "post_data_file: $post_data_file\nmonitor_file: $monitor_file\n"; close (ERRFILE); } # clear out the previous files if they exist if (-e "$post_data_file") { unlink("$post_data_file"); } print "$mes\n"; exit; } print "Content-type: text/html\n\n"; print ""; # Get the ClientID from the Query String @qstring=split(/&/,$ENV{'QUERY_STRING'}); @p1 = split(/=/,$qstring[0]); $ClientID = $p1[1]; $ClientID =~ s/[^0-9]//g; if($ClientID !~ /^[0-9]+$/ ) {&UploadFailure("Invalid ID");} $error_file = "$file_location/$ClientID/tmp_err"; $post_data_file = "$file_location/$ClientID/"."tmp_postdata"; $monitor_file = "$file_location/$ClientID/"."tmp_flength"; #$signal_file = "$file_location/$ClientID/"."tmp_signal"; #$qstring_file = "$file_location/$ClientID/"."tmp_qstring"; print "ClientID: $ClientID"; # Connect to the database $dbase = "XXXXX"; $dbuser = "XXXXX"; $dbpass = "XXXXX"; $host='localhost'; $drh = DBI ->install_driver('mysql'); $dbh = $drh->connect("$dbase:$host", $dbuser, $dbpass); # Test Connection &UploadFailure("Cannot Connect to Database") unless $dbh; $query='SELECT Clients.ID,ClientPackages.StorageSpace,ClientPackages.MaxFileSize FROM Clients INNER JOIN ClientPackages ON Clients.ClientPackagesID=ClientPackages.ID WHERE Clients.ID='.$ClientID.' AND Clients.Status="Active"'; $ClientQuery = $dbh->prepare($query); $ClientFound = $ClientQuery->execute; if($ClientFound<1) {&UploadFailure("Invalid Client ID");} ($ID,$MaxSpace,$MaxSize) = $ClientQuery->fetchrow_array; $MaxSpace*=1024 * 1024; $MaxSize*=1024*1024; # calculate how much space is currently being used $TmpFile=$file_location.'/'.$ClientID.'/disk_use.txt'; $command='/usr/bin/du -sb ' . $file_location.'/'.$ClientID.' > '.$TmpFile; system $command; open(FILEIN, "<$TmpFile") or die "Could not open $TmpFile\n"; $UsedSpace=0; while () { chop; ($DiskUsage, $Key) = split(' ', $_); $UsedSpace += $DiskUsage; }; close(FILEIN); unlink $TmpFile; $max_upload=$MaxSpace-$UsedSpace; if($max_upload>$MaxSize) {$max_upload=$MaxSize;} # See if there is enough space available for this file $content_type = $ENV{'CONTENT_TYPE'}; $total_bytes = $ENV{'CONTENT_LENGTH'}; #if($UsedSpace + $total_bytes > $MaxSpace) {&UploadFailure("This file is too large.");} $bytes_read=0; $|=1; #Make sure the directory exists opendir(IMD, "$file_location/$ClientID/") || &UploadFailure("Cannot open directory"); closedir(IMD); #Make sure this file is not larger than the maiximum file size for their account level if($total_bytes > $max_upload) { close (STDIN); &UploadFailure("The file is too large."); } # clear out the previous files if they exist if (-e "$post_data_file") { unlink("$post_data_file"); } if (-e "$monitor_file") { unlink("$monitor_file"); } if (-e "$error_file") { unlink("$error_file"); } # Initialize the upload status file. Contents are {Bytes Received}-{Bytes Total} sysopen(FH, $monitor_file, O_RDWR | O_CREAT) or &UploadFailure ("cannot open numfile $monitor_file : $!"); $ofh = select(FH); $| = 1; select ($ofh); seek(FH, 0, 0) or &UploadFailure ("cannot rewind numfile : $!"); print FH "0-".$total_bytes; # Receive a little bit of content before proceeding sleep(1); #Open a file to store the uploaded data open(TMP,">","$post_data_file") or &UploadFailure ("cannot open temp file"); binmode TMP, ':raw'; # Read in one 1KB chunk of data while (read (STDIN ,$LINE, 1024 * 100) && $bytes_read < $total_bytes ) { $bytes_read += length $LINE; select(undef, undef, undef,0.35); # sleep for 0.35 of a second. #rewind to the beginning of the file to overwrite what is there seek(FH, 0, 0); #output the current status print FH $bytes_read."-".$total_bytes; #store this data to the temporary upload file print TMP $LINE; } #If we received less bytes than the browser reported were coming, there is a problem with the upload. if($bytes_read<$total_bytes) { &UploadFailure("The upload was cancelled."); } else { #Mark the upload complete in the status file seek(FH, 0, 0); print FH "Complete "; #Extra space to overwrite any numbers that were there } close(FH); close (TMP); print "";