# cpanel - Cpanel/Easy/PHPSuHosin.pm              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

package Cpanel::Easy::PHPSuHosin;

use Cpanel::PHPINI;

our $easyconfig = {
    'version'   => '0.9.38',
    'note'      => 'Please read documentation that can found by clicking "More Info" to fully understand this option',
    'verify_on' => 'Are you fully aware of the implications of this option and able to administer it?',
    'name'      => 'Suhosin %s for PHP',
    'src_cd2'   => 'suhosin',
    'hastargz'  => 1,
    'url'       => 'http://www.suhosin.org/stories/index.html',
    'modself'   => sub {
        my $easy       = shift;
        my $self_hr    = shift;
        my $profile_hr = shift;
        my $depends    = {};
        if ( $profile_hr->{'Cpanel::Easy::PHP5'} ) {
            my $ns = 'Cpanel::Easy::PHP5';
            eval qq{ require $ns; };

            my $php_version = get_php_version( $easy, '5' );
            return ( 0, 'Could not determine PHP version' ) unless $php_version;
            if ( $php_version !~ /^5\.[3456]\./ ) {
                $self_hr->{'hastargz'} = 0;
                $self_hr->{'skip'}     = 1;
                return ( 1, 'Ok' );
            }

            # The version above is for php 5.4/5.5; 5.3 uses a different one
            if ( $php_version =~ /^5\.3\./ ) {
                $self_hr->{'version'} = '0.9.33';
            }

            $self_hr->{'name'} = sprintf( $self_hr->{'name'}, $self_hr->{'version'} );

            foreach my $subversion ( $ns->versions() ) {
                $depends->{ $ns . '::' . $subversion } = 1 if ( $profile_hr->{ $ns . '::' . $subversion } );
            }
            $self_hr->{'depends'} = { 'optmods' => $depends };

        }
        else {
            $self_hr->{'hastargz'} = 0;
            $self_hr->{'skip'}     = 1;
        }

    },
    'when_i_am_off' => sub {
        my $self = shift;
        return ( 1, 'ok' ) if $self->get_param('makecpphp');

      EXT:
        for my $ext ( Cpanel::PHPINI::installed_extensions() ) {
            if ( $ext eq 'suhosin.so' ) {
                my ( $rc, $text ) = Cpanel::PHPINI::uninstall_extension($ext);
                print $text . "\n";
                last EXT;
            }
        }

        return ( 1, 'Ok' );
    },
    'step' => {
        '1' => {
            'name'    => 'Install PHPSuHosin',
            'command' => sub {
                my $self = shift;
                if ( !-e './cpanel-install' ) {
                    return ( 0, q{cpanel-install script is missing in source directory} );
                }
                my @rc = $self->run_system_cmd_returnable( [ './cpanel-install', '/usr/local' ] );
                return @rc;
            },
        },
    },
};

sub get_extension_status_coderef {
    return sub {
        my ($self) = @_;

        return if ref $self->{'_'}{'php_ini'}{'extension'} ne 'ARRAY';
        if ( grep / \A suhosin [.] so \z/xi, @{ $self->{'_'}{'php_ini'}{'extension'} } ) {
            return 1;
        }

        return;
      }
}

sub get_php_version {
    my ( $easy, $mainversion ) = @_;
    my $php_version = '';
    my $pns         = "Cpanel::Easy::PHP5";
    if ( $easy->{'working_profile'}{$pns} ) {
        for my $spec ( $pns->versions() ) {
            if ( $easy->{'working_profile'}{ $pns . '::' . $spec } ) {
                $php_version = '5.' . $spec;
                $php_version =~ s/\D+/./g;
                next;
            }
        }
    }

    return $php_version;
}

1;
