\!/ KyuuKazami \!/

Path : /scripts/
Upload :
Current File : //scripts/findphpversion

#!/usr/local/cpanel/3rdparty/bin/perl
# cpanel - findphpversion                          Copyright 2019 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::findphpversion;

use strict;
use warnings;

use Cpanel::ConfigFiles::Apache ();
use Cpanel::HttpUtils::Version  ();
use Cpanel::SafeRun::Simple     ();
use Cpanel::SafeFile            ();
use Cpanel::Services::Installed ();
use Cpanel::Logger              ();
use Getopt::Long                ();

my $logger = Cpanel::Logger->new();

exit( run(@ARGV) // 0 ) unless caller;

sub run {
    if ( !Cpanel::Services::Installed::service_is_installed('httpd') ) {
        $logger->info('httpd is not present on this system, exiting...');
        return;
    }

    return _find_php_version_and_update_version_file();
}

sub _find_php_version_and_update_version_file {
    Getopt::Long::GetOptions(
        'force' => \my $force,
    );

    my $apacheconf = Cpanel::ConfigFiles::Apache->new();

    my $usebin = 0;
    my $phpso  = '';
    my $sl     = Cpanel::SafeFile::safeopen( \*APC, $apacheconf->file_conf() );
    if ( !$sl ) {
        $logger->die( 'Could not read from ' . $apacheconf->file_conf() );
    }
    while (<APC>) {
        next if (/^#/);
        if (/(libphp\S+)/) {
            $phpso = $1;
            last();
        }
    }

    my $binphp = '/usr/local/bin/php';    # if it ever becomes so it could be other places use a find function

    Cpanel::SafeFile::safeclose( \*APC, $sl );
    if ( $phpso !~ /libphp\d+\.so/ && -e $binphp ) {
        $usebin = 1;
    }

    my $so_dir = Cpanel::HttpUtils::Version::get_current_apache_version_key() eq '1' ? 'libexec' : 'modules';

    my ( $phpsosize,      $phpsomtime )      = ( stat( $apacheconf->dir_base() . "/$so_dir/$phpso" ) )[ 7, 9 ];
    my ( $phpbinsize,     $phpbinmtime )     = ( stat $binphp )[ 7, 9 ];
    my ( $phpverfilesize, $phpverfilemtime ) = ( stat( $apacheconf->dir_conf() . '/php.version' ) )[ 7, 9 ];
    my $now = time();

    if (   !$force
        && defined $phpverfilesize
        && defined $phpverfilemtime
        && $phpverfilesize > 0
        && $phpverfilemtime > ( $phpbinmtime + 86400 )
        && $phpverfilemtime > ( $phpsomtime + 86400 )
        && $phpverfilemtime <= $now ) {
        print "PHP version file is up to date\n";
        return;
    }

    my $phpv = '';
    if ($usebin) {
        $phpv = Cpanel::SafeRun::Simple::saferun( $binphp, '-v' );
        $phpv = $1 if $phpv =~ /^PHP[\s\t]+(\S+)/;
    }
    else {
        if ( open( my $so_bin_fh, '<', $apacheconf->dir_base() . "/$so_dir/$phpso" ) ) {
            while ( readline($so_bin_fh) ) {
                if (/X-Powered-By:\s*[HP\/]*([\d+\.]*)/) {
                    $phpv = $1;
                    last;
                }
            }
            close($so_bin_fh);
        }
    }
    if ( open( my $phpv_fh, '>', $apacheconf->dir_conf() . '/php.version' ) ) {
        print {$phpv_fh} $phpv;
        close($phpv_fh);
    }

    $phpv ||= 'nothing';
    print "PHP version file has been updated to $phpv\n";

    # We don't call exec here because build_global_cache returns 141 and maintenance
    # doesn't like that.
    system '/usr/local/cpanel/bin/build_global_cache';
    return 0;
}

@KyuuKazami