# cpanel - Cpanel/Easy/IonCubeLoader.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::IonCubeLoader;

use strict;

use Cpanel::FileUtils        ();
use Cpanel::Version::Compare ();

our $version = '4.7.5';

our $easyconfig = {
    'version'   => $version,
    'note'      => qq{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'      => qq{IonCube 4 Loader v$version for PHP},
    'src_cd2'   => 'ioncube',
    'hastargz'  => 1,
    'alttargz'  => '',
    'url'       => 'http://www.ioncube.com/loaders.php',
    'implies'   => {
        'Cpanel::Easy::IonCube5' => 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::IonCube5'} = 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::IonCube5' => 0,
        },
    },
    'when_i_am_off' => sub {
        my $self = shift;
        if ( !$self->get_param('makecpphp') && !$self->get_ns_value_from_profile('Cpanel::Easy::IonCube5') ) {
            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 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;
