2007-08-30 02:11:09 +08:00
< ? php
2018-01-29 23:21:31 +08:00
error_reporting ( 0 );
2007-08-30 02:11:09 +08:00
2017-11-01 04:32:42 +08:00
$start_time = time ();
2018-08-12 01:35:57 +08:00
define ( 'MSG_TIMEOUT' , ZM_WEB_AJAX_TIMEOUT / 2 );
define ( 'MSG_DATA_SIZE' , 4 + 256 );
2007-08-30 02:11:09 +08:00
2017-10-04 22:48:24 +08:00
if ( ! ( $_REQUEST [ 'connkey' ] && $_REQUEST [ 'command' ]) ) {
2018-08-12 01:35:57 +08:00
ajaxError ( " Unexpected received message type ' $type ' " );
}
2019-03-20 00:02:42 +08:00
#mkdir(ZM_PATH_SOCKS);
2007-08-30 02:11:09 +08:00
2018-04-11 22:38:13 +08:00
# The file that we point ftok to has to exist, and only exist if zms is running, so we are pointing it at the .sock
$key = ftok ( ZM_PATH_SOCKS . '/zms-' . sprintf ( '%06d' , $_REQUEST [ 'connkey' ]) . 's.sock' , 'Z' );
2018-01-28 04:03:22 +08:00
$semaphore = sem_get ( $key , 1 );
if ( sem_acquire ( $semaphore , 1 ) !== false ) {
2018-04-11 22:38:13 +08:00
if ( ! ( $socket = @ socket_create ( AF_UNIX , SOCK_DGRAM , 0 )) ) {
ajaxError ( 'socket_create() failed: ' . socket_strerror ( socket_last_error ()));
2018-02-01 04:51:12 +08:00
}
2018-01-28 04:03:22 +08:00
$localSocketFile = ZM_PATH_SOCKS . '/zms-' . sprintf ( '%06d' , $_REQUEST [ 'connkey' ]) . 'w.sock' ;
if ( file_exists ( $localSocketFile ) ) {
2019-02-22 22:19:07 +08:00
ZM\Warning ( " sock file $localSocketFile already exists?! Is someone else talking to zms? " );
2018-01-28 04:03:22 +08:00
// They could be. We can maybe have concurrent requests from a browser.
}
2019-01-23 05:45:38 +08:00
if ( ! socket_bind ( $socket , $localSocketFile ) ) {
2018-06-06 23:39:41 +08:00
ajaxError ( " socket_bind( $localSocketFile ) failed: " . socket_strerror ( socket_last_error ()) );
2018-01-28 04:03:22 +08:00
}
2007-08-30 02:11:09 +08:00
2018-01-28 04:03:22 +08:00
switch ( $_REQUEST [ 'command' ] ) {
2017-10-04 22:48:24 +08:00
case CMD_VARPLAY :
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( 'Varplaying to ' . $_REQUEST [ 'rate' ] );
2017-11-01 04:32:42 +08:00
$msg = pack ( 'lcn' , MSG_CMD , $_REQUEST [ 'command' ], $_REQUEST [ 'rate' ] + 32768 );
2017-10-04 22:48:24 +08:00
break ;
case CMD_ZOOMIN :
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( 'Zooming to ' . $_REQUEST [ 'x' ] . " , " . $_REQUEST [ 'y' ] );
2017-11-01 04:32:42 +08:00
$msg = pack ( 'lcnn' , MSG_CMD , $_REQUEST [ 'command' ], $_REQUEST [ 'x' ], $_REQUEST [ 'y' ] );
2017-10-04 22:48:24 +08:00
break ;
case CMD_PAN :
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( 'Panning to ' . $_REQUEST [ 'x' ] . " , " . $_REQUEST [ 'y' ] );
2017-11-01 04:32:42 +08:00
$msg = pack ( 'lcnn' , MSG_CMD , $_REQUEST [ 'command' ], $_REQUEST [ 'x' ], $_REQUEST [ 'y' ] );
2017-10-04 22:48:24 +08:00
break ;
case CMD_SCALE :
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( 'Scaling to ' . $_REQUEST [ 'scale' ] );
2017-11-01 04:32:42 +08:00
$msg = pack ( 'lcn' , MSG_CMD , $_REQUEST [ 'command' ], $_REQUEST [ 'scale' ] );
2017-10-04 22:48:24 +08:00
break ;
case CMD_SEEK :
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( 'Seeking to ' . $_REQUEST [ 'offset' ] );
2017-11-01 04:32:42 +08:00
$msg = pack ( 'lcN' , MSG_CMD , $_REQUEST [ 'command' ], $_REQUEST [ 'offset' ] );
2017-10-04 22:48:24 +08:00
break ;
default :
2019-03-20 00:05:45 +08:00
ZM\Logger :: Debug ( 'Sending command ' . $_REQUEST [ 'command' ]);
2017-11-01 04:32:42 +08:00
$msg = pack ( 'lc' , MSG_CMD , $_REQUEST [ 'command' ] );
2017-10-04 22:48:24 +08:00
break ;
2018-01-28 04:03:22 +08:00
}
2007-08-30 02:11:09 +08:00
2018-01-28 04:03:22 +08:00
$remSockFile = ZM_PATH_SOCKS . '/zms-' . sprintf ( '%06d' , $_REQUEST [ 'connkey' ]) . 's.sock' ;
2019-01-23 05:45:38 +08:00
// Pi can take up to 3 seconds for zms to start up.
$max_socket_tries = 1000 ;
2018-01-28 04:03:22 +08:00
// FIXME This should not exceed web_ajax_timeout
2018-02-01 04:51:12 +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.
// WHY? We will just send another one...
// ANSWER: Because otherwise we get a log of errors logged
2019-03-20 00:05:45 +08:00
//ZM\Logger::Debug("$remSockFile does not exist, waiting, current " . (time() - $start_time) . ' seconds' );
2018-02-01 04:51:12 +08:00
usleep ( 1000 );
2018-01-28 04:03:22 +08:00
}
2009-05-08 18:04:12 +08:00
2018-01-28 04:03:22 +08:00
if ( ! file_exists ( $remSockFile ) ) {
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. " );
} else {
if ( !@ socket_sendto ( $socket , $msg , strlen ( $msg ), 0 , $remSockFile ) ) {
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
2018-01-28 04:03:22 +08:00
$rSockets = array ( $socket );
$wSockets = NULL ;
$eSockets = NULL ;
$timeout = MSG_TIMEOUT - ( time () - $start_time );
2018-01-29 23:21:31 +08:00
$numSockets = socket_select ( $rSockets , $wSockets , $eSockets , intval ( $timeout / 1000 ), ( $timeout % 1000 ) * 1000 );
2018-01-28 04:03:22 +08:00
if ( $numSockets === false ) {
2019-02-22 22:19:07 +08:00
ZM\Error ( 'socket_select failed: ' . socket_strerror ( socket_last_error ()) );
2018-01-28 04:03:22 +08:00
ajaxError ( 'socket_select failed: ' . socket_strerror ( socket_last_error ()) );
} else if ( $numSockets < 0 ) {
2019-02-22 22:19:07 +08:00
ZM\Error ( " Socket closed $remSockFile " );
2018-01-28 04:03:22 +08:00
ajaxError ( " Socket closed $remSockFile " );
} else if ( $numSockets == 0 ) {
2019-02-22 22:19:07 +08:00
ZM\Error ( " Timed out waiting for msg $remSockFile " );
2019-03-20 00:05:45 +08:00
socket_set_nonblock ( $socket );
2019-01-23 05:45:38 +08:00
#ajaxError("Timed out waiting for msg $remSockFile");
2018-01-28 04:03:22 +08:00
} else if ( $numSockets > 0 ) {
if ( count ( $rSockets ) != 1 ) {
2019-02-27 00:43:07 +08:00
ZM\Error ( 'Bogus return from select, ' . count ( $rSockets ) . ' sockets available' );
2019-01-23 05:45:38 +08:00
ajaxError ( 'Bogus return from select, ' . count ( $rSockets ) . ' sockets available' );
2018-01-28 04:03:22 +08:00
}
2017-11-01 04:32:42 +08:00
}
2007-08-30 02:11:09 +08:00
2018-01-28 04:03:22 +08:00
switch ( $nbytes = @ socket_recvfrom ( $socket , $msg , MSG_DATA_SIZE , 0 , $remSockFile ) ) {
2017-10-04 22:48:24 +08:00
case - 1 :
2019-01-23 05:45:38 +08:00
ajaxError ( " socket_recvfrom( $remSockFile ) failed: " . socket_strerror ( socket_last_error ()));
2017-10-04 22:48:24 +08:00
break ;
case 0 :
2019-01-23 05:45:38 +08:00
ajaxError ( 'No data to read from socket' );
2017-10-04 22:48:24 +08:00
break ;
default :
if ( $nbytes != MSG_DATA_SIZE )
2019-01-23 05:45:38 +08:00
ajaxError ( " Got unexpected message size, got $nbytes , expected " . MSG_DATA_SIZE );
2017-10-04 22:48:24 +08:00
break ;
}
2007-08-30 02:11:09 +08:00
2019-01-23 05:45:38 +08:00
$data = unpack ( 'ltype' , $msg );
2018-01-28 04:03:22 +08:00
switch ( $data [ 'type' ] ) {
2017-10-04 22:48:24 +08:00
case MSG_DATA_WATCH :
2019-01-23 05:45:38 +08:00
$data = unpack ( 'ltype/imonitor/istate/dfps/ilevel/irate/ddelay/izoom/Cdelayed/Cpaused/Cenabled/Cforced' , $msg );
2019-02-27 00:43:07 +08:00
ZM\Logger :: Debug ( 'FPS: ' . $data [ 'fps' ]);
2017-10-04 22:48:24 +08:00
$data [ 'fps' ] = round ( $data [ 'fps' ], 2 );
2019-03-20 00:02:42 +08:00
ZM\Logger :: Debug ( 'FPS: ' . $data [ 'fps' ] );
2017-10-04 22:48:24 +08:00
$data [ 'rate' ] /= RATE_BASE ;
$data [ 'delay' ] = round ( $data [ 'delay' ], 2 );
$data [ 'zoom' ] = round ( $data [ 'zoom' ] / SCALE_BASE , 1 );
2017-11-01 04:32:42 +08:00
if ( ZM_OPT_USE_AUTH && ZM_AUTH_RELAY == 'hashed' ) {
2017-10-04 22:48:24 +08:00
$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 ) ) ) {
2018-04-13 06:43:57 +08:00
$data [ 'auth' ] = generateAuthHash ( ZM_AUTH_HASH_IPS );
2017-10-04 22:48:24 +08:00
}
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 :
2019-03-20 00:02:42 +08:00
if ( version_compare ( phpversion (), '5.6.0' , '<' ) ) {
ZM\Logger :: Debug ( 'Using old unpack methods to handle 64bit event id' );
2019-01-23 05:45:38 +08:00
$data = unpack ( 'ltype/ieventlow/ieventhigh/iprogress/irate/izoom/Cpaused' , $msg );
$data [ 'event' ] = $data [ 'eventhigh' ] << 32 | $data [ 'eventlow' ];
} else {
$data = unpack ( 'ltype/Qevent/iprogress/irate/izoom/Cpaused' , $msg );
}
2017-10-04 22:48:24 +08:00
$data [ 'rate' ] /= RATE_BASE ;
$data [ 'zoom' ] = round ( $data [ 'zoom' ] / SCALE_BASE , 1 );
2017-11-01 04:32:42 +08:00
if ( ZM_OPT_USE_AUTH && ZM_AUTH_RELAY == 'hashed' ) {
2017-10-04 22:48:24 +08:00
$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 ) ) ) {
2018-04-13 06:43:57 +08:00
$data [ 'auth' ] = generateAuthHash ( ZM_AUTH_HASH_IPS );
2017-10-04 22:48:24 +08:00
}
2007-08-30 02:11:09 +08:00
}
2019-01-23 05:45:38 +08:00
ajaxResponse ( array ( 'status' => $data ));
2017-10-04 22:48:24 +08:00
break ;
default :
2019-01-23 05:45:38 +08:00
ajaxError ( " Unexpected received message type ' $type ' " );
2018-01-28 04:03:22 +08:00
}
sem_release ( $semaphore );
} else {
2019-02-22 22:19:07 +08:00
ZM\Logger :: Debug ( " Couldn't get semaphore " );
2018-01-30 00:25:46 +08:00
ajaxResponse ( array () );
2007-08-30 02:11:09 +08:00
}
2018-01-30 00:25:46 +08:00
ajaxError ( 'Unrecognised action or insufficient permissions in ajax/stream' );
2007-08-30 02:11:09 +08:00
2017-10-04 22:48:24 +08:00
function ajaxCleanup () {
2018-01-28 04:03:22 +08:00
global $socket , $localSocketFile ;
2017-10-04 22:48:24 +08:00
if ( ! empty ( $socket ) )
@ socket_close ( $socket );
2018-01-28 04:03:22 +08:00
if ( ! empty ( $localSocketFile ) )
@ unlink ( $localSocketFile );
2008-10-17 00:12:23 +08:00
}
2007-08-30 02:11:09 +08:00
?>