Files
HRM-System/public/optimize.php

135 lines
4.4 KiB
PHP

<?php
/**
* Standalone Laravel Cache Optimization Script
* Location: public/optimize.php
*/
// Enable full error reporting for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
define('SECURE_TOKEN', '7f3b89e902a2cdb8b5de9f2e30f14d8a245fca9b');
// Verify token
if (!isset($_GET['token']) || $_GET['token'] !== SECURE_TOKEN) {
header('HTTP/1.0 403 Forbidden');
echo "Access Denied: Invalid or missing security token.";
exit;
}
header('Content-Type: text/plain; charset=utf-8');
echo "=========================================\n";
echo "Starting Laravel Cache Optimization Workflow\n";
echo "Current Time: " . date('Y-m-d H:i:s') . "\n";
echo "=========================================\n\n";
// Print system diagnostics
echo "System Diagnostics:\n";
echo "PHP Version: " . PHP_VERSION . "\n";
echo "Disabled Functions: " . (ini_get('disable_functions') ?: '(none)') . "\n";
echo "shell_exec status: " . (function_exists('shell_exec') ? 'Enabled' : 'Disabled') . "\n";
echo "exec status: " . (function_exists('exec') ? 'Enabled' : 'Disabled') . "\n";
echo "system status: " . (function_exists('system') ? 'Enabled' : 'Disabled') . "\n";
echo "proc_open status: " . (function_exists('proc_open') ? 'Enabled' : 'Disabled') . "\n";
echo "-----------------------------------------\n\n";
// Robust Laravel Root Directory Auto-Detection
$targetDir = __DIR__;
for ($i = 0; $i < 4; $i++) {
if (file_exists($targetDir . '/artisan')) {
break;
}
$parent = realpath($targetDir . '/../');
if (!$parent || $parent === $targetDir) {
break;
}
$targetDir = $parent;
}
if (file_exists($targetDir . '/artisan')) {
chdir($targetDir);
echo "Laravel Root Detected: " . getcwd() . "\n\n";
} else {
chdir(__DIR__);
echo "Warning: artisan file not found in path hierarchy. Falling back to script directory: " . getcwd() . "\n\n";
}
// Helper function to execute commands with proc_open or standard fallbacks
function execute($cmd) {
echo "$ {$cmd}\n";
$output = '';
if (function_exists('proc_open')) {
$descriptorspec = [
0 => ["pipe", "r"], // stdin
1 => ["pipe", "w"], // stdout
2 => ["pipe", "w"] // stderr
];
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
proc_close($process);
$output = $stdout . $stderr;
} else {
$output = "Error: Failed to open process using proc_open.";
}
} elseif (function_exists('shell_exec')) {
$output = shell_exec($cmd);
} elseif (function_exists('exec')) {
$outputLines = [];
exec($cmd, $outputLines);
$output = implode("\n", $outputLines);
} elseif (function_exists('system')) {
ob_start();
system($cmd);
$output = ob_get_clean();
} else {
echo "Error: No command execution functions (proc_open, shell_exec, exec, system) are enabled on this hosting server.\n";
echo "-----------------------------------------\n";
return;
}
echo $output ? trim($output) : "(no output)";
echo "\n-----------------------------------------\n";
}
// Determine action
$action = isset($_GET['action']) ? $_GET['action'] : 'optimize';
if ($action === 'clear') {
echo "Action: Clearing All Caches\n\n";
execute("php artisan optimize:clear 2>&1");
} elseif ($action === 'migrate') {
echo "Action: Running Migrations\n\n";
execute("php artisan migrate --force 2>&1");
} elseif ($action === 'all') {
echo "Action: Full Optimization & Migration Workflow\n\n";
execute("php artisan optimize:clear 2>&1");
execute("php artisan config:cache 2>&1");
execute("php artisan route:cache 2>&1");
execute("php artisan view:cache 2>&1");
execute("php artisan event:cache 2>&1");
execute("php artisan migrate --force 2>&1");
} else {
echo "Action: Standard Cache Optimization\n\n";
execute("php artisan optimize:clear 2>&1");
execute("php artisan config:cache 2>&1");
execute("php artisan route:cache 2>&1");
execute("php artisan view:cache 2>&1");
execute("php artisan event:cache 2>&1");
}
echo "Optimization workflow finished.\n";