Unify push config resolution for subscribe and send paths
This commit is contained in:
parent
01b5584d16
commit
de1c753a42
2 changed files with 123 additions and 21 deletions
|
|
@ -1,11 +1,53 @@
|
|||
<?php
|
||||
header('Content-Type: application/json; charset=UTF-8');
|
||||
header('Cache-Control: no-store');
|
||||
$cfgFile = '/etc/mvlog/push.php';
|
||||
if (!is_file($cfgFile)) {
|
||||
|
||||
function mvlog_load_push_config_for_public_key(string $mvlogRoot): ?array {
|
||||
$candidates = [
|
||||
'/etc/mvlog/push.php',
|
||||
'/etc/mvlog/push.json',
|
||||
$mvlogRoot . '/push.json',
|
||||
(getenv('HOME') !== false ? getenv('HOME') . '/.config/mvlog/push.json' : null),
|
||||
];
|
||||
|
||||
foreach ($candidates as $candidate) {
|
||||
if (!$candidate || !is_file($candidate) || !is_readable($candidate)) continue;
|
||||
|
||||
$cfg = null;
|
||||
if (str_ends_with($candidate, '.php')) {
|
||||
$loaded = @require $candidate;
|
||||
if (is_array($loaded)) $cfg = $loaded;
|
||||
} else {
|
||||
$decoded = json_decode((string)@file_get_contents($candidate), true);
|
||||
if (is_array($decoded)) $cfg = $decoded;
|
||||
}
|
||||
|
||||
if (!is_array($cfg)) continue;
|
||||
|
||||
$public = (string)($cfg['public_key'] ?? $cfg['vapidPublicKey'] ?? '');
|
||||
$private = (string)($cfg['private_key'] ?? $cfg['vapidPrivateKey'] ?? '');
|
||||
$subject = (string)($cfg['subject'] ?? $cfg['vapidSubject'] ?? '');
|
||||
|
||||
if ($public !== '' && $private !== '') {
|
||||
return [
|
||||
'public_key' => $public,
|
||||
'private_key' => $private,
|
||||
'subject' => $subject,
|
||||
'_source' => $candidate,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
$cfg = mvlog_load_push_config_for_public_key(__DIR__);
|
||||
if (!$cfg) {
|
||||
echo json_encode(['enabled' => false]);
|
||||
exit;
|
||||
}
|
||||
$cfg = require $cfgFile;
|
||||
$public = (string)($cfg['public_key'] ?? '');
|
||||
echo json_encode(['enabled' => $public !== '', 'publicKey' => $public], JSON_UNESCAPED_SLASHES);
|
||||
|
||||
echo json_encode([
|
||||
'enabled' => true,
|
||||
'publicKey' => (string)$cfg['public_key'],
|
||||
], JSON_UNESCAPED_SLASHES);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue