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']) and is_string($_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']) and is_string($_REQUEST['advsearch'])) ? json_decode($_REQUEST['advsearch'], JSON_OBJECT_AS_ARRAY) : array(); // json_decode can return null, and count(null) is a fatal error on PHP 8. $advsearch = is_array($advsearch) ? $advsearch : array(); // Sort specifies the name of the column to sort on $sort = 'FrameId'; if ( isset($_REQUEST['sort']) and is_string($_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 is_string($_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($eid); $data['totalNotFiltered'] = (int)dbFetchOne('SELECT COUNT(*) AS Total FROM `Frames` WHERE EventId=?', 'Total', $values); $where = 'WHERE EventId=?'; require_once('includes/Filter.php'); if ( count($advsearch) or $search != '' ) { $search_filter = new ZM\Filter(); // 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 $search_sql = $search_filter->sql(); if ($search_sql) $where .= ' AND '.$search_sql; $data['total'] = (int)dbFetchOne('SELECT COUNT(*) AS Total FROM `Frames` '.$where, 'Total', $values); } else { $data['total'] = $data['totalNotFiltered']; } $sql = 'SELECT * FROM `Frames` '.$where.' ORDER BY '.$sort.' '.$order.($sort != 'FrameId' ? ', FrameId' : ''); if ( $limit ) $sql .= ' LIMIT '.(int)$offset.', '.(int)$limit; $returned_rows = array(); foreach ( dbFetchAll($sql, NULL, $values) 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 $thmb_width = ZM_WEB_LIST_THUMB_WIDTH ? 'width='.ZM_WEB_LIST_THUMB_WIDTH : ''; # The ratio is only needed when the height has to be derived from the # width. An event whose monitor row was deleted has an empty Monitor, so # ViewWidth() is null and dividing by it would be a DivisionByZeroError. # Fall back to the event's own dimensions, or leave the height out. if ( ZM_WEB_LIST_THUMB_HEIGHT ) { $thmb_height = 'height='.(int)round(ZM_WEB_LIST_THUMB_HEIGHT); } else { $ratio_factor = $Monitor->ViewWidth() ? $Monitor->ViewHeight() / $Monitor->ViewWidth() : 0; if ( !$ratio_factor and $Event->Width() ) $ratio_factor = $Event->Height() / $Event->Width(); $thmb_height = $ratio_factor ? 'height='.(int)round(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'] = ''; } $returned_rows[] = $row; } # end foreach row matching search $data['rows'] = $returned_rows; return $data; }