#!/bin/sh
eval 'if [ -x /usr/local/cpanel/3rdparty/bin/perl ]; then exec /usr/local/cpanel/3rdparty/bin/perl -x -- $0 ${1+"$@"}; else exec /usr/bin/perl -x $0 ${1+"$@"}; fi;'
  if 0;

#!/usr/bin/perl

# cpanel - targz/Cpanel/Easy/PHPSuHosin.pm.tar.gz.d/suhosin/cpanel-install
#                                                 Copyright(c) 2014 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::SafeRun;
use Cpanel::FileUtils;

my $installed       = 0;
my $current_version = 'suhosin-0.9.38';
my $legacy_version  = 'suhosin-0.9.33';

my @prefixes = ('/usr/local');
if (@ARGV) {
    @prefixes = @ARGV;
}

foreach my $php_prefix (@prefixes) {
    $php_prefix =~ s/\/$//;
    my $php_ini = $php_prefix . '/lib/php.ini';
    if ( $php_prefix eq '/usr/local/cpanel/3rdparty' || $php_prefix eq '/var/cpanel/3rdparty' ) {
        $php_ini = '/usr/local/cpanel/3rdparty/etc/php.ini';
    }

    # determine version to use
    print "Determining PHP version\n";
    my @cmd;
    if ( -e '/usr/local/cpanel/scripts/php.ini' ) {
        @cmd = ( $php_prefix . '/bin/php', '-c', '/usr/local/cpanel/scripts/php.ini', '-v' );
    }
    else {
        @cmd = ( $php_prefix . '/bin/php', '-v' );
    }
    my $phpout = Cpanel::SafeRun::saferunallerrors(@cmd);
    my $php_version;
    my $src_version;
    if ( $phpout =~ m/^PHP\s+(\d+\.\d+)\.\d+\S*\s+\(([^\)]+)\)/m ) {
        $php_version = $1;
    }
    else {
        print "Could not determine PHP version from output:\n";
        print $phpout;
        next;
    }

    my $suhosin_version;
    if ( $php_version >= 5.4 ) {
        chdir $current_version || die "Could not chdir to $current_version";
        $suhosin_version = $current_version;
    }
    else {
        chdir $legacy_version || die "Could not chdir to $legacy_version";
        $suhosin_version = $legacy_version;
    }

    next unless ( -e $php_ini && -e $php_prefix . '/bin/php' && -e $php_prefix . '/bin/phpize' && -e $php_prefix . '/bin/php-config' );
    system( $php_prefix . '/bin/phpize' );
    system( './configure', '--with-php-config=' . $php_prefix . '/bin/php-config' ) && next;
    system('make clean') && next;
    system('make')       && next;
    system( 'make', 'install' ) && next;
    system( 'make', 'clean' )   && next;
    my ( $ok, $res ) = activate_suhosin( $php_prefix, $php_ini, $php_version, $suhosin_version );
    $installed = 1 if $ok;
    print $res . "\n";
    chdir '..' || die "Could not return to starting directory: $!";
}

if ($installed) {
    exit 0;
}
else {
    exit 1;
}

sub activate_suhosin {
    my $prefix          = shift;
    my $ini_file        = shift;
    my $php_version     = shift;
    my $suhosin_version = shift;
    my $phpconfig       = $prefix . '/bin/php-config';

    my $ext_dir = Cpanel::SafeRun::saferun( $phpconfig, '--extension-dir' );
    chomp $ext_dir;

    return ( 0, q{Could not locate EAccelerator extenstion at '[_1]}, $ext_dir . '/suhosin.so' ) unless ( -e $ext_dir . '/suhosin.so' );

    # We have all the information we need.  Update extension_dir and activate suhosin.so
    Cpanel::FileUtils::regex_rep_file( $ini_file, { qr{^\s*extension\s*=\s*"?suhosin\.so"?\s*$}is => q{} }, {}, );
    my $success = Cpanel::FileUtils::regex_rep_file(
        $ini_file,
        {
            qr{^\s*;?\s*extension_dir\s*=.*$}i => qq{extension_dir = "$ext_dir"\nextension="suhosin.so"},
        },
        {},
    );

    return ( 1, "SuHosin extension, version $suhosin_version, activated for php version $php_version" ) if $success;
    return ( 0, "Failed to activate SuHosin extension, version $suhosin_version, for php version $php_version" );
}

