diff --git a/lib/controller/doCameraOperation.php b/lib/controller/doCameraOperation.php index 23d061d..c008a5e 100644 --- a/lib/controller/doCameraOperation.php +++ b/lib/controller/doCameraOperation.php @@ -31,6 +31,25 @@ }; return array( $U, $P ); }; + // Stream / snapshot URI of the camera -> array( proto, port, path ): the camera may report its LAN address + // (192.168.x.y behind NAT / port forwarding) - the path is taken whatever the host is. Before, only + // "proto://" was cut off, so with another host the whole URL was stored as the path. + function camMediaUri( $U ){ + if( !is_string( $U ) || $U == '' ) return array( '', 0, '' ); + $H = parse_url( $U ); + if( $H === false || !isset( $H['scheme'] ) ) return array( '', 0, $U ); + $Proto = strtolower( $H['scheme'] ); + $Port = isset( $H['port'] ) ? intval( $H['port'] ) : 0; + if( $Port == 0 ){ + switch( $Proto ){ + case 'rtsp': $Port = 554; break; + case 'http': $Port = 80; break; + case 'https': $Port = 443; break; + }; + }; + $Path = ( isset( $H['path'] ) ? $H['path'] : '/' ) . ( isset( $H['query'] ) ? '?' . $H['query'] : '' ); + return array( $Proto, $Port, $Path ); + }; if( $CUser->Data['id'] > 1 ){ $Data = array(); $Data['status'] = 'na'; @@ -211,28 +230,10 @@ }; $mrl = $onvif->media_GetStreamUri($source['profiletoken']); $pic = $onvif->media_GetSnapshotUri($source['profiletoken']); - $TPArr = explode(':',$mrl); - $mrl_proto = $TPArr[0]; - $mrl_port = intval( $TPArr[2] ); - if( $mrl_port == 0 ) { - switch( $mrl_proto ){ - case 'rtsp': $mrl_port = 554; break; - case 'http': $mrl_port = 80; break; - case 'https': $mrl_port = 443; break; - }; - }; - $Emrl = $DATABASE->escape( $mrl ); - $Emrl = str_replace( $mrl_proto . '://' . $IP . ':' . $mrl_port, '', $Emrl ); - $Emrl = str_replace( $mrl_proto . '://' . $IP, '', $Emrl ); - $TPArr = explode(':',$pic ); - $pic_proto = $TPArr[0]; - if( count( $TPArr ) > 2 ){ - $pic_port = intval( $TPArr[2] ); - if($pic_port == 0 ) $pic_port = 80; - } else $pic_port = 80; - $Epic = $DATABASE->escape( $pic ); - $Epic = str_replace( $pic_proto . '://' . $IP . ':' . $pic_port, '', $Epic ); - $Epic = str_replace( $pic_proto . '://' . $IP , '', $Epic ); + list( $mrl_proto, $mrl_port, $Emrl ) = camMediaUri( $mrl ); + $Emrl = $DATABASE->escape( $Emrl ); + list( $pic_proto, $pic_port, $Epic ) = camMediaUri( $pic ); + $Epic = $DATABASE->escape( $Epic ); $QS = "INSERT INTO `media_streams`(`source_id`,`source_type`,`valid`,`name`,`token`,`img_width`,`img_height`,`compression_type`,`bitrate`,`fps`,`mrl`,`mrl_proto`,`mrl_port`,`picture_proto`,`picture_port`,`picture`) VALUES({$CameraID},'D',1,'{$Ename}','{$Etoken}',{$Ewidth},{$Eheight},'{$Eencode}',{$Ebitrate},{$Efps},'{$Emrl}','{$mrl_proto}','{$mrl_port}','{$pic_proto}','{$pic_port}','{$Epic}') ON CONFLICT(`source_id`,`token`) DO UPDATE SET `source_type`='D', `valid`=1, `mrl`='{$Emrl}', `mrl_proto`='{$mrl_proto}', `mrl_port`='{$mrl_port}', `picture_proto`='{$pic_proto}', `picture_port`='{$pic_port}', `picture`='{$Epic}'"; $Data['ss'] .='Added'; $Data['QS'] = $QS; @@ -280,26 +281,10 @@ }; $mrl = $onvif->media_GetStreamUri($source['profiletoken']); $pic = $onvif->media_GetSnapshotUri($source['profiletoken']); - $TPArr = explode(':',$mrl); - $mrl_proto = $TPArr[0]; - $mrl_port = intval( $TPArr[2] ); - if( $mrl_port == 0 ) { - switch( $mrl_proto ){ - case 'rtsp': $mrl_port = 554; break; - case 'http': $mrl_port = 80; break; - case 'https': $mrl_port = 443; break; - }; - }; - $Emrl = $DATABASE->escape( $mrl ); - $Emrl = str_replace( $mrl_proto . '://' . $IP . ':' . $mrl_port, '', $Emrl ); - $Emrl = str_replace( $mrl_proto . '://' . $IP, '', $Emrl ); - $TPArr = explode(':',$pic ); - $pic_proto = $TPArr[0]; - $pic_port = intval( $TPArr[2] ); - if($pic_port == 0 ) $pic_port = 80; - $Epic = $DATABASE->escape( $pic ); - $Epic = str_replace( $pic_proto . '://' . $IP . ':' . $pic_port, '', $Epic ); - $Epic = str_replace( $pic_proto . '://' . $IP , '', $Epic ); + list( $mrl_proto, $mrl_port, $Emrl ) = camMediaUri( $mrl ); + $Emrl = $DATABASE->escape( $Emrl ); + list( $pic_proto, $pic_port, $Epic ) = camMediaUri( $pic ); + $Epic = $DATABASE->escape( $Epic ); $QS = "INSERT INTO `media_streams`(`source_id`,`source_type`,`valid`,`name`,`token`,`img_width`,`img_height`,`compression_type`,`bitrate`,`fps`,`mrl`,`mrl_proto`,`mrl_port`,`picture_proto`,`picture_port`,`picture`) VALUES({$CameraID},'D',1,'{$Ename}','{$Etoken}',{$Ewidth},{$Eheight},'{$Eencode}',{$Ebitrate},{$Efps},'{$Emrl}','{$mrl_proto}','{$mrl_port}','{$pic_proto}','{$pic_port}','{$Epic}') ON CONFLICT(`source_id`,`token`) DO UPDATE SET `source_type`='D', `valid`=1, `mrl`='{$Emrl}', `mrl_proto`='{$mrl_proto}', `mrl_port`='{$mrl_port}', `picture_proto`='{$pic_proto}', `picture_port`='{$pic_port}', `picture`='{$Epic}'"; $DATABASE->Query( $QS, false ); }; diff --git a/lib/php/onvif/toto.php b/lib/php/onvif/toto.php index 1d95aca..18224ea 100644 --- a/lib/php/onvif/toto.php +++ b/lib/php/onvif/toto.php @@ -142,9 +142,9 @@ class Ponvif { if (!is_array($this->capabilities) || !isset($this->capabilities['Media']['XAddr'])) throw new Exception('GetCapabilities: no media service in the reply'); $onvifVersion=$this->_getOnvifVersion($this->capabilities); - $this->mediauri=$onvifVersion['media']; - $this->deviceuri=$onvifVersion['device']; - $this->ptzuri=$onvifVersion['ptz']; + $this->mediauri=$this->_reachableUri($onvifVersion['media']); + $this->deviceuri=$this->_reachableUri($onvifVersion['device']); + $this->ptzuri=$this->_reachableUri($onvifVersion['ptz']); preg_match("/^http(.*)onvif\//",$this->mediauri,$matches); $this->baseuri=isset($matches[0]) ? $matches[0] : ''; $this->onvifversion=array('major'=>$onvifVersion['major'],'minor'=>$onvifVersion['minor']); @@ -662,6 +662,22 @@ class Ponvif { return $version; } + /* + A camera behind NAT / port forwarding reports its services at its LAN address (e.g. + http://192.168.200.118:8080/onvif/media while it is reached as 2.54.237.28:8080): the next + request went to an address that cannot be reached and the UI said "Credentials are Wrong". + A service URL on another host is moved to the address the device service was reached at + (same scheme, host and port), keeping its path. A service on the same host is kept as is. + */ + protected function _reachableUri($url) { + if (!is_string($url) || $url=='') return $url; + $u=parse_url($url); + $r=parse_url('http://'.$this->ipaddress); + if ($u===false || $r===false || !isset($u['host']) || !isset($r['host'])) return $url; + if (strcasecmp($u['host'], $r['host'])==0) return $url; + return 'http://'.$this->ipaddress.(isset($u['path']) ? $u['path'] : '/').(isset($u['query']) ? '?'.$u['query'] : ''); + } + // Why the last request failed: " (HTTP 401: reply is not a SOAP envelope)" or the SOAP Fault reason protected function _faultText() { $r=$this->lastresponse;