'error', 'message' => 'OPCache is not installed']); exit(1); } // Function to get current OPCache status function get_opcache_status() { $status = opcache_get_status(false); if ($status === false) { return false; } return $status; } // Function to get OPCache configuration function get_opcache_config() { $config = opcache_get_configuration(); if ($config === false) { return false; } return $config; } // Function to find PHP configuration files function find_php_config_files() { $possible_paths = [ '/etc/php.d/', '/etc/php/8.4/cli/conf.d/', '/etc/php/8.3/cli/conf.d/', '/etc/php/8.2/cli/conf.d/', '/etc/php/8.1/cli/conf.d/', '/etc/php/8.0/cli/conf.d/', '/usr/local/lsws/lsphp84/etc/php.d/', '/usr/local/lsws/lsphp83/etc/php.d/', '/usr/local/lsws/lsphp82/etc/php.d/', '/usr/local/lsws/lsphp81/etc/php.d/', '/usr/local/lsws/lsphp80/etc/php.d/' ]; $config_files = []; foreach ($possible_paths as $path) { if (is_dir($path)) { $files = glob($path . '*opcache*'); if (!empty($files)) { $config_files = array_merge($config_files, $files); } } } return $config_files; } // Function to modify OPcache configuration function modify_opcache_config($enable) { $config_files = find_php_config_files(); if (empty($config_files)) { return false; } $success = false; foreach ($config_files as $file) { if (is_writable($file)) { $content = file_get_contents($file); if ($content !== false) { // Replace opcache.enable setting if ($enable) { $content = preg_replace('/^opcache\.enable\s*=\s*.*$/m', 'opcache.enable=1', $content); $content = preg_replace('/^;opcache\.enable\s*=\s*.*$/m', 'opcache.enable=1', $content); } else { $content = preg_replace('/^opcache\.enable\s*=\s*.*$/m', 'opcache.enable=0', $content); } // If no opcache.enable line exists, add it if (!preg_match('/^opcache\.enable\s*=/m', $content)) { $content .= "\nopcache.enable=" . ($enable ? '1' : '0') . "\n"; } if (file_put_contents($file, $content) !== false) { $success = true; } } } } return $success; } // Handle different actions switch ($action) { case 'enable': // Enable OPCache if (!modify_opcache_config(true)) { echo json_encode(['status' => 'error', 'message' => 'Failed to enable OPCache - could not modify configuration files']); exit(1); } $message = 'OPCache enabled successfully. PHP restart required to take effect.'; break; case 'disable': // Disable OPCache if (!modify_opcache_config(false)) { echo json_encode(['status' => 'error', 'message' => 'Failed to disable OPCache - could not modify configuration files']); exit(1); } $message = 'OPCache disabled successfully. PHP restart required to take effect.'; break; case 'status': default: // Get current status $status = get_opcache_status(); $config = get_opcache_config(); if ($status === false || $config === false) { echo json_encode(['status' => 'error', 'message' => 'Failed to get OPCache status']); exit(1); } echo json_encode([ 'status' => 'success', 'data' => [ 'enabled' => $config['directives']['opcache.enable'], 'memory_usage' => [ 'used' => $status['memory_usage']['used_memory'], 'free' => $status['memory_usage']['free_memory'], 'wasted' => $status['memory_usage']['wasted_memory'] ], 'statistics' => [ 'num_cached_scripts' => $status['opcache_statistics']['num_cached_scripts'], 'hits' => $status['opcache_statistics']['hits'], 'misses' => $status['opcache_statistics']['misses'] ] ] ], JSON_PRETTY_PRINT); exit(0); } // For enable/disable actions, return success message echo json_encode([ 'status' => 'success', 'message' => $message ], JSON_PRETTY_PRINT);