Browse Source

Cameras: keep a custom ONVIF port (e.g. 8080) when credentials change

The camera address is ip + port + onvif_ctrl (la_mojo builds
http://ip[:port]onvif_ctrl). The web UI broke it for cameras whose ONVIF
interface isn't on port 80.

doCameraOperation.php, CMD 4 (change IP / credentials)
- An address without ":port" set port=NULL. The camera editor showed the
  IP without the port, so changing only the credentials moved a camera on
  8080 back to port 80. Now "ip:port" sets the port and "ip" keeps the
  stored one.
- A port kept inside onvif_ctrl (":8080/onvif/device_service", cameras
  added by discovery) is moved to `port`. Once port was set too, la_mojo
  built http://ip:8080:8080/onvif/device_service.

doCameraOperation.php, CMD 3 (add camera / check credentials)
- Camera from the DB: the port may also come from onvif_ctrl (it was read
  from `port` only, so the check went to port 80 and failed).
- Direct address: "ip:port" or the dialog's Port field (PO) is the ONVIF
  port. Discovery stores ip, port and the device service path separately.
  Without discovery, onvif_ctrl was the full MEDIA service URL and ip
  was "ip:port"; now it is /onvif/device_service, ip and port.
- count() of a string (PHP 8 TypeError, not caught) killed the request
  when the snapshot URI was read.

getCameraEditor.php
- The address field shows "ip:port" when the camera has a port.

Tested with PHP 8.3, a SQLite cameras table and a fake ONVIF camera on a
custom port. The old code dropped the port, built ip:8080:8080, stored
"ip:port" + the media URL, and crashed. Now the record and the
credential check are correct.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
pull/2/head
Claude 12 hours ago
parent
commit
1e3eb8a892
Failed to extract signature
  1. 92
      lib/controller/doCameraOperation.php
  2. 6
      lib/controller/getCameraEditor.php

92
lib/controller/doCameraOperation.php

@ -1,5 +1,36 @@
<?php
require_once('../ajax_asm.php');
/*
Camera address model (as la_mojo reads it): `ip` = host only, `port` = ONVIF/HTTP port (NULL = 80),
`onvif_ctrl` = path of the ONVIF device service. la_mojo builds http://ip[:port]onvif_ctrl.
*/
// "1.2.3.4" / "1.2.3.4:8080" -> array( host, port ) - port 0 when not given
function camSplitHostPort( $S ){
$S = trim( $S );
$P = 0;
if( preg_match( '/^(.*):(\d{1,5})$/', $S, $M ) ){
$S = $M[1];
$P = intval( $M[2] );
if( $P > 65535 ) $P = 0;
};
return array( $S, $P );
};
// ONVIF service URL or stored onvif_ctrl -> array( path, port ):
// "http://1.2.3.4:8080/onvif/device_service" and ":8080/onvif/device_service" -> ( "/onvif/device_service", 8080 )
function camOnvifPath( $U ){
$U = trim( $U );
$P = 0;
if( preg_match( '#^[a-z]+://#i', $U ) ){
$H = parse_url( $U );
$P = isset( $H['port'] ) ? intval( $H['port'] ) : 0;
$U = isset( $H['path'] ) ? $H['path'] : '/';
} elseif( preg_match( '#^:(\d{1,5})(/.*)?$#', $U, $M ) ){
$P = intval( $M[1] );
$U = isset( $M[2] ) ? $M[2] : '/';
};
return array( $U, $P );
};
if( $CUser->Data['id'] > 1 ){
$Data = array();
$Data['status'] = 'na';
@ -69,12 +100,17 @@
$AUTH = intval( $_POST['AU'] );
$onvif = new Ponvif();
$IP = '';
$PORT = 0;
if( $CameraID ){ // Cam From DB
$QS ="SELECT `ip`,`port` FROM `cameras` WHERE `id`={$CameraID}";
$QS ="SELECT `ip`,`port`,`onvif_ctrl` FROM `cameras` WHERE `id`={$CameraID}";
$JJ = $DATABASE->Query( $QS );
if( count( $JJ ) ){
$IP = $JJ[0]['ip'];
$PORT = intval($JJ[0]['port']);
if( !$PORT ){
// Older records keep the port in onvif_ctrl (":8080/onvif/device_service")
list( $CTRL, $PORT ) = camOnvifPath( $JJ[0]['onvif_ctrl'] );
};
if( $PORT ){
$onvif->setIPAddress( $IP.':'.$PORT );
} else {
@ -83,8 +119,14 @@
} else $CameraID = 0;
} else { // Direct IP
$Data['ss']='DirectIP';
$IP = $DATABASE->escape( $_POST['IP'] );
$onvif->setIPAddress( $IP );
// "1.2.3.4:8080" or the Port field of the dialog (PO): a camera whose ONVIF interface is not on port 80
list( $IP, $PORT ) = camSplitHostPort( $_POST['IP'] );
$IP = $DATABASE->escape( $IP );
if( !$PORT && isset( $_POST['PO'] ) ){
$PORT = intval( $_POST['PO'] );
if( $PORT < 1 || $PORT > 65535 ) $PORT = 0;
};
$onvif->setIPAddress( $IP ); // WS-Discovery probe goes to the host
$RR = $onvif->discover();
if( count( $RR ) ){
$Data['ss']='Discover Have';
@ -97,8 +139,11 @@
$CM = '';
$CI = $DATABASE->escape($R['IPAddr']);
$Frr = explode( ' ', $R['XAddrs']);
$Crr = explode( $CI, $Frr[0] );
$CP = $DATABASE->escape($Crr[1]);
// Path and port of the device service: the port was left inside onvif_ctrl (":8080/onvif/...")
list( $CTRL, $XPORT ) = camOnvifPath( $Frr[0] );
$CP = $DATABASE->escape( $CTRL );
if( !$PORT ) $PORT = $XPORT; // a port typed by the user wins (NAT / port forwarding)
$SPORT = $PORT ? $PORT : 'NULL';
$CN = '';
$CH = '';
$CC = 0;
@ -114,7 +159,7 @@
if( stripos($TBrr[0],'hardware') !== false ) $CH = $DATABASE->escape(urldecode($TBrr[1]));
if( stripos($TBrr[0],'mac') !== false ) $CM = $DATABASE->escape(urldecode($TBrr[1]));
};
$QS = "INSERT INTO `cameras`(hardware,type,name,uuid,mac,ip,status,onvif_ctrl,online,manual) VALUES('{$CH}',13,'{$CN}','{$CA}','{$CM}','{$CI}',0,'{$CP}',1,1) ON CONFLICT( uuid ) DO UPDATE SET ip='{$CI}', online=1";
$QS = "INSERT INTO `cameras`(hardware,type,name,uuid,mac,ip,port,status,onvif_ctrl,online,manual) VALUES('{$CH}',13,'{$CN}','{$CA}','{$CM}','{$CI}',{$SPORT},0,'{$CP}',1,1) ON CONFLICT( uuid ) DO UPDATE SET ip='{$CI}', port={$SPORT}, onvif_ctrl='{$CP}', online=1";
$DATABASE->Query($QS,false);
$CameraID = $DATABASE->getLastInsertID();
if( $CameraID == 0 ){
@ -126,10 +171,12 @@
};
} else {
$Data['ss'] .='No Discover';
$onvif->setIPAddress( $PORT ? $IP.':'.$PORT : $IP );
$onvif->setUsername( $EUser );
$onvif->setPassword( $EPass );
$onvif->setAuthMethod( $AUTH );
$CI = $IP;
$SPORT = $PORT ? $PORT : 'NULL';
$CameraID = 0;
try {
$onvif->initialize();
@ -139,8 +186,9 @@
$CN = $info['Manufacturer'].'_CAM'.$info['HardwareId'];
$CA = $info['SerialNumber'];
$CM = $info['SerialNumber'];
$CP = $onvif->getDeviceUri();
$QS = "INSERT INTO `cameras`(hardware,type,name,uuid,mac,ip,status,onvif_ctrl,online,manual) VALUES('{$CH}',13,'{$CN}','{$CA}','{$CM}','{$CI}',0,'{$CP}',1,1) ON CONFLICT( uuid ) DO UPDATE SET ip='{$CI}', online=1";
// getDeviceUri() returns the MEDIA service URL (full URL): la_mojo needs the device service path
$CP = '/onvif/device_service';
$QS = "INSERT INTO `cameras`(hardware,type,name,uuid,mac,ip,port,status,onvif_ctrl,online,manual) VALUES('{$CH}',13,'{$CN}','{$CA}','{$CM}','{$CI}',{$SPORT},0,'{$CP}',1,1) ON CONFLICT( uuid ) DO UPDATE SET ip='{$CI}', port={$SPORT}, onvif_ctrl='{$CP}', online=1";
$DATABASE->Query($QS,false);
$CameraID = $DATABASE->getLastInsertID();
};
@ -178,7 +226,7 @@
$Emrl = str_replace( $mrl_proto . '://' . $IP, '', $Emrl );
$TPArr = explode(':',$pic );
$pic_proto = $TPArr[0];
if( count( $TPArr[0] ) > 2 ){
if( count( $TPArr ) > 2 ){
$pic_port = intval( $TPArr[2] );
if($pic_port == 0 ) $pic_port = 80;
} else $pic_port = 80;
@ -209,6 +257,7 @@
if( $CameraID ){
$Data['status'] = 'ok';
if( $EUser!= '' ){
if( $PORT ) $onvif->setIPAddress( $IP.':'.$PORT ); // not port 80
$onvif->setUsername( $EUser );
$onvif->setPassword( $EPass );
$onvif->setAuthMethod( $AUTH );
@ -300,16 +349,21 @@
//$PA[2] = $EIP;
//$OVC = implode('/',$PA);
$AUTH = intval( $_POST['AU'] );
$TAr = explode( ':', $EIP );
if( count( $TAr ) > 1 ){
$EIP = $TAr[0];
$POR = intval($TAr[1]);
//$QS = "UPDATE cameras SET access_login='$EUser', access_pass='$EPass', auth_mode={$AUTH},`ip`='{$EIP}',port={$POR}, onvif_ctrl='{$OVC}' WHERE id={$CameraID}";
$QS = "UPDATE cameras SET access_login='$EUser', access_pass='$EPass', auth_mode={$AUTH},`ip`='{$EIP}',port={$POR} WHERE id={$CameraID}";
} else {
//$QS = "UPDATE cameras SET access_login='$EUser', access_pass='$EPass', auth_mode={$AUTH},`ip`='{$EIP}', port=NULL, onvif_ctrl='{$OVC}' WHERE id={$CameraID}";
$QS = "UPDATE cameras SET access_login='$EUser', access_pass='$EPass', auth_mode={$AUTH},`ip`='{$EIP}', port=NULL WHERE id={$CameraID}";
};
/*
Before: an address without ":port" set port=NULL. The editor showed the IP without the
port, so changing only the credentials moved a camera on 8080 back to port 80.
Now: "ip:port" sets the port, "ip" keeps the stored one. A port kept inside onvif_ctrl
(":8080/onvif/...", cameras added by discovery) is moved to `port`: with both, la_mojo
built http://ip:8080:8080/onvif/...
*/
list( $EIP, $POR ) = camSplitHostPort( $_POST['UI'] );
$EIP = $DATABASE->escape( $EIP );
list( $CTRL, $CTRL_PORT ) = camOnvifPath( $CC['onvif_ctrl'] );
if( !$POR ) $POR = intval( $CC['port'] );
if( !$POR ) $POR = $CTRL_PORT;
$SPOR = $POR ? $POR : 'NULL';
$ECTRL = $DATABASE->escape( $CTRL );
$QS = "UPDATE cameras SET access_login='$EUser', access_pass='$EPass', auth_mode={$AUTH},`ip`='{$EIP}', port={$SPOR}, onvif_ctrl='{$ECTRL}' WHERE id={$CameraID}";
$DATABASE->Query( $QS, false );
$Data['status'] = 'ok';
} else {

6
lib/controller/getCameraEditor.php

@ -19,6 +19,10 @@
};
$Cam['node_name'] = $Node['name'];
$Cam['name']=urldecode( $Cam['name'] );
// Address field "ip:port" for a camera not on port 80 (port also kept inside onvif_ctrl by older records)
$CamPort = intval( $Cam['port'] );
if( !$CamPort && preg_match( '#^:(\d{1,5})/#', trim( $Cam['onvif_ctrl'] ), $CamM ) ) $CamPort = intval( $CamM[1] );
$Cam['ip_port'] = $Cam['ip'] . ( $CamPort ? ':' . $CamPort : '' );
$TB = explode( ' ', $Cam['name'] );
$Cam['name'] = $TB[0];
if( stripos($Cam['ip'],'/dev/video') !==false ){
@ -110,7 +114,7 @@
id: 'am_ip_addr',
fieldLabel: '{$Labels->GetLabel('_CAM_IP_ADDRESS_')}',
width: '100%',
value: '{$Cam['ip']}'
value: '{$Cam['ip_port']}'
},{
xtype: 'textfield',
id: 'am_login',

Loading…
Cancel
Save