111 lines
3.8 KiB
PHP
111 lines
3.8 KiB
PHP
<?php
|
|
// API: enable_show.php - Toggle Show and (if authenticated) attempt to send a "Show enabled" push.
|
|
require __DIR__ . '/../auth.php';
|
|
mvlog_require_login();
|
|
|
|
// Web-server side handler to toggle "Show" for an MVLog job and send a web-push immediately if appropriate.
|
|
// Preferred usage (POST): id=<article_id> show=1|0
|
|
// Legacy fallback (POST): job=<job_dir_name> show=1|0
|
|
|
|
$MVLOG_ROOT = dirname(__DIR__);
|
|
$id = trim((string)($_POST['id'] ?? ''));
|
|
$job = trim((string)($_POST['job'] ?? ''));
|
|
$show = isset($_POST['show']) ? filter_var($_POST['show'], FILTER_VALIDATE_BOOLEAN) : true;
|
|
|
|
function mvlog_article_id_is_valid(string $id): bool {
|
|
return (bool)preg_match('/^[0-9]{14}[a-f0-9]{16}$/', $id);
|
|
}
|
|
|
|
function mvlog_legacy_job_is_valid(string $job): bool {
|
|
return (bool)preg_match('/^[0-9]{8}[-_][A-Za-z0-9._-]+$/', $job);
|
|
}
|
|
|
|
function mvlog_read_article_id(string $jobdir): string {
|
|
$path = $jobdir . '/.mvlog-id';
|
|
if (!is_file($path)) return '';
|
|
$id = strtolower(trim((string)@file_get_contents($path)));
|
|
return mvlog_article_id_is_valid($id) ? $id : '';
|
|
}
|
|
|
|
function mvlog_find_jobdir_by_id(string $mvlogRoot, string $articleId): ?string {
|
|
if (!mvlog_article_id_is_valid($articleId)) return null;
|
|
|
|
$cacheFile = $mvlogRoot . '/cache/articles-index.json';
|
|
if (is_file($cacheFile)) {
|
|
$cache = json_decode((string)@file_get_contents($cacheFile), true);
|
|
if (is_array($cache) && is_array($cache['by_id'] ?? null)) {
|
|
$name = (string)($cache['by_id'][$articleId] ?? '');
|
|
if ($name !== '') {
|
|
$candidate = $mvlogRoot . '/in-dir/' . basename($name);
|
|
if (is_dir($candidate)) return $candidate;
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (glob($mvlogRoot . '/in-dir/*', GLOB_ONLYDIR) ?: [] as $dir) {
|
|
if (mvlog_read_article_id($dir) === $articleId) return $dir;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
function mvlog_resolve_jobdir(string $mvlogRoot, string $id, string $job, ?string &$resolvedJob, ?string &$resolvedId): ?string {
|
|
$resolvedJob = null;
|
|
$resolvedId = null;
|
|
|
|
if ($id !== '' && mvlog_article_id_is_valid($id)) {
|
|
$dir = mvlog_find_jobdir_by_id($mvlogRoot, strtolower($id));
|
|
if ($dir !== null) {
|
|
$resolvedJob = basename($dir);
|
|
$resolvedId = mvlog_read_article_id($dir);
|
|
return $dir;
|
|
}
|
|
}
|
|
|
|
if ($job !== '' && mvlog_legacy_job_is_valid($job)) {
|
|
$dir = $mvlogRoot . '/in-dir/' . basename($job);
|
|
if (is_dir($dir)) {
|
|
$resolvedJob = basename($dir);
|
|
$resolvedId = mvlog_read_article_id($dir);
|
|
return $dir;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
$resolvedJob = null;
|
|
$resolvedId = null;
|
|
$jobdir = mvlog_resolve_jobdir($MVLOG_ROOT, strtolower($id), $job, $resolvedJob, $resolvedId);
|
|
if ($jobdir === null) {
|
|
http_response_code(404);
|
|
echo 'job not found';
|
|
exit;
|
|
}
|
|
|
|
$enabled_file = "$jobdir/.movmaker-enabled";
|
|
|
|
if ($show) {
|
|
file_put_contents($enabled_file, date(DATE_ATOM) . PHP_EOL, LOCK_EX);
|
|
|
|
// Attempt to send push (non-fatal). Uses lib/send_push.php
|
|
$sent = false;
|
|
if (is_file(__DIR__ . '/../lib/send_push.php')) {
|
|
include_once __DIR__ . '/../lib/send_push.php';
|
|
try {
|
|
$sent = try_send_show_notification($resolvedId ?: $resolvedJob, $jobdir, $MVLOG_ROOT, '/var/log/mvlog_notify.log');
|
|
} catch (Throwable $e) {
|
|
error_log("[mvlog] notify exception for " . ($resolvedId ?: $resolvedJob) . ": " . $e->getMessage() . "\n", 3, '/var/log/mvlog_notify.log');
|
|
$sent = false;
|
|
}
|
|
} else {
|
|
error_log("[mvlog] send_push helper missing, cannot send notification for " . ($resolvedId ?: $resolvedJob) . "\n", 3, '/var/log/mvlog_notify.log');
|
|
}
|
|
|
|
echo $sent ? 'enabled' : 'enabled (no notification)';
|
|
} else {
|
|
@unlink($enabled_file);
|
|
echo 'disabled';
|
|
}
|
|
|
|
exit;
|