2017-10-28 11:52:08 +08:00
|
|
|
<?php
|
2017-11-17 09:39:48 +08:00
|
|
|
error_reporting(0);
|
2017-10-28 11:52:08 +08:00
|
|
|
|
2019-02-22 22:19:07 +08:00
|
|
|
$defaultMonitor = new ZM\Monitor();
|
2017-10-28 11:52:08 +08:00
|
|
|
$defaultMonitor->set(array(
|
|
|
|
'StorageId' => 1,
|
|
|
|
'ServerId' => 'auto',
|
2017-11-17 09:39:48 +08:00
|
|
|
'Function' => 'Mocord',
|
2017-10-28 11:52:08 +08:00
|
|
|
'Type' => 'Ffmpeg',
|
|
|
|
'Enabled' => '1',
|
|
|
|
'Colour' => '4', // 32bit
|
2017-11-17 09:39:48 +08:00
|
|
|
'ImageBufferCount' => '20',
|
|
|
|
'WarmupCount' => '0',
|
|
|
|
'PreEventCount' => '0',
|
|
|
|
'StreamReplayBuffer' => '0',
|
|
|
|
'SaveJPEGs' => '4',
|
|
|
|
'VideoWriter' => '1',
|
|
|
|
'MaxFPS' => '20',
|
|
|
|
'AlarmMaxFPS' => '20',
|
2017-10-28 11:52:08 +08:00
|
|
|
) );
|
|
|
|
|
|
|
|
function probe( &$url_bits ) {
|
2018-01-29 10:06:13 +08:00
|
|
|
error_reporting(0);
|
2017-10-28 11:52:08 +08:00
|
|
|
global $defaultMonitor;
|
|
|
|
$available_streams = array();
|
|
|
|
if ( ! isset($url_bits['port']) ) {
|
2018-01-29 10:06:13 +08:00
|
|
|
|
|
|
|
$cam_list_html = file_get_contents('http://'.$url_bits['host'].':5000/monitoring/');
|
|
|
|
if ( $cam_list_html ) {
|
2020-10-14 22:39:25 +08:00
|
|
|
ZM\Debug("Have content at port 5000/monitoring");
|
2018-01-29 10:06:13 +08:00
|
|
|
$matches_count = preg_match_all(
|
|
|
|
'/<a href="http:\/\/([.[:digit:]]+):([[:digit:]]+)\/\?action=stream" target="_blank">([^<]+)<\/a>/',
|
|
|
|
$cam_list_html, $cam_list );
|
2020-10-14 22:39:25 +08:00
|
|
|
ZM\Debug(print_r($cam_list,true));
|
2018-01-29 10:06:13 +08:00
|
|
|
}
|
|
|
|
if ( $matches_count ) {
|
|
|
|
for( $index = 0; $index < $matches_count; $index ++ ) {
|
|
|
|
$new_stream = $url_bits; // make a copy
|
|
|
|
$new_stream['port'] = $cam_list[2][$index];
|
|
|
|
$new_stream['Name'] = trim($cam_list[3][$index]);
|
|
|
|
if ( ! isset($new_stream['scheme'] ) )
|
|
|
|
$new_stream['scheme'] = 'http';
|
|
|
|
$available_streams[] = $new_stream;
|
2020-10-14 22:39:25 +08:00
|
|
|
ZM\Debug("Have new stream " . print_r($new_stream,true) );
|
2018-01-29 10:06:13 +08:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-22 22:19:07 +08:00
|
|
|
ZM\Info('No matches');
|
2018-01-29 10:06:13 +08:00
|
|
|
}
|
|
|
|
if ( 0 ) {
|
2017-10-28 11:52:08 +08:00
|
|
|
// No port given, do a port scan
|
|
|
|
foreach ( range( 2000, 2007 ) as $port ) {
|
|
|
|
$socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
|
|
|
|
socket_set_option( $socket,
|
|
|
|
SOL_SOCKET, // socket level
|
|
|
|
SO_SNDTIMEO, // timeout option
|
|
|
|
array(
|
2020-02-07 02:20:35 +08:00
|
|
|
'sec'=>0, // Timeout in seconds
|
|
|
|
'usec'=>500 // I assume timeout in microseconds
|
2017-10-28 11:52:08 +08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
$new_stream = null;
|
2020-02-07 02:20:35 +08:00
|
|
|
Info('Testing connection to '.$url_bits['host'].':'.$port);
|
2017-10-28 11:52:08 +08:00
|
|
|
if ( socket_connect( $socket, $url_bits['host'], $port ) ) {
|
|
|
|
$new_stream = $url_bits; // make a copy
|
|
|
|
$new_stream['port'] = $port;
|
|
|
|
} else {
|
|
|
|
socket_close($socket);
|
2020-02-07 02:20:35 +08:00
|
|
|
ZM\Info('No connection to '.$url_bits['host'].' on port '.$port);
|
2017-10-28 11:52:08 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( $new_stream ) {
|
|
|
|
if ( ! isset($new_stream['scheme'] ) )
|
|
|
|
$new_stream['scheme'] = 'http';
|
|
|
|
$url = unparse_url($new_stream, array('path'=>'/', 'query'=>'action=snapshot'));
|
|
|
|
list($width, $height, $type, $attr) = getimagesize( $url );
|
2019-02-22 22:19:07 +08:00
|
|
|
ZM\Info("Got $width x $height from $url");
|
2017-10-28 11:52:08 +08:00
|
|
|
$new_stream['Width'] = $width;
|
|
|
|
$new_stream['Height'] = $height;
|
|
|
|
|
|
|
|
//try {
|
|
|
|
//if ( $response = do_request( 'GET', $url ) ) {
|
|
|
|
//$new_stream['path'] = '/';
|
|
|
|
//$new_stream['query'] = '?action=stream';
|
|
|
|
//$image = imagecreatefromstring($response);
|
|
|
|
////$size = getimagesize( $image );
|
|
|
|
//
|
|
|
|
//} else {
|
|
|
|
//Info("No response from $url");
|
|
|
|
//}
|
|
|
|
//} catch ( EXception $e ) {
|
|
|
|
//Info("No response from $url");
|
|
|
|
//}
|
|
|
|
$available_streams[] = $new_stream;
|
|
|
|
} // end if new_Stream
|
|
|
|
} // end foreach port to scan
|
2018-01-29 10:06:13 +08:00
|
|
|
} # end if 0
|
2017-10-28 11:52:08 +08:00
|
|
|
} else {
|
|
|
|
// A port was specified, so don't need to port scan.
|
|
|
|
$available_streams[] = $url_bits;
|
|
|
|
}
|
|
|
|
foreach ( $available_streams as &$stream ) {
|
|
|
|
# check for existence in db.
|
2020-02-07 02:20:35 +08:00
|
|
|
$stream['url'] = unparse_url($stream, array('path'=>'/','query'=>'action=stream'));
|
|
|
|
$monitors = ZM\Monitor::find(array('Path'=>$stream['url']));
|
2018-01-29 10:06:13 +08:00
|
|
|
if ( count($monitors) ) {
|
2020-02-07 02:20:35 +08:00
|
|
|
ZM\Info('Found monitors matching ' . $stream['url'] );
|
2017-10-28 11:52:08 +08:00
|
|
|
$stream['Monitor'] = $monitors[0];
|
|
|
|
if ( isset( $stream['Width'] ) and ( $stream['Monitor']->Width() != $stream['Width'] ) ) {
|
|
|
|
$stream['Warning'] .= 'Monitor width ('.$stream['Monitor']->Width().') and stream width ('.$stream['Width'].") do not match!\n";
|
|
|
|
}
|
|
|
|
if ( isset( $stream['Height'] ) and ( $stream['Monitor']->Height() != $stream['Height'] ) ) {
|
|
|
|
$stream['Warning'] .= 'Monitor height ('.$stream['Monitor']->Height().') and stream width ('.$stream['Height'].") do not match!\n";
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-29 10:06:13 +08:00
|
|
|
$stream['Monitor'] = clone $defaultMonitor;
|
2017-10-28 11:52:08 +08:00
|
|
|
if ( isset($stream['Width']) ) {
|
2020-02-07 02:20:35 +08:00
|
|
|
$stream['Monitor']->Width($stream['Width']);
|
|
|
|
$stream['Monitor']->Height($stream['Height']);
|
2017-10-28 11:52:08 +08:00
|
|
|
}
|
2018-01-29 10:06:13 +08:00
|
|
|
if ( isset($stream['Name']) ) {
|
2020-02-07 02:20:35 +08:00
|
|
|
$stream['Monitor']->Name($stream['Name']);
|
2018-01-29 10:06:13 +08:00
|
|
|
}
|
2017-10-28 11:52:08 +08:00
|
|
|
} // Monitor found or not
|
|
|
|
} // end foreach Stream
|
|
|
|
|
|
|
|
#$macCommandString = 'arp ' . $url_bits['host'] . " | awk 'BEGIN{ i=1; } { i++; if(i==3) print $3 }'";
|
|
|
|
#$mac = exec($macCommandString);
|
|
|
|
#Info("Mac $mac");
|
|
|
|
return $available_streams;
|
|
|
|
} // end function probe
|
|
|
|
|
2020-02-07 02:20:35 +08:00
|
|
|
if ( canEdit('Monitors') ) {
|
2017-10-28 11:52:08 +08:00
|
|
|
switch ( $_REQUEST['action'] ) {
|
|
|
|
case 'probe' :
|
|
|
|
{
|
|
|
|
$available_streams = array();
|
|
|
|
$url_bits = null;
|
2020-02-07 02:20:35 +08:00
|
|
|
if ( preg_match('/(\d+)\.(\d+)\.(\d+)\.(\d+)/', $_REQUEST['url']) ) {
|
|
|
|
$url_bits = array('host'=>$_REQUEST['url']);
|
2017-10-28 11:52:08 +08:00
|
|
|
} else {
|
2020-02-07 02:20:35 +08:00
|
|
|
$url_bits = parse_url($_REQUEST['url']);
|
2017-10-28 11:52:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( 0 ) {
|
|
|
|
// Shortcut test
|
2019-02-22 22:19:07 +08:00
|
|
|
$monitors = ZM\Monitor::find( array('Path'=>$_REQUEST['url']) );
|
2017-10-28 11:52:08 +08:00
|
|
|
if ( count( $monitors ) ) {
|
2019-02-22 22:19:07 +08:00
|
|
|
ZM\Info("Monitor found for " . $_REQUEST['url']);
|
2017-10-28 11:52:08 +08:00
|
|
|
$url_bits['url'] = $_REQUEST['url'];
|
|
|
|
$url_bits['Monitor'] = $monitors[0];
|
|
|
|
$available_stream[] = $url_bits;
|
|
|
|
ajaxResponse( array ( 'Streams'=>$available_streams) );
|
|
|
|
return;
|
|
|
|
} # end url already has a monitor
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $url_bits ) {
|
2020-02-07 02:20:35 +08:00
|
|
|
ajaxError('The given URL was too malformed to parse.');
|
2017-10-28 11:52:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-07 02:20:35 +08:00
|
|
|
$available_streams = probe($url_bits);
|
2017-10-28 11:52:08 +08:00
|
|
|
|
2020-02-07 02:20:35 +08:00
|
|
|
ajaxResponse(array('Streams'=>$available_streams));
|
2017-10-28 11:52:08 +08:00
|
|
|
return;
|
|
|
|
} // end case url_probe
|
|
|
|
case 'import':
|
|
|
|
{
|
|
|
|
|
|
|
|
$file = $_FILES['import_file'];
|
|
|
|
|
2020-02-07 02:20:35 +08:00
|
|
|
if ( $file['error'] > 0 ) {
|
|
|
|
ajaxError($file['error']);
|
2017-10-28 11:52:08 +08:00
|
|
|
return;
|
|
|
|
} else {
|
2020-02-07 02:20:35 +08:00
|
|
|
$filename = $file['name'];
|
2017-10-28 11:52:08 +08:00
|
|
|
|
2020-02-07 02:20:35 +08:00
|
|
|
$available_streams = array();
|
2017-10-28 11:52:08 +08:00
|
|
|
$row = 1;
|
2020-02-07 02:20:35 +08:00
|
|
|
if ( ($handle = fopen($file['tmp_name'], 'r')) !== FALSE ) {
|
|
|
|
while ( ($data = fgetcsv($handle, 1000, ',')) !== FALSE ) {
|
2017-10-28 11:52:08 +08:00
|
|
|
$name = $data[0];
|
|
|
|
$url = $data[1];
|
|
|
|
$group = $data[2];
|
2019-02-22 22:19:07 +08:00
|
|
|
ZM\Info("Have the following line data $name $url $group");
|
2017-10-28 11:52:08 +08:00
|
|
|
|
|
|
|
$url_bits = null;
|
|
|
|
if ( preg_match('/(\d+)\.(\d+)\.(\d+)\.(\d+)/', $url) ) {
|
2020-02-07 02:20:35 +08:00
|
|
|
$url_bits = array('host'=>$url, 'scheme'=>'http');
|
2017-10-28 11:52:08 +08:00
|
|
|
} else {
|
2020-02-07 02:20:35 +08:00
|
|
|
$url_bits = parse_url($url);
|
2017-10-28 11:52:08 +08:00
|
|
|
}
|
|
|
|
if ( ! $url_bits ) {
|
2019-02-22 22:19:07 +08:00
|
|
|
ZM\Info("Bad url, skipping line $name $url $group");
|
2017-10-28 11:52:08 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-07 02:20:35 +08:00
|
|
|
$available_streams += probe($url_bits);
|
2017-10-28 11:52:08 +08:00
|
|
|
|
|
|
|
//$url_bits['url'] = unparse_url( $url_bits );
|
|
|
|
//$url_bits['Monitor'] = $defaultMonitor;
|
|
|
|
//$url_bits['Monitor']->Name( $name );
|
|
|
|
//$url_bits['Monitor']->merge( $_POST['newMonitor'] );
|
|
|
|
//$available_streams[] = $url_bits;
|
|
|
|
|
|
|
|
} // end while rows
|
|
|
|
fclose($handle);
|
2020-02-07 02:20:35 +08:00
|
|
|
ajaxResponse(array('Streams'=>$available_streams));
|
2017-10-28 11:52:08 +08:00
|
|
|
} else {
|
2020-02-07 02:20:35 +08:00
|
|
|
ajaxError('Uploaded file does not exist');
|
2017-10-28 11:52:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end case import
|
|
|
|
default:
|
2020-02-07 02:20:35 +08:00
|
|
|
ZM\Warning('unknown action '.$_REQUEST['action']);
|
|
|
|
} // end switch action
|
2017-10-28 11:52:08 +08:00
|
|
|
} else {
|
2020-02-07 02:20:35 +08:00
|
|
|
ZM\Warning('Cannot edit monitors');
|
2017-10-28 11:52:08 +08:00
|
|
|
}
|
|
|
|
|
2020-02-07 02:20:35 +08:00
|
|
|
ajaxError('Unrecognised action '.$_REQUEST['action'].' or insufficient permissions for user ' . $user['Username']);
|
2017-10-28 11:52:08 +08:00
|
|
|
?>
|