#!/usr/bin/perl
##
##  printenv -- demo CGI program which just prints its environment
## 


# Using Perl Modules on 1stmuse.com server
$path  = '/home/hz4pouml/perl';

BEGIN {
    my $homedir = ( getpwuid($>) )[7];
    my @user_include;
    foreach my $path (@INC) {
        if ( -d $homedir . '/perl' . $path ) {
            push @user_include, $homedir . '/perl' . $path;
        }
    }
    unshift @INC, @user_include;
}
# end of enabling Perl Modules on this server




use CGI qw (:standard);
use CGI::Carp qw(fatalsToBrowser);


print "Content-type: text/html\n\n";
print "CLEAN UP 1stmuse environment by JJP MMXXII<br>\n";
foreach $var (keys(%ENV)) {
        $value = $ENV{$var};
       # print "$var=\"$value\" <br>\n";
}


$verson =$];
print "<br>this perl version is $verson <br>";


my $path = "$ENV{DOCUMENT_ROOT}";
print "<br>root path is $path \n <br>\n <br>";


sub clean_directory{
      my $path = shift;
          
      opendir my $directory, "$path/" or die "Cannot open directory: $path  $!";
      my @f = readdir $directory;
      closedir $directory;
  
      foreach my $file (@f) {
          my $path_file = "$path/$file";
          if (-f $path_file ){     
            if (  $path_file =~ /php/i ) { 
              print $path_file,"   : PHP file was REMOVED ********** <br>\n"; 
              unlink $path_file; 
            }
            if (  $path_file =~ /error_log/i ) { 
              unlink $path_file; 
            }   
          
          }
          elsif(-d $path_file){
            if ($path_file !~ /\.$/) { 
              # print $path_file,"   : cleanup subfolder<br>\n";
              clean_directory($path_file);
            }           
          }
      }

}




opendir my $directory, "$path/" or die "Cannot open directory: $path  $!";
my @dir = readdir $directory;
closedir $directory;

my @folder;

foreach (@dir){
    $file =$path . "/" . $_;
    if (-f $file ){
      
       if (-f $file ){
          if (  $file =~ /php/i ) { 
            unlink $file; 
            print $file,"   : deleted file<br>\n";
          }
          if (  $file =~ /error_log/ ) { 
            unlink $file; 
          }    
    } 
        
    }elsif(-d $file){
          # print $file,"   : folder<br>\n";
          if ($file !~ /\.$/) {
             # print $file,"   : regular subdirectory<br>\n";  
             push (@folder, $file);           
          }
    }else{
         print $_,"   : other<br>\n";
    }
}


foreach my $subdirectory (@folder){
           # print $subdirectory,"   : subfolder to cleanup<br>\n";
           clean_directory($subdirectory);
}

 

