Skip to content

Commit

Permalink
Merge pull request #673 from bluecherrydvr/feature/645-test-camera-au…
Browse files Browse the repository at this point in the history
…thenication

Feature/645 Test devices authentication
  • Loading branch information
JahleelAbraham authored Mar 21, 2024
2 parents ee91de2 + 18d2c3b commit 7373647
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 35 deletions.
23 changes: 20 additions & 3 deletions www/ajax/editip.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,26 @@ public function postData()
$id = intval($_POST['id']);
$camera = new ipCamera($id);
$result = $camera->edit($_POST);
data::responseJSON($result[0], $result[1]);
$camera = new ipCamera($id); // Reinstance the object to update information
$camera->checkConnection(); // Test the connection
data::responseJSON($result[0], $result[1], array(
json_encode($camera->info['connection_status']),
str_replace("%TYPE%", str_replace("IP-", "", $camera->info['protocol']),
$camera->info['connection_status']['success'] ? AIP_CONNECTION_SUCCESS : AIP_CONNECTION_FAIL)
));
exit();
}
}


public function postTest()
{
$id = intval($_POST['id']);
$camera = new ipCamera($id);
$camera->checkConnection();
data::responseJSON(10, "", array(
json_encode($camera->info['connection_status']),
str_replace("%TYPE%", str_replace("IP-", "", $camera->info['protocol']),
$camera->info['connection_status']['success'] ? AIP_CONNECTION_SUCCESS : AIP_CONNECTION_FAIL)
));
exit();
}
}
10 changes: 3 additions & 7 deletions www/ajax/ipcameracheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,9 @@ private function initProc()

$camera->checkConnection();

$status_message = '';
foreach($camera->info['connection_status'] as $type => $status){
if ($status!='OK'){
$status_message .= str_replace('%TYPE%', $type, constant('IP_ACCESS_STATUS_'.$status)).'<br /><br />';
}
};

$status_message = str_replace("%TYPE%", str_replace("IP-", "", $camera->info['protocol']),
$camera->info['connection_status']['success'] ? AIP_CONNECTION_SUCCESS : AIP_CONNECTION_FAIL);

echo $status_message;
}
}
Expand Down
4 changes: 4 additions & 0 deletions www/lib/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@
define('AIP_CHECK_ONVIF_SUCCESS', 'Successfull');
define('AIP_CHECK_ONVIF_ERROR', 'Unsuccessful');
define('AIP_LIMIT_ALLOWED_DEVICES', 'Could not add a camera, because exceeds the limit of the allowed devices.');
define('AIP_TEST_CONNECTION', 'Test Connection');
define('AIP_TEST_CONNECTION_MESSAGE', 'Test connection to IP Camera');
define('AIP_CONNECTION_SUCCESS', 'Connection Successful using %TYPE%');
define('AIP_CONNECTION_FAIL', 'Connection Unsuccessful using %TYPE%');

# HLS configuration
define('AIP_HLS_WINDOW_SIZE', 'HLS window size');
Expand Down
40 changes: 29 additions & 11 deletions www/lib/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -767,20 +767,37 @@ public function getInfo($id){
$this->ptzControl = new cameraPtz($this);
}
}
public function checkConnection(){
public function checkConnection() {
//FIXME: Currently only supports RTSP. MJPEG is currently untested
ini_set('default_socket_timeout', 1);
#needs server to check for RTSP //// $paths['rtsp'] = 'http://'.((empty($this->info['rtsp_username'])) ? '' : $this->info['rtsp_username'].':'.$this->info['rtsp_password'].'@').$this->info['ipAddr'].':'.$this->info['port'].$this->info['rtsp'];
$paths['mjpeg'] = 'http://'.((empty($this->info['rtsp_username'])) ? '' : $this->info['rtsp_username'].':'.$this->info['rtsp_password'].'@').((empty($this->info['ipAddrMjpeg'])) ? $this->info['ipAddr'] : $this->info['ipAddrMjpeg']).':'.$this->info['portMjpeg'].$this->info['mjpeg_path'];
$paths['http'] = 'http://'.((empty($this->info['rtsp_username'])) ? '' : $this->info['rtsp_username'].':'.$this->info['rtsp_password'].'@').((empty($this->info['ipAddrMjpeg'])) ? $this->info['ipAddr'] : $this->info['ipAddrMjpeg']);
foreach($paths as $type => $path){
$headers = @get_headers($path);
$contents = @file_get_contents($path);
if (!$headers) { $this->info['connection_status'][$type] = 'F'; continue; }
preg_match("/([0-9]{3})/", $headers[0], $response_code);
$this->info['connection_status'][$type] = ($response_code[0]=='200') ? 'OK' : $response_code[0];

$path = "";
$args = "";

switch($this->info['protocol']) {
case 'IP-RTSP':
$path = 'rtsp://'.((empty($this->info['rtsp_username'])) ? '' : $this->info['rtsp_username'].':'.$this->info['rtsp_password'].'@').$this->info['ipAddr'].':'.$this->info['port'].$this->info['rtsp'];
$args = array("-rtsp_flags +prefer_tcp", "-rtsp_transport tcp", "-rtsp_transport tcp")[$this->info['rtsp_rtp_prefer_tcp']];
break;
case 'IP-MJPEG':
//FIXME: This is the old logic for testing MJPEG. Testing for MJPEG is currently not supported by the bundled ffprobe method used for RTSP
$path = 'http://'.((empty($this->info['rtsp_username'])) ? '' : $this->info['rtsp_username'].':'.$this->info['rtsp_password'].'@').((empty($this->info['ipAddrMjpeg'])) ? $this->info['ipAddr'] : $this->info['ipAddrMjpeg']).':'.$this->info['portMjpeg'].$this->info['mjpeg_path'];
$headers = @get_headers($path);
if (!$headers) { $this->info['connection_status']['success'] = false; return; }
preg_match("/([0-9]{3})/", $headers[0], $response_code);
$this->info['connection_status']['success'] = ($response_code[0]=='200') ? true : false;
return;
}
return $this->info['connection_status'];

//-> '-stimeout' is measured in microseconds
$ffprobe_output = shell_exec(
"/usr/lib/bluecherry/ffprobe -stimeout 5000000 -hide_banner -show_format -show_streams -print_format json ".$args. " " . escapeshellarg($path));

$rtsp_data = json_decode($ffprobe_output, true);

$this->info['connection_status']['success'] = array_key_exists('streams', $rtsp_data) ? count($rtsp_data['streams']) > 0 : false;
}

protected static function autoConfigure($driver, $info){ #auto configure known cameras
include_once("ipcamlib.php");
$result = false;
Expand Down Expand Up @@ -857,6 +874,7 @@ public function edit($data){
#if there were no errors, edit the camera
$query = data::formQueryFromArray('update', 'Devices', $data[1], 'id', $this->info['id']);
$result = data::query($query, true);

return array($result, false);
}
public static function create($rawData){
Expand Down
40 changes: 26 additions & 14 deletions www/template/ajax/editip.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,27 @@

<div class="row">
<div class="col-lg-12 col-md-12">
<form action="/ajax/editip.php" method="POST" class="form-horizontal">
<input type="hidden" name="mode" value="editIp" />
<input type="hidden" name="id" value="<?php echo $ipCamera->info['id']; ?>" />

<div class="panel panel-default">
<div class="panel-body">

<div class="panel panel-default">
<div class="panel-body">

<div class="form-group">
<div class="col-lg-7 col-lg-offset-3 col-md-7 col-md-offset-2">
<div id="connStat" class="alert alert-info">
<span id="csText" style="line-height: 34px;"><?php echo AIP_TEST_CONNECTION_MESSAGE ?></span>
<form action="/ajax/editip.php/test" method="POST" class="form-horizontal" style="display: inline;">
<input type="hidden" name="mode" value="editIp" />
<input type="hidden" name="id" value="<?php echo $ipCamera->info['id']; ?>" />
<a type="submit" class="btn btn-warning send-req-form pull-right" data-func="testCameraLoadingHeader" data-func-after="testCameraConnection">
<i class="fa fa-check fa-fw"></i><?php echo AIP_TEST_CONNECTION ?>
</a>
</form>
</div>
</div>
</div>

<form action="/ajax/editip.php" method="POST" class="form-horizontal">
<input type="hidden" name="mode" value="editIp" />
<input type="hidden" name="id" value="<?php echo $ipCamera->info['id']; ?>" />
<div class="form-group">
<label class="col-lg-4 col-md-4 control-label"><?php echo VA_AUDIO_ENABLE; ?></label>

Expand Down Expand Up @@ -196,18 +210,16 @@
<input type="checkbox" name="debug_level" <?php echo ($ipCamera->info['debug_level']==1) ? 'checked' : ''; ?> />
</div>
</div>

<div class="form-group">
<div class="col-lg-6 col-lg-offset-4 col-md-6 col-md-offset-4">
<button class="btn btn-success send-req-form" type="submit"><i class="fa fa-check fa-fw"></i> <?php echo SAVE_CHANGES; ?></button>
<button class="btn btn-success send-req-form" data-func="testCameraLoadingHeader" data-func-after="testCameraConnection" type="submit">
<i class="fa fa-check fa-fw"></i> <?php echo SAVE_CHANGES." & ".AIP_TEST_CONNECTION?>
</button>
</div>
</div>

</div>
</form>
</div>


</form>
</div>
</div>
</div>

16 changes: 16 additions & 0 deletions www/template/dist/js/editip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$(function() {
});
function testCameraConnection(form, msg) {
let data = JSON.parse(msg.data[0]);

$('#connStat').find('form').show();
if(data.success)
$('#connStat').removeClass().addClass('alert alert-success').find('span').html(msg.data[1]);
else
$('#connStat').removeClass().addClass('alert alert-danger').find('span').html(msg.data[1]);
}

function testCameraLoadingHeader(form, msg) {
$('#connStat').removeClass().addClass('alert alert-warning').find('span').html('Testing...');
$('#connStat').find('form').hide();
}
1 change: 1 addition & 0 deletions www/template/main_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@
<script src="/template/dist/js/users.js"></script>
<script src="/template/dist/js/devices.js"></script>
<script src="/template/dist/js/addip.js"></script>
<script src="/template/dist/js/editip.js"></script>
<script src="/template/dist/js/ptzpresetlist.js"></script>
<script src="/template/dist/js/notifications.js"></script>
<script src="/template/dist/js/statistics.js"></script>
Expand Down

0 comments on commit 7373647

Please sign in to comment.