File: //proc/self/root/scripts.20110531.215904.25158/check_cpanel_apache_aliases
#!/usr/bin/perl
# cpanel - check_cpanel_apache_aliases 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::HttpUtils ();
use Cpanel::RcsRecord ();
use Cpanel::Logger ();
use Cpanel::AdvConfig::apache::includes ();
my $logger = Cpanel::Logger->new();
my $apache_base = '/usr/local/apache';
my $httpdconf = $apache_base . '/conf/httpd.conf';
my $restart = @ARGV && grep( /^--no-restart$/, @ARGV ) ? 0 : 1;
my $verbose = @ARGV && grep( /^--verbose$/, @ARGV ) ? 1 : 0;
if ( !-e '/usr/local/cpanel/APACHE_CONFIG' ) {
print "Missing Alias and ScriptAias definition file!\n";
exit 1;
}
Cpanel::AdvConfig::apache::includes::init();
my %add_lookup;
my %remove_lookup;
my @aliases;
if ( open my $alias_fh, '<', '/usr/local/cpanel/APACHE_CONFIG' ) {
while ( my $line = readline $alias_fh ) {
chomp $line;
next if !$line || $line =~ m/^\s*$/;
$line =~ s/(?:^\s+|\s+$)//g; # Just in case
my @items = split /\s+/, $line;
if ( $items[0] eq '*REMOVE*' ) {
$remove_lookup{ $items[1] }{ join ' ', @items[ 2 .. $#items ] } = 1;
}
elsif (@items) {
$add_lookup{ $items[0] }{ join( ' ', @items[ 1 .. $#items ] ) } = $line;
}
}
close $alias_fh;
}
my @directive_match = keys %add_lookup;
push @directive_match, keys %remove_lookup;
my $start_match = join '|', @directive_match;
my $hlock = Cpanel::SafeFile::safeopen( \*HTTPC, '+<', $httpdconf );
if ( !$hlock ) {
$logger->die("Could not edit $httpdconf");
}
my $needs_rewrite;
my @httpdconf;
LINE:
while ( my $line = <HTTPC> ) {
chomp $line;
if ( $line =~ m{^\s*($start_match)\s+(.+)\s*}o ) {
my $directive = $1;
my $value = $2;
if ( exists $remove_lookup{$directive} ) {
$value =~ s/(?:^\s+|\s+$)//g;
$value =~ s/\s+/ /g;
foreach my $match ( keys %{ $remove_lookup{$directive} } ) {
if ( $value eq $match ) {
$needs_rewrite = 1;
print "Removing $line\n" if $verbose;
next LINE;
}
}
}
if ( exists $add_lookup{$directive} ) {
foreach my $match ( keys %{ $add_lookup{$directive} } ) {
$value =~ s/(?:^\s+|\s+$)//g;
$value =~ s/\s+/ /g;
if ( $value eq $match ) {
push @httpdconf, $line;
delete $add_lookup{$directive}{$match};
$remove_lookup{$directive}{$match} = 1;
next LINE;
}
}
}
}
push @httpdconf, $line;
}
my @missing;
foreach my $directive ( sort keys %add_lookup ) {
foreach my $match ( sort keys %{ $add_lookup{$directive} } ) {
print "Adding $add_lookup{$directive}{$match}\n" if $verbose;
push @missing, $add_lookup{$directive}{$match};
}
}
if ( !$needs_rewrite && !@missing ) {
Cpanel::SafeFile::safeclose( \*HTTPC, $hlock );
exit;
}
push @httpdconf, @missing;
print "Updating Apache configuration\n" if $verbose;
seek( HTTPC, 0, 0 );
print HTTPC "\n" . join( "\n", @httpdconf ) . "\n";
truncate( HTTPC, tell(HTTPC) );
Cpanel::SafeFile::safeclose( \*HTTPC, $hlock );
if ($restart) {
Cpanel::RcsRecord::rcsrecord( $httpdconf, 'Added cPanel default Aliases' );
Cpanel::HttpUtils::safeaprestart();
}
exit;