????

Your IP : 3.145.96.163


Current Path : /proc/326615/root/scripts/
Upload File :
Current File : //proc/326615/root/scripts/check_cpanel_apache_aliases

#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - check_cpanel_apache_aliases               Copyright 2015 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;
use warnings;

use Cpanel::SafeFile                     ();
use Cpanel::Logger                       ();
use Cpanel::ConfigFiles::Apache          ();
use Cpanel::AdvConfig::apache::includes  ();
use Cpanel::HttpUtils::ApRestart::BgSafe ();
use Cpanel::Config::Httpd::EA3           ();

if ( !Cpanel::Config::Httpd::EA3::is_ea3() ) {
    print STDERR "check_cpanel_apache_aliases can only be run on an EA 3 system.\n";
    exit 2;
}

my $apacheconf = Cpanel::ConfigFiles::Apache->new();

my $logger = Cpanel::Logger->new();

my $httpdconf    = $apacheconf->file_conf();
my $restart      = @ARGV && grep( /^--no-restart$/, @ARGV ) ? 0 : 1;
my $verbose      = @ARGV && grep( /^--verbose$/, @ARGV ) ? 1 : 0;
my $alias_config = '/usr/local/cpanel/APACHE_CONFIG';
my $alt_config   = '/var/cpanel/conf/apache/APACHE_CONFIG';

if ( !-e $alias_config ) {
    print "Missing Alias and ScriptAias definition file!\n";
    exit 1;
}

Cpanel::AdvConfig::apache::includes::init();

my %add_lookup;
my %remove_lookup;
my @aliases;

$alias_config = $alt_config if ( -e $alt_config && !-z $alt_config );

if ( open my $alias_fh, '<', $alias_config ) {
    while ( my $line = readline $alias_fh ) {
        chomp $line;
        next if !$line || $line =~ m/^\s*$/;
        next if ( $line =~ m{\s*#} );    #allow comments
        $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::HttpUtils::ApRestart::BgSafe::restart();
}

exit;