#!/usr/local/cpanel/3rdparty/bin/perl
#******************************************************************************
# Copyright 2009-2016, Way to the Web Limited
# URL: http://www.waytotheweb.com
# Email: sales@waytotheweb.com
#******************************************************************************
# Example to disable a directory containing a match using a .htaccess file
#
# NOTE: If you intend to use this script, copy it to a different filename as
# this file will be overwritten when cxs upgrades
use strict;
use File::Basename;

# Set to 1 to have more verbose output
my $verbose = 0;

if (@ARGV < 2) {print "\ncpanelsuspend.pl: Not enough Args to suspend: @ARGV\n"; exit}

# Assign arguments to variables
my $filename = $ARGV[0];
my $option = $ARGV[1];
my $message = $ARGV[2];
my $account = $ARGV[3];

# Get the directory name from the file name
my $dirname  = dirname($filename);

# If option is a Virus or Known Exploit
if ($option =~ /v|M$/) {

	# Check we have a directory
	if (-d $dirname) {

		# Check that we haven't already disabled a .htaccess file in this directory
		if (!(-e "$dirname/.htaccess.cxs_disable") and -e "$dirname/.htaccess") {
			system("/bin/cp","-a","$dirname/.htaccess","$dirname/.htaccess.cxs_disable");
		}
		elsif (-e "$dirname/.htaccess.cxs_disable" and !(-e "$dirname/.htaccess")) {
			unlink("$dirname/.htaccess.cxs_disable");
		}

		# Write out a new .htaccess file denying access
		open (my $HTACCESS, ">", "$dirname/.htaccess") or die "htaccessdisable.pl: Unable to create [$dirname/.htaccess] - $!";
		print $HTACCESS "#cxs has disabled web access to this directory\n";
		print $HTACCESS "deny from all\n";
		close ($HTACCESS) or die "htaccessdisable.pl: Unable to create [$dirname/.htaccess] - $!";

		if ($verbose) {print "\nhtaccessdisable.pl: Directory [$dirname] disabled\n"}

	} else {if ($verbose) {print "\nhtaccessdisable.pl: Directory for [$filename] does not exist\n"}}


} else {if ($verbose) {print "\nhtaccessdisable.pl: Not a suspend option ($option)\n"}}

exit;
