72 lines
2.7 KiB
PHP
72 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
if (PHP_SAPI !== 'cli') {
|
|
http_response_code(404);
|
|
exit;
|
|
}
|
|
|
|
$createFolders = false;
|
|
$mailboxId = null;
|
|
foreach (array_slice($argv, 1) as $argument) {
|
|
if ($argument === '--create-folders') {
|
|
$createFolders = true;
|
|
} elseif (str_starts_with($argument, '--mailbox=')) {
|
|
$mailboxId = substr($argument, strlen('--mailbox='));
|
|
} else {
|
|
fwrite(STDERR, "Usage: php tools/test-imap.php [--mailbox=email-or-id] [--create-folders]\n");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
require dirname(__DIR__, 3) . '/load.php';
|
|
try {
|
|
$store = new \Box\Mod\Mailgateway\Settings($di);
|
|
$mailboxes = $store->getAll();
|
|
$id = $mailboxId;
|
|
if ($id === null) {
|
|
$id = isset($mailboxes['default']) ? 'default' : array_key_first($mailboxes);
|
|
} elseif (str_contains($id, '@')) {
|
|
$id = null;
|
|
foreach ($mailboxes as $candidate => $mailbox) {
|
|
if (strcasecmp($mailbox['mailbox'], $mailboxId) === 0) {
|
|
$id = $candidate;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if ($id === null) {
|
|
throw new \FOSSBilling\InformationException('Mailbox not found');
|
|
}
|
|
$settings = $store->get($id);
|
|
printf("Checking %s at %s:%d using %s with certificate validation enabled...\n",
|
|
$settings['mailbox'], $settings['host'], $settings['port'],
|
|
$settings['encryption'] === 'ssl' ? 'implicit TLS' : 'STARTTLS');
|
|
$result = (new \Box\Mod\Mailgateway\ImapConnectionTest())->test($settings, $createFolders);
|
|
// A read-only check does not change the saved readiness state.
|
|
if ($createFolders) {
|
|
$store->recordTest($id, $settings, $result);
|
|
}
|
|
echo $result['connected'] ? "IMAP login succeeded.\n" : "IMAP login failed.\n";
|
|
if ($result['connected']) {
|
|
echo 'Native MOVE support: ', $result['move_supported'] === true ? 'available' : 'unavailable or unverified', PHP_EOL;
|
|
}
|
|
foreach ($result['folders'] as $folder) {
|
|
printf("%s — %s\n", $folder['path'], $folder['error']
|
|
?? ($folder['created'] ? 'created' : ($folder['exists'] ? 'found' : 'MISSING')));
|
|
}
|
|
foreach ($result['errors'] as $error) {
|
|
fwrite(STDERR, $error . PHP_EOL);
|
|
}
|
|
echo $createFolders ? "Folder setup finished. " : "Read-only check finished. ";
|
|
echo "No messages were fetched, marked, moved, deleted, or imported.\n";
|
|
exit($result['success'] ? 0 : ($result['connected'] && $result['move_supported'] === true ? 2 : 1));
|
|
} catch (\FOSSBilling\InformationException $error) {
|
|
fwrite(STDERR, $error->getMessage() . PHP_EOL);
|
|
exit(1);
|
|
} catch (\Throwable $error) {
|
|
fwrite(STDERR, 'IMAP check could not complete (' . $error::class . "). Check the module configuration and tables.\n");
|
|
exit(1);
|
|
}
|