56 lines
1.7 KiB
PHP
56 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Commands;
|
|
|
|
use CodeIgniter\CLI\BaseCommand;
|
|
use CodeIgniter\CLI\CLI;
|
|
|
|
class Down extends BaseCommand
|
|
{
|
|
protected $group = 'Housekeeping';
|
|
protected $name = 'down';
|
|
protected $description = 'Put the application into maintenance mode.';
|
|
protected $usage = 'down [--allow IP] [--message "text"]';
|
|
protected $options = [
|
|
'allow' => 'IP address(es) to whitelist (comma-separated)',
|
|
'message' => 'Custom message for the maintenance page',
|
|
];
|
|
|
|
public function run(array $params)
|
|
{
|
|
$allowedIps = [];
|
|
|
|
// Get allowed IPs from command line
|
|
$allow = CLI::getOption('allow');
|
|
if ($allow !== null && $allow !== false) {
|
|
$allowedIps = array_map('trim', explode(',', (string) $allow));
|
|
}
|
|
|
|
// Get custom message
|
|
$message = CLI::getOption('message');
|
|
$message = ($message !== null && $message !== false) ? (string) $message : '';
|
|
|
|
// Create maintenance data
|
|
$data = [
|
|
'time' => date('Y-m-d H:i:s'),
|
|
'allowed' => array_values(array_filter($allowedIps)),
|
|
'message' => $message,
|
|
];
|
|
|
|
$filePath = WRITEPATH . 'down';
|
|
|
|
if (file_put_contents($filePath, json_encode($data, JSON_PRETTY_PRINT))) {
|
|
CLI::write('Application is now in maintenance mode.', 'yellow');
|
|
|
|
if (! empty($allowedIps)) {
|
|
CLI::write('Whitelisted IPs: ' . implode(', ', $allowedIps), 'green');
|
|
}
|
|
} else {
|
|
CLI::error('Failed to enable maintenance mode. Check writable directory permissions.');
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|