File: //scripts.20110531.215904.25158/ensure_includes
#!/usr/bin/perl
# cpanel - ensure_includes 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::EditHttpdconf ();
use Cpanel::HttpUtils::Version ();
die if !-e '/usr/local/apache/conf/httpd.conf';
my $apv = Cpanel::HttpUtils::Version::get_current_apache_version_key();
die 'Could not determine apache verison' if !$apv;
if ( $apv =~ m/2/ ) {
$apv = 2;
}
my $skip_rcs = 0;
if ( grep( /skip-rcs/, @ARGV ) ) {
$skip_rcs = 1;
}
Cpanel::EditHttpdconf::edit_httpdconf(
sub {
my ( $rw_fh, $safe_replace_content_coderef ) = @_;
my @new_contents;
push @new_contents, qq{Include "/usr/local/apache/conf/includes/pre_main_global.conf"\nInclude "/usr/local/apache/conf/includes/pre_main_$apv.conf"\n};
my $first_vhost = 0;
my $pre_vhost_include = 0;
my $pre_main_include = 0;
my $post_virtualhost_include = 0;
my $proxy_vhost = '';
my $in_proxy_vhost = 0;
my %proxy_ips = ( '*' => 1 );
while ( my $line = readline($rw_fh) ) {
if ($in_proxy_vhost) {
$proxy_vhost .= $line;
$in_proxy_vhost = 0 if ( $line =~ m/^\s*<\/virtualhost>/i );
next;
}
elsif ( $line !~ m/^\s*#/ ) {
if ( $line =~ m/^\s*Include\s+/) {
if ( $line =~ m/pre_main_/ ) {
$pre_main_include=1;
next;
} elsif ( $line =~ m/post_virtualhost_/ ) {
$post_virtualhost_include=1;
next;
} elsif ( !$pre_vhost_include && $line =~ m/pre_virtualhost_/ ) {
$pre_vhost_include = 1;
}
} elsif ( !$first_vhost && $line =~ /^\s*<virtualhost/i ) {
$first_vhost = 1;
if ( !$pre_vhost_include ) {
push @new_contents, qq{Include "/usr/local/apache/conf/includes/pre_virtualhost_global.conf"\nInclude "/usr/local/apache/conf/includes/pre_virtualhost_$apv.conf"\n};
}
} elsif ( $line =~ m/^\s*namevirtualhost\s+(\S+.*?)\s*$/i ) {
my @ip_ports = split( /\s+/, $1 );
foreach $ipp (@ip_ports) {
my ( $ip, $port ) = split( /:/, $ipp );
next if $ip eq '*';
next unless $ip && $port;
$proxy_ips{$ip . ':' . $port} = 1;
}
}
}
elsif ( $line =~ m/^\s*# CPANEL\/WHM\/WEBMAIL(?:\/WEBDISK)? PROXY SUBDOMAINS/ ) {
$in_proxy_vhost = 1;
$proxy_vhost .= $line;
next;
}
push @new_contents, $line;
} # end while
my $proxy_ip_list = join( ' ', sort keys %proxy_ips );
my $old_proxy_ip_list = '';
if ($proxy_vhost =~ m/<virtualhost ([^>]+)>/i) {
$old_proxy_ip_list = $1;
};
if ($pre_main_include && $post_virtualhost_include && $pre_vhost_include && $proxy_ip_list eq $old_proxy_ip_list) {
# No changes needed -- already up to date
$Cpanel::EditHttpdconf::local_flags{'0E0_means_no_changes_were_made'} = 1; # perldoc Cpanel::EditHttpdconf
return '0E0'; # perldoc Cpanel::SafeFile
}
$proxy_vhost =~ s/<virtualhost [^>]+>/<VirtualHost $proxy_ip_list>/i;
push @new_contents, qq{${proxy_vhost}Include "/usr/local/apache/conf/includes/post_virtualhost_global.conf"\nInclude "/usr/local/apache/conf/includes/post_virtualhost_$apv.conf"\n};
if ($safe_replace_content_coderef->( $rw_fh, \@new_contents )) {
return '0E0' if $skip_rcs; # perldoc Cpanel::SafeFile
return "Edited by $0"
}
return;
},
);