File: //proc/self/root/scripts.20110531.215904.25158/strip_apache_directive
#!/usr/bin/perl
# cpanel - strip_apache_directive Copyright(c) 2010 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use Cpanel::SafeFile ();
use Cpanel::RcsRecord ();
use Cpanel::HttpUtils ();
use Cpanel::Logger ();
my $logger = Cpanel::Logger->new();
my $t_file = ( grep( s/^--apache-conf=(\S+).*/$1/, @ARGV ) )[0];
my $httpdconf = $t_file && -e $t_file ? $t_file : '/usr/local/apache/conf/httpd.conf';
my $restart = @ARGV && grep( /^--no-restart$/, @ARGV ) ? 0 : 1;
my $verbose = @ARGV && grep( /^--verbose$/, @ARGV ) ? 1 : 0;
my $stripped_directives;
my %matching;
foreach my $directive (@ARGV) {
next if $directive =~ m/^--/;
my @parts = split /\s+/, $directive;
my $match;
if ( scalar @parts > 1 ) {
$match = join '\s+', @parts[ 1 .. $#parts ];
}
else {
$match = $directive;
}
$matching{ $parts[0] }{$match} = $directive;
$stripped_directives .= ' ' . $directive;
}
if ( !scalar keys %matching ) {
print "No directives specified\n";
exit;
}
my @directives = keys %matching;
my $start_match = join '|', @directives;
my @in_section;
my $needs_rewrite;
my @httpdconf;
if ( !-e $httpdconf ) { $logger->die("Could not find $httpdconf"); }
my $hlock = Cpanel::SafeFile::safeopen( \*HTTPC, '+<', $httpdconf );
if ( !$hlock ) {
$logger->die("Could not edit $httpdconf");
}
LINE:
while ( my $line = <HTTPC> ) {
if (@in_section) {
if ( $line =~ m/^\s*<\// && $line =~ m/^\s*<\/$in_section[$#in_section]/ ) {
print "Clearing section $in_section[$#in_section]\n" if $verbose;
pop @in_section;
}
elsif ( $line =~ m/^\s*($start_match)\s*/ ) {
my $directive = $1;
if ( $directive =~ m/^</ ) {
$directive =~ s/^<//;
print "Adding section $directive\n" if $verbose;
push @in_section, $directive;
}
}
print "Removing $line" if $verbose;
$needs_rewrite = 1;
next LINE;
}
if ( $line =~ m/^\s*($start_match)\s*/ ) {
my $directive = $1;
foreach my $match ( keys %{ $matching{$directive} } ) {
if ( $line =~ m/\s*\Q$match\E/ ) {
if ( $directive =~ m/^</ ) {
$directive =~ s/^<//;
print "Adding section $directive\n" if $verbose;
push @in_section, $directive;
}
print "Removing $line" if $verbose;
$needs_rewrite = 1;
next LINE;
}
}
}
push @httpdconf, $line;
}
if ( !$needs_rewrite ) {
Cpanel::SafeFile::safeclose( \*HTTPC, $hlock );
exit;
}
print "Updating Apache configuration\n";
if ( $restart && $httpdconf eq '/usr/local/apache/conf/httpd.conf' ) {
Cpanel::RcsRecord::rcsrecord( $httpdconf, "strip_apache_directive initial checkin" );
}
seek( HTTPC, 0, 0 );
print HTTPC join( '', @httpdconf );
truncate( HTTPC, tell(HTTPC) );
Cpanel::SafeFile::safeclose( \*HTTPC, $hlock );
if ($restart) {
Cpanel::RcsRecord::rcsrecord( $httpdconf, "strip_apache_directive:$stripped_directives" ) if ( $httpdconf eq '/usr/local/apache/conf/httpd.conf' );
Cpanel::HttpUtils::safeaprestart();
}
exit;