#!/usr/bin/perl BEGIN { use lib '/usr/home/cfg' ; require push_inc ; } require cfg ; use CGI::Carp qw(fatalsToBrowser); use CGI; use File::Find; use File::Path 'rmtree'; use File::Copy; use File::Basename; use MIME::Lite; use Mail::Sendmail; use Date::Calc qw(:all); #--------------------------------------------------------------------------------------------------------------------------------------------------------------------- my ($q) = CGI -> new() ; $iaction = $q -> param('iaction') || '' ; $idelaction = $q -> param('idelaction') || '' ; $izip = $q -> param('izip') || '' ; # quote-Sep-2016 if ($izip) { our ($ztype,$zmonth,$zyear) = split(/\-/,$izip) ; } unless ((lc $username eq 'vivian') or ($username eq 'rory')) { print "<<<<<<<<<<<<<<<< UNAUTHORIZED >>>>>>>>>>>>>>>" ; exit ; } # unless ($username eq 'rory') { print "<<<<<<<<<<<<<<<< CLOSED FOR MAINTENANCE >>>>>>>>>>>>>>>" ; exit ; } my $uploadspath = "$htmlpath/uploads" ; # my $uploadspath = "$htmlpath/uploads_test" ; # my $archivezip = "$htmlpath/archives/uploads.zip" ; my $archivezipdir = "$htmlpath/archives" ; # our $debug = 1 ; our $to = 'vivian@filmfreight.co.za' ; if ($debug) { $to = 'rory@kre8it.co.za' ; } #--------------------------------------------------------------------------------------------------------------------------------------------------------------------- print "Content-type: text/html\n\n"; &today; if ($iaction eq ''){ &get_uploads ; &server_usage ; &display_screen1 ; } if ($iaction eq 'zip'){ &get_uploads ; &delete_previous_zips ; &archive_zip ; &send_email ; &update_sent ; &server_usage ; &display_screen1 ; } if ($iaction eq 'deluploads'){ &get_uploads ; &delete_uploads ; &server_usage ; &display_screen1 ; } exit ; #-------------------------------------------------------------------------------------------- sub get_uploads { &read_uploads_dir("$uploadspath") ; # &read_uploads_dir("$uploadspath/journeys") ; &process_uploads ; } #-------------------------------------------------------------------------------------------- sub update_sent { # if (-e $archivezip) { unlink $archivezip ; } unless ($izip) { return ; } $i{sent_to} = "$to" ; $i{send_date} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ; &db_min_upd('uploads_backup',"`month`='$zmonth' AND `year`='$zyear' AND `type`='$ztype'") ; } #-------------------------------------------------------------------------------------------- sub read_uploads_dir { my ($path) = @_ ; opendir(DIR,"$path") or die "cant open directory: $path - $!\n"; while(defined($folder = readdir(DIR))) { if (length $folder > 2) { my ($file,$type) = split(/\./,$folder) ; # if ($folder eq 'journeys') { next ; } # print "
$path" ; if (-d "$path/$folder"){ # check if directory &read_uploads_dir_drill_1("$path/$folder") ; } # $ff++; # if ($ff == 50) { last ; } } } closedir(DIR) ; } #-------------------------------------------------------------------------------------------- sub read_uploads_dir_drill_1 { my ($path) = @_ ; # print "
$path" ; opendir(DIR2, "$path") or die "cant open directory: $path - $!\n"; while (defined($folder2 = readdir(DIR2))) { if (length $folder2 > 2) { my ($file,$type) = split(/\./,$folder2) ; my @file_attributes = stat ("$path/$folder2") ; my $file_modified_time_readable = localtime ($file_attributes[9]) ; # print "
$folder2 : $file_modified_time_readable" ; my @file_name = split(/\_/,$folder2) ; my @date_data = split(/ /,$file_modified_time_readable) ; my $year = substr($file_modified_time_readable,-4,4) ; if ($izip) { if (($year eq $zyear) and ($zmonth eq $date_data[1]) and ($ztype eq $file_name[0])) { push @zip_file_arr, "$path/$folder2" ; } } # $files_to_zip_hash{$file_name[0]}{$date_data[4]}{$date_data[1]}++ ; $files_to_zip_hash{$file_name[0]}{$year}{$date_data[1]}++ ; # for (1 .. 30) { # if ($file_attributes[$_]) { print ', ' . $_ . '.' . $file_attributes[$_] ; } # } } } closedir(DIR2) ; } #-------------------------------------------------------------------------------------------- sub process_uploads { &db_min_ro('uploads_backup','*','','','') ; foreach my $id (sort {$db{uploads_backup}{$a}{year} cmp $db{uploads_backup}{$b}{year}} keys %{$db{uploads_backup}}) { my $send_date = $db{uploads_backup}{$id}{send_date} ; my $sent_to = $db{uploads_backup}{$id}{sent_to} ; my $bak_count = $db{uploads_backup}{$id}{count} ; my $bak_year = $db{uploads_backup}{$id}{year} ; my $bak_month = $db{uploads_backup}{$id}{month} ; my $bak_type = $db{uploads_backup}{$id}{type} ; my $ucf_bak_type = ucfirst $bak_type ; $db_entry_exists{$bak_year}{$bak_month}{$bak_type} = $bak_count ; $sent{$bak_year}{$bak_month}{$bak_type} = $sent_to ; my $print_sent_to = '' ; if ($sent_to) { $print_sent_to = qq(sent to $sent_to ($send_date)) ; } my $selected = '' ; if ($izip eq "$bak_type-$bak_month-$bak_year") { $selected = 'SELECTED' ; } if ($bak_type eq 'pod') { next ; } if ($bak_type eq 'vat') { next ; } $opts{zip} .= qq() ; } foreach my $type (keys %files_to_zip_hash) { foreach my $year (keys %{$files_to_zip_hash{$type}}) { foreach my $month (keys %{$files_to_zip_hash{$type}{$year}}) { # print "$type $month $year [$files_to_zip_hash{$type}{$year}{$month}] \n" ; %i = () ; if ($db_entry_exists{$year}{$month}{$type}) { unless ($db_entry_exists{$year}{$month}{$type} == $files_to_zip_hash{$type}{$year}{$month}) { $i{count} = "$files_to_zip_hash{$type}{$year}{$month}" ; $i{sent_to} = '' ; &db_min_upd('uploads_backup',"`month`='$month' AND `year`='$year' AND `type`='$type'") ; } } else { $i{month} = "$month" ; $i{type} = "$type" ; $i{year} = "$year" ; $i{count} = "$files_to_zip_hash{$type}{$year}{$month}" ; $i{sent_to} = '' ; &db_min_insert('uploads_backup') ; my $ucf_type = ucfirst $type ; $opts{zip} .= qq() ; } } } } $archivezip = "$htmlpath/archives/$izip.zip" ; our $g_archiveziplink = $archivezip ; $g_archiveziplink =~ s/\/var\/www\/html/https:\/\/www.ffwaybill.co.za/gi ; our $g_archivezipname = $archivezip ; $g_archivezipname =~ s/\/var\/www\/html//gi ; our $g_zip_link = qq(DOWNLOAD NOW as it will be deleted if older than a week : $g_archivezipname
); } #-------------------------------------------------------------------------------------------- sub delete_uploads { # CHECK ZIP EXISTS THEN OPEN DOWNLOAD LINK AFTER DELETE unless (-e $archivezip) { $alert = qq() ; return ; } # our $download_zip_after_delete = qq( # \$(function () { # window.open("$archiveziplink") # }); # ); our $download_zip_after_delete = qq( \$(document).ready(function(){ \$('#open-zip').submit(); }) ); my $sent_to = $sent{$zyear}{$zmonth}{$ztype} ; unless ($sent_to) { $alert = qq() ; return ; } my $deleted = '' ; foreach my $filepath (@zip_file_arr) { # print "unlink $filepath \n" ; $files_affected .= qq(delete $filepath
) ; unlink ($filepath) ; $deleted = 1 ; } if ($deleted) { $i{delete_date} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ; &db_min_upd('uploads_backup',"`month`='$zmonth' AND `year`='$zyear' AND `type`='$ztype'") ; } $alert = qq() ; } #-------------------------------------------------------------------------------------------- sub archive_zip { use Archive::Zip; use Archive::Zip::Tree ; $archivezip = "$htmlpath/archives/$izip.zip" ; if (-e $archivezip) { unlink $archivezip ; } my $zip = Archive::Zip->new() ; foreach my $filepath (@zip_file_arr) { # print "$filepath \n" ; $zip->addTree($filepath,''); # my $link = $filepath ; $link =~ s/\\var\\www\\html/https:\/\/www.ffwaybill.co.za/gi ; $link =~ s/\\/\//gi ; my $name = $filepath ; $name =~ s/\/var\/www\/html//gi ; my $link = $filepath ; $link =~ s/\/var\/www\/html/https:\/\/www.ffwaybill.co.za/gi ; $c++; $emsg .= qq( $c  $name  ) ; $files_affected .= qq(zipped $name
) ; } my $archivezipname = $archivezip ; $archivezipname =~ s/\/var\/www\/html//gi ; my $archiveziplink = $archivezip ; $archiveziplink =~ s/\/var\/www\/html/https:\/\/www.ffwaybill.co.za/gi ; $zip_link = qq(  Download ZIP now as it will be deleted : $archivezipname ); $zip->writeToFileNamed($archivezip) ; # or die "could not execute : $!" ; if (-e $archivezip) { $alert = qq() ; } else { $alert = qq() ; } } #-------------------------------------------------------------------------------------------- sub delete_previous_zips { opendir(DIR, "$archivezipdir") or die "cant open Directory $archivezipdir: $!\n"; while(defined($folder = readdir(DIR))) { if (length $folder > 2) { my ($file,$type) = split(/\./,$folder) ; #only delete if more than a week old my ($chk_year,$chk_month,$chk_day,$chk_hour,$chk_min,$chk_sec) = Add_Delta_DHMS($now_year,$now_mm,$now_dd,$now_hour,$now_min,$now_sec,-7,0,0,0); # check n days back $chk_month = sprintf("%02s", $chk_month) ; $chk_day = sprintf("%02s", $chk_day) ; my @file_attributes = stat ("$archivezipdir/$file.zip") ; # my $file_modified_time_readable = localtime ($file_attributes[9]) ; ($file_sec,$file_min,$file_hour,$file_dd,$file_mm,$file_year,$file_wday,$file_yday,$isdst)=localtime($file_attributes[9]); $file_year += 1900; $file_dd = sprintf("%02s", $file_dd) ; $file_mm = sprintf("%02s", $file_mm + 1) ; $file_ccyymmdd = $file_year . $file_mm . $file_dd ; if ("$file_ccyymmdd" < "$chk_year$chk_month$chk_day") { unlink ("$archivezipdir/$file.zip") or print "unable to unlink - $archivezipdir/$folder : $!"; # print "unlink - $archivezipdir/$folder"; } } } closedir(DIR) ; } #------------------------------------------------------------------------------------ sub archive_unzip { # my ($uploadpath,$uploadfile) = @_ ; # use Archive::Extract; # $ae = Archive::Extract->new(archive => "$uploadpath/$uploadfile.zip") ; # $ae->extract( to => "$uploadpath"); } #-------------------------------------------------------------------------------------------- sub server_usage { my $size = 0; find( sub { $size += -f $_ ? -s _ : 0 }, "$htmlpath" ); $size = $size / 1000000 ; # convert to MB $serverspace = 19000 ; # MB (19.00 GB) $usage = sprintf("%.2f",($size / $serverspace * 100)) ; # % if ($alert) { return ; } if ($usage > 90) { $alert = qq() ; } elsif ($usage > 80) { $alert = qq() ; } else { $alert = qq() ; } } #-------------------------------------------------------------------------------------------- sub display_screen1 { if ($files_affected) { $print_files_affected = qq(

Files

 
$g_zip_link $files_affected
 
); } $print_usage = qq(

Server Usage

 
$usage\%
 

Backup Option

 
 
 
$print_files_affected) ; $print_buttons = qq(
ZIP & SEND SELECTED UPLOADS DELETE SELECTED UPLOADS
) ; &screen1 ; } #------------------------------------------------------------------------------------------ sub screen1 { require archive ; exit; } #------------------------------------------------------------------------------------------ sub send_email { $msg = MIME::Lite -> new( From => 'Film Freight ', To => $to, Subject => "FF Backup : $izip : $now_year-$now_mm-$now_dd", Type => 'multipart/mixed' ); # my $boundary = "====" . time() . "===="; my $email_body = <
Film Freight

$zip_link $emsg
 Link

END_OF_BODY $msg -> attach( Type => 'text/html', Data => $email_body ); # $msg -> attach ( # Type => 'AUTO', # Path => "$archivezip", # Filename => "$izip.zip", # Disposition => 'attachment' # ) or die "Error adding $path : $attach $!\n"; $msg -> send ('smtp','localhost', Debug=>0 ); } #------------------------------------------------------------------------------- use today ; use common ; 1;