stream.php checked the target event's permission using the request
string and then packed intval() of it. On 32-bit PHP intval('2147483648')
is 2147483647, so the check and zms could see different events, and a
non-numeric or array value was cast to whatever intval() made of it.
The ids now have to be digit strings that fit in this PHP's int, or the
request is refused. The checked int is what gets packed. The packing
moved to includes/stream_set_event.php so tests/php can check the bytes
against EventStream::DecodeSetEvent's layout.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ajax/stream.php sends stream commands (query, pause, seek, zoom, quit)
to the zms whose unix socket matches a connkey, with no permission
check. Connkeys are six random digits chosen when the stream starts,
so any logged-in user who guessed or saw one could read the status of
another user's stream, of a monitor they may not view included, and
pause or end it. A second zms started with the same connkey also waited
on the first one's lock and then took over its socket.
zms already authenticates the user and checks their access to the
monitor or event before it streams. Keep that user's id and put it in
the lock, command socket and reply socket names
(zms-<connkey>-<user id>s.sock); stream.php builds the names from the
caller's own id through zmsSocketPath(), so a request reaches only the
caller's streams. Without authentication the id is 0 on both sides.
Adds a StreamBase test in tests/zm_stream.cpp and
tests/php/test_zms_socket_path.php, which pin the same names. The
existing command-socket-teardown test is updated for the owner-suffixed
names (it postdates the reporter's base commit).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
When an event is streamed by zms (the MJPEG/mpeg path, not the video.js MP4
player), zms advanced to the next/prev event itself with a hardcoded query:
SELECT Id FROM Events WHERE MonitorId = <same> AND Id <>> <current> ...
That ignores the filter and sort the user is viewing, so gapless playback of a
cross-monitor filtered list skipped every event that did not belong to the
current event's monitor -- e.g. playing monitor A's event jumped straight to
A's next event, hopping over all the interleaved monitor B events. The earlier
getNearEvents fix only corrected the video.js path and the Prev/Next buttons;
this zms auto-advance was a separate path and still wrong.
Rather than reimplement filter/sort knowledge in C++, let the UI -- which
already computes the correct next/prev from the active filter via getNearEvents
-- tell zms which events those are. A new CMD_SET_EVENT command carries a
direction, a target event id, and the source event the hint was computed for;
EventStream stores them and checkEventLoaded loads the UI-provided event when
advancing. When the UI has not supplied a usable hint it falls back to the
existing by-monitor query, so behaviour is unchanged for clients that do not
send the command.
The event view sends CMD_SET_EVENT for both next and prev after each
getNearEvents response, but only when playback is served by zms (vid == null);
the video.js path already navigates correctly on its own.
Hardening in this path:
- Authorisation: zms only authorises the event the stream started on and loads
forwarded targets without re-checking, so a client could hand it an event
from a monitor it cannot view. ajax/stream.php now resolves the target's
monitor and requires canView('Events', MonitorId) before forwarding (gated on
ZM_OPT_USE_AUTH, matching zms).
- Filter boundaries: getNearEvents returns 0 when the filter has no neighbour
in a direction. A hint now records whether the UI supplied a value, so an
explicit 0 stops at the boundary instead of being treated as "no hint" and
falling back to the same-monitor query (which would leave the filtered list).
- Stale hints: next and prev arrive as separate requests and can land out of
order. Each hint carries the source event id; processCommand refuses to store
one whose source is not the event playing (so a late command cannot overwrite
a valid hint), and checkEventLoaded honours a stored hint only while its
source still matches. The event view tags each hint with the getNearEvents
response's own EventId, not the current event, so a slow response cannot send
a self-referential hint that loops zms on one event.
- Deleted targets: a cached neighbour may be removed by retention or another
user before it is used. checkEventLoaded checks existence first and falls back
rather than hitting loadEventData's fatal "not found" path; ajax/stream.php
treats a target that no longer exists as a filter boundary instead of
returning an error that would flag the stream broken and stop UI polling.
- Cross-resolution MPEG: the mpeg encoder is created once at the first event's
dimensions and cannot resize mid-stream. Frame and text-frame paths share a
scaleToEncoder() helper that scales a later, differently sized image (a
cross-monitor advance, or a boundary text frame built at another monitor's
size) to the encoder's dimensions before encoding rather than overrunning its
buffers.
- Portable packing: stream.php encodes the ids as big-endian 32-bit words
instead of pack('J'), which 32-bit PHP (still supported in the status decode)
cannot feed a 64-bit value.
Known limitation: in a multi-server install with non-shared storage a
cross-monitor neighbour can live on another server, whose recording this zms
cannot read. Such an advance degrades to failed frames; zms self-advance is
effectively same-server (or shared-storage) only. Routing it to the serving
server, as the video.js reload path does, is left to a separate change.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
ajax/stream.php only included `auth` in its reply when it differed from
the hash the request carried. That hash comes from auth_relay, while the
browser stamps stream <img> URLs from a separate auth_hash global, so once
the two drifted the condition held permanently and the stale copy was
never corrected.
Observed on a montage page: a reload minted a new connkey and baked the
drifted hash into the src, after which every native <img> reconnect got a
403 from zms for nearly five hours.
Send it unconditionally; it is 32 bytes on a response that already carries
auth_relay.
Three points raised on #5038 after it was merged.
ajaxError() documents the reason field as included "only when set", but
tested it for truthiness, which would also drop '' and '0'. None of the
four STREAM_ERR_ constants are falsy so nothing changed behaviour, but
the check now matches the documented contract. The client already treats
an empty reason as fatal, so a caller that does pass one still gets the
old handling.
The case 0 branch called ajaxError() twice in sequence and relied on the
first one exiting to keep the second from running on a timeout. Made the
two paths mutually exclusive so it no longer depends on that.
The test's "every ajaxError call is classified" assertion compared the
number of call sites to the number of STREAM_ERR_ occurrences anywhere in
the file. The four define()s are part of that count, so up to four calls
could lose their classification and the test would still pass - verified
by dropping the argument from one call, which the old assertion accepted.
It now matches each call to the end of its statement and requires every
one to carry a constant.
getStreamCmdResponse() responded to every ajax/stream.php failure the same way:
mint a fresh connkey and reload the img src. ajaxError() returns HTTP 200 with
result=Error, so these arrive in jQuery's done() rather than fail(), and all
twelve error paths in stream.php took that branch.
Only one of them means zms is gone. For the rest the process is still running
and streaming, and replacing the connkey makes it unaddressable: CMD_STOP,
CMD_QUIT and mode=single all then go to the new key, so nothing can reach the
old process and only SIGPIPE can stop it, which we know is unreliable. That is
why the reports of lingering zms after switching monitors were unaffected by
changes to what the stop path sends.
The timeout path made this routine rather than rare. On select() expiry
ajaxError is commented out, so the script carries on to socket_recvfrom() on a
now non-blocking socket. That returns false, and false == 0 under switch's loose
comparison, so a merely slow zms was reported as 'No data to read from socket'
and torn down.
stream.php now classifies each failure as no_socket, timeout, transient or
invalid, and sends it as 'reason'. The client restarts the stream only for
no_socket. A missing reason is still treated as fatal, so a php that predates
this keeps the old behaviour.
Before replacing the connkey the client now sends CMD_QUIT to the old one, so
the process we are about to lose track of is asked to exit. That is deliberately
not routed through streamCommand(): it must name its target explicitly, since
this.connKey is about to change, and its response must not feed back into
getStreamCmdResponse(), or a QUIT that also failed would re-enter the error path
and loop.
ajaxError() takes the classification as a third argument, named $reason because
$code is already the HTTP status, and only includes it when set, so the other
131 callers are unaffected.
Tests: tests/js covers the fatal/non-fatal decision including the no-reason
fallback, tests/php pins the classification mapping and the switch(false)
semantics the timeout branch depends on. Both verified to fail when the
behaviour is reverted.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
When CMD_STOP is sent via AJAX using ZMS MJPEG streaming, the response
was incorrectly returning paused=true instead of indicating a stopped
state (paused=false).
Changes:
- Add `stopped` boolean to StreamBase (zm_stream.h)
- MonitorStream: CMD_STOP now sets stopped=true, paused=false instead
of paused=true; run loop skips frame sending when stopped
- EventStream: CMD_STOP sets stopped=true (was already setting
paused=false); run loop skips frame sending when stopped
- All other play/pause commands reset stopped=false
- Both streams include stopped field in the status response struct
- stream.php unpacks the new stopped field from MSG_DATA_WATCH and
MSG_DATA_EVENT responses
- MonitorStream.js handles stopped status in UI (shows 'Stopped' mode)
- EventStream.js tracks stopped state from server response
Fixes issue: CMD_STOP response paused=true should be paused=false
Agent-Logs-Url: https://github.com/ZoneMinder/zoneminder/sessions/ba9cb47a-a3e8-4e13-aec7-c9cd258e2a3d
Co-authored-by: connortechnology <925519+connortechnology@users.noreply.github.com>
Add a boolean analysis_image field to the CMD_QUERY status response that
reports whether zms is actually sending analysis images (with motion zone
overlays) or regular capture images. This lets MonitorStream.js detect
when the stream state is out of sync with what the client requested and
re-send the CMD_ANALYZE_ON/OFF command to correct it.
The field is true only when frame_type is FRAME_ANALYSIS, shared memory
is valid, and the monitor has analysis enabled — matching the same
condition used to select the image in the streaming loop.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Keep track of last_id which is the last frame id that we have info for. Use it instead of Frame Count to prevent
accessing non-existent frame records
close ffmpeg_input on event change
include duration in status messages because it may change when playing an in-progress event.
when we hit the end of our frame data if it is an in-progress event, reload the event data instead of moving to the next event.
Include X-Timestamp in http headers to match live stream behaviour
Only send time to next event when mode=ALL
Fix scale behaviour on event view
* experiment with namespaces on the Server class
* experiment with namespaces on the Server class
* Implement the ZM namespace on objects
* Implement the ZM namespace on objects
* Implement the ZM namespace on objects