79 lines
2.1 KiB
PHP
Executable File
79 lines
2.1 KiB
PHP
Executable File
#!/usr/local/bin/php
|
|
<?php
|
|
declare(strict_types=1);
|
|
|
|
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::load(Settings::EXTERNAL_SETTINGS);
|
|
|
|
$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;
|
|
}
|