Files
zoneminder/web/views/view_hls.php
Isaac Connor 275b675ac8 fix: stop view_hls.php emitting /zm/index.php?index.php?view=… refs #4757
The segment-URL rewrite captured "index.php?…" in $1 and then prepended
$base_url . '?', producing a double-prefixed URL that drops the view=
routing parameter. Players fetched the manifest itself instead of the
mp4 byte range, so Firefox showed a spinner and Chrome silently failed.

Capture only the query string (everything after "index.php?") for both
the bare segment URLs and the EXT-X-MAP URI rewrite.

Same fix that landed (unmerged) in PR #4803 and PR #4806; pulled in here
so the full HLS path can be reviewed and merged together.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-16 11:21:21 -04:00

81 lines
2.2 KiB
PHP

<?php
//
// ZoneMinder HLS manifest server
// Serves the pre-built m3u8 manifest for an event's byte-range HLS playback.
//
if (!canView('Events')) {
$view = 'error';
return;
}
ob_end_clean();
require_once('includes/Event.php');
if (empty($_REQUEST['eid'])) {
header('HTTP/1.1 400 Bad Request');
die('Missing eid parameter');
}
$Event = new ZM\Event($_REQUEST['eid']);
if (!$Event->Id()) {
header('HTTP/1.1 404 Not Found');
die('Event not found');
}
$m3u8_path = $Event->Path() . '/index.m3u8';
if (!file_exists($m3u8_path)) {
header('HTTP/1.1 404 Not Found');
die('HLS manifest not available for this event');
}
// Don't serve an m3u8 with no fragments — the event may have just started
$m3u8_content_check = file_get_contents($m3u8_path);
if (strpos($m3u8_content_check, '#EXTINF:') === false) {
header('HTTP/1.1 404 Not Found');
die('HLS manifest has no fragments yet');
}
// Build auth query string for segment URLs
$auth_query = '';
if (ZM_OPT_USE_AUTH) {
if (ZM_AUTH_RELAY == 'hashed') {
$auth_query = '&auth=' . generateAuthHash(ZM_AUTH_HASH_IPS);
} else if (ZM_AUTH_RELAY == 'plain') {
$auth_query = '&user=' . $_SESSION['username'] . '&pass=' . $_SESSION['password'];
} else if (ZM_AUTH_RELAY == 'none') {
$auth_query = '&user=' . $_SESSION['username'];
}
}
// Read the m3u8 and inject auth tokens into segment URLs
$content = file_get_contents($m3u8_path);
// The m3u8 has relative URLs like "index.php?view=view_video&eid=123"
// We need to make them absolute with the server path and add auth
$Server = $Event->Server();
$base_url = $Server->PathToIndex();
// Replace bare relative segment URLs with full paths including auth.
// The m3u8 has lines like "index.php?view=view_video&eid=N&file=F" — capture
// only the query string (after "index.php?") so the replacement doesn't emit
// "/zm/index.php?index.php?view=…".
$content = preg_replace(
'/^index\.php\?(.+)$/m',
$base_url . '?$1' . $auth_query,
$content
);
// Also fix the EXT-X-MAP URI (initialization segment) the same way.
$content = preg_replace(
'/URI="index\.php\?([^"]+)"/m',
'URI="' . $base_url . '?$1' . $auth_query . '"',
$content
);
header('Content-Type: application/vnd.apple.mpegurl');
header('Cache-Control: no-cache');
echo $content;
exit;