2008-07-14 13:54:50 +00:00
< ? php
2022-12-12 18:06:13 +00:00
if ( ! isset ( $_REQUEST [ 'entity' ])) {
Error ( " No entity pass to status request. " );
http_response_code ( 404 );
return ;
2022-12-14 13:28:21 +00:00
} else {
2022-12-12 18:06:13 +00:00
}
2022-06-21 15:42:09 +00:00
2022-12-12 18:06:13 +00:00
if ( $_REQUEST [ 'entity' ] == 'navBar' ) {
2020-08-02 18:01:20 +00:00
global $bandwidth_options , $user ;
2020-03-02 16:50:40 +00:00
$data = array ();
if ( ZM_OPT_USE_AUTH && ( ZM_AUTH_RELAY == 'hashed' ) ) {
$auth_hash = generateAuthHash ( ZM_AUTH_HASH_IPS );
2021-03-13 17:10:35 +00:00
$data [ 'auth' ] = $auth_hash ;
$data [ 'auth_relay' ] = get_auth_relay ();
2018-02-14 15:44:48 +00:00
}
2020-08-02 18:01:20 +00:00
// Each widget on the navbar has its own function
// Call the functions we want to dynamically update
$data [ 'getBandwidthHTML' ] = getBandwidthHTML ( $bandwidth_options , $user );
$data [ 'getSysLoadHTML' ] = getSysLoadHTML ();
$data [ 'getDbConHTML' ] = getDbConHTML ();
$data [ 'getStorageHTML' ] = getStorageHTML ();
2021-10-14 17:33:29 +00:00
//$data['getShmHTML'] = getShmHTML();
$data [ 'getRamHTML' ] = getRamHTML ();
2020-08-02 18:01:20 +00:00
2018-02-14 15:44:48 +00:00
ajaxResponse ( $data );
return ;
2017-11-30 04:04:38 +00:00
}
2008-07-14 13:54:50 +00:00
$statusData = array (
2019-09-25 14:13:32 +00:00
'system' => array (
'permission' => 'System' ,
'table' => 'Monitors' ,
'limit' => 1 ,
'elements' => array (
'MonitorCount' => array ( 'sql' => 'count(*)' ),
2022-12-14 13:28:21 +00:00
'ActiveMonitorCount' => array ( 'sql' => 'count(if(`Capturing` != \'None\',1,NULL))' ),
2022-12-12 18:06:13 +00:00
'State' => array ( 'func' => 'daemonCheck()?\'' . translate ( 'Running' ) . '\':\'' . translate ( 'Stopped' ) . '\'' ),
2019-09-25 14:13:32 +00:00
'Load' => array ( 'func' => 'getLoad()' ),
'Disk' => array ( 'func' => 'getDiskPercent()' ),
),
),
'monitor' => array (
'permission' => 'Monitors' ,
'table' => 'Monitors' ,
'limit' => 1 ,
'selector' => 'Monitors.Id' ,
'elements' => array (
'Id' => array ( 'sql' => 'Monitors.Id' ),
'Name' => array ( 'sql' => 'Monitors.Name' ),
'Type' => true ,
2022-12-14 13:28:21 +00:00
'Capturing' => true ,
'Analysing' => true ,
'Capturing' => true ,
2019-09-25 14:13:32 +00:00
'Enabled' => true ,
'LinkedMonitors' => true ,
'Triggers' => true ,
'Device' => true ,
'Channel' => true ,
'Format' => true ,
'Host' => true ,
'Port' => true ,
'Path' => true ,
'Width' => array ( 'sql' => 'Monitors.Width' ),
'Height' => array ( 'sql' => 'Monitors.Height' ),
'Palette' => true ,
'Orientation' => true ,
'Brightness' => true ,
'Contrast' => true ,
'Hue' => true ,
'Colour' => true ,
'EventPrefix' => true ,
'LabelFormat' => true ,
'LabelX' => true ,
'LabelY' => true ,
'LabelSize' => true ,
'ImageBufferCount' => true ,
'WarmupCount' => true ,
'PreEventCount' => true ,
'PostEventCount' => true ,
'AlarmFrameCount' => true ,
'SectionLength' => true ,
'FrameSkip' => true ,
'MotionFrameSkip' => true ,
'MaxFPS' => true ,
'AlarmMaxFPS' => true ,
'FPSReportInterval' => true ,
'RefBlendPerc' => true ,
'Controllable' => true ,
'ControlId' => true ,
'ControlDevice' => true ,
'ControlAddress' => true ,
'AutoStopTimeout' => true ,
'TrackMotion' => true ,
'TrackDelay' => true ,
'ReturnLocation' => true ,
'ReturnDelay' => true ,
'DefaultView' => true ,
'DefaultRate' => true ,
'DefaultScale' => true ,
'WebColour' => true ,
'Sequence' => true ,
'MinEventId' => array ( 'sql' => '(SELECT min(Events.Id) FROM Events WHERE Events.MonitorId = Monitors.Id' ),
'MaxEventId' => array ( 'sql' => '(SELECT max(Events.Id) FROM Events WHERE Events.MonitorId = Monitors.Id' ),
'TotalEvents' => array ( 'sql' => '(SELECT count(Events.Id) FROM Events WHERE Events.MonitorId = Monitors.Id' ),
2019-12-06 19:31:06 +00:00
'Status' => ( isset ( $_REQUEST [ 'id' ]) ? array ( 'zmu' => '-m ' . escapeshellarg ( $_REQUEST [ 'id' ][ 0 ]) . ' -s' ) : null ),
'FrameRate' => ( isset ( $_REQUEST [ 'id' ]) ? array ( 'zmu' => '-m ' . escapeshellarg ( $_REQUEST [ 'id' ][ 0 ]) . ' -f' ) : null ),
2019-09-25 14:13:32 +00:00
),
),
'events' => array (
'permission' => 'Events' ,
'table' => 'Events' ,
'selector' => 'Events.MonitorId' ,
'elements' => array (
'Id' => true ,
2020-04-10 15:11:24 +00:00
'MonitorId' => true ,
2019-09-25 14:13:32 +00:00
'Name' => true ,
'Cause' => true ,
'Notes' => true ,
2020-11-04 23:27:51 +00:00
'StartDateTime' => true ,
2022-06-21 15:42:09 +00:00
'StartDateTimeFormatted' => array ( 'postFunction' => function ( $row ){
global $dateTimeFormatter ;
return $dateTimeFormatter -> format ( strtotime ( $row [ 'StartDateTime' ]));
}),
2021-06-08 19:54:14 +00:00
# Left for backwards compatability. Remove in 1.37
2020-11-04 23:39:02 +00:00
'EndDateTime' => true ,
2022-06-21 15:42:09 +00:00
'EndDateTimeFormatted' => array ( 'postFunction' => function ( $row ){
global $dateTimeFormatter ;
return $dateTimeFormatter -> format ( strtotime ( $row [ 'EndDateTime' ]));
}),
2019-09-25 14:13:32 +00:00
'Width' => true ,
'Height' => true ,
'Length' => true ,
'Frames' => true ,
'AlarmFrames' => true ,
'TotScore' => true ,
'AvgScore' => true ,
'MaxScore' => true ,
),
),
'event' => array (
'permission' => 'Events' ,
'table' => 'Events' ,
'limit' => 1 ,
'selector' => 'Events.Id' ,
'elements' => array (
'Id' => array ( 'sql' => 'Events.Id' ),
'MonitorId' => true ,
'MonitorName' => array ( 'sql' => '(SELECT Monitors.Name FROM Monitors WHERE Monitors.Id = Events.MonitorId)' ),
'Name' => true ,
'Cause' => true ,
2021-06-08 19:54:14 +00:00
'DiskSpace' => true ,
'Storage' => array ( 'sql' => '(SELECT Storage.Name FROM Storage WHERE Storage.Id=Events.StorageId)' ),
2020-11-04 23:27:51 +00:00
'StartDateTime' => true ,
2022-06-21 15:42:09 +00:00
'StartDateTimeFormatted' => array ( 'postFunction' => function ( $row ){
global $dateTimeFormatter ;
return $dateTimeFormatter -> format ( strtotime ( $row [ 'StartDateTime' ]));
}),
2021-06-08 19:54:14 +00:00
# Left for backwards compatability. Remove in 1.37
2020-11-04 23:39:02 +00:00
'EndDateTime' => true ,
2022-06-21 15:42:09 +00:00
'EndDateTimeFormatted' => array ( 'postFunction' => function ( $row ){
global $dateTimeFormatter ;
return $dateTimeFormatter -> format ( strtotime ( $row [ 'EndDateTime' ]));
}),
2019-09-25 14:13:32 +00:00
'Width' => true ,
'Height' => true ,
'Length' => true ,
'Frames' => true ,
'DefaultVideo' => true ,
'AlarmFrames' => true ,
'TotScore' => true ,
'AvgScore' => true ,
'MaxScore' => true ,
'Archived' => true ,
'Videoed' => true ,
'Uploaded' => true ,
'Emailed' => true ,
'Messaged' => true ,
'Executed' => true ,
'Notes' => true ,
'MinFrameId' => array ( 'sql' => '(SELECT min(Frames.FrameId) FROM Frames WHERE EventId=Events.Id)' ),
'MaxFrameId' => array ( 'sql' => '(SELECT max(Frames.FrameId) FROM Frames WHERE Events.Id = Frames.EventId)' ),
'MinFrameDelta' => array ( 'sql' => '(SELECT min(Frames.Delta) FROM Frames WHERE Events.Id = Frames.EventId)' ),
'MaxFrameDelta' => array ( 'sql' => '(SELECT max(Frames.Delta) FROM Frames WHERE Events.Id = Frames.EventId)' ),
),
),
'frames' => array (
'permission' => 'Events' ,
'table' => 'Frames' ,
'selector' => 'EventId' ,
'elements' => array (
'EventId' => true ,
'FrameId' => true ,
'Type' => true ,
'Delta' => true ,
),
),
'frame' => array (
'permission' => 'Events' ,
'table' => 'Frames' ,
'limit' => 1 ,
'selector' => array ( array ( 'table' => 'Events' , 'join' => 'Events.Id = Frames.EventId' , 'selector' => 'Events.Id' ), 'Frames.FrameId' ),
'elements' => array (
//'Id' => array( 'sql' => 'Frames.FrameId' ),
'FrameId' => true ,
'EventId' => true ,
'Type' => true ,
'TimeStamp' => true ,
2020-11-04 23:27:51 +00:00
'TimeStampShort' => array ( 'sql' => 'date_format( StartDateTime, \'' . MYSQL_FMT_DATETIME_SHORT . '\' )' ),
2019-09-25 14:13:32 +00:00
'Delta' => true ,
'Score' => true ,
//'Image' => array( 'postFunc' => 'getFrameImage' ),
),
),
'frameimage' => array (
'permission' => 'Events' ,
'func' => 'getFrameImage()'
),
'nearframe' => array (
'permission' => 'Events' ,
'func' => 'getNearFrame()'
),
'nearevents' => array (
'permission' => 'Events' ,
'func' => 'getNearEvents()'
)
);
2008-07-14 13:54:50 +00:00
2016-06-27 14:26:37 +00:00
function collectData () {
global $statusData ;
2008-07-14 13:54:50 +00:00
2023-06-08 13:17:16 +00:00
$entity = strtolower ( validJsStr ( $_REQUEST [ 'entity' ]));
$entitySpec = & $statusData [ $entity ];
2022-12-14 13:28:21 +00:00
#print_r( $entitySpec );
if ( ! canView ( $entitySpec [ 'permission' ])) {
2023-06-08 13:17:16 +00:00
ajaxError ( 'Unrecognised action or insufficient permissions for ' . $entity . ' permission: ' . $$entitySpec [ 'permission' ]);
2022-12-14 13:28:21 +00:00
return ;
}
2008-07-14 13:54:50 +00:00
2016-06-27 14:26:37 +00:00
if ( ! empty ( $entitySpec [ 'func' ]) ) {
2019-09-25 14:13:32 +00:00
$data = eval ( 'return(' . $entitySpec [ 'func' ] . ');' );
2016-06-27 14:26:37 +00:00
} else {
$data = array ();
$postFuncs = array ();
2022-06-21 15:42:09 +00:00
$postFunctions = array ();
2008-07-14 13:54:50 +00:00
2016-06-27 14:26:37 +00:00
$fieldSql = array ();
$joinSql = array ();
$groupSql = array ();
2019-12-06 19:31:06 +00:00
$values = array ();
2008-07-14 13:54:50 +00:00
2016-06-27 14:26:37 +00:00
$elements = & $entitySpec [ 'elements' ];
2020-05-07 17:57:37 +00:00
$lc_elements = array_change_key_case ( $elements );
2008-07-14 13:54:50 +00:00
2016-06-27 14:26:37 +00:00
$id = false ;
if ( isset ( $_REQUEST [ 'id' ]) )
if ( ! is_array ( $_REQUEST [ 'id' ]) )
$id = array ( validJsStr ( $_REQUEST [ 'id' ]) );
else
2020-05-07 17:57:37 +00:00
$id = array_values ( $_REQUEST [ 'id' ]);
2008-07-14 13:54:50 +00:00
2016-06-27 14:26:37 +00:00
if ( ! isset ( $_REQUEST [ 'element' ]) )
2020-05-07 17:57:37 +00:00
$_REQUEST [ 'element' ] = array_keys ( $elements );
2016-06-27 14:26:37 +00:00
else if ( ! is_array ( $_REQUEST [ 'element' ]) )
$_REQUEST [ 'element' ] = array ( validJsStr ( $_REQUEST [ 'element' ]) );
2008-07-14 13:54:50 +00:00
2016-06-27 14:26:37 +00:00
if ( isset ( $entitySpec [ 'selector' ]) ) {
if ( ! is_array ( $entitySpec [ 'selector' ]) )
$entitySpec [ 'selector' ] = array ( $entitySpec [ 'selector' ] );
foreach ( $entitySpec [ 'selector' ] as $selector )
if ( is_array ( $selector ) && isset ( $selector [ 'table' ]) && isset ( $selector [ 'join' ]) )
2017-12-07 15:31:25 +00:00
$joinSql [] = 'left join ' . $selector [ 'table' ] . ' on ' . $selector [ 'join' ];
2016-06-27 14:26:37 +00:00
}
2008-07-14 13:54:50 +00:00
2016-06-27 14:26:37 +00:00
foreach ( $_REQUEST [ 'element' ] as $element ) {
if ( ! ( $elementData = $lc_elements [ strtolower ( $element )]) )
2020-05-07 17:57:37 +00:00
ajaxError ( 'Bad ' . validJsStr ( $_REQUEST [ 'entity' ]) . ' element ' . $element );
2016-06-27 14:26:37 +00:00
if ( isset ( $elementData [ 'func' ]) )
2020-05-07 17:57:37 +00:00
$data [ $element ] = eval ( 'return( ' . $elementData [ 'func' ] . ' );' );
2016-06-27 14:26:37 +00:00
else if ( isset ( $elementData [ 'postFunc' ]) )
$postFuncs [ $element ] = $elementData [ 'postFunc' ];
2022-06-21 15:42:09 +00:00
else if ( isset ( $elementData [ 'postFunction' ]) )
$postFunctions [ $element ] = $elementData [ 'postFunction' ];
2016-06-27 14:26:37 +00:00
else if ( isset ( $elementData [ 'zmu' ]) )
2020-05-07 17:57:37 +00:00
$data [ $element ] = exec ( escapeshellcmd ( getZmuCommand ( ' ' . $elementData [ 'zmu' ])));
2016-06-27 14:26:37 +00:00
else {
if ( isset ( $elementData [ 'sql' ]) )
2017-12-07 15:31:25 +00:00
$fieldSql [] = $elementData [ 'sql' ] . ' as ' . $element ;
2016-06-27 14:26:37 +00:00
else
2020-05-07 17:57:37 +00:00
$fieldSql [] = '`' . $element . '`' ;
2016-06-27 14:26:37 +00:00
if ( isset ( $elementData [ 'table' ]) && isset ( $elementData [ 'join' ]) ) {
2017-12-07 15:31:25 +00:00
$joinSql [] = 'left join ' . $elementData [ 'table' ] . ' on ' . $elementData [ 'join' ];
2016-06-27 14:26:37 +00:00
}
if ( isset ( $elementData [ 'group' ]) ) {
$groupSql [] = $elementData [ 'group' ];
2008-07-14 13:54:50 +00:00
}
2016-06-27 14:26:37 +00:00
}
2019-09-25 14:13:32 +00:00
} # end foreach element
2008-07-14 13:54:50 +00:00
2016-06-27 14:26:37 +00:00
if ( count ( $fieldSql ) ) {
2019-09-25 14:13:32 +00:00
$sql = 'SELECT ' . join ( ', ' , $fieldSql ) . ' FROM ' . $entitySpec [ 'table' ];
2016-06-27 14:26:37 +00:00
if ( $joinSql )
2019-09-25 14:13:32 +00:00
$sql .= ' ' . join ( ' ' , array_unique ( $joinSql ));
2016-06-27 14:26:37 +00:00
if ( $id && ! empty ( $entitySpec [ 'selector' ]) ) {
$index = 0 ;
$where = array ();
2020-05-11 13:19:16 +00:00
foreach ( $entitySpec [ 'selector' ] as $selIndex => $selector ) {
2019-01-19 14:46:08 +00:00
$selectorParamName = ':selector' . $selIndex ;
2020-05-11 13:19:16 +00:00
if ( is_array ( $selector ) ) {
2019-01-19 14:46:08 +00:00
$where [] = $selector [ 'selector' ] . ' = ' . $selectorParamName ;
$values [ $selectorParamName ] = validInt ( $id [ $index ]);
2016-06-27 14:26:37 +00:00
} else {
2019-01-19 14:46:08 +00:00
$where [] = $selector . ' = ' . $selectorParamName ;
$values [ $selectorParamName ] = validInt ( $id [ $index ]);
2016-06-27 14:26:37 +00:00
}
$index ++ ;
}
2019-09-25 14:13:32 +00:00
$sql .= ' WHERE ' . join ( ' AND ' , $where );
2016-06-27 14:26:37 +00:00
}
if ( $groupSql )
2019-09-25 14:13:32 +00:00
$sql .= ' GROUP BY ' . join ( ',' , array_unique ( $groupSql ));
2019-01-21 16:20:56 +00:00
if ( ! empty ( $_REQUEST [ 'sort' ]) ) {
2019-01-23 17:22:00 +00:00
$sql .= ' ORDER BY ' ;
2020-05-11 13:19:16 +00:00
$sort_fields = explode ( ',' , $_REQUEST [ 'sort' ]);
2019-01-23 17:22:00 +00:00
foreach ( $sort_fields as $sort_field ) {
2020-05-11 12:34:07 +00:00
preg_match ( '/^`?(\w+)`?\s*(ASC|DESC)?( NULLS FIRST)?$/i' , $sort_field , $matches );
2019-01-23 17:22:00 +00:00
if ( count ( $matches ) ) {
2020-08-06 14:37:56 +00:00
if ( in_array ( $matches [ 1 ], $fieldSql ) or in_array ( '`' . $matches [ 1 ] . '`' , $fieldSql ) ) {
2019-01-23 17:22:00 +00:00
$sql .= $matches [ 1 ];
} else {
2020-05-12 13:16:37 +00:00
ZM\Error ( 'Sort field ' . $matches [ 1 ] . ' from ' . $sort_field . ' not in SQL Fields: ' . join ( ',' , $sort_field ));
2019-01-23 17:22:00 +00:00
}
if ( count ( $matches ) > 2 ) {
$sql .= ' ' . strtoupper ( $matches [ 2 ]);
if ( count ( $matches ) > 3 )
$sql .= ' ' . strtoupper ( $matches [ 3 ]);
}
} else {
2020-05-07 17:57:37 +00:00
ZM\Error ( 'Sort field didn\'t match regexp ' . $sort_field );
2019-01-23 17:22:00 +00:00
}
} # end foreach sort field
} # end if has sort
2016-06-27 14:26:37 +00:00
if ( ! empty ( $entitySpec [ 'limit' ]) )
$limit = $entitySpec [ 'limit' ];
elseif ( ! empty ( $_REQUEST [ 'count' ]) )
$limit = validInt ( $_REQUEST [ 'count' ]);
2020-11-13 22:05:23 +00:00
$limit_offset = '' ;
2016-06-27 14:26:37 +00:00
if ( ! empty ( $_REQUEST [ 'offset' ]) )
2017-12-07 15:31:25 +00:00
$limit_offset = validInt ( $_REQUEST [ 'offset' ]) . ', ' ;
2020-11-13 22:05:23 +00:00
if ( ! empty ( $limit ) )
2017-12-07 15:31:25 +00:00
$sql .= ' limit ' . $limit_offset . $limit ;
2022-06-21 15:42:09 +00:00
if ( isset ( $limit ) && ( $limit == 1 ) ) {
2020-05-07 17:57:37 +00:00
if ( $sqlData = dbFetchOne ( $sql , NULL , $values ) ) {
2016-06-27 14:26:37 +00:00
foreach ( $postFuncs as $element => $func )
$sqlData [ $element ] = eval ( 'return( ' . $func . '( $sqlData ) );' );
2022-06-21 15:42:09 +00:00
foreach ( $postFunctions as $element => $function )
$sqlData [ $element ] = $function ( $sqlData );
2020-11-13 22:05:23 +00:00
$data = array_merge ( $data , $sqlData );
2016-06-27 14:26:37 +00:00
}
} else {
$count = 0 ;
2020-11-13 22:05:23 +00:00
foreach ( dbFetchAll ( $sql , NULL , $values ) as $sqlData ) {
2016-06-27 14:26:37 +00:00
foreach ( $postFuncs as $element => $func )
2020-11-13 22:05:23 +00:00
$sqlData [ $element ] = eval ( 'return( ' . $func . '( $sqlData ) );' );
2022-06-21 15:42:09 +00:00
foreach ( $postFunctions as $element => $function )
$sqlData [ $element ] = $function ( $sqlData );
2016-06-27 14:26:37 +00:00
$data [] = $sqlData ;
2020-12-01 19:29:43 +00:00
if ( isset ( $limit ) && ++ $count >= $limit )
2016-06-27 14:26:37 +00:00
break ;
2020-11-13 22:05:23 +00:00
} # end foreach
} # end if have limit == 1
2008-07-14 13:54:50 +00:00
}
2016-06-27 14:26:37 +00:00
}
2022-06-21 15:42:09 +00:00
ZM\Debug ( print_r ( $data , true ));
2018-04-20 18:21:41 +00:00
return $data ;
2008-07-14 13:54:50 +00:00
}
2022-06-21 15:42:09 +00:00
function formatDateTime ( $dt ) {
return $dateTimeFormatter -> format ( strtotime ( $dt ));
}
2008-07-14 13:54:50 +00:00
$data = collectData ();
2016-06-27 14:26:37 +00:00
if ( ! isset ( $_REQUEST [ 'layout' ]) ) {
2017-11-03 19:09:19 +00:00
$_REQUEST [ 'layout' ] = 'json' ;
2008-07-14 13:54:50 +00:00
}
2016-06-27 14:26:37 +00:00
2020-11-13 22:05:23 +00:00
switch ( $_REQUEST [ 'layout' ] ) {
2016-06-27 14:26:37 +00:00
case 'xml NOT CURRENTLY SUPPORTED' :
2020-11-13 22:05:23 +00:00
header ( 'Content-type: application/xml' );
echo ( ' < ? xml version = " 1.0 " encoding = " iso-8859-1 " ?>
' );
echo '<' . strtolower ( $_REQUEST [ 'entity' ]) . ' >
2019-09-28 18:14:27 +00:00
' ;
2020-11-13 22:05:23 +00:00
foreach ( $data as $key => $value ) {
$key = strtolower ( $key );
echo " < $key > " . htmlentities ( $value ) . " </ $key > \n " ;
}
echo '</' . strtolower ( $_REQUEST [ 'entity' ]) . " > \n " ;
break ;
2016-06-27 14:26:37 +00:00
case 'json' :
2008-07-14 13:54:50 +00:00
{
2016-06-27 14:26:37 +00:00
$response = array ( strtolower ( validJsStr ( $_REQUEST [ 'entity' ])) => $data );
if ( isset ( $_REQUEST [ 'loopback' ]) )
$response [ 'loopback' ] = validJsStr ( $_REQUEST [ 'loopback' ]);
2020-12-01 19:29:43 +00:00
#ZM\Warning(print_r($response, true));
2019-09-25 14:13:32 +00:00
ajaxResponse ( $response );
2016-06-27 14:26:37 +00:00
break ;
2008-07-14 13:54:50 +00:00
}
2016-06-27 14:26:37 +00:00
case 'text' :
2020-11-13 22:05:23 +00:00
header ( 'Content-type: text/plain' );
echo join ( ' ' , array_values ( $data ));
break ;
2019-09-25 14:13:32 +00:00
default :
2020-11-13 22:05:23 +00:00
ZM\Error ( 'Unsupported layout: ' . $_REQUEST [ 'layout' ]);
2008-07-14 13:54:50 +00:00
}
2016-06-27 14:26:37 +00:00
function getFrameImage () {
$eventId = $_REQUEST [ 'id' ][ 0 ];
$frameId = $_REQUEST [ 'id' ][ 1 ];
2008-07-14 13:54:50 +00:00
2018-04-20 18:21:41 +00:00
$sql = 'SELECT * FROM Frames WHERE EventId = ? AND FrameId = ?' ;
2020-11-13 22:05:23 +00:00
if ( ! ( $frame = dbFetchOne ( $sql , NULL , array ( $eventId , $frameId ))) ) {
2022-02-11 23:03:35 +00:00
ZM\Error ( " Frame not found for event $eventId frame $frameId " );
2016-06-27 14:26:37 +00:00
$frame = array ();
$frame [ 'EventId' ] = $eventId ;
$frame [ 'FrameId' ] = $frameId ;
2017-11-03 19:09:19 +00:00
$frame [ 'Type' ] = 'Virtual' ;
2016-06-27 14:26:37 +00:00
}
2020-11-13 22:05:23 +00:00
$event = dbFetchOne ( 'SELECT * FROM Events WHERE Id = ?' , NULL , array ( $frame [ 'EventId' ]));
$frame [ 'Image' ] = getImageSrc ( $event , $frame , SCALE_BASE );
return $frame ;
2008-07-14 13:54:50 +00:00
}
2016-06-27 14:26:37 +00:00
function getNearFrame () {
$eventId = $_REQUEST [ 'id' ][ 0 ];
$frameId = $_REQUEST [ 'id' ][ 1 ];
2008-07-14 13:54:50 +00:00
2020-11-13 22:05:23 +00:00
$sql = 'SELECT FrameId FROM Frames WHERE EventId = ? AND FrameId <= ? ORDER BY FrameId DESC LIMIT 1' ;
if ( ! $nearFrameId = dbFetchOne ( $sql , 'FrameId' , array ( $eventId , $frameId )) ) {
$sql = 'SELECT * FROM Frames WHERE EventId = ? AND FrameId > ? ORDER BY FrameId ASC LIMIT 1' ;
if ( ! $nearFrameId = dbFetchOne ( $sql , 'FrameId' , array ( $eventId , $frameId )) ) {
2016-06-27 14:26:37 +00:00
return ( array () );
2008-07-14 13:54:50 +00:00
}
2016-06-27 14:26:37 +00:00
}
2017-11-03 19:09:19 +00:00
$_REQUEST [ 'entity' ] = 'frame' ;
2016-06-27 14:26:37 +00:00
$_REQUEST [ 'id' ][ 1 ] = $nearFrameId ;
2020-11-13 22:05:23 +00:00
return collectData ();
2008-07-14 13:54:50 +00:00
}
2016-06-27 14:26:37 +00:00
function getNearEvents () {
global $user , $sortColumn , $sortOrder ;
2008-07-14 13:54:50 +00:00
2016-06-27 14:26:37 +00:00
$eventId = $_REQUEST [ 'id' ];
2020-11-13 22:05:23 +00:00
$NearEvents = array ( 'EventId' => $eventId );
2020-10-05 13:30:33 +00:00
2019-05-24 14:00:15 +00:00
$event = dbFetchOne ( 'SELECT * FROM Events WHERE Id=?' , NULL , array ( $eventId ));
2020-10-05 13:30:33 +00:00
if ( ! $event ) return $NearEvents ;
2008-07-14 13:54:50 +00:00
2020-10-21 14:36:40 +00:00
$filter = ZM\Filter :: parse ( $_REQUEST [ 'filter' ]);
2016-06-27 14:26:37 +00:00
parseSort ();
2023-04-23 16:57:29 +00:00
if ( count ( $user -> unviewableMonitorIds ()) ) {
$filter = $filter -> addTerm ( array ( 'cnj' => 'and' , 'attr' => 'MonitorId' , 'op' => 'IN' , 'val' => $user -> viewableMonitorIds ()));
2020-10-21 14:36:40 +00:00
}
2022-12-07 15:33:45 +00:00
$filter_sql = $filter -> sql ();
2008-07-14 13:54:50 +00:00
2020-11-13 22:05:23 +00:00
# When listing, it may make sense to list them in descending order.
# But when viewing Prev should timewise earlier and Next should be after.
2020-11-04 23:27:51 +00:00
if ( $sortColumn == 'E.Id' or $sortColumn == 'E.StartDateTime' ) {
2020-10-05 13:30:33 +00:00
$sortOrder = 'ASC' ;
2018-01-28 22:30:49 +00:00
}
2022-12-07 15:33:45 +00:00
$sql = 'SELECT E.Id AS Id, E.StartDateTime AS StartDateTime FROM Events AS E INNER JOIN Monitors AS M ON E.MonitorId = M.Id WHERE ' . $sortColumn . ' ' . ( $sortOrder == 'ASC' ? '<=' : '>=' ) . ' \'' . $event [ $_REQUEST [ 'sort_field' ]] . '\'' ;
if ( $filter -> sql ()) {
$sql .= ' AND (' . $filter -> sql () . ')' ;
}
$sql .= ' AND E.Id<' . $event [ 'Id' ] . ' ORDER BY ' . $sortColumn . ' ' . ( $sortOrder == 'ASC' ? 'DESC' : 'ASC' );
2018-09-28 17:24:50 +00:00
if ( $sortColumn != 'E.Id' ) {
# When sorting by starttime, if we have two events with the same starttime (diffreent monitors) then we should sort secondly by Id
$sql .= ', E.Id DESC' ;
2016-06-27 14:26:37 +00:00
}
2018-09-28 17:24:50 +00:00
$sql .= ' LIMIT 1' ;
2019-05-24 14:00:15 +00:00
$result = dbQuery ( $sql );
2020-10-21 14:36:40 +00:00
if ( ! $result ) {
2020-11-13 22:05:23 +00:00
ZM\Error ( 'Failed to load previous event using ' . $sql );
2020-10-21 14:36:40 +00:00
return $NearEvents ;
}
2019-05-24 14:00:15 +00:00
$prevEvent = dbFetchNext ( $result );
2018-09-28 17:24:50 +00:00
2022-12-07 15:33:45 +00:00
$sql = 'SELECT E.Id AS Id, E.StartDateTime AS StartDateTime FROM Events AS E INNER JOIN Monitors AS M ON E.MonitorId = M.Id WHERE ' . $sortColumn . ' ' . ( $sortOrder == 'ASC' ? '>=' : '<=' ) . ' \'' . $event [ $_REQUEST [ 'sort_field' ]] . '\'' ;
if ( $filter -> sql ()) {
$sql .= ' AND (' . $filter -> sql () . ')' ;
}
$sql .= ' AND E.Id>' . $event [ 'Id' ] . ' ORDER BY ' . $sortColumn . ' ' . ( $sortOrder == 'ASC' ? 'ASC' : 'DESC' );
2018-09-28 17:24:50 +00:00
if ( $sortColumn != 'E.Id' ) {
# When sorting by starttime, if we have two events with the same starttime (diffreent monitors) then we should sort secondly by Id
$sql .= ', E.Id ASC' ;
2016-06-27 14:26:37 +00:00
}
2018-09-28 17:24:50 +00:00
$sql .= ' LIMIT 1' ;
2020-10-05 13:30:33 +00:00
$result = dbQuery ( $sql );
2020-10-21 14:36:40 +00:00
if ( ! $result ) {
2020-11-13 22:05:23 +00:00
ZM\Error ( 'Failed to load next event using ' . $sql );
2020-10-21 14:36:40 +00:00
return $NearEvents ;
}
2020-10-05 13:30:33 +00:00
$nextEvent = dbFetchNext ( $result );
2008-07-14 13:54:50 +00:00
2018-09-28 17:24:50 +00:00
if ( $prevEvent ) {
2020-10-05 13:30:33 +00:00
$NearEvents [ 'PrevEventId' ] = $prevEvent [ 'Id' ];
2020-11-04 23:27:51 +00:00
$NearEvents [ 'PrevEventStartTime' ] = $prevEvent [ 'StartDateTime' ];
2020-10-05 13:30:33 +00:00
$NearEvents [ 'PrevEventDefVideoPath' ] = getEventDefaultVideoPath ( $prevEvent [ 'Id' ]);
2018-09-28 17:24:50 +00:00
} else {
2020-10-21 14:40:26 +00:00
$NearEvents [ 'PrevEventId' ] = $NearEvents [ 'PrevEventStartTime' ] = $NearEvents [ 'PrevEventDefVideoPath' ] = 0 ;
2018-09-28 17:24:50 +00:00
}
if ( $nextEvent ) {
2020-10-05 13:30:33 +00:00
$NearEvents [ 'NextEventId' ] = $nextEvent [ 'Id' ];
2020-11-04 23:27:51 +00:00
$NearEvents [ 'NextEventStartTime' ] = $nextEvent [ 'StartDateTime' ];
2020-10-05 13:30:33 +00:00
$NearEvents [ 'NextEventDefVideoPath' ] = getEventDefaultVideoPath ( $nextEvent [ 'Id' ]);
2018-09-28 17:24:50 +00:00
} else {
2020-10-05 13:30:33 +00:00
$NearEvents [ 'NextEventId' ] = $NearEvents [ 'NextEventStartTime' ] = $NearEvents [ 'NextEventDefVideoPath' ] = 0 ;
2018-09-28 17:24:50 +00:00
}
2020-10-05 13:30:33 +00:00
return $NearEvents ;
2020-11-13 22:05:23 +00:00
} # end function getNearEvents()
2008-07-14 13:54:50 +00:00
?>