#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - etc/ea4/999-php_ini                     Copyright 2023 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
use cPstrict;

use Whostmgr::API::1::Lang::PHP ();
use Cpanel::ProgLang;    # oooold versions do not have this so require it after the version check

my $php            = Cpanel::ProgLang->new( type => 'php' );
my $installed_phps = $php->get_installed_packages();           # this always returns an array ref
for my $php_version ( @{$installed_phps} ) {                   # ea-php74
    my $ini_settings       = Whostmgr::API::1::Lang::PHP::php_ini_get_directives( { version => $php_version } );
    my $current_mem_limits = {};
    next unless ref $ini_settings->{'directives'} eq 'ARRAY';
    foreach my $setting ( $ini_settings->{'directives'}->@* ) {
        next unless $setting->{'key'} eq 'memory_limit';
        $current_mem_limits = $setting;
        last;
    }

    next unless $current_mem_limits->{'key'} eq 'memory_limit';

    my $limit = $current_mem_limits->{'value'};
    next unless $limit =~ m/^\s*(\d+)M\s*$/a;    # look for a pattern like 32M. If we don't see it, pass on messing with it. root must have explicitly set this.
    $limit = $1;
    next if $limit >= 128;

    say "Setting php.ini to 128M for $php_version";
    my $metadata = {};
    Whostmgr::API::1::Lang::PHP::php_ini_set_directives( { version => $php_version, directive => "memory_limit:128M" }, $metadata );
}
