id = $id; $GLOBALS['events_constructed'][] = $id; } // Mirrors the real Event::canView(): needs Events view (or the event in a // snapshot, not modelled here) AND the event's monitor to be viewable; a // non-existent event is not viewable. public function canView() { return $GLOBALS['event_exists'] and $GLOBALS['monitor_visible'] and \canView('Events'); } public function Monitor() { return new Monitor(); } public function MonitorId() { return 1; } public function Path() { return '/var/cache/zoneminder/events/x'; } } class Monitor { public function ViewHeight() { return 480; } public function ViewWidth() { return 640; } } } namespace { class StopRequest extends Exception {} // frames.php does require_once('includes/Filter.php') unconditionally; point // the include path at an empty stand-in (the class, if ever reached, is not // exercised because these cases use no search terms). $stubdir = sys_get_temp_dir().'/zm_frames_stubs_'.getmypid(); @mkdir($stubdir.'/includes', 0700, true); file_put_contents($stubdir.'/includes/Filter.php', " 1, 'EventId' => 2, 'Type' => 'Normal', 'TimeStamp' => '2026-11-01 03:14:00', 'Delta' => 0, 'Score' => 0), array('FrameId' => 2, 'EventId' => 2, 'Type' => 'Alarm', 'TimeStamp' => '2026-11-01 03:14:02', 'Delta' => 2, 'Score' => 75), ); } function ajaxResponse($data = array()) { throw new StopRequest('response'); } function ajaxError($message, $code = 200) { $GLOBALS['last_error'] = $message; throw new StopRequest('error'); } class StubFormatter { public function format($t) { return 'now'; } } $dateTimeFormatter = new StubFormatter(); class StubUser { public function Username() { return 'tester'; } } $user = new StubUser(); function frames_read() { foreach ($GLOBALS['queries'] as $q) { if (stripos($q, 'FROM `Frames`') !== false) return true; } return false; } // Child mode: set up the case, include the real file once, print the result. if (isset($argv[1])) { $case = json_decode($argv[1], true); $GLOBALS['perms'] = $case['perms']; $GLOBALS['monitor_visible'] = $case['monitor_visible']; $GLOBALS['event_exists'] = $case['event_exists']; $_REQUEST = array('task' => 'query', 'eid' => '2'); $outcome = 'fell through'; ob_start(); try { include __DIR__.'/../../web/ajax/frames.php'; } catch (StopRequest $e) { $outcome = $e->getMessage(); } catch (\Throwable $e) { $outcome = 'threw: '.$e->getMessage(); } ob_end_clean(); echo json_encode(array( 'outcome' => $outcome, 'frames_read' => frames_read(), 'events_constructed' => $GLOBALS['events_constructed'], )); exit(0); } $failures = 0; $passes = 0; function check($name, $got, $want) { global $failures, $passes; if ($got === $want) { $passes++; echo "ok - $name\n"; } else { $failures++; echo "FAIL - $name (got ".var_export($got, true).", want ".var_export($want, true).")\n"; } } function run_query(array $perms, $monitor_visible = true, $event_exists = true) { $arg = json_encode(array( 'perms' => $perms, 'monitor_visible' => $monitor_visible, 'event_exists' => $event_exists, )); $out = shell_exec(escapeshellarg(PHP_BINARY).' '.escapeshellarg(__FILE__).' '.escapeshellarg($arg)); $r = json_decode($out, true); if (!is_array($r)) { echo "child failed: $out\n"; exit(1); } return $r; } $view = array('Events' => 'View', 'Monitors' => 'View'); // Viewable event: answered, and the Frames table is read. $r = run_query($view, true, true); check('Events=View of a viewable event is answered', $r['outcome'], 'response'); check('a viewable event reads the Frames table', $r['frames_read'], true); // Denied monitor: refused, and the Frames table is never touched. $r = run_query($view, false, true); check("Events=View of a denied monitor's event is refused", $r['outcome'], 'error'); check("a denied monitor's event never reads the Frames table", $r['frames_read'], false); check('the denied event was tested through Event::canView', in_array(2, $r['events_constructed']), true); // A non-existent event is refused (Event::canView is false with no Id). $r = run_query($view, true, false); check('a missing event is refused', $r['outcome'], 'error'); check('a missing event reads no Frames', $r['frames_read'], false); // No Events view at all: the pre-existing global gate still refuses. $r = run_query(array('Monitors' => 'View'), true, true); check('no Events view is refused', $r['outcome'], 'error'); check('no Events view reads no Frames', $r['frames_read'], false); // Structural: the real file must route the event id through Event::canView() // before dispatching the query. Fails if the guard is reverted. $src = file_get_contents(__DIR__.'/../../web/ajax/frames.php'); check('source constructs ZM\\Event and calls canView() in the sanity block', (bool)preg_match('/new\s+ZM\\\\Event\(\s*\$eid\s*\)\)->canView\(\)/', $src), true); check('the canView guard precedes the queryRequest dispatch', (strpos($src, '->canView()') !== false) and (strpos($src, '->canView()') < strpos($src, 'queryRequest(')), true); @unlink($stubdir.'/includes/Filter.php'); @rmdir($stubdir.'/includes'); @rmdir($stubdir); echo "\n$passes passed, $failures failed\n"; exit($failures ? 1 : 0); }