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
|
|
@ -8,6 +8,66 @@
|
||||||
* Returns true on successful send and cache update, false otherwise.
|
* Returns true on successful send and cache update, false otherwise.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
function mvlog_load_push_config(string $mvlog_root): ?array
|
||||||
|
{
|
||||||
|
$candidates = [
|
||||||
|
'/etc/mvlog/push.php',
|
||||||
|
'/etc/mvlog/push.json',
|
||||||
|
$mvlog_root . '/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 [
|
||||||
|
'subject' => $subject,
|
||||||
|
'public_key' => $public,
|
||||||
|
'private_key' => $private,
|
||||||
|
'_source' => $candidate,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mvlog_write_temp_push_json(array $cfg): ?string
|
||||||
|
{
|
||||||
|
$tmp = tempnam(sys_get_temp_dir(), 'mvlog-push-');
|
||||||
|
if ($tmp === false) return null;
|
||||||
|
|
||||||
|
$payload = json_encode([
|
||||||
|
'subject' => (string)($cfg['subject'] ?? ''),
|
||||||
|
'public_key' => (string)($cfg['public_key'] ?? ''),
|
||||||
|
'private_key' => (string)($cfg['private_key'] ?? ''),
|
||||||
|
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||||
|
|
||||||
|
if (!is_string($payload) || @file_put_contents($tmp, $payload, LOCK_EX) === false) {
|
||||||
|
@unlink($tmp);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@chmod($tmp, 0600);
|
||||||
|
return $tmp;
|
||||||
|
}
|
||||||
|
|
||||||
function try_send_show_notification($job, $jobdir, $mvlog_root = null, $logfile = null)
|
function try_send_show_notification($job, $jobdir, $mvlog_root = null, $logfile = null)
|
||||||
{
|
{
|
||||||
if ($mvlog_root === null) $mvlog_root = dirname(__DIR__);
|
if ($mvlog_root === null) $mvlog_root = dirname(__DIR__);
|
||||||
|
|
@ -15,11 +75,6 @@ function try_send_show_notification($job, $jobdir, $mvlog_root = null, $logfile
|
||||||
|
|
||||||
$subs_file = $mvlog_root . '/cache/push_subscriptions.json';
|
$subs_file = $mvlog_root . '/cache/push_subscriptions.json';
|
||||||
$cache_file = $mvlog_root . '/cache/push_notifications.json';
|
$cache_file = $mvlog_root . '/cache/push_notifications.json';
|
||||||
$push_config_candidates = [
|
|
||||||
$mvlog_root . '/push.json',
|
|
||||||
(getenv('HOME') !== false ? getenv('HOME') . '/.config/mvlog/push.json' : null),
|
|
||||||
'/etc/mvlog/push.json',
|
|
||||||
];
|
|
||||||
|
|
||||||
// Don't notify for hidden jobs
|
// Don't notify for hidden jobs
|
||||||
if (is_file($jobdir . '/.mvlog-hidden')) {
|
if (is_file($jobdir . '/.mvlog-hidden')) {
|
||||||
|
|
@ -54,14 +109,16 @@ function try_send_show_notification($job, $jobdir, $mvlog_root = null, $logfile
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine push config file
|
// Resolve push config consistently (same source model as push_config.php)
|
||||||
$push_config = null;
|
$cfg = mvlog_load_push_config($mvlog_root);
|
||||||
foreach ($push_config_candidates as $candidate) {
|
if (!$cfg) {
|
||||||
if (!$candidate) continue;
|
error_log("[mvlog] push config not found/invalid, skipping push for $job\n", 3, $logfile);
|
||||||
if (is_file($candidate) && is_readable($candidate)) { $push_config = $candidate; break; }
|
return false;
|
||||||
}
|
}
|
||||||
if (!$push_config) {
|
|
||||||
error_log("[mvlog] push config not found, skipping push for $job\n", 3, $logfile);
|
$tmp_push_config = mvlog_write_temp_push_json($cfg);
|
||||||
|
if (!$tmp_push_config) {
|
||||||
|
error_log("[mvlog] failed to create temp push config for $job\n", 3, $logfile);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,8 +146,8 @@ function try_send_show_notification($job, $jobdir, $mvlog_root = null, $logfile
|
||||||
|
|
||||||
// Call node send_push.js with subscriptions on stdin
|
// Call node send_push.js with subscriptions on stdin
|
||||||
$send_script = escapeshellarg($mvlog_root . '/send_push.js');
|
$send_script = escapeshellarg($mvlog_root . '/send_push.js');
|
||||||
$push_config_esc = escapeshellarg($push_config);
|
$push_config_esc = escapeshellarg($tmp_push_config);
|
||||||
$payload_esc = escapeshellarg($payload);
|
$payload_esc = escapeshellarg((string)$payload);
|
||||||
$cmd = "node $send_script $push_config_esc $payload_esc";
|
$cmd = "node $send_script $push_config_esc $payload_esc";
|
||||||
|
|
||||||
$descriptorspec = [
|
$descriptorspec = [
|
||||||
|
|
@ -101,6 +158,7 @@ function try_send_show_notification($job, $jobdir, $mvlog_root = null, $logfile
|
||||||
|
|
||||||
$proc = proc_open($cmd, $descriptorspec, $pipes);
|
$proc = proc_open($cmd, $descriptorspec, $pipes);
|
||||||
if (!is_resource($proc)) {
|
if (!is_resource($proc)) {
|
||||||
|
@unlink($tmp_push_config);
|
||||||
error_log("[mvlog] failed to start node send_push process for $job\n", 3, $logfile);
|
error_log("[mvlog] failed to start node send_push process for $job\n", 3, $logfile);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -113,8 +171,10 @@ function try_send_show_notification($job, $jobdir, $mvlog_root = null, $logfile
|
||||||
$stderr = stream_get_contents($pipes[2]); fclose($pipes[2]);
|
$stderr = stream_get_contents($pipes[2]); fclose($pipes[2]);
|
||||||
|
|
||||||
$rc = proc_close($proc);
|
$rc = proc_close($proc);
|
||||||
|
@unlink($tmp_push_config);
|
||||||
|
|
||||||
if ($rc !== 0) {
|
if ($rc !== 0) {
|
||||||
error_log("[mvlog] send_push failed for $job rc=$rc stdout=" . trim($stdout) . " stderr=" . trim($stderr) . "\n", 3, $logfile);
|
error_log("[mvlog] send_push failed for $job rc=$rc stdout=" . trim((string)$stdout) . " stderr=" . trim((string)$stderr) . "\n", 3, $logfile);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,6 +197,6 @@ function try_send_show_notification($job, $jobdir, $mvlog_root = null, $logfile
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
error_log("[mvlog] show-notification sent: $job\n", 3, $logfile);
|
error_log("[mvlog] show-notification sent: $job (cfg=" . ($cfg['_source'] ?? 'unknown') . ")\n", 3, $logfile);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,53 @@
|
||||||
<?php
|
<?php
|
||||||
header('Content-Type: application/json; charset=UTF-8');
|
header('Content-Type: application/json; charset=UTF-8');
|
||||||
header('Cache-Control: no-store');
|
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]);
|
echo json_encode(['enabled' => false]);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
$cfg = require $cfgFile;
|
|
||||||
$public = (string)($cfg['public_key'] ?? '');
|
echo json_encode([
|
||||||
echo json_encode(['enabled' => $public !== '', 'publicKey' => $public], JSON_UNESCAPED_SLASHES);
|
'enabled' => true,
|
||||||
|
'publicKey' => (string)$cfg['public_key'],
|
||||||
|
], JSON_UNESCAPED_SLASHES);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue