-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshowpilot_proxy.php
More file actions
132 lines (121 loc) · 4.93 KB
/
Copy pathshowpilot_proxy.php
File metadata and controls
132 lines (121 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
// ShowPilot proxy — forwards UI requests to ShowPilot server so the browser
// never makes a cross-origin request (avoids ad blocker interference).
// Reads serverUrl + showToken from plugin config — token never touches browser.
header('Content-Type: application/json');
header('Cache-Control: no-store');
$skipJSsettings = true;
include_once "/opt/fpp/www/config.php";
include_once "/opt/fpp/www/common.php";
$pluginName = "showpilot";
$pluginConfigFile = $settings['configDirectory'] . "/plugin." . $pluginName;
$pluginSettings = @parse_ini_file($pluginConfigFile);
if (!$pluginSettings) {
http_response_code(500);
echo json_encode(['error' => 'Could not read plugin config']);
exit;
}
// Plugin config values may be stored URL-encoded or plain depending on
// which save path wrote them. Decode only when we see %XX patterns so that
// plain values containing '+' don't get mangled. See showpilot_listener.php
// smartDecode() for full rationale.
$smartDecode = function($v) {
if ($v === '' || $v === null) return $v;
return preg_match('/%[0-9a-fA-F]{2}/', $v) ? urldecode($v) : $v;
};
$serverUrl = rtrim($smartDecode($pluginSettings['serverUrl'] ?? ''), '/');
$showToken = $smartDecode($pluginSettings['showToken'] ?? '');
if (empty($serverUrl) || empty($showToken)) {
http_response_code(400);
echo json_encode(['error' => 'Server URL or Show Token not configured']);
exit;
}
// Only allow specific paths — don't let this become an open proxy
$allowedPaths = [
'/api/plugin/sync-sequences',
'/api/plugin/health',
'/api/plugin/heartbeat',
'/api/plugin/playing',
'/api/plugin/next',
'/api/plugin/state',
'/api/plugin/viewer-mode',
// Audio cache — used by syncAudioCache() in the UI
'/api/plugin/audio-cache/manifest',
'/api/plugin/audio-cache/link',
'/api/plugin/audio-cache/upload',
];
$path = $_GET['path'] ?? '';
$path = str_replace('\\', '/', $path);
if ($path !== '' && $path[0] !== '/') {
$path = '/' . $path;
}
// The JS passes hash/mediaName as query params embedded in the path string
// (e.g. /api/plugin/audio-cache/upload?hash=...&mediaName=...).
// Strip the query portion before the allow-list check and parse it as a
// fallback source for those params so the proxy picks them up correctly.
$embeddedQuery = '';
if (strpos($path, '?') !== false) {
[$path, $embeddedQuery] = explode('?', $path, 2);
}
parse_str($embeddedQuery, $embeddedParams);
$method = $_SERVER['REQUEST_METHOD'];
if (!in_array($path, $allowedPaths, true)) {
http_response_code(403);
echo json_encode(['error' => 'Path not allowed: ' . $path]);
exit;
}
$body = ($method === 'POST') ? file_get_contents('php://input') : null;
$headers = [
"Authorization: Bearer $showToken",
"Accept: application/json",
];
if ($body !== null) {
// For the audio upload endpoint the browser sends raw binary with a
// media Content-Type (audio/mpeg, audio/mp4, etc.). Forwarding that
// type intact is what tells ShowPilot what kind of audio it received.
// For every other POST the browser sends JSON, so we fall back to
// application/json — same behaviour as before for those paths.
$incomingContentType = $_SERVER['HTTP_CONTENT_TYPE']
?? $_SERVER['CONTENT_TYPE']
?? '';
$headers[] = $path === '/api/plugin/audio-cache/upload' && $incomingContentType !== ''
? 'Content-Type: ' . $incomingContentType
: 'Content-Type: application/json';
}
// The upload endpoint also passes hash and mediaName as query params.
// Forward them so ShowPilot receives them on the proxied request.
$queryForward = '';
if ($path === '/api/plugin/audio-cache/upload') {
$params = [];
// Accept hash/mediaName from either their own $_GET keys (future-proof)
// or from the query string that was embedded in the path param (current JS behaviour).
$hash = $_GET['hash'] ?? $embeddedParams['hash'] ?? '';
$mediaName = $_GET['mediaName'] ?? $embeddedParams['mediaName'] ?? '';
if ($hash) $params[] = 'hash=' . rawurlencode($hash);
if ($mediaName) $params[] = 'mediaName=' . rawurlencode($mediaName);
if ($params) $queryForward = '?' . implode('&', $params);
}
$options = [
'http' => [
'method' => $method,
'timeout' => 60, // raised from 15s — large audio uploads need more headroom
'header' => implode("\r\n", $headers),
'ignore_errors' => true,
],
];
if ($body !== null) {
$options['http']['content'] = $body;
}
$context = stream_context_create($options);
$result = @file_get_contents($serverUrl . $path . $queryForward, false, $context);
// Forward the response code
$code = 500;
if (isset($http_response_header)) {
foreach ($http_response_header as $h) {
if (preg_match('#^HTTP/\S+\s+(\d+)#', $h, $m)) {
$code = (int)$m[1];
}
}
}
http_response_code($code);
echo $result !== false ? $result : json_encode(['error' => 'Request to ShowPilot failed']);