unsupported
  #   ˆl–äY>”ªh¥8 _¸®Õ1hßÀ„h?¿Â ?÷     #!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - ea_convert_php_ini                     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

# !!! This should only be run during migration !!!
#
# Description
#   Updates each user's 'php.ini' file so it's compatible with EA4.
#
# Conditions (ALL must be met for this script to run)
#   1. This is being run by the migrate_ea3_to_ea4 script.
#   2. The system default PHP version is assigned to the mod_suphp
#      Apache handler.
#   3. User is assigned to the system default PHP version.
#   4. The user has defined the 'suPHP_ConfigPath' setting within
#      an .htaccess file in the docroot of a vhost.
#
# Design
#   You'll notice 4 packages in this script.  These are separate
#   because:
#    1. It's overkill right now to create a whole new RPM package
#       which would contain the code we need to correctly parse
#       PHP ini files, get it into cpanel & whm, etc.
#    2. This used to be broken up into 2 scripts and designed to
#       allow future cmd-line execution.  However, it was too slow.
#       In an effort to speed it up (1.5 hrs to 5 mins against
#       10k accounts), the files were merged (/bin/cat a b > c) and
#       it was easier to combine the files and keep the packages
#       intact.
#
#   The 4 packages in here are as follows:
#    - Parse::PHP::Ini -- logic to parse/merge/render PHP ini files
#    - ea_convert_php_ini_file -- logic to convert a single php ini file
#    - ea_convert_php_ini_system -- logic to convert an entire
#        cpanel system
#    - main -- main script interface
#
# TODO:
#   1. Allow user to manually run this script to convert the system at-will.
#   2. Allow script to convert a vhost's ini files assigned to a php version
#      other than the system default.
#   3. Allow script to run if the php version is assigned to a non-suphp handler.
#   4. Allow user to convert an individual ini file.
#
# !!! This should only be run during migration !!!

package Parse::PHP::Ini;

use strict;
use warnings;
use Cpanel::Fcntl     ();
use Cpanel::ArrayFunc ();
use Time::HiRes qw( CLOCK_REALTIME );

# the special name we apply to things we find at file-scope within a php ini file.  this
# name should not be a valid section name
our $ROOT_NAME = '!_ROOT_!';

sub new {
    my $class = shift;
    my %args  = @_;
    require Tree::DAG_Node;    # this wasn't available on cpanel 11.54 and older
    return bless( \%args, $class );
}

# Accessor method for profiling the parser
# NOTE: Would be nice to have some sort of Aspect-oriented api for this
sub add_timestamp {
    my $self = shift;
    return 1 unless $self->{debug};

    my $label = shift;
    my $ts    = Time::HiRes::clock_gettime();
    push @{ $self->{timestamp} }, { label => $label, ts => $ts };

    return 1;
}

# Get a list of profiling timestamps
sub get_timestamps {
    my $self = shift;
    return ( defined $self->{timestamp} ? @{ $self->{timestamp} } : [] );
}

sub parse_init {
    my $self = shift;
    my %args = @_;
    my %struct;

    if ( $args{path} ) {
        require Tie::File;
        open( my $fh, '<', $args{path} ) or die Cpanel::Exception::create( 'IO::FileOpenError', { path => $args{path}, error => $!, mode => '<' } );
        my @content;
        my $tie = tie @content, 'Tie::File', $fh, recsep => "\n", mode => Cpanel::Fcntl::or_flags(qw(O_RDONLY));
        $struct{fh}      = $fh;
        $struct{content} = \@content;
    }
    elsif ( $args{str} ) {
        my @content = split( /\n/, ${ $args{str} } );
        $struct{content} = \@content;
    }
    else {
        die Cpanel::Exception::create( 'MissingParameter', { name => 'path' } ) if ( !defined $args{path} && !defined $args{str} );
    }

    return \%struct;
}

sub parse_clean {
    my ( $self, $struct ) = @_;

    if ( $struc