#!/usr/bin/env perl

use warnings;
use strict;
use v5.10;
use Getopt::Long;
use Net::Domain qw(hostname);
use Sys::Syslog qw(:DEFAULT setlogsock);
use File::Basename;
use File::Copy qw(move);
use Proc::PID::File;
die "Already running!" if Proc::PID::File->running();

my $debug;
my $enforce;
my $host = hostname();

# 512kb
my $size_thresh_bytes = 512000;

GetOptions ("debug"   => \$debug,
            "size=s"  => \$size_thresh_bytes,
            "enforce" => \$enforce);

(bless {}, __PACKAGE__)->run();
exit;

sub run {
    my $self = shift;

    my $docroots = $self->get_docroots();
    $self->process_htaccess($docroots);

    return;
}

sub get_docroots {
    my $self = shift;

    my @docroots = qx{grep ^documentroot /var/cpanel/userdata/*/*};

    my $dpaths;
    foreach my $docroot (@docroots) {
        if ($docroot =~ m/documentroot: (.*)/) {
            $dpaths->{$1}++;
        }
    }

    return $dpaths;
}

sub process_htaccess {
    my $self = shift;
    my $docroots = shift;

    foreach my $dpath ( keys %{$docroots} ) {
        if (-f "$dpath/.htaccess") {
            my $fsize = -s "$dpath/.htaccess";
            if ($fsize >= $size_thresh_bytes) {
                say "$host:$dpath/.htaccess:$fsize" if $debug;
                $self->log("$dpath/.htaccess is over size threshold of $size_thresh_bytes (Found $fsize)");
                $self->disable_htaccess("$dpath/.htaccess") if $enforce;
            }
        }
    }

    return;
}

sub disable_htaccess {
    my $self = shift;
    my $htaccess = shift;

    #my $epoch = time();
    if (!-f "${htaccess}_size_violation_disable_strike1") {
        move("$htaccess", "${htaccess}_size_violation_disable_strike1")
            or $self->log( "FAIL: mv $htaccess ${htaccess}_size_violation_disable_strike1 : $!" );
    }
    elsif (!-f "${htaccess}_size_violation_disable_strike2") {
        move("$htaccess", "${htaccess}_size_violation_disable_strike2")
            or $self->log( "FAIL: mv $htaccess ${htaccess}_size_violation_disable_strike2 : $!" );
    }
    elsif (!-f "${htaccess}_size_violation_disable_strike3") {
        $self->log( "3rd violoation: Disabling $htaccess" );
        move("$htaccess", "${htaccess}_size_violation_disable_strike3")
            or $self->log( "FAIL: mv $htaccess ${htaccess}_size_violation_disable_strike3 : $!" );

        # Create an empty file owned by root forcing customer acknowledgement
        open my $fh, ">>$htaccess";
        close $fh;
    }
    else {
        $self->log( "4th violation: Removing $htaccess" );
        unlink $htaccess;
    }

    return;
}

sub log {
    my $self = shift;
    my $msg  = shift;

    openlog(basename($0), "pid", "local3");
    syslog("warning", "$msg");
    closelog( );

    return;
}

__END__;

Description:
Remove large htaccess files that have potential to cause apache memory and performance issues.

Logic:
First offense  -  Rename htaccess file
Second offense -  Rename htaccess file
Third offense  -  Rename htaccess file and replace with root owned 0 byte file
Fourth offense -  Delete htaccess file
