# cpanel - SourceGuardian.pm                      Copyright(c) 2013 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::SourceGuardian;

use strict;

my $version = q{10.1};

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{SourceGuardian Loader $version for PHP},
    'src_cd2'   => 'sourceguardian',
    'hastargz'  => 1,
    'alttargz'  => '',
    'url'       => 'http://www.sourceguardian.com/',
    'modself'   => sub {
        my ( $easy, $self_hr, $profile_hr ) = @_;

        # 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);
        }

        # 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;
        }

        $self_hr->{'alttargz'} = sprintf( '%s-generic-%s', $easy->{'generic_os'}, $easy->{'cpu_bits'} );

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

        require Cpanel::SafeFile;
        require RcsRecord;

        my $ini_file = '/usr/local/lib/php.ini';
        if ( -e $ini_file ) {

            my $file_changed = 0;

            my $ini_lock = SafeFile::safeopen( \*INI, "+<", $ini_file );
            my @ini_data = <INI>;
            seek( INI, 0, 0 );
            foreach my $line (@ini_data) {
                if ( $line =~ m{^\s*extension(_ts)?\s*=\s*"?ixed\.\d+\.\d+(\.\d+)?(ts)?\.(fre|lin)"?\s*$} ) {
                    $file_changed = 1;
                    next;
                }
                print INI $line;
            }

            truncate( INI, tell(INI) );
            SafeFile::safeclose( \*INI, $ini_lock );

            RcsRecord::rcsrecord( $ini_file, 'SourceGuardian Loader removed' ) if ($file_changed);
        }

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

        return if ref $self->{'_'}{'php_ini'}{'extension'} ne 'ARRAY';
        if ( grep /^"?ixed\.\d+\.\d+(\.\d+)?(ts)?\.(fre|lin)"?/i, @{ $self->{'_'}{'php_ini'}{'extension'} } ) {
            return 1;
        }
        return;
      }
}

1;
