2007-08-30 02:11:09 +08:00
< ? php
2009-04-03 20:10:54 +08:00
define ( " MSG_TIMEOUT " , ZM_WEB_AJAX_TIMEOUT );
2007-08-30 02:11:09 +08:00
define ( " MSG_DATA_SIZE " , 4 + 256 );
2017-10-04 22:48:24 +08:00
if ( ! ( $_REQUEST [ 'connkey' ] && $_REQUEST [ 'command' ]) ) {
ajaxError ( " Unexpected received message type ' $type ' " );
2007-08-30 02:11:09 +08:00
}
2017-10-04 22:48:24 +08:00
if ( ! ( $socket = @ socket_create ( AF_UNIX , SOCK_DGRAM , 0 )) ) {
ajaxError ( " socket_create() failed: " . socket_strerror ( socket_last_error ()) );
2007-08-30 02:11:09 +08:00
}
2008-07-16 23:19:12 +08:00
$locSockFile = ZM_PATH_SOCKS . '/zms-' . sprintf ( " %06d " , $_REQUEST [ 'connkey' ]) . 'w.sock' ;
2017-10-04 22:48:24 +08:00
if ( !@ socket_bind ( $socket , $locSockFile ) ) {
ajaxError ( " socket_bind( $locSockFile ) failed: " . socket_strerror ( socket_last_error ()) );
2007-08-30 02:11:09 +08:00
}
2017-10-04 22:48:24 +08:00
switch ( $_REQUEST [ 'command' ] ) {
case CMD_VARPLAY :
Logger :: Debug ( " Varplaying to " . $_REQUEST [ 'rate' ] );
$msg = pack ( " lcn " , MSG_CMD , $_REQUEST [ 'command' ], $_REQUEST [ 'rate' ] + 32768 );
break ;
case CMD_ZOOMIN :
Logger :: Debug ( " Zooming to " . $_REQUEST [ 'x' ] . " , " . $_REQUEST [ 'y' ] );
$msg = pack ( " lcnn " , MSG_CMD , $_REQUEST [ 'command' ], $_REQUEST [ 'x' ], $_REQUEST [ 'y' ] );
break ;
case CMD_PAN :
Logger :: Debug ( " Panning to " . $_REQUEST [ 'x' ] . " , " . $_REQUEST [ 'y' ] );
$msg = pack ( " lcnn " , MSG_CMD , $_REQUEST [ 'command' ], $_REQUEST [ 'x' ], $_REQUEST [ 'y' ] );
break ;
case CMD_SCALE :
Logger :: Debug ( " Scaling to " . $_REQUEST [ 'scale' ] );
$msg = pack ( " lcn " , MSG_CMD , $_REQUEST [ 'command' ], $_REQUEST [ 'scale' ] );
break ;
case CMD_SEEK :
Logger :: Debug ( " Seeking to " . $_REQUEST [ 'offset' ] );
$msg = pack ( " lcN " , MSG_CMD , $_REQUEST [ 'command' ], $_REQUEST [ 'offset' ] );
break ;
default :
$msg = pack ( " lc " , MSG_CMD , $_REQUEST [ 'command' ] );
break ;
2007-08-30 02:11:09 +08:00
}
2008-07-16 23:19:12 +08:00
$remSockFile = ZM_PATH_SOCKS . '/zms-' . sprintf ( " %06d " , $_REQUEST [ 'connkey' ]) . 's.sock' ;
2014-07-11 22:51:24 +08:00
$max_socket_tries = 10 ;
2016-12-16 22:12:27 +08:00
while ( ! file_exists ( $remSockFile ) && $max_socket_tries -- ) { //sometimes we are too fast for our own good, if it hasn't been setup yet give it a second.
2017-10-28 04:37:51 +08:00
usleep ( 2000000 );
2016-12-16 22:12:27 +08:00
}
2009-05-08 18:04:12 +08:00
2016-12-16 22:12:27 +08:00
if ( ! file_exists ( $remSockFile ) ) {
2017-01-12 00:14:00 +08:00
ajaxError ( " Socket $remSockFile does not exist. This file is created by zms, and since it does not exist, either zms did not run, or zms exited early. Please check your zms logs and ensure that CGI is enabled in apache and check that the PATH_ZMS is set correctly. Make sure that ZM is actually recording. If you are trying to view a live stream and the capture process (zmc) is not running then zms will exit. Please go to http://zoneminder.readthedocs.io/en/latest/faq.html#why-can-t-i-see-streamed-images-when-i-can-see-stills-in-the-zone-window-etc for more information. " );
2016-12-16 22:12:27 +08:00
} else {
if ( !@ socket_sendto ( $socket , $msg , strlen ( $msg ), 0 , $remSockFile ) ) {
2008-10-30 07:08:21 +08:00
ajaxError ( " socket_sendto( $remSockFile ) failed: " . socket_strerror ( socket_last_error ()) );
2016-12-16 22:12:27 +08:00
}
2007-08-30 02:11:09 +08:00
}
2008-09-26 17:47:20 +08:00
$rSockets = array ( $socket );
$wSockets = NULL ;
2008-07-16 23:19:12 +08:00
$eSockets = NULL ;
2011-06-28 00:15:16 +08:00
$numSockets = @ socket_select ( $rSockets , $wSockets , $eSockets , intval ( MSG_TIMEOUT / 1000 ), ( MSG_TIMEOUT % 1000 ) * 1000 );
2007-08-30 02:11:09 +08:00
2017-10-04 22:48:24 +08:00
if ( $numSockets === false ) {
ajaxError ( " socket_select failed: " . socket_strerror ( socket_last_error ()) );
} else if ( $numSockets < 0 ) {
ajaxError ( " Socket closed $remSockFile " );
} else if ( $numSockets == 0 ) {
ajaxError ( " Timed out waiting for msg $remSockFile " );
} else if ( $numSockets > 0 ) {
if ( count ( $rSockets ) != 1 )
ajaxError ( " Bogus return from select, " . count ( $rSockets ) . " sockets available " );
2007-08-30 02:11:09 +08:00
}
2017-10-04 22:48:24 +08:00
switch ( $nbytes = @ socket_recvfrom ( $socket , $msg , MSG_DATA_SIZE , 0 , $remSockFile ) ) {
case - 1 :
{
ajaxError ( " socket_recvfrom( $remSockFile ) failed: " . socket_strerror ( socket_last_error ()) );
break ;
}
case 0 :
{
ajaxError ( " No data to read from socket " );
break ;
}
default :
{
if ( $nbytes != MSG_DATA_SIZE )
ajaxError ( " Got unexpected message size, got $nbytes , expected " . MSG_DATA_SIZE );
break ;
}
2007-08-30 02:11:09 +08:00
}
$data = unpack ( " ltype " , $msg );
2017-10-04 22:48:24 +08:00
switch ( $data [ 'type' ] ) {
case MSG_DATA_WATCH :
{
$data = unpack ( " ltype/imonitor/istate/dfps/ilevel/irate/ddelay/izoom/Cdelayed/Cpaused/Cenabled/Cforced " , $msg );
$data [ 'fps' ] = round ( $data [ 'fps' ], 2 );
$data [ 'rate' ] /= RATE_BASE ;
$data [ 'delay' ] = round ( $data [ 'delay' ], 2 );
$data [ 'zoom' ] = round ( $data [ 'zoom' ] / SCALE_BASE , 1 );
if ( ZM_OPT_USE_AUTH && ZM_AUTH_RELAY == " hashed " ) {
session_start ();
$time = time ();
// Regenerate auth hash after half the lifetime of the hash
if ( ( ! isset ( $_SESSION [ 'AuthHashGeneratedAt' ])) or ( $_SESSION [ 'AuthHashGeneratedAt' ] < $time - ( ZM_AUTH_HASH_TTL * 1800 ) ) ) {
$data [ 'auth' ] = generateAuthHash ( ZM_AUTH_HASH_IPS );
}
session_write_close ();
2007-08-30 02:11:09 +08:00
}
2017-10-04 22:48:24 +08:00
ajaxResponse ( array ( 'status' => $data ) );
break ;
}
case MSG_DATA_EVENT :
{
$data = unpack ( " ltype/ievent/iprogress/irate/izoom/Cpaused " , $msg );
//$data['progress'] = sprintf( "%.2f", $data['progress'] );
$data [ 'rate' ] /= RATE_BASE ;
$data [ 'zoom' ] = round ( $data [ 'zoom' ] / SCALE_BASE , 1 );
if ( ZM_OPT_USE_AUTH && ZM_AUTH_RELAY == " hashed " ) {
session_start ();
$time = time ();
// Regenerate auth hash after half the lifetime of the hash
if ( ( ! isset ( $_SESSION [ 'AuthHashGeneratedAt' ])) or ( $_SESSION [ 'AuthHashGeneratedAt' ] < $time - ( ZM_AUTH_HASH_TTL * 1800 ) ) ) {
$data [ 'auth' ] = generateAuthHash ( ZM_AUTH_HASH_IPS );
}
session_write_close ();
2007-08-30 02:11:09 +08:00
}
2017-10-04 22:48:24 +08:00
ajaxResponse ( array ( 'status' => $data ) );
break ;
}
default :
{
ajaxError ( " Unexpected received message type ' $type ' " );
}
2007-08-30 02:11:09 +08:00
}
2008-10-17 00:12:23 +08:00
ajaxError ( 'Unrecognised action or insufficient permissions' );
2007-08-30 02:11:09 +08:00
2017-10-04 22:48:24 +08:00
function ajaxCleanup () {
global $socket , $locSockFile ;
if ( ! empty ( $socket ) )
@ socket_close ( $socket );
if ( ! empty ( $locSockFile ) )
@ unlink ( $locSockFile );
2008-10-17 00:12:23 +08:00
}
2007-08-30 02:11:09 +08:00
?>