diff --git a/VIRTUAL_VACATION/PHP_README.md b/VIRTUAL_VACATION/PHP_README.md index f8a91cae..f2a2e456 100644 --- a/VIRTUAL_VACATION/PHP_README.md +++ b/VIRTUAL_VACATION/PHP_README.md @@ -232,6 +232,60 @@ file_enabled = false file = "/var/log/vacation.log" ``` +## Optional reply interval choices + +PostfixAdmin starts with `vacation_choice_of_reply` from `config.inc.php`. +A definition in `config.local.php` replaces that array. If `vacation.ini` +contains `[reply_intervals]`, that section replaces the effective array for +its web Vacation selector. If the file or section is absent, the effective +PostfixAdmin array remains unchanged, including numeric expressions and custom +intervals. The generated INI leaves this optional section commented out. + +```ini +[reply_intervals] +once = 0 +; every = 1 +day = 86400 +week = 604800 +``` + +Use the documented equivalents: `once = 0`, `every = 1`, `day = 86400`, +`week = 604800`. Uncomment `every` only when a reply to every message is wanted. +The section must contain at least one enabled entry; unknown names or other +values are configuration errors. Custom numeric intervals remain available +through the existing PHP array when this section is omitted. + +The web application defaults to `/etc/postfixadmin/vacation.ini`. For another +location, set this in `config.local.php` (and use the same file for the CLI): + +```php +$CONF['vacation_config'] = '/etc/mail/postfixadmin/vacation.ini'; +``` + +The web process must be able to read that file. Retain restrictive permissions +because it may contain credentials; grant access through the appropriate group, +not world-readable permissions. An existing unreadable or malformed file fails +rather than silently ignoring its choices. Set `vacation_config` to an empty +string to disable web INI loading explicitly. Independent Roundcube plugins +must load the same choices themselves; this does not change their settings. + +For example, without the INI section this legacy local array still works: + +```php +$CONF['vacation_choice_of_reply'] = array ( + 0 => 'reply_once', + // 1 => 'reply_every_mail', + 60*60 *24*1 => 'reply_once_per_day', + 60*60 *24*7 => 'reply_once_per_week' +); +``` + +The selector submits numeric seconds; stored mailbox intervals, XML-RPC and +transport timing remain unchanged. Existing records are not rewritten or +restricted by the offered choices. Daily and weekly intervals are elapsed time +since the last reply per sender, not calendar boundaries. No schema upgrade +is required. + Defaults and behavior: - `smtp.server`: `localhost` diff --git a/VIRTUAL_VACATION/vacation.php b/VIRTUAL_VACATION/vacation.php index 5766aded..b40bc3ba 100755 --- a/VIRTUAL_VACATION/vacation.php +++ b/VIRTUAL_VACATION/vacation.php @@ -21,6 +21,8 @@ use PDOException; use RuntimeException; use Throwable; +require_once dirname(__DIR__) . '/model/VacationReplyIntervals.php'; + abstract class VacationCliException extends RuntimeException { public function __construct(string $message, public readonly int $exitStatus) @@ -980,9 +982,13 @@ final class VacationCli throw new ConfigurationException("Invalid INI configuration: {$path}"); } $warnings = []; + $replyChoices = \VacationReplyIntervals::fromIni($parsed); $values = [ 'message_no_vacation_pattern' => VacationMessageInspector::DEFAULT_NO_VACATION_PATTERN, ]; + if ($replyChoices !== null) { + $values['vacation_choice_of_reply'] = $replyChoices; + } if (isset($parsed['postfixadmin']) && is_array($parsed['postfixadmin'])) { $values['postfixadmin_root'] = trim((string)($parsed['postfixadmin']['root'] ?? '')); } else { @@ -1218,6 +1224,18 @@ final class VacationCli $lines[] = 'account_name = true'; } } + array_push( + $lines, + '', + '; Optional web reply choices. Uncomment the section and desired entries.', + '; Without this section, config.local.php overrides config.inc.php as usual.', + '; Configure vacation_config in PostfixAdmin to point to this file.', + '; [reply_intervals]', + '; once = 0', + '; every = 1', + '; day = 86400', + '; week = 604800', + ); $logSyslog = filter_var($loggingOptions['syslog'] ?? true, FILTER_VALIDATE_BOOL); $logFileEnabled = filter_var($loggingOptions['file_enabled'] ?? false, FILTER_VALIDATE_BOOL); array_push($lines, '', '[logging]'); diff --git a/config.inc.php b/config.inc.php index dcae6903..61d2d38c 100644 --- a/config.inc.php +++ b/config.inc.php @@ -468,6 +468,10 @@ $CONF['vacation_control'] ='YES'; // Set to 'YES' if your domain admins should be able to edit user vacation. $CONF['vacation_control_admin'] = 'YES'; +// Optional PHP Vacation INI file used by the web reply selector. +// A declared [reply_intervals] section overrides the choices below. +$CONF['vacation_config'] = '/etc/postfixadmin/vacation.ini'; + // ReplyType options // If you want to define additional reply options put them in array below. // The array has the format seconds between replies => $PALANG text @@ -478,6 +482,7 @@ $CONF['vacation_choice_of_reply'] = array ( 0 => 'reply_once', // Sends only Once the message during Out of Office # considered annoying - only send a reply on every mail if you really need it # 1 => 'reply_every_mail', // Reply on every email + 60*60 *24*1 => 'reply_once_per_day', // Reply if last autoreply was at least a day ago 60*60 *24*7 => 'reply_once_per_week' // Reply if last autoreply was at least a week ago ); diff --git a/model/VacationReplyIntervals.php b/model/VacationReplyIntervals.php new file mode 100644 index 00000000..0ac31f2a --- /dev/null +++ b/model/VacationReplyIntervals.php @@ -0,0 +1,50 @@ + [0, 'reply_once'], + 'every' => [1, 'reply_every_mail'], + 'day' => [86400, 'reply_once_per_day'], + 'week' => [604800, 'reply_once_per_week'], + ]; + + /** @return array|null Null means the section was not declared. */ + public static function fromIni(array $configuration): ?array + { + if (!array_key_exists('reply_intervals', $configuration)) { + return null; + } + $section = $configuration['reply_intervals']; + if (!is_array($section) || $section === []) { + throw new InvalidArgumentException('Vacation reply_intervals must contain at least one choice'); + } + $choices = []; + foreach ($section as $name => $seconds) { + if (!isset(self::CHOICES[$name]) || !is_scalar($seconds)) { + throw new InvalidArgumentException('Invalid Vacation reply_intervals entry'); + } + [$interval, $label] = self::CHOICES[$name]; + if ((string)$seconds !== (string)$interval) { + throw new InvalidArgumentException('Vacation reply_intervals must use the documented numeric equivalents'); + } + $choices[$interval] = $label; + } + return $choices; + } + + /** Missing files/sections preserve the effective PostfixAdmin choices. */ + public static function load(array $fallback, string $path): array + { + if ($path === '' || !file_exists($path)) { + return $fallback; + } + // Parser warnings may contain configuration values, including credentials. + $configuration = @parse_ini_file($path, true, INI_SCANNER_RAW); + if ($configuration === false) { + throw new RuntimeException('Cannot read or parse the Vacation INI configuration'); + } + return self::fromIni($configuration) ?? $fallback; + } +} diff --git a/public/vacation.php b/public/vacation.php index fc95f9c8..ef2bd541 100644 --- a/public/vacation.php +++ b/public/vacation.php @@ -78,7 +78,11 @@ $error = 0; $vh = new VacationHandler($fUsername); -$choice_of_reply = Config::read_array('vacation_choice_of_reply'); +require_once dirname(__DIR__) . '/model/VacationReplyIntervals.php'; +$choice_of_reply = VacationReplyIntervals::load( + Config::read_array('vacation_choice_of_reply'), + Config::read_string('vacation_config') +); foreach (array_keys($choice_of_reply) as $key) { $choice_of_reply[$key] = Config::Lang($choice_of_reply[$key]); } diff --git a/tests/VacationReplyChoicesTest.php b/tests/VacationReplyChoicesTest.php new file mode 100644 index 00000000..97b77013 --- /dev/null +++ b/tests/VacationReplyChoicesTest.php @@ -0,0 +1,98 @@ +assertSame($expected, $choices); + } finally { + $CONF = $original; + if (file_exists($directory . '/config.local.php')) { + unlink($directory . '/config.local.php'); + } + if (file_exists($directory . '/vacation.ini')) { + unlink($directory . '/vacation.ini'); + } + unlink($directory . '/config.inc.php'); + rmdir($directory); + } + } + + public function testDisabledIniLoadingPreservesChoices(): void + { + $fallback = [3600 => 'x_hour']; + $this->assertSame($fallback, VacationReplyIntervals::load($fallback, '')); + } + + public function testMalformedIniFailsWithoutParserDetails(): void + { + $path = tempnam(sys_get_temp_dir(), 'pfa-reply-invalid-'); + try { + file_put_contents($path, "[reply_intervals\n"); + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage('Cannot read or parse the Vacation INI configuration'); + VacationReplyIntervals::load([0 => 'reply_once'], $path); + } finally { + unlink($path); + } + } + + public static function replyChoices(): array + { + return [ + 'defaults include daily but not every mail' => [ + null, + [0 => 'reply_once', 86400 => 'reply_once_per_day', 604800 => 'reply_once_per_week'], + ], + 'legacy local choices are not expanded' => [ + [0 => 'reply_once', 60 * 60 * 24 * 7 => 'reply_once_per_week'], + [0 => 'reply_once', 604800 => 'reply_once_per_week'], + ], + 'numeric expressions and custom intervals' => [ + [0 => 'reply_once', 1 => 'reply_every_mail', 60 * 60 * 24 => 'reply_once_per_day', 3600 => 'x_hour'], + [0 => 'reply_once', 1 => 'reply_every_mail', 86400 => 'reply_once_per_day', 3600 => 'x_hour'], + ], + 'empty local choices remain empty' => [[], []], + 'INI section overrides effective local choices' => [ + [3600 => 'x_hour'], + [0 => 'reply_once', 86400 => 'reply_once_per_day', 604800 => 'reply_once_per_week'], + "[reply_intervals]\nonce = 0\n; every = 1\nday = 86400\nweek = 604800\n", + ], + 'absent section preserves effective local choices' => [ + [3600 => 'x_hour'], + [3600 => 'x_hour'], + "[reply]\naccount_name = true\n", + ], + 'every mail remains opt-in' => [ + null, + [1 => 'reply_every_mail'], + "[reply_intervals]\nevery = 1\n", + ], + ]; + } +} diff --git a/tests/VacationTest.php b/tests/VacationTest.php index a43a40ef..b19b65f9 100644 --- a/tests/VacationTest.php +++ b/tests/VacationTest.php @@ -37,6 +37,51 @@ final class VacationShortWriteStream final class VacationTest extends TestCase { + public function testReplyIntervalConfiguration(): void + { + $cli = new VacationCli(); + $directory = $this->temporaryDirectory(); + try { + $path = $directory . '/vacation.ini'; + $generated = $cli->renderConfig('/var/www/postfixadmin', 'localhost', 25, 'mail.example.org'); + $this->assertStringContainsString('; [reply_intervals]', $generated); + file_put_contents($path, $generated); + $this->assertArrayNotHasKey('vacation_choice_of_reply', $cli->loadVacationConfig($path)['values']); + file_put_contents($path, $generated . "[reply_intervals]\nonce = 0\nday = 86400\nweek = 604800\n"); + $this->assertSame( + [0 => 'reply_once', 86400 => 'reply_once_per_day', 604800 => 'reply_once_per_week'], + $cli->loadVacationConfig($path)['values']['vacation_choice_of_reply'] + ); + } finally { + $this->removeDirectory($directory); + } + } + + #[\PHPUnit\Framework\Attributes\DataProvider('invalidReplyIntervals')] + public function testInvalidReplyIntervalsAreRejected(string $ini): void + { + $directory = $this->temporaryDirectory(); + try { + file_put_contents($directory . '/vacation.ini', $ini); + $this->expectException(\InvalidArgumentException::class); + (new VacationCli())->loadVacationConfig($directory . '/vacation.ini'); + } finally { + $this->removeDirectory($directory); + } + } + + public static function invalidReplyIntervals(): array + { + return [ + 'empty section' => ["[reply_intervals]\n"], + 'unknown choice' => ["[reply_intervals]\nhour = 3600\n"], + 'wrong equivalent' => ["[reply_intervals]\nday = 3600\n"], + 'negative interval' => ["[reply_intervals]\nday = -1\n"], + 'non-numeric interval' => ["[reply_intervals]\nday = invalid\n"], + 'array value' => ["[reply_intervals]\nday[] = 86400\n"], + ]; + } + public function testNoActionPrintsOneLine(): void { $input = fopen('php://memory', 'r+');