Files
Claude 18783dc137 Fix errors unmasked by re-enabling error_reporting(E_ALL)
- config.example.php: error_reporting(0) -> E_ALL (live config.php matches); add JELLYFIN_URL/JELLYFIN_API_KEY placeholders

- history.php, jellyfin.php: drop require of nonexistent includes/auth.php + AuthMiddleware call (router enforces auth centrally) — both endpoints were fataling on every request

- remove stale kb_intent_generator .bak files from deployed tree

DB (not in repo): kb_facts.fact_value TEXT -> MEDIUMTEXT; ha/entity_map had failed every write since Jul 2 at >64KB

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 19:43:37 -05:00

108 lines
4.5 KiB
PHP

<?php
require_once __DIR__ . '/../config.php';
header('Content-Type: application/json');
$action = $_GET['action'] ?? 'sessions';
function jf_get(string $path): array {
$url = JELLYFIN_URL . $path . (str_contains($path, '?') ? '&' : '?') . 'api_key=' . JELLYFIN_API_KEY;
$ctx = stream_context_create(['http' => ['timeout' => 5, 'ignore_errors' => true]]);
$raw = @file_get_contents($url, false, $ctx);
return $raw ? (json_decode($raw, true) ?? []) : [];
}
switch ($action) {
case 'sessions':
$sessions = jf_get('/Sessions');
$active = array_filter($sessions, fn($s) => isset($s['NowPlayingItem']));
$out = [];
foreach ($active as $s) {
$np = $s['NowPlayingItem'];
$pos = $s['PlayState']['PositionTicks'] ?? 0;
$dur = $np['RunTimeTicks'] ?? 0;
$out[] = [
'session_id' => $s['Id'],
'user' => $s['UserName'] ?? 'Unknown',
'device' => $s['DeviceName'] ?? '',
'client' => $s['Client'] ?? '',
'title' => $np['Name'] ?? '',
'type' => $np['Type'] ?? '',
'series' => $np['SeriesName'] ?? null,
'year' => $np['ProductionYear'] ?? null,
'paused' => $s['PlayState']['IsPaused'] ?? false,
'position_pct'=> $dur > 0 ? round($pos / $dur * 100) : 0,
];
}
echo json_encode(['sessions' => array_values($out), 'total_active' => count($out)]);
break;
case 'library':
$movies = jf_get('/Items?IncludeItemTypes=Movie&Recursive=true&Limit=0');
$series = jf_get('/Items?IncludeItemTypes=Series&Recursive=true&Limit=0');
$episodes= jf_get('/Items?IncludeItemTypes=Episode&Recursive=true&Limit=0');
echo json_encode([
'movies' => $movies['TotalRecordCount'] ?? 0,
'series' => $series['TotalRecordCount'] ?? 0,
'episodes' => $episodes['TotalRecordCount'] ?? 0,
]);
break;
case 'search':
$q = trim($_GET['q'] ?? '');
if (!$q) { echo json_encode(['results' => []]); break; }
$data = jf_get('/Search/Hints?SearchTerm=' . urlencode($q) . '&Limit=8&IncludeItemTypes=Movie,Series,Episode');
$hints = $data['SearchHints'] ?? [];
$results = array_map(fn($h) => [
'id' => $h['ItemId'],
'name' => $h['Name'],
'type' => $h['Type'],
'year' => $h['ProductionYear'] ?? null,
'series'=> $h['Series'] ?? null,
], $hints);
echo json_encode(['results' => $results]);
break;
case 'recent':
$data = jf_get('/Items/Latest?Limit=8&IncludeItemTypes=Movie,Episode&Fields=Overview');
$out = array_map(fn($i) => [
'name' => $i['Name'],
'type' => $i['Type'],
'series' => $i['SeriesName'] ?? null,
'year' => $i['ProductionYear'] ?? null,
], is_array($data) ? $data : []);
echo json_encode(['recent' => $out]);
break;
case 'control':
$jfSessionId = $_GET['session_id'] ?? '';
$jfCommand = $_GET['command'] ?? '';
// General commands supported by Jellyfin session control
$allowed = ['TogglePause', 'Stop', 'NextTrack', 'PreviousTrack', 'VolumeUp', 'VolumeDown'];
if (!$jfSessionId || !in_array($jfCommand, $allowed, true)) {
// No session_id: get the first active session automatically
if (!$jfSessionId && in_array($jfCommand, $allowed, true)) {
$sessions = jf_get('/Sessions');
foreach ($sessions as $s) {
if (isset($s['NowPlayingItem'])) { $jfSessionId = $s['Id']; break; }
}
}
if (!$jfSessionId) { echo json_encode(['error' => 'No active session or invalid command']); break; }
}
$url = JELLYFIN_URL . '/Sessions/' . rawurlencode($jfSessionId) . '/Command/' . $jfCommand
. '?api_key=' . JELLYFIN_API_KEY;
$ctx = stream_context_create(['http' => [
'method' => 'POST',
'timeout' => 5,
'header' => 'Content-Type: application/json',
'content' => '{}',
'ignore_errors' => true,
]]);
@file_get_contents($url, false, $ctx);
echo json_encode(['success' => true, 'command' => $jfCommand, 'session_id' => $jfSessionId]);
break;
default:
echo json_encode(['error' => 'Unknown action']);
}