#!/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(
| Link |
|---|