{

use esmith::ConfigDB;
use esmith::php;
my $pool_db = esmith::ConfigDB->open_ro('php') || esmith::ConfigDB->create('php');

foreach my $pool ($pool_db->get_all_by_prop(type => 'pool')){
  my $version = $pool->prop('Version') || '';
  my $status  = $pool->prop('status')  || 'enabled';
  next unless ($version eq $PHP_VERSION && $status eq 'enabled');

  my $key                 = $pool->key;
  my $pool_name           = 'php' . $version . '-' . $key;
  my $memory_limit        = $pool->prop('MemoryLimit')       || $defaultPHPproperties{'MemoryLimit'};
  my $max_execution_time  = $pool->prop('MaxExecutionTime')  || $defaultPHPproperties{'MaxExecutionTime'};
  my $max_input_time      = $pool->prop('MaxInputTime')      || $defaultPHPproperties{'MaxInputTime'};
  my $allow_url_fopen     = $pool->prop('AllowUrlFopen')     || $defaultPHPproperties{'AllowUrlFopen'};
  my $post_max_size       = $pool->prop('PostMaxSize')       || $defaultPHPproperties{'PostMaxSize'};
  my $upload_max_filesize = $pool->prop('UploadMaxFilesize') || $defaultPHPproperties{'UploadMaxFilesize'};
  my $file_upload         = $pool->prop('FileUpload')        || $defaultPHPproperties{'FileUpload'};
  my $open_basedir        = $pool->prop('PHPBaseDir')        || undef;
  my $disabled_functions  = $pool->prop('DisabledFunctions') || $defaultPHPproperties{'DisabledFunctions'};
  my $user                = $pool->prop('User')              || 'www';
  my $group               = $pool->prop('Group')             || $user;
  my $max_children        = $pool->prop('MaxChildren')       || '15';
  my $display_errors      = $pool->prop('DisplayErrors')     || 'disabled';
  my $log_errors          = $pool->prop('LogErrors')         || 'enabled';
  my $autoprepend         = $pool->prop('AutoPrependFile')  || $defaultPHPproperties{'AutoPrependFile'};
  my $MailForceSender     = $pool->prop('MailForceSender')  || "$key\@$DomainName";

  # Format vars
  my $autoprep            = ( $autoprepend eq "disabled") ? "" : "/usr/share/php/auth_translation.php";
  my $autoprepath         = ( $autoprepend eq "disabled") ? "" : ",/usr/share/php/";
  $file_upload        = ($file_upload =~ m/^1|yes|on|enabled$/) ?     'on' : 'off';
  $allow_url_fopen    = ($allow_url_fopen =~ m/^1|yes|on|enabled$/) ? 'on' : 'off';
  $display_errors     = ($display_errors =~ m/^1|yes|on|enabled$/) ?  'on' : 'off';
  $log_errors         = ($log_errors =~ m/^1|yes|on|enabled$/) ?      'on' : 'off';
  $disabled_functions = join(', ', split /[,;:]/, $disabled_functions);
  $open_basedir       = (defined $open_basedir) ? 'php_admin_value[open_basedir] = ' . join(':', split(/[,;:]/, $open_basedir . $autoprepath)) :
                                                                                   '; open_basedir is not set';

    my $apcu_so = "/opt/remi/php${PHP_VERSION}/root/usr/lib64/php/modules/apcu.so";
    my $apc = "; APCu disabled for $pool_name";
    # Check if APCu is enabled via ibay property or global default (defaults to disabled unless set)

    if (-f $apcu_so ) {
    my $apcEnabled = (defined $pool->prop('apcEnabled'))
        ? $pool->prop('apcEnabled')
        : ($defaultPHPproperties{'apcEnabled'} || 'disabled');

    # Only output settings if the Remi module is installed AND enabled in SME DB
    if (-f $apcu_so && $apcEnabled eq 'enabled') {
        my $apcTtl = (defined $pool->prop('apcTtl'))
            ? $pool->prop('apcTtl')
            : ($defaultPHPproperties{'apcTtl'} || '7200');

        $apc .= "php_admin_flag[apc.enabled] = on\n";
        $apc .= "php_admin_value[apc.ttl] = $apcTtl\n";
        }
        else {
            $apc .= "php_admin_flag[apc.enabled] = off\n";
    }
  }

###OPcache
    my $opcache_so  = "/opt/remi/php${PHP_VERSION}/root/usr/lib64/php/modules/opcache.so";
    my ($opcache_ini) = glob("/etc/opt/remi/php${PHP_VERSION}/php.d/*opcache.ini");
    my $opcache = "; opcache disable ";

    # Only configure OPcache directives if the Remi module and its active .ini exist
    if (-f $opcache_so && defined $opcache_ini && -f $opcache_ini) {

        my $opcacheEnabled = (defined $pool->prop('opcacheEnabled'))
            ? $pool->prop('opcacheEnabled')
            : ($defaultPHPproperties{'opcacheEnabled'} || 'enabled');

        if ($opcacheEnabled eq 'enabled') {

            my $opcacheRevalFreq = (defined $pool->prop('opcacheRevalFreq'))
                ? $pool->prop('opcacheRevalFreq')
                : ($defaultPHPproperties{'opcacheRevalFreq'} || '1');

            my $opcacheSaveComments = (defined $pool->prop('opcacheSaveComments'))
                ? $pool->prop('opcacheSaveComments')
                : ($defaultPHPproperties{'opcacheSaveComments'} || '1');

            my $opcacheJit = (defined $pool->prop('opcacheJit'))
                ? $pool->prop('opcacheJit')
                : ($defaultPHPproperties{'opcacheJit'} || 'off');

            $opcache .= "php_admin_flag[opcache.enable] = on\n";
            $opcache .= "php_admin_value[opcache.file_cache] =\n";
            $opcache .= "php_admin_value[opcache.enable_cli] = 1\n";
            $opcache .= "php_admin_value[opcache.save_comments] = $opcacheSaveComments\n";
            $opcache .= "php_admin_value[opcache.revalidate_freq] = $opcacheRevalFreq\n";
            
            # opcache.jit was introduced in PHP 8.0 (PHP_VERSION >= 80)
            if ($PHP_VERSION >= 80) {
                $opcache .= "php_admin_value[opcache.jit] = $opcacheJit\n";
            }
        }
        else {
            # Explicitly disable OPcache for this pool since Remi enables it globally by default
            $opcache .= "php_admin_flag[opcache.enable] = off\n";
            $opcache .= "php_admin_value[opcache.file_cache] =\n";
        }
    }



  $OUT .=<<"_EOF";

[$pool_name]
user = $user
group = $group
listen.owner = root
listen.group = www
listen.mode = 0660
listen = /run/php-fpm/$pool_name.sock
pm = dynamic
pm.max_children = $max_children
pm.start_servers = 3
pm.min_spare_servers = 3
pm.max_spare_servers = 4
pm.max_requests = 1000
slowlog = /var/log/php/$key/slow.log
php_admin_value[session.save_path] = /var/lib/php/$key/session
php_admin_value[opcache.file_cache]  = /var/lib/php/$key/opcache
php_admin_value[upload_tmp_dir] = /var/lib/php/$key/tmp
php_admin_value[sys_temp_dir] = /var/lib/php/$key/tmp
php_admin_value[mail.force_extra_parameters]            = '-f $MailForceSender'
php_admin_flag[display_errors] = $display_errors
php_admin_flag[log_errors] = $log_errors
php_admin_value[error_log] = /var/log/php/$key/error.log
php_admin_value[memory_limit] = $memory_limit
php_admin_value[max_execution_time] = $max_execution_time
php_admin_value[post_max_size] = $post_max_size
php_admin_value[upload_max_filesize] = $upload_max_filesize
php_admin_value[max_input_time] = $max_input_time
php_admin_value[disable_functions] = $disabled_functions
php_admin_flag[allow_url_fopen] = $allow_url_fopen
php_admin_flag[file_uploads] = $file_upload
php_admin_flag[session.cookie_httponly] = on
php_admin_flag[allow_url_include] = off
php_admin_value[auto_prepend_file] = $autoprep
php_value[session.save_handler] = files
$open_basedir
$apc
$opcache

_EOF

}

}
