????

Your IP : 18.188.97.200


Current Path : /proc/322175/task/322175/root/scripts/
Upload File :
Current File : //proc/322175/task/322175/root/scripts/update_apache_directive

#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - scripts/update_apache_directive         Copyright 2020 cPanel, L.L.C.
#                                                           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::HttpUtils::ApRestart::BgSafe ();
use Cpanel::Config::Httpd::EA4           ();

if ( !Cpanel::Config::Httpd::EA4::is_ea4() ) {
    print "Not applicable on non-webserver servers\n";
    exit;
}

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 $replaced_directives;
my %matching;
foreach my $setting (@ARGV) {
    next if $setting =~ m/^--/;
    next if $setting !~ m/==/;
    my ( $directive, $replacement ) = split /==/, $setting, 2;
    my @parts = split /\s+/, $directive;
    my $match;
    if ( scalar @parts > 1 ) {
        $match = join '\s+', @parts[ 1 .. $#parts ];
    }
    else {
        $match = $directive;
    }
    $matching{ $parts[0] }{$match} = $replacement;
    $replaced_directives .= ' ' . $directive;
}

if ( !scalar keys %matching ) {
    print "No directives specified\n";
    exit;
}

my @directives  = keys %matching;
my $start_match = join '|', @directives;

my $needs_rewrite;
my @httpdconf;
my $hlock = Cpanel::SafeFile::safeopen( \*HTTPC, '+<', $httpdconf );
if ( !$hlock ) {
    $logger->die("Could not edit $httpdconf");
}
LINE:
while ( my $line = <HTTPC> ) {
    if ( $line =~ m/^(\s*)($start_match)\s*/ ) {
        my $spacing   = $1;
        my $directive = $2;
        foreach my $match ( keys %{ $matching{$directive} } ) {
            if ( $line =~ m/\s*\Q$match\E/ ) {
                print "Replacing $line with $matching{$directive}{$match}\n" if $verbose;
                push @httpdconf, $spacing . $matching{$directive}{$match} . "\n";
                $needs_rewrite = 1;
                next LINE;
            }
        }
    }
    push @httpdconf, $line;
}

if ( !$needs_rewrite ) {
    Cpanel::SafeFile::safeclose( \*HTTPC, $hlock );
    exit;
}

print "Updating Apache configuration\n";

seek( HTTPC, 0, 0 );

print HTTPC join( '', @httpdconf );

truncate( HTTPC, tell(HTTPC) );

Cpanel::SafeFile::safeclose( \*HTTPC, $hlock );

if ($restart) {
    Cpanel::HttpUtils::ApRestart::BgSafe::restart();
}

exit;