45 lines
1.8 KiB
PHP
45 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Box\Mod\Mailgateway;
|
|
|
|
class RuntimeConfig
|
|
{
|
|
public static function get(string $key): mixed
|
|
{
|
|
static $settings = null;
|
|
$settings ??= self::load(__DIR__ . '/config.php');
|
|
return $settings[$key] ?? throw new \RuntimeException('Unknown Mail Gateway operational setting: ' . $key);
|
|
}
|
|
|
|
public static function load(string $path): array
|
|
{
|
|
$settings = require $path;
|
|
if (!is_array($settings)) {
|
|
throw new \RuntimeException('Mail Gateway config.php must return an array');
|
|
}
|
|
foreach (['poll_interval_seconds', 'messages_per_mailbox', 'poll_budget_seconds',
|
|
'imap_timeout_seconds', 'max_message_bytes', 'max_ticket_body_bytes',
|
|
'notification_cooldown_seconds', 'composer_timeout_seconds'] as $key) {
|
|
if (!isset($settings[$key]) || !is_int($settings[$key]) || $settings[$key] < 1) {
|
|
throw new \RuntimeException('Mail Gateway config.php requires a positive integer for ' . $key);
|
|
}
|
|
}
|
|
if ($settings['max_ticket_body_bytes'] > 65535) {
|
|
throw new \RuntimeException('Mail Gateway ticket body limit cannot exceed 65535 bytes');
|
|
}
|
|
foreach (['php_cli_paths', 'composer_paths'] as $key) {
|
|
if (!isset($settings[$key]) || !is_array($settings[$key]) || $settings[$key] === []) {
|
|
throw new \RuntimeException('Mail Gateway config.php requires executable paths for ' . $key);
|
|
}
|
|
foreach ($settings[$key] as $value) {
|
|
if (!is_string($value) || !str_starts_with($value, '/') || str_contains($value, "\0")) {
|
|
throw new \RuntimeException('Mail Gateway executable paths must be absolute paths');
|
|
}
|
|
}
|
|
}
|
|
return $settings;
|
|
}
|
|
}
|