zoneminder/web/ajax/add_monitors.php

224 lines
7.7 KiB
PHP
Raw Normal View History

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
$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 ) {
error_reporting(0);
2017-10-28 11:52:08 +08:00
global $defaultMonitor;
$available_streams = array();
if ( ! isset($url_bits['port']) ) {
$cam_list_html = file_get_contents('http://'.$url_bits['host'].':5000/monitoring/');
if ( $cam_list_html ) {
ZM\Debug("Have content at port 5000/monitoring");
$matches_count = preg_match_all(
'/<a href="http:\/\/([.[:digit:]]+):([[:digit:]]+)\/\?action=stream" target="_blank">([^<]+)<\/a>/',
$cam_list_html, $cam_list );
ZM\Debug(print_r($cam_list,true));
}
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;
ZM\Debug("Have new stream " . print_r($new_stream,true) );
}
} else {
ZM\Info('No matches');
}
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(
'sec'=>0, // Timeout in seconds
'usec'=>500 // I assume timeout in microseconds
2017-10-28 11:52:08 +08:00
)
);
$new_stream = null;
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);
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 );
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
} # 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.
$stream['url'] = unparse_url($stream, array('path'=>'/','query'=>'action=stream'));
$monitors = ZM\Monitor::find(array('Path'=>$stream['url']));
if ( count($monitors) ) {
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 {
$stream['Monitor'] = clone $defaultMonitor;
2017-10-28 11:52:08 +08:00
if ( isset($stream['Width']) ) {
$stream['Monitor']->Width($stream['Width']);
$stream['Monitor']->Height($stream['Height']);
2017-10-28 11:52:08 +08:00
}
if ( isset($stream['Name']) ) {
$stream['Monitor']->Name($stream['Name']);
}
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
if ( canEdit('Monitors') ) {
2017-10-28 11:52:08 +08:00
switch ( $_REQUEST['action'] ) {
case 'probe' :
{
$available_streams = array();
$url_bits = null;
if ( preg_match('/(\d+)\.(\d+)\.(\d+)\.(\d+)/', $_REQUEST['url']) ) {
$url_bits = array('host'=>$_REQUEST['url']);
2017-10-28 11:52:08 +08:00
} else {
$url_bits = parse_url($_REQUEST['url']);
2017-10-28 11:52:08 +08:00
}
if ( 0 ) {
// Shortcut test
$monitors = ZM\Monitor::find( array('Path'=>$_REQUEST['url']) );
2017-10-28 11:52:08 +08:00
if ( count( $monitors ) ) {
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 ) {
ajaxError('The given URL was too malformed to parse.');
2017-10-28 11:52:08 +08:00
return;
}
$available_streams = probe($url_bits);
2017-10-28 11:52:08 +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'];
if ( $file['error'] > 0 ) {
ajaxError($file['error']);
2017-10-28 11:52:08 +08:00
return;
} else {
$filename = $file['name'];
2017-10-28 11:52:08 +08:00
$available_streams = array();
2017-10-28 11:52:08 +08:00
$row = 1;
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];
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) ) {
$url_bits = array('host'=>$url, 'scheme'=>'http');
2017-10-28 11:52:08 +08:00
} else {
$url_bits = parse_url($url);
2017-10-28 11:52:08 +08:00
}
if ( ! $url_bits ) {
ZM\Info("Bad url, skipping line $name $url $group");
2017-10-28 11:52:08 +08:00
continue;
}
$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);
ajaxResponse(array('Streams'=>$available_streams));
2017-10-28 11:52:08 +08:00
} else {
ajaxError('Uploaded file does not exist');
2017-10-28 11:52:08 +08:00
return;
}
}
} // end case import
default:
ZM\Warning('unknown action '.$_REQUEST['action']);
} // end switch action
2017-10-28 11:52:08 +08:00
} else {
ZM\Warning('Cannot edit monitors');
2017-10-28 11:52:08 +08:00
}
ajaxError('Unrecognised action '.$_REQUEST['action'].' or insufficient permissions for user ' . $user['Username']);
2017-10-28 11:52:08 +08:00
?>