Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/645 Test devices authentication #673

Merged
merged 5 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
JahleelAbraham marked this conversation as resolved.
Show resolved Hide resolved
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']];
JahleelAbraham marked this conversation as resolved.
Show resolved Hide resolved
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
Loading