feat: complete production backend integration
This commit is contained in:
@@ -2,4 +2,81 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
echo "DirectAdmin vacation synchronization is executed by plugin handlers when DA backend is active.\n";
|
||||
define('PLUGIN_ROOT', dirname(__DIR__));
|
||||
define('PLUGIN_EXEC_DIR', PLUGIN_ROOT . '/exec');
|
||||
|
||||
foreach ([
|
||||
'Settings.php',
|
||||
'RuleRepository.php',
|
||||
'DirectAdminSyncRepository.php',
|
||||
'MailboxDirectory.php',
|
||||
'DirectAdminVacationApi.php',
|
||||
'DirectAdminVacationSyncService.php',
|
||||
] as $file) {
|
||||
require_once PLUGIN_EXEC_DIR . '/lib/' . $file;
|
||||
}
|
||||
|
||||
function sync_usage(): void
|
||||
{
|
||||
fwrite(STDERR, "Usage: sync_directadmin_vacations.php [--user=USERNAME]\n");
|
||||
}
|
||||
|
||||
try {
|
||||
$targetUser = '';
|
||||
foreach (array_slice($_SERVER['argv'] ?? [], 1) as $arg) {
|
||||
if (str_starts_with($arg, '--user=')) {
|
||||
$targetUser = substr($arg, 7);
|
||||
continue;
|
||||
}
|
||||
sync_usage();
|
||||
exit(2);
|
||||
}
|
||||
if ($targetUser === '') {
|
||||
$targetUser = getenv('USERNAME') ?: getenv('USER') ?: '';
|
||||
}
|
||||
if ($targetUser !== '' && !preg_match('/^[A-Za-z0-9._-]+$/', $targetUser)) {
|
||||
throw new InvalidArgumentException('Invalid username');
|
||||
}
|
||||
|
||||
$settings = Settings::load(Settings::EXTERNAL_SETTINGS);
|
||||
if ($settings->backendMode() !== 'da') {
|
||||
echo "DirectAdmin backend is not active; nothing to sync.\n";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
$users = $targetUser !== '' ? [$targetUser] : users_with_rules(Settings::DATA_DIR);
|
||||
$service = new DirectAdminVacationSyncService(
|
||||
new RuleRepository(),
|
||||
new DirectAdminSyncRepository(),
|
||||
new MailboxDirectory(),
|
||||
new DirectAdminVacationApi()
|
||||
);
|
||||
foreach ($users as $username) {
|
||||
$service->syncUser($username);
|
||||
echo "Synced {$username}\n";
|
||||
}
|
||||
exit(0);
|
||||
} catch (Throwable $e) {
|
||||
fwrite(STDERR, "DirectAdmin vacation synchronization failed: " . $e->getMessage() . "\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/** @return string[] */
|
||||
function users_with_rules(string $baseDir): array
|
||||
{
|
||||
$dir = rtrim($baseDir, '/') . '/users';
|
||||
if (!is_dir($dir)) {
|
||||
return [];
|
||||
}
|
||||
$users = [];
|
||||
foreach (scandir($dir) ?: [] as $entry) {
|
||||
if ($entry === '.' || $entry === '..' || !preg_match('/^[A-Za-z0-9._-]+$/', $entry)) {
|
||||
continue;
|
||||
}
|
||||
if (is_file($dir . '/' . $entry . '/rules.json')) {
|
||||
$users[] = $entry;
|
||||
}
|
||||
}
|
||||
sort($users);
|
||||
return $users;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user