mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-11-30 04:04:03 -05:00
ajax/frames.php task=query checked the global canView('Events') and then
built the Frames query from the request event id alone, without the
per-event check its siblings make. A user with Events=View but a monitor
permission of None on an event's monitor could read that event's frame
records -- frame number, Normal/Alarm type, timestamp, delta and score --
for any event on a denied monitor, learning when activity happened and how
strong the alarm scores were across monitor boundaries. This is the web
copy of the disclosure GHSA-mg2g-jmfc-3w8g fixed in the Frames API;
frame images stay protected by views/image.php.
Construct the event in the sanity block and refuse the request unless
ZM\Event::canView() passes, which enforces the per-monitor permission
through Monitor()->canView(), matching ajax/event.php and the API.
Test tests/php/test_frames_query_requires_view.php includes the real file
with the web helpers stubbed (one child php process per case, as the file
declares queryRequest() at file scope) and asserts the Frames table is
never read for a denied or missing event. 11 checks; 6 fail before the fix.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
209 lines
7.3 KiB
PHP
209 lines
7.3 KiB
PHP
<?php
|
|
$data = array();
|
|
$message = '';
|
|
|
|
//
|
|
// INITIALIZE AND CHECK SANITY
|
|
//
|
|
|
|
if ( !canView('Events') ) $message = 'Insufficient permissions to view frames for user '.$user->Username();
|
|
|
|
// task must be set
|
|
if ( empty($_REQUEST['task']) ) {
|
|
$message = 'This request requires a task to be set';
|
|
// query is the only supported task at the moment
|
|
} else if ( $_REQUEST['task'] != 'query' ) {
|
|
$message = 'Unrecognised task '.$_REQUEST['task'];
|
|
} else {
|
|
$task = $_REQUEST['task'];
|
|
}
|
|
|
|
if ( empty($_REQUEST['eid']) ) {
|
|
$message = 'No event id supplied';
|
|
} else {
|
|
$eid = validInt($_REQUEST['eid']);
|
|
# canView('Events') above is global; a user with Events=View can still be
|
|
# denied individual monitors. Require the per-event permission before listing
|
|
# its frame records, as ajax/event.php and the Frames API already do.
|
|
if ( !$message and !(new ZM\Event($eid))->canView() )
|
|
$message = 'Insufficient permissions to view frames for event '.$eid;
|
|
}
|
|
|
|
if ( $message ) {
|
|
ajaxError($message);
|
|
return;
|
|
}
|
|
|
|
// Search contains a user entered string to search on
|
|
$search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';
|
|
|
|
// Advanced search contains an array of "column name" => "search text" pairs
|
|
// Bootstrap table sends json_ecoded array, which we must decode
|
|
$advsearch = isset($_REQUEST['advsearch']) ? json_decode($_REQUEST['advsearch'], JSON_OBJECT_AS_ARRAY) : array();
|
|
|
|
// Sort specifies the name of the column to sort on
|
|
$sort = 'FrameId';
|
|
if ( isset($_REQUEST['sort']) ) {
|
|
$sort = $_REQUEST['sort'];
|
|
}
|
|
|
|
// Offset specifies the starting row to return, used for pagination
|
|
$offset = 0;
|
|
if ( isset($_REQUEST['offset']) ) {
|
|
if ( ( !is_int($_REQUEST['offset']) and !ctype_digit($_REQUEST['offset']) ) ) {
|
|
ZM\Error('Invalid value for offset: ' . $_REQUEST['offset']);
|
|
} else {
|
|
$offset = $_REQUEST['offset'];
|
|
}
|
|
}
|
|
|
|
// Order specifies the sort direction, either asc or desc
|
|
$order = (isset($_REQUEST['order']) and (strtolower($_REQUEST['order']) == 'asc')) ? 'ASC' : 'DESC';
|
|
|
|
// Limit specifies the number of rows to return
|
|
$limit = 0;
|
|
if ( isset($_REQUEST['limit']) ) {
|
|
if ( ( !is_int($_REQUEST['limit']) and !ctype_digit($_REQUEST['limit']) ) ) {
|
|
ZM\Error('Invalid value for limit: ' . $_REQUEST['limit']);
|
|
} else {
|
|
$limit = $_REQUEST['limit'];
|
|
}
|
|
}
|
|
|
|
//
|
|
// MAIN LOOP
|
|
//
|
|
|
|
// Only one supported task at the moment
|
|
switch ( $task ) {
|
|
case 'query' :
|
|
$data = queryRequest($eid, $search, $advsearch, $sort, $offset, $order, $limit);
|
|
break;
|
|
default :
|
|
ZM\Fatal("Unrecognised task '$task'");
|
|
} // end switch task
|
|
|
|
ajaxResponse($data);
|
|
|
|
//
|
|
// FUNCTION DEFINITIONS
|
|
//
|
|
|
|
function queryRequest($eid, $search, $advsearch, $sort, $offset, $order, $limit) {
|
|
global $dateTimeFormatter;
|
|
|
|
$data = array(
|
|
'total' => 0,
|
|
'totalNotFiltered' => 0,
|
|
'rows' => array(),
|
|
'updated' => $dateTimeFormatter->format(time())
|
|
);
|
|
|
|
// The names of the dB columns in the events table we are interested in
|
|
$columns = array('FrameId', 'Type', 'TimeStamp', 'Delta', 'Score');
|
|
|
|
if ( !in_array($sort, $columns) ) {
|
|
ZM\Error('Invalid sort field: ' . $sort);
|
|
$sort = 'FrameId';
|
|
}
|
|
|
|
$Event = new ZM\Event($eid);
|
|
$Monitor = $Event->Monitor();
|
|
$values = array();
|
|
$likes = array();
|
|
$where = 'WHERE EventId = '.$eid;
|
|
|
|
$sql = 'SELECT * FROM `Frames` '.$where.' ORDER BY '.$sort.' '.$order;
|
|
|
|
//ZM\Debug('Calling the following sql query: ' .$sql);
|
|
|
|
$unfiltered_rows = array();
|
|
$frame_ids = array();
|
|
foreach ( dbFetchAll($sql, NULL, $values) as $row ) {
|
|
$frame_ids[] = $row['FrameId'];
|
|
$unfiltered_rows[] = $row;
|
|
}
|
|
|
|
ZM\Debug('Have ' . count($unfiltered_rows) . ' frames matching base filter.');
|
|
|
|
$filtered_rows = null;
|
|
require_once('includes/Filter.php');
|
|
if ( count($advsearch) or $search != '' ) {
|
|
$search_filter = new ZM\Filter();
|
|
$search_filter = $search_filter->addTerm(array('cnj'=>'and', 'attr'=>'FrameId', 'op'=>'IN', 'val'=>$frame_ids));
|
|
|
|
// There are two search bars in the log view, normal and advanced
|
|
// Making an exuctive decision to ignore the normal search, when advanced search is in use
|
|
// Alternatively we could try to do both
|
|
if ( count($advsearch) ) {
|
|
$terms = array();
|
|
foreach ( $advsearch as $col=>$text ) {
|
|
$terms[] = array('cnj'=>'and', 'attr'=>$col, 'op'=>'LIKE', 'val'=>$text);
|
|
} # end foreach col in advsearch
|
|
$terms[0]['obr'] = 1;
|
|
$terms[count($terms)-1]['cbr'] = 1;
|
|
$search_filter->addTerms($terms);
|
|
} else if ( $search != '' ) {
|
|
$search = '%' .$search. '%';
|
|
$terms = array();
|
|
foreach ( $columns as $col ) {
|
|
$terms[] = array('cnj'=>'or', 'attr'=>$col, 'op'=>'LIKE', 'val'=>$search);
|
|
}
|
|
$terms[0]['obr'] = 1;
|
|
$terms[0]['cnj'] = 'and';
|
|
$terms[count($terms)-1]['cbr'] = 1;
|
|
$search_filter = $search_filter->addTerms($terms, array('obr'=>1, 'cbr'=>1, 'op'=>'OR'));
|
|
} # end if search
|
|
|
|
$sql = 'SELECT * FROM `Frames` '.$where.' AND '.$search_filter->sql().' ORDER BY ' .$sort. ' ' .$order;
|
|
$filtered_rows = dbFetchAll($sql);
|
|
ZM\Debug('Have ' . count($filtered_rows) . ' frames matching search filter.');
|
|
} else {
|
|
$filtered_rows = $unfiltered_rows;
|
|
} # end if search_filter->terms() > 1
|
|
|
|
$returned_rows = array();
|
|
foreach ( array_slice($filtered_rows, $offset, ($limit ? $limit : count($filtered_rows))) as $row ) {
|
|
if ( ZM_WEB_LIST_THUMBS ) {
|
|
|
|
# Build the path to the potential analysis image
|
|
$analysisImage = sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d-analyse.jpg', $row['FrameId']);
|
|
$analysisPath = $Event->Path().'/'.$analysisImage;
|
|
$alarmFrame = $row['Type'] == 'Alarm';
|
|
$hasAnalysisImage = $alarmFrame && file_exists($analysisPath) && filesize($analysisPath);
|
|
|
|
# Our base img source component, which we will add on to
|
|
$base_img_src = '?view=image&eid=' .$row['EventId']. '&fid=' .$row['FrameId'];
|
|
|
|
# if an analysis images exists, use it as the thumbnail
|
|
if ( $hasAnalysisImage ) $base_img_src .= '&show=analyse';
|
|
|
|
# Build the subcomponents needed for the image source
|
|
$ratio_factor = $Monitor->ViewHeight() / $Monitor->ViewWidth();
|
|
$thmb_width = ZM_WEB_LIST_THUMB_WIDTH ? 'width='.ZM_WEB_LIST_THUMB_WIDTH : '';
|
|
$thmb_height = 'height="'.( ZM_WEB_LIST_THUMB_HEIGHT ? ZM_WEB_LIST_THUMB_HEIGHT : ZM_WEB_LIST_THUMB_WIDTH*$ratio_factor ) .'"';
|
|
$thmb_fn = 'filename=' .$Event->MonitorId(). '_' .$row['EventId']. '_' .$row['FrameId']. '.jpg';
|
|
|
|
# Assemble the scaled and unscaled image source image source components
|
|
$img_src = join('&', array_filter(array($base_img_src, $thmb_width, $thmb_height, $thmb_fn)));
|
|
$full_img_src = join('&', array_filter(array($base_img_src, $thmb_fn)));
|
|
|
|
# finally, we assemble the the entire thumbnail img src structure, whew
|
|
$row['Thumbnail'] = '<img src="' .$img_src. '" '.$thmb_width. ' ' .$thmb_height. 'img_src="' .$img_src. '" full_img_src="' .$full_img_src. '">';
|
|
}
|
|
$returned_rows[] = $row;
|
|
} # end foreach row matching search
|
|
|
|
$data['rows'] = $returned_rows;
|
|
|
|
# totalNotFiltered must equal total, except when either search bar has been used
|
|
$data['totalNotFiltered'] = count($unfiltered_rows);
|
|
if ( $search != '' || count($advsearch) ) {
|
|
$data['total'] = count($filtered_rows);
|
|
} else {
|
|
$data['total'] = $data['totalNotFiltered'];
|
|
}
|
|
|
|
return $data;
|
|
}
|