mb-admin/scripts/toggle_opcache.php

192 lines
5.6 KiB
PHP
Raw Normal View History

2025-06-13 17:17:12 +00:00
<?php
2025-06-13 17:23:54 +00:00
// Get the action from command line arguments
2025-06-13 17:17:12 +00:00
$action = $argv[1] ?? 'status';
2025-06-13 17:23:54 +00:00
// Check if OPCache is installed
if (!function_exists('opcache_get_status')) {
echo json_encode(['status' => 'error', 'message' => 'OPCache is not installed']);
2025-06-13 17:17:12 +00:00
exit(1);
}
2025-06-13 17:23:54 +00:00
// 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;
2025-06-13 17:17:12 +00:00
}
2025-06-16 17:02:24 +00:00
// 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/lsphp/etc/php.d/',
2025-06-16 17:02:24 +00:00
'/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/'
];
$possible_files = [
'/usr/local/lsws/lsphp/etc/php.d/10-opcache.ini',
'/etc/php.d/10-opcache.ini'
];
2025-06-16 17:02:24 +00:00
$config_files = [];
foreach ($possible_files as $file) {
if (is_file($file)) {
$config_files[] = $file;
}
}
$loaded_ini = php_ini_loaded_file();
if ($loaded_ini !== false && is_file($loaded_ini)) {
$content = file_get_contents($loaded_ini);
if ($content !== false && preg_match('/^\s*;?\s*opcache\.enable\s*=\s*/mi', $content)) {
$config_files[] = $loaded_ini;
}
}
$scanned_ini = php_ini_scanned_files();
if ($scanned_ini !== false && trim($scanned_ini) !== '') {
foreach (explode(',', $scanned_ini) as $ini_file) {
$ini_file = trim($ini_file);
if ($ini_file === '' || !is_file($ini_file)) {
continue;
}
if (stripos(basename($ini_file), 'opcache') !== false) {
$config_files[] = $ini_file;
}
}
}
2025-06-16 17:02:24 +00:00
foreach ($possible_paths as $path) {
if (is_dir($path)) {
$files = glob($path . '*opcache*');
if (!empty($files)) {
$config_files = array_merge($config_files, $files);
}
}
}
return array_values(array_unique($config_files));
2025-06-16 17:02:24 +00:00
}
// 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_file($file)) {
continue;
}
$content = file_get_contents($file);
if ($content === false) {
continue;
}
$replacement_count = 0;
$updated_content = preg_replace(
'/^\s*;?\s*opcache\.enable\s*=\s*.*$/mi',
'opcache.enable=' . ($enable ? '1' : '0'),
$content,
-1,
$replacement_count
);
if ($updated_content === null) {
continue;
}
// If no opcache.enable line exists, add it once.
if ($replacement_count === 0) {
$updated_content = rtrim($updated_content) . "\nopcache.enable=" . ($enable ? '1' : '0') . "\n";
}
if (file_put_contents($file, $updated_content) !== false) {
$success = true;
2025-06-16 17:02:24 +00:00
}
}
return $success;
}
2025-06-13 17:23:54 +00:00
// Handle different actions
2025-06-13 17:17:12 +00:00
switch ($action) {
case 'enable':
2025-06-13 17:23:54 +00:00
// Enable OPCache
2025-06-16 17:02:24 +00:00
if (!modify_opcache_config(true)) {
echo json_encode(['status' => 'error', 'message' => 'Failed to enable OPCache - could not modify configuration files']);
2025-06-13 17:23:54 +00:00
exit(1);
}
2025-06-16 17:02:24 +00:00
$message = 'OPCache enabled successfully. PHP restart required to take effect.';
2025-06-13 17:17:12 +00:00
break;
2025-06-13 17:23:54 +00:00
2025-06-13 17:17:12 +00:00
case 'disable':
2025-06-13 17:23:54 +00:00
// Disable OPCache
2025-06-16 17:02:24 +00:00
if (!modify_opcache_config(false)) {
echo json_encode(['status' => 'error', 'message' => 'Failed to disable OPCache - could not modify configuration files']);
2025-06-13 17:23:54 +00:00
exit(1);
}
2025-06-16 17:02:24 +00:00
$message = 'OPCache disabled successfully. PHP restart required to take effect.';
2025-06-13 17:17:12 +00:00
break;
2025-06-13 17:23:54 +00:00
case 'status':
2025-06-13 17:17:12 +00:00
default:
2025-06-13 17:23:54 +00:00
// 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);
}
2025-06-13 17:17:12 +00:00
2025-06-13 17:23:54 +00:00
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);
2025-06-13 17:17:12 +00:00
}
2025-06-16 17:02:24 +00:00
// For enable/disable actions, return success message
2025-06-13 17:23:54 +00:00
echo json_encode([
'status' => 'success',
2025-06-16 17:02:24 +00:00
'message' => $message
2025-06-13 17:23:54 +00:00
], JSON_PRETTY_PRINT);