# cpanel - Cpanel/Easy/IonCube5.pm                Copyright(c) 2016 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::IonCube5;

use strict;

use Cpanel::FileUtils ();

our $version = '5.1.1';

our $easyconfig = {
    'version'   => $version,
    'note'      => "Use IonCube Loader 5 at your own risk. Check the IonCube 5 page (by clicking “More Info”) and report any unexpected behavior to IonCube.",
    'verify_on' => 'Are you fully aware of the implications of this option and able to administer it?',
    'name'      => qq{IonCube 5 Loader v$version for PHP},
    'src_cd2'   => 'ioncube',
    'hastargz'  => 1,
    'alttargz'  => '',
    'url'       => 'http://go.cpanel.net/IonCube5',
    'implies'   => {
        'Cpanel::Easy::IonCubeLoader' => 0,
    },
    'modself' => sub {
        my $easy       = shift;
        my $self_hr    = shift;
        my $profile_hr = shift;

        # Only supported under PHP 5.x
        if ( $profile_hr->{'Cpanel::Easy::PHP5'} ) {

            # This controls the ordering of the build, forcing this module to be built *after* PHP
            $self_hr->{'depends'}{'optmods'} = $easy->get_php_optmods_depends($profile_hr);
            $self_hr->{'depends'}{'optmods'}{'Cpanel::Easy::IonCubeLoader'} = 0;
        }

        # All other versions of PHP aren't supported (e.g. Custom OptMods of PHP 4.x)
        else {
            $self_hr->{'hastargz'}                   = 0;
            $self_hr->{'skip'}                       = 1;
            $self_hr->{'treat_as_off_while_skipped'} = 1;
            return;
        }

        my $chipset = $easy->{'cpu_bits'};
        my $os      = $easy->{'generic_os'};
        my $release = 'generic';

        $self_hr->{'alttargz'} = $os . '-' . $release . '-' . $chipset;

    },
    'depends' => {
        'optmods' => {
            'Cpanel::Easy::IonCubeLoader' => 0,
        },
    },
    'when_i_am_off' => sub {
        my $self = shift;
        if ( !$self->get_param('makecpphp') && !$self->get_ns_value_from_profile('Cpanel::Easy::IonCubeLoader') ) {
            my $ini_file = '/usr/local/lib/php.ini';
            if ( -e $ini_file ) {
                Cpanel::FileUtils::regex_rep_file( $ini_file, { qr{^\s*zend_extension(_ts)?\s*=\s*"?\S+\/ioncube_loader_(fre|lin)_\d\.\d(_ts)?.so"?\s*$}is => q{}, }, {}, );
            }
        }

        return ( 1, 'Ok' );
    },
    'step' => {
        '1' => {
            'name'    => 'Install IonCube 5 loader',
            '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) = @_;

      KEYS:
        for my $key (qw(zend_extension zend_extension_ts)) {
            if ( exists $self->{'_'}{'php_ini'}{$key} ) {
                if ( grep / \/ioncube_loader_(?:fre|lin)_\d\.\d(?:_ts)? [.] so /xi, @{ $self->{'_'}{'php_ini'}{$key} } ) {
                    return 1;
                }
            }
        }
        return;
      }
}

1;
