????

Your IP : 3.145.106.127


Current Path : /proc/322915/root/scripts/
Upload File :
Current File : //proc/322915/root/scripts/set_php_memory_limits

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

# cpanel - scripts/set_php_memory_limits           Copyright 2022 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

package scripts::set_php_memory_limits;

use strict;
use warnings;

use Getopt::Long                  ();
use Cpanel::Sys::Hardware::Memory ();
use Whostmgr::API::1::Lang::PHP   ();
use Cpanel::Imports;
use Data::Dumper;

our $gsw_message_dir      = '/var/cpanel/gsw_messages';
our $message_caller_depth = 1;

exit( run(@ARGV) ) if !caller();

sub run {

    my @args = @_;
    my ( $run, $gsw );
    Getopt::Long::GetOptionsFromArray(
        \@args,
        "run" => \$run,
        "gsw" => \$gsw,
    );

    if ( not( $run or $gsw ) ) {
        die qq{Must pass "--run" flag to execute script.\nThis script will adjust the memory_limit setting for all cPanel PHPs installed in /opt/cpanel/ to a reasonable setting based on the servers available memory.\n};
    }

    # We want to raise the PHP memory_limit if possible in regards to the amount of installed memory they have on the system
    my $installed_memory = eval { Cpanel::Sys::Hardware::Memory::get_installed(); };
    my ( $mid_mem, $high_mem ) = qw( 2000 4000 );
    my $set_memory;

    if ( $installed_memory < $mid_mem ) {
        $set_memory = '32M';
    }
    elsif ( ( $installed_memory >= $mid_mem ) && ( $installed_memory < $high_mem ) ) {
        $set_memory = '64M';
    }
    elsif ( $installed_memory >= $high_mem ) {
        $set_memory = '128M';
    }

    eval {
        my $installed_versions = Whostmgr::API::1::Lang::PHP::php_get_installed_versions();
        my $bytes              = $installed_memory * 1024 * 1024;                             # Cpanel::Sys::Hardware::Memory is MiB

        if ($gsw) {
            create_gsw_message(
                "php_memory_limit",
                "[% locale.maketext('We changed the webserver’s PHP “[output,strong,_1]” directive to “[_2]” to better support your server’s [format_bytes,_3] of memory.', 'memory_limit', '$set_memory', '$bytes') %]\n",
                "[% locale.maketext('You can change it at anytime via the [output,url,_1,MultiPHP INI Editor in WHM,target,_blank].', '../scripts2/multiphp_ini_editor') %]\n"
            );
        }

        foreach my $php_version ( @{ $installed_versions->{versions} } ) {
            my $metadata = {};
            Whostmgr::API::1::Lang::PHP::php_ini_set_directives( { version => $php_version, directive => "memory_limit:$set_memory" }, $metadata );
            logger->info("The memory limit for $php_version has been set to $set_memory based on the servers installed memory ($installed_memory MiB)");
            if ( !$metadata->{'result'} ) {
                logger->error("There was a problem setting the memory_limit in your php.ini files.\n");
            }
        }
    };
    return 0;
}

sub create_gsw_message {
    my ( $name, @lines ) = @_;

    # check for non words
    if ( $name =~ /\W/ ) {
        logger->warn("'$name' is not valid as a GSW message filename.\n");
        return;
    }

    # create the directory for custom Getting Started Wizard message tt files
    if ( !-e $gsw_message_dir ) {
        mkdir $gsw_message_dir, 0755;
    }

    if ( open( my $fh, ">", "$gsw_message_dir/$name.tt" ) ) {
        foreach my $line (@lines) {    # concat anything passed into the subroutine.
            print {$fh} $line;
        }

        close $fh;
    }
    else {
        logger->warn("Could not write to GSW message directory: $!");
        return;
    }
    return 1;
}
1;