Data['id'] > 1 ){ $Data = array(); $Data['success'] = false; if( isset( $_POST['CID'] ) ){ $ECID = intval( $_POST['CID'] ); if( $ECID > 0 ){ //Check if Camera Already has password and discovered sources! $QS = "SELECT * FROM media_streams WHERE source_id={$ECID}"; $Result = $DATABASE->Query($QS,false); if( count( $Result ) > 0 ){ foreach( $Result as $CD ){ $cnode['id'] = $CD['id']; $cnode['name'] = $CD['name']; $cnode['size'] = $CD['img_width'].'x'.$CD['img_height'].'@'.$CD['fps']; $cnode['encode'] = $CD['compression_type']; $cnode['bitrate'] = $CD['bitrate']; $cnode['pic'] = $CD['picture_proto'] . '://' . $IP . ':' . $CD['picture_port'] . $CD['picture']; $Data['nodes'][] = $cnode; }; $Data['success'] = true; echo json_encode( $Data ); exit(); }; }; $EUser = $DATABASE->escape( $_POST['GUG'] ); $EPass = $DATABASE->escape( $_POST['GOG'] ); $AUTH = intval( $_POST['GAT'] ); $QS = "SELECT * FROM cameras WHERE id = {$ECID}"; $Result = $DATABASE->Query( $QS, false ); if( count($Result) ){ $IP = $Result[0]['ip']; $onvif = new Ponvif(); $onvif->setUsername( $EUser ); $onvif->setPassword( $EPass ); $onvif->setIPAddress( $IP ); $onvif->setAuthMethod( $AUTH ); try { $onvif->initialize(); if ( !$onvif->isFault($devices=$onvif->getSources())){// die("Error getting sources"); foreach( $devices[0] as $source ) if( is_array( $source ) ){ //print_r( $source ); $Ename = $DATABASE->escape( $source['profilename']); $Etoken = $DATABASE->escape( $source['profiletoken']); $Eencode = $DATABASE->escape( $source['encoding']); $Ewidth = intval($source['width']); $Eheight = intval($source['height']); $Efps = intval($source['fps']); $Ebitrate = intval($source['bitrate']); if( isset( $source['ptz'] ) ){ $Eptz = $DATABASE->escape( $source['ptz']['nodetoken'] ); } else { $Eptz=''; }; $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 ); $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({$ECID},'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 ); }; $QS = "UPDATE cameras SET access_login='$EUser', access_pass='$EPass', auth_mode={$AUTH}, ptz_token='{$Eptz}' WHERE id={$ECID}"; $DATABASE->Query( $QS, false ); } else { $Data['error'] = 'Provided Credentials are Wrong!'; echo json_encode( $Data ); exit(); }; } catch (Exception $e) { if( stripos( $e->getMessage(), 'GetCapabilities')!== false ){ $Data['error'] = '

Camera IP Address is Wrong!

Please consider run network discovery.

To refresh all aveable cameras addreses.

'; } else { $Data['error'] = 'Provided Credentials are Wrong!'; }; //$Data['error'] = 'Caught exception: '.$e->getMessage(); echo json_encode( $Data ); exit(); }; }; $QS = "SELECT * FROM media_streams WHERE source_id={$ECID}"; $Result = $DATABASE->Query($QS,false); foreach( $Result as $CD ){ $cnode['id'] = $CD['id']; $cnode['name'] = $CD['name']; $cnode['size'] = $CD['img_width'].'x'.$CD['img_height'].'@'.$CD['fps']; $cnode['encode'] = $CD['compression_type']; $cnode['bitrate'] = $CD['bitrate']; $Data['nodes'][] = $cnode; }; $Data['success'] = true; }; echo json_encode( $Data ); } else { header('location: /'); }; ?>