You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1330 lines
39 KiB
1330 lines
39 KiB
<?php
|
|
|
|
require_once( '/srv/scripts/SQLite_Class.php');
|
|
$WEB_USER_SUDO_PWD='ujpQFE/<\'Deq+kK5=tL>8~';
|
|
$WEB_USER_SUDO_PWD='terrarium';
|
|
|
|
if( file_exists( '/srv/etc/system.json' ) ){
|
|
$SYS_CONFIG = json_decode( file_get_contents( '/srv/etc/system.json' ), true );
|
|
} else {
|
|
$SYS_CONFIG = array();
|
|
};
|
|
|
|
function save_system_config(){
|
|
global $SYS_CONFIG;
|
|
$FD = fopen( '/srv/etc/system.json', "w+" );
|
|
if( $FD ){
|
|
fprintf( $FD, json_encode( $SYS_CONFIG ) );
|
|
fclose( $FD );
|
|
};
|
|
};
|
|
|
|
function reload_system_config(){
|
|
global $SYS_CONFIG;
|
|
if( file_exists( '/srv/etc/system.json' ) ){
|
|
$SYS_CONFIG = json_decode( file_get_contents( '/srv/etc/system.json' ), true );
|
|
} else {
|
|
$SYS_CONFIG = array();
|
|
};
|
|
};
|
|
|
|
|
|
function out_log( $str ){
|
|
$FD = fopen( '/srv/tmp/task_log.var', "a+" );
|
|
if( $FD ){
|
|
fprintf( $FD, "%s\n", trim($str) );
|
|
fclose( $FD );
|
|
};
|
|
};
|
|
|
|
function run_command( $CMD ){
|
|
$FD = @popen( $CMD, "r" );
|
|
$Out = array();
|
|
if( $FD ){
|
|
while (($buffer = fgets( $FD ) ) !== false ){
|
|
//out_log( $buffer );
|
|
$Out[] = $buffer;
|
|
};
|
|
if( !feof( $FD ) ){
|
|
out_log( "Error: unexpected fgets() fail" );
|
|
}
|
|
pclose($FD);
|
|
};
|
|
};
|
|
|
|
function run_root_command( $CMD ){
|
|
$sodo_user_pwd = 'terrarium';
|
|
$EXEC = "echo \"{$sodo_user_pwd}\" | sudo -S {$CMD}";
|
|
$FD = @popen( $EXEC, "r" );
|
|
$Out = array();
|
|
if( $FD ){
|
|
while (($buffer = fgets( $FD ) ) !== false ){
|
|
//out_log( $buffer );
|
|
$Out[] = $buffer;
|
|
};
|
|
if( !feof( $FD ) ){
|
|
out_log( "Error: unexpected fgets() fail" );
|
|
}
|
|
pclose($FD);
|
|
};
|
|
return $Out;
|
|
};
|
|
|
|
|
|
function nmcli_list_gsm_connections(){
|
|
$O = run_root_command( "nmcli -c no -s -t connection show" );
|
|
$Out = array();
|
|
foreach( $O as $CL ){
|
|
$T = explode( ':', trim( $CL ) );
|
|
if( $T[2] == 'gsm' ) $Out[]=$T[0];
|
|
};
|
|
return $Out;
|
|
};
|
|
|
|
function nmcli_get_connection_info( $CN ){
|
|
$O = run_root_command( "nmcli -c no -s -t connection show {$CN}" );
|
|
$Out = array();
|
|
foreach( $O as $CL ){
|
|
$T = explode( ':', trim( $CL ) );
|
|
$D = explode( '.', $T[0] );
|
|
$Out[$D[0]][$D[1]] = $T[1];
|
|
};
|
|
return $Out;
|
|
};
|
|
|
|
function nmcli_create_connection( $name, $type ){
|
|
$O = run_root_command( "nmcli -c no -s -t add con-name \"{$name}\" type {$type} ifname \"*\"" );
|
|
};
|
|
|
|
//sudo nmcli c mod mycon connection.autoconnect yes
|
|
|
|
function create_cron_task( $TID, $Params = '' ){
|
|
if( file_exists( '/srv/etc/cron.json' ) ) return false;
|
|
@unlink( '/srv/tmp/task_log.var' );
|
|
$Data['operation'] = intval( $TID );
|
|
if( $Params!= '' ){
|
|
$Data['parameter'] = $Params;
|
|
};
|
|
$FD = fopen( '/srv/etc/cron_.json', "w+" );
|
|
if( $FD ){
|
|
fprintf( $FD, json_encode( $Data ) );
|
|
fclose( $FD );
|
|
rename( '/srv/etc/cron_.json', '/srv/etc/cron.json' );
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
function parse_samba_config( $file='' ){
|
|
if( $file == '' ) $file = '/etc/samba/smb.conf';
|
|
$Data = file_get_contents( $file );
|
|
$TArr = explode( "\n", $Data );
|
|
$mumu_array = array();
|
|
$cSection = 'no_section';
|
|
foreach( $TArr as $T ){
|
|
$CL = trim( $T );
|
|
if( strlen( $CL ) ){
|
|
$CArr = explode( '#', $CL );
|
|
if( count( $CArr ) > 1 ) $CL = $CArr[0];
|
|
$CArr = explode( ';', $CL );
|
|
if( count( $CArr ) > 1 ) $CL = $CArr[0];
|
|
if( strlen( $CL ) ){
|
|
if( ( stripos( $CL, '[' ) !== false ) && ( stripos( $CL, '[' ) !== false ) ) {
|
|
$cSection = str_replace(']','',str_replace('[','',$CL));
|
|
$mumu_array[$cSection] = array();
|
|
} else {
|
|
$TArr = explode( '=', $CL );
|
|
if( count( $TArr ) == 2 ){
|
|
$Key = strtolower( trim( $TArr[0] ));
|
|
$Val = trim( $TArr[1] );
|
|
$mumu_array[$cSection][$Key] = $Val;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
};
|
|
return $mumu_array;
|
|
};
|
|
|
|
function validate_samba_config( $file='', $dist='' ){
|
|
$rv = false;
|
|
if( $file == '' ) $file = '/etc/samba/smb.conf';
|
|
if( $dist == '' ) $dist = '/srv/etc/samba.json';
|
|
$Actual = parse_samba_config( $file );
|
|
//print_r( $Actual );
|
|
$Params = json_decode( file_get_contents( $dist ), true );
|
|
//print_r( $Params );
|
|
foreach( $Actual as $CS=>$MEMS ){
|
|
if( isset( $Params[$CS] ) )
|
|
foreach( $MEMS as $CK=>$CV )
|
|
if( isset( $Params[$CS][$CK] ))
|
|
if($Params[$CS][$CK]!= $CV ) {
|
|
echo "{$CS}:{$CK} does not match {$Params[$CS][$CK]}!={$CV}\n";
|
|
return false;
|
|
};
|
|
};
|
|
return true;
|
|
};
|
|
|
|
|
|
function restart_samba(){
|
|
$CMD = 'service samba restart';
|
|
$R = `{$CMD}`;
|
|
if( stripos( $R, 'not found' ) !== false ){
|
|
$CMD = '/etc/init.d/samba-ad-dc restart';
|
|
$R = `{$CMD}`;
|
|
if( stripos( $R, 'not found' ) !== false ){
|
|
$CMD = '/etc/init.d/nmbd restart';
|
|
$R = `{$CMD}`;
|
|
$CMD = '/etc/init.d/smbd restart';
|
|
$R = `{$CMD}`;
|
|
};
|
|
};
|
|
};
|
|
|
|
|
|
function get_nif_config( ){
|
|
$CMD ='ip -j a';
|
|
$Data = json_decode(`{$CMD}`, true );
|
|
return $Data;
|
|
};
|
|
|
|
function set_nginx_certificate( $cert_file ){
|
|
|
|
};
|
|
function sync_time(){
|
|
|
|
};
|
|
|
|
|
|
function get_network_config(){
|
|
$Data = file_get_contents( '/etc/network/interfaces' );
|
|
$TArr = explode("\n", $Data );
|
|
$IFS = array();
|
|
$CI = '';
|
|
foreach( $TArr as $T ){
|
|
$CL = trim( $T );
|
|
$JJ = explode( '#', $CL );
|
|
if( is_array( $JJ ) ) if( strlen( $JJ[0] ) == 0 ) continue;
|
|
if( stripos( $CL, 'iface') !== false ){
|
|
$JJ = explode(' ', $CL );
|
|
$CI = $JJ[1];
|
|
if( !isset( $IFS[$JJ[1]] ) ) $IFS[$JJ[1]] = array();
|
|
if( !isset( $IFS[$JJ[1]]['flags'] ) ){
|
|
$IFS[$JJ[1]]['flags'] = array();
|
|
$IFS[$JJ[1]]['flags']['hotplug'] = 0;
|
|
$IFS[$JJ[1]]['flags']['auto'] = 0;
|
|
};
|
|
$IFS[$JJ[1]]['net'] = array();
|
|
$IFS[$JJ[1]]['fam'] = $JJ[2];
|
|
$IFS[$JJ[1]]['mode'] = $JJ[3];
|
|
} else if( stripos( $CL, 'auto') !== false ){
|
|
$JJ = explode( ' ', $CL );
|
|
if( count( $JJ ) > 2 ){
|
|
for( $i = 1; $i < count( $JJ ); $i++){
|
|
if( !isset( $IFS[$JJ[$i]] ) ) $IFS[$JJ[1]] = array();
|
|
if( !isset( $IFS[$JJ[$i]]['flags'] ) ){
|
|
$IFS[$JJ[$i]]['flags'] = array();
|
|
$IFS[$JJ[$i]]['flags']['hotplug'] = 0;
|
|
$IFS[$JJ[$i]]['flags']['auto'] = 0;
|
|
};
|
|
$IFS[$JJ[$i]]['flags']['auto'] = 1;
|
|
};
|
|
} else {
|
|
if( !isset( $IFS[$JJ[1]] ) ) $IFS[$JJ[1]] = array();
|
|
if( !isset( $IFS[$JJ[1]]['flags'] ) ){
|
|
$IFS[$JJ[1]]['flags'] = array();
|
|
$IFS[$JJ[1]]['flags']['hotplug'] = 0;
|
|
$IFS[$JJ[1]]['flags']['auto'] = 0;
|
|
};
|
|
$IFS[$JJ[1]]['flags']['auto'] = 1;
|
|
};
|
|
} else if( stripos( $CL, 'allow-hotplug' ) !== false ){
|
|
$JJ = explode( ' ', $CL );
|
|
if( count( $JJ ) > 2 ){
|
|
for( $i = 1; $i < count( $JJ ); $i++){
|
|
if( !isset( $IFS[$JJ[1]] ) ) $IFS[$JJ[1]] = array();
|
|
if( !isset( $IFS[$JJ[1]]['flags'] ) ){
|
|
$IFS[$JJ[$i]]['flags'] = array();
|
|
$IFS[$JJ[$i]]['flags']['hotplug'] = 0;
|
|
$IFS[$JJ[$i]]['flags']['auto'] = 0;
|
|
};
|
|
$IFS[$JJ[$i]]['flags']['hotplug'] = 1;
|
|
};
|
|
} else {
|
|
if( !isset( $IFS[$JJ[1]] ) ) $IFS[$JJ[1]] = array();
|
|
if( !isset( $IFS[$JJ[1]]['flags'] ) ){
|
|
$IFS[$JJ[1]]['flags'] = array();
|
|
$IFS[$JJ[1]]['flags']['hotplug'] = 0;
|
|
$IFS[$JJ[1]]['flags']['auto'] = 0;
|
|
};
|
|
$IFS[$JJ[1]]['flags']['hotplug'] = 1;
|
|
};
|
|
} else if( $CI!= '' ) {
|
|
$JJ = explode(' ', $CL );
|
|
$IFS[$CI]['net'][$JJ[0]] = $JJ[1];
|
|
};
|
|
};
|
|
return $IFS;
|
|
};
|
|
|
|
function get_NetGateway( $ADDR, $NM ){
|
|
$TArr = explode( '.', $ADDR );
|
|
$ba = 0xFFFFFFFF;
|
|
foreach( $TArr as $T ) {
|
|
$ba = ( $ba << 8 );
|
|
$ba |= intval( $T );
|
|
};
|
|
$ba &= 0xFFFFFFFF;
|
|
$TArr = explode( '.', $NM );
|
|
$bn = 0;
|
|
foreach( $TArr as $T ) {
|
|
$bn = ( $bn << 8 );
|
|
$bn += intval( $T );
|
|
};
|
|
$bc = ( $ba & $bn ) + 1;
|
|
$byteArray = unpack("C*", pack("L",$bc ) );
|
|
return sprintf( "%d.%d.%d.%d", $byteArray[4],$byteArray[3],$byteArray[2],$byteArray[1]);
|
|
};
|
|
function get_Network( $ADDR, $NM ){
|
|
$TArr = explode( '.', $ADDR );
|
|
$ba = 0xFFFFFFFF;
|
|
foreach( $TArr as $T ) {
|
|
$ba = ( $ba << 8 );
|
|
$ba |= intval( $T );
|
|
};
|
|
$ba &= 0xFFFFFFFF;
|
|
$TArr = explode( '.', $NM );
|
|
$bn = 0;
|
|
foreach( $TArr as $T ) {
|
|
$bn = ( $bn << 8 );
|
|
$bn += intval( $T );
|
|
};
|
|
$bc = ( $ba & $bn );
|
|
$byteArray = unpack("C*", pack("L",$bc ) );
|
|
return sprintf( "%d.%d.%d.%d", $byteArray[4],$byteArray[3],$byteArray[2],$byteArray[1]);
|
|
};
|
|
|
|
function get_NetBroadcast( $ADDR, $NM ){
|
|
$TArr = explode( '.', $ADDR );
|
|
$ba = 0xFFFFFFFF;
|
|
foreach( $TArr as $T ) {
|
|
$ba = ( $ba << 8 );
|
|
$ba |= intval( $T );
|
|
};
|
|
$ba &= 0xFFFFFFFF;
|
|
$TArr = explode( '.', $NM );
|
|
$bn = 0;
|
|
foreach( $TArr as $T ) {
|
|
$bn = ( $bn << 8 );
|
|
$bn += intval( $T );
|
|
};
|
|
$bc = ( $ba & $bn ) | ( ~ $bn );
|
|
$byteArray = unpack("C*", pack("L",$bc ) );
|
|
return sprintf( "%d.%d.%d.%d", $byteArray[4],$byteArray[3],$byteArray[2],$byteArray[1]);
|
|
};
|
|
|
|
function build_network_config( $NCFG, $FNAM = '' ){
|
|
if( !is_array( $NCFG ) ) return;
|
|
if( $FNAM == '' )$FNAM = '/etc/network/interfaces';
|
|
$FD = fopen( $FNAM, "w+" );
|
|
if( $FD ){
|
|
foreach( $NCFG as $Key=>$NIF ){
|
|
if( $NIF['flags']['auto'] ) fprintf( $FD, "auto %s\n", $Key );
|
|
if( $NIF['flags']['hotplug'] ) fprintf( $FD, "allow-hotplug %s\n", $Key );
|
|
fprintf( $FD, "iface %s %s %s\n", $Key, $NIF['fam'], $NIF['mode'] );
|
|
if( $NIF['mode'] == 'static' && count( $NIF['net'] ) ){
|
|
foreach( $NIF['net'] as $L=>$V ) fprintf( $FD, "\t%s %s\n", $L, $V );
|
|
/*
|
|
address 192.168.2.236
|
|
netmask 255.255.255.0
|
|
gateway 192.168.2.254
|
|
dns-domain sweet.home
|
|
dns-nameservers 192.168.2.254
|
|
*/
|
|
};
|
|
};
|
|
fclose( $FD );
|
|
};
|
|
};
|
|
|
|
function get_dhcpd_config(){
|
|
$Data = file_get_contents( '/etc/dnsmasq.conf' );
|
|
$TArr = explode("\n", $Data );
|
|
$IFS = array();
|
|
$IFS['dns'] = array();
|
|
$IFS['nifs'] = array();
|
|
$CANIF = '';
|
|
foreach( $TArr as $T ){
|
|
$CL = trim( $T );
|
|
$JJ = explode( '#', $CL );
|
|
if( is_array( $JJ ) ) if( strlen( $JJ[0] ) == 0 ) continue;
|
|
//echo $CL . "\n";
|
|
if( stripos( $CL, 'interface') !== false ){
|
|
$JJ = explode('=', $CL );
|
|
$CANIF = trim( $JJ[1] );
|
|
$IFS['nifs'][$CANIF] = array();
|
|
$IFS['nifs'][$CANIF]['range'] = array();
|
|
$IFS['nifs'][$CANIF]['range']['start'] = '';
|
|
$IFS['nifs'][$CANIF]['range']['end'] = '';
|
|
$IFS['nifs'][$CANIF]['range']['netmask'] = '';
|
|
$IFS['nifs'][$CANIF]['range']['lease'] = '';
|
|
};
|
|
if( stripos( $CL, 'server') !== false ){
|
|
$JJ = explode('=', $CL );
|
|
$IFS['dns'][] = $JJ[1];
|
|
};
|
|
if( stripos( $CL, 'dhcp-range') !== false ){
|
|
$JJ = explode( '=', $CL );
|
|
$BB = explode( ',', $JJ[1] );
|
|
$IFS['nifs'][$CANIF]['range']['start'] = $BB[0];
|
|
$IFS['nifs'][$CANIF]['range']['end'] = $BB[1];
|
|
if( count( $BB ) == 4 ){
|
|
$IFS['nifs'][$CANIF]['range']['netmask'] = $BB[2];
|
|
$IFS['nifs'][$CANIF]['range']['lease'] = $BB[3];
|
|
} else {
|
|
$IFS['nifs'][$CANIF]['range']['lease'] = $BB[2];
|
|
};
|
|
};
|
|
};
|
|
return $IFS;
|
|
};
|
|
|
|
function list_tz(){
|
|
$Data = `timedatectl list-timezones`;
|
|
return explode( "\n", $Data );
|
|
};
|
|
|
|
function get_tz_nfo(){
|
|
$Data = `timedatectl show`;
|
|
$TArr = explode( "\n", $Data );
|
|
$RData = array();
|
|
foreach( $TArr as $T ){
|
|
$CL = trim( $T );
|
|
if( stripos( $CL, 'NTPSynchronized' ) !== false ){
|
|
$BArr = explode( "=", $CL );
|
|
$RData['SYNC'] = $BArr[1];
|
|
} else {
|
|
if( stripos( $CL, 'CanNTP' ) !== false ){
|
|
$BArr = explode( "=", $CL );
|
|
$RData['NTA'] = $BArr[1];
|
|
} else {
|
|
if( stripos( $CL, 'Timezone' ) !== false ){
|
|
$BArr = explode( "=", $CL );
|
|
$RData['TZ'] = $BArr[1];
|
|
};
|
|
if( stripos( $CL, 'LocalRTC' ) !== false ){
|
|
$BArr = explode( "=", $CL );
|
|
$RData['RTC'] = $BArr[1];
|
|
};
|
|
if( stripos( $CL, 'TimeUSec' ) !== false ){
|
|
$BArr = explode( "=", $CL );
|
|
$RData['TIME'] = $BArr[1];
|
|
};
|
|
if( stripos( $CL, 'NTP' ) !== false ){
|
|
$BArr = explode( "=", $CL );
|
|
$RData['NTP'] = $BArr[1];
|
|
};
|
|
};
|
|
};
|
|
};
|
|
return $RData;
|
|
};
|
|
function build_dhcp_config( $CFG, $FNAM='' ){
|
|
if( !is_array( $CFG ) ) return;
|
|
if( $FNAM == '' )$FNAM = '/etc/dnsmasq.conf';
|
|
$FD = fopen( $FNAM, "w+" );
|
|
if( $FD ){
|
|
foreach( $CFG['nifs'] as $Key=>$NIF ){
|
|
fprintf( $FD, "interface=%s\n", $Key );
|
|
if( $NIF['netmask']!=''){
|
|
fprintf( $FD, "dhcp-range=%s,%s,%s,%s\n", $NIF['range']['start'],$NIF['range']['end'],$NIF['range']['netmask'],$NIF['range']['lease'] );
|
|
} else {
|
|
fprintf( $FD, "dhcp-range=%s,%s,%s\n", $NIF['range']['start'],$NIF['range']['end'],$NIF['range']['netmask'],$NIF['range']['lease'] );
|
|
};
|
|
};
|
|
foreach( $CFG['dns'] as $DR ){
|
|
fprintf( $FD, "server=%s\n", $DR );
|
|
};
|
|
fclose( $FD );
|
|
};
|
|
};
|
|
|
|
function get_wifi_ap_configuration(){
|
|
$Data = file_get_contents( '/etc/hostapd/hostapd.conf' );
|
|
$TArr = explode("\n", $Data );
|
|
$IFS = array();
|
|
foreach( $TArr as $T ){
|
|
$CL = trim( $T );
|
|
$JJ = explode( '#', $CL );
|
|
if( is_array( $JJ ) ) if( strlen( $JJ[0] ) == 0 ) continue;
|
|
$JJ = explode('=', $CL );
|
|
$IFS[$JJ[0]]=trim( $JJ[1] );
|
|
};
|
|
return $IFS;
|
|
};
|
|
|
|
function build_wifi_ap_configuration( $CFG, $FNAM='' ){
|
|
if( !is_array( $CFG ) ) return;
|
|
if( $FNAM == '' )$FNAM = '/etc/hostapd/hostapd.conf';
|
|
$FD = fopen( $FNAM, "w+" );
|
|
if( $FD ){
|
|
foreach( $CFG as $Key=>$Val ) fprintf( $FD, "%s=%s\n", $Key, $Val );
|
|
fclose( $FD );
|
|
};
|
|
};
|
|
|
|
function scan_WIFI_networks( $Dev = '' ){
|
|
if( $Dev == '' ) $Dev = 'wlan0';
|
|
$JAJA = `iw dev {$Dev} scan | grep SSID`;
|
|
|
|
};
|
|
|
|
function apply_NGINX_AUTH( $ROOTDIR, $AFILE ){
|
|
$Data = file_get_contents( '/etc/nginx/sites-available/default' );
|
|
$TArr = explode( "\n", $Data );
|
|
$ACTIVE = false;
|
|
$FD = fopen( '/etc/nginx/sites-available/default.tmp', "w+");
|
|
if( $FD ){
|
|
foreach( $TArr as $T ){
|
|
$JU = explode( '#', $T );
|
|
$CL = trim( $JU[0] );
|
|
if( $CL == '' ) continue;
|
|
if( $ACTIVE ){
|
|
if( stripos( $CL, 'auth_basic' ) === false ) fprintf( $FD, "%s\n", $CL );
|
|
} else fprintf( $FD, "%s\n", $CL );
|
|
|
|
if( stripos( $CL, 'root' ) !== false ) {
|
|
$BArr = explode( " ", $CL );
|
|
$CArr = explode( ';', $BArr[1] );
|
|
$ACTIVE = ( $CArr[0] == $ROOTDIR );
|
|
if( $ACTIVE ){
|
|
fprintf( $FD, "auth_basic \"Platform's Configuration\";\n" );
|
|
fprintf( $FD, "auth_basic_user_file %s;\n", $AFILE );
|
|
};
|
|
};
|
|
};
|
|
fclose( $FD );
|
|
unlink( '/etc/nginx/sites-available/default' );
|
|
rename( '/etc/nginx/sites-available/default.tmp', '/etc/nginx/sites-available/default' );
|
|
};
|
|
};
|
|
|
|
function get_SERVICE_STATUS( $ServName ){
|
|
$Data = `systemctl status {$ServName}`;
|
|
$TArr = explode( "\n", $Data );
|
|
$RD = array();
|
|
foreach( $TArr as $T ){
|
|
$CL = trim( $T );
|
|
if( stripos( $CL, 'loaded' ) !== false ){
|
|
$BArr = explode( ';', $CL );
|
|
if( count( $BArr ) > 1 ){
|
|
if( stripos( $BArr[1], 'enabled' )!==false ) {
|
|
$RD['ENA'] = true;
|
|
} else {
|
|
$RD['ENA'] = false;
|
|
};
|
|
};
|
|
};
|
|
if( stripos( $CL, 'active' ) !== false ){
|
|
if( stripos( $CL, 'inactive' )!==false ) {
|
|
$RD['WORKS'] = false;
|
|
} else {
|
|
$RD['WORKS'] = true;
|
|
};
|
|
};
|
|
};
|
|
return $RD;
|
|
};
|
|
|
|
function remove_NGINX_AUTH( $ROOTDIR ){
|
|
$Data = file_get_contents( '/etc/nginx/sites-available/default' );
|
|
$TArr = explode( "\n", $Data );
|
|
$ACTIVE = false;
|
|
$FD = fopen( '/etc/nginx/sites-available/default.tmp', "w+");
|
|
if( $FD ){
|
|
foreach( $TArr as $T ){
|
|
$JU = explode( '#', $T );
|
|
$CL = trim( $JU[0] );
|
|
if( $CL == '' ) continue;
|
|
if( $ACTIVE ){
|
|
if( stripos( $CL, 'auth_basic' ) === false ) fprintf( $FD, "%s\n", $CL );
|
|
} else fprintf( $FD, "%s\n", $CL );
|
|
if( stripos( $CL, 'root' ) !== false ) {
|
|
$BArr = explode( " ", $CL );
|
|
$CArr = explode( ';', $BArr[1] );
|
|
$ACTIVE = ( $CArr[0] == $ROOTDIR );
|
|
};
|
|
};
|
|
fclose( $FD );
|
|
unlink( '/etc/nginx/sites-available/default' );
|
|
rename( '/etc/nginx/sites-available/default.tmp', '/etc/nginx/sites-available/default' );
|
|
};
|
|
};
|
|
|
|
function set_NGINX_APP_ENABLE(){
|
|
global $SYS_CONFIG;
|
|
$Data = file_get_contents( '/etc/nginx/sites-available/default' );
|
|
$TArr = explode( "\n", $Data );
|
|
$BLOCKED = false;
|
|
$FD = fopen( '/etc/nginx/sites-available/default.tmp', "w+");
|
|
if( $FD ){
|
|
foreach( $TArr as $T ){
|
|
$JU = explode( '#', $T );
|
|
$CL = trim( $JU[0] );
|
|
if( $CL == '' ) continue;
|
|
if( stripos( $CL, 'server' ) === false ) {
|
|
if( !$BLOCKED ) fprintf( $FD, "%s\n", $CL );
|
|
};
|
|
if( stripos( $CL, 'root' ) !== false ) {
|
|
$BArr = explode( " ", $CL );
|
|
$CArr = explode( ';', $BArr[1] );
|
|
$BLOCKED = ( $CArr[0] == '/srv/www/distro' ) && ( stripos( $CL, '8080' ) === false );
|
|
if( !$BLOCKED ){
|
|
fprintf( $FD, "server {\n %s\nserver_name _;\n", $CL );
|
|
};
|
|
};
|
|
};
|
|
//Output App Web
|
|
fprintf( $FD, "server {\nroot /srv/www/web; listen 80 default_server; listen [::]:80 default_server;\n" );
|
|
if( $SYS_CONFIG['NGINX_CERTIFICATE_INSTALLED'] ){
|
|
if( $SYS_CONFIG['NGINX_CERTIFICATE_ENABLED'] ){
|
|
fprintf( $FD, "listen 443 ssl default_server;listen [::]:443 ssl default_server;\n" );
|
|
fprintf( $FD, "include /srv/etc/nginx/self-signed.conf;\n" );
|
|
fprintf( $FD, "include /srv/etc/nginx/ssl-params.conf;\n" );
|
|
};
|
|
};
|
|
fprintf( $FD, "index index.html index.htm index.nginx-debian.html index.php;\n" );
|
|
fprintf( $FD, "server_name _;\n" );
|
|
fprintf( $FD, 'location / { try_files $uri $uri/ =404; index index.html index.php; }'."\n" );
|
|
fprintf( $FD, 'location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php-fpm.sock;}'."\n" );
|
|
fprintf( $FD, 'location ~* /jsapi_dynamic/.+\.js$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php-fpm.sock; }'."\n" );
|
|
fprintf( $FD, 'location /video_feed/ { proxy_pass http://127.0.0.1:8972; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }'."\n" );
|
|
fprintf( $FD, "location ~ /\.ht {deny all;}\n}\n" );
|
|
fclose( $FD );
|
|
unlink( '/etc/nginx/sites-available/default' );
|
|
rename( '/etc/nginx/sites-available/default.tmp', '/etc/nginx/sites-available/default' );
|
|
};
|
|
};
|
|
|
|
function set_NGINX_APP_DISABLE(){
|
|
global $SYS_CONFIG;
|
|
$Data = file_get_contents( '/etc/nginx/sites-available/default' );
|
|
$TArr = explode( "\n", $Data );
|
|
$BLOCKED = false;
|
|
$FD = fopen( '/etc/nginx/sites-available/default.tmp', "w+");
|
|
if( $FD ){
|
|
foreach( $TArr as $T ){
|
|
$JU = explode( '#', $T );
|
|
$CL = trim( $JU[0] );
|
|
if( $CL == '' ) continue;
|
|
if( stripos( $CL, 'server' ) === false ) {
|
|
if( !$BLOCKED ) fprintf( $FD, "%s\n", $CL );
|
|
};
|
|
if( stripos( $CL, 'root' ) !== false ) {
|
|
$BArr = explode( " ", $CL );
|
|
$CArr = explode( ';', $BArr[1] );
|
|
$BLOCKED = ( $CArr[0] == '/srv/www/web' );
|
|
if( !$BLOCKED ){
|
|
fprintf( $FD, "server {\n %s\nserver_name _;\n", $CL );
|
|
};
|
|
};
|
|
};
|
|
//Output App Web
|
|
fprintf( $FD, "server {\nroot /srv/www/distro; listen 80 default_server; listen [::]:80 default_server;\n" );
|
|
if( $SYS_CONFIG['NGINX_CERTIFICATE_INSTALLED'] ){
|
|
|
|
};
|
|
fprintf( $FD, "index index.html index.htm index.nginx-debian.html index.php;\n" );
|
|
fprintf( $FD, "server_name _;\n" );
|
|
fprintf( $FD, 'location / { try_files $uri $uri/ =404; index index.html index.php; }'."\n" );
|
|
fprintf( $FD, 'location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php-fpm.sock;}'."\n" );
|
|
fprintf( $FD, "location ~ /\.ht {deny all;}\n}\n" );
|
|
fclose( $FD );
|
|
unlink( '/etc/nginx/sites-available/default' );
|
|
rename( '/etc/nginx/sites-available/default.tmp', '/etc/nginx/sites-available/default' );
|
|
};
|
|
};
|
|
|
|
function process_is_running( $PName = '', $NArgs = '' ){
|
|
$GS = '';
|
|
if( is_array( $PName ) ){
|
|
foreach( $PName as $P ) $GS .= "| grep \"{$P}\" ";
|
|
if( is_array( $NArgs ) ){
|
|
foreach( $NArgs as $N ) $GS .= "| grep -v \"{$N}\" ";
|
|
} else {
|
|
if( $NArgs!= '' ){
|
|
$GS .= "| grep -v \"{$NArgs}\" ";
|
|
};
|
|
};
|
|
} else {
|
|
if( $PName == '' ) return false;
|
|
$GS = "| grep \"{$PName}\" ";
|
|
if( is_array( $NArgs ) ){
|
|
foreach( $NArgs as $N ) $GS .= "| grep -v \"{$N}\"";
|
|
} else {
|
|
if( $NArgs != '' ){
|
|
$GS .= "| grep -v \"{$NArgs}\" ";
|
|
};
|
|
};
|
|
};
|
|
$JOJO = `ps -A -o args {$GS} 2>&1 `;
|
|
if( $JOJO == '' ) return false;
|
|
$TArr = explode( "\n", $JOJO );
|
|
foreach( $TArr as $T ){
|
|
$CC = trim( $T );
|
|
if( $CC == '' ) continue;
|
|
if( ( stripos( $T, 'ps' ) === false ) && ( stripos( $T, 'grep' ) === false ) && ( stripos( $T, 'sshd' ) === false ) ) return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
function stop_BACKEND_APPLICATION(){
|
|
if( process_is_running('starProcDaemon') ){
|
|
$L = `killall starProcDaemon`;
|
|
sleep( 1 );
|
|
};
|
|
if( process_is_running('mojo_d') ){
|
|
$L = `killall mojo_d`;
|
|
sleep( 4 );
|
|
};
|
|
if( process_is_running('mojo') ){
|
|
$L = `killall mojo`;
|
|
sleep( 4 );
|
|
};
|
|
/// Shutdown Argus Web UI
|
|
if( process_is_running( array( 'nginx', 'master process' ), 'ag_nginx' ) ){
|
|
$L = `systemctl stop nginx.service`;
|
|
sleep( 1 );
|
|
};
|
|
// Fix unclosed Databases
|
|
if( file_exists( '/srv/www/web/lib/controller/data/mysqlitedb.db' ) ){
|
|
$DATABASE = new SQLite_DB( '/srv/www/web/lib/controller/data/mysqlitedb.db');
|
|
unset( $DATABASE );
|
|
};
|
|
|
|
if( file_exists( '/srv/www/web/lib/controller/data/reports.db' ) ){
|
|
$DATABASE = new SQLite_DB( '/srv/www/web/lib/controller/data/reports.db');
|
|
unset( $DATABASE );
|
|
};
|
|
};
|
|
|
|
function start_BACKEND_APPLICATION(){
|
|
$CWD = getcwd();
|
|
chdir( '/srv/start' );
|
|
$L = `./starProcDaemon`;
|
|
sleep( 1 );
|
|
chdir( $CWD );
|
|
if( !process_is_running( array( 'nginx', 'master process' ), 'ag_nginx' ) ){
|
|
$L = `systemctl start nginx.service`;
|
|
sleep( 1 );
|
|
};
|
|
};
|
|
|
|
function verify_CERT( $ROOT, $CHEK ){
|
|
$DADA = trim( `/usr/bin/openssl verify -verbose -CAfile {$ROOT} {$CHEK}` );
|
|
$TArr = explode( ':', $DADA );
|
|
return ( stripos( $TArr[1],'ok' ) !== false );
|
|
};
|
|
|
|
function verify_CERT_KEY( $CERT, $KEY ){
|
|
$L = `/usr/bin/openssl rsa -in {$KEY} -check -noout`;
|
|
if( stripos( $L, 'RSA key ok' ) !== false ){
|
|
$A = `openssl x509 -noout -modulus -in {$CERT}`;
|
|
$B = `openssl rsa -noout -modulus -in {$KEY}`;
|
|
if( $A == $B ) return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
function get_embed_data_CERT( $CERT ){
|
|
$DADA = `openssl x509 -text -noout -in {$CERT} -certopt no_header,no_version,no_serial,no_signame,no_validity,no_issuer,no_pubkey,no_sigdump,no_aux`;
|
|
$TArr = explode( "\n", $DADA );
|
|
$RD = array();
|
|
$RD['subject'] = array();
|
|
$RD['ip'] = array();
|
|
foreach( $TArr as $T ){
|
|
$CL = trim( $T );
|
|
if( stripos( $CL, 'Subject:')!== false ){
|
|
$BArr = explode( ':', $CL );
|
|
$RR = explode( ',', $BArr[1] );
|
|
foreach( $RR as $R ){
|
|
$ZZ = explode( '=', $R );
|
|
$RD['subject'][trim( $ZZ[0])]=trim($ZZ[1]);
|
|
};
|
|
};
|
|
if( stripos( $CL, 'IP Address' )!== false ){
|
|
$BArr = explode( ',', $CL );
|
|
foreach( $BArr as $B ){
|
|
$ZZ = explode( ':', $B );
|
|
$RD['ip'][] = trim( $ZZ[1] );
|
|
};
|
|
};
|
|
};
|
|
return $RD;
|
|
};
|
|
|
|
function list_files( $Path, $Filter = '' ){
|
|
$RetVal = array();
|
|
if( is_dir( $Path ) ){
|
|
$DD = opendir( $Path );
|
|
if( $DD !== false ){
|
|
while(( $file = readdir($DD)) !== false ){
|
|
if( $Filter == '' && $file!= '.' && $file!= '..' ){
|
|
$RetVal[] = $file;
|
|
} else {
|
|
if( preg_match( $Filter, $file ) ) $RetVal[] = $file;
|
|
};
|
|
};
|
|
closedir($DD);
|
|
};
|
|
};
|
|
return $RetVal;
|
|
};
|
|
|
|
function list_AP_NEARBY( $DEV ){
|
|
$RV = array();
|
|
$L = `sudo ip link set {$DEV} up`;
|
|
$DATA = `sudo iw dev {$DEV} scan | grep SSID`;
|
|
$L = `sudo ip link set {$DEV} down`;
|
|
if( $DATA!= '' ){
|
|
$TArr = explode( "\n", $DATA );
|
|
foreach( $TArr as $T ){
|
|
$CL = trim( $T );
|
|
if( $CL!='' ){
|
|
$TBarr = explode( ':', $CL );
|
|
if( isset( $TBarr[1] ) ){
|
|
$RV[] = $TBarr[1];
|
|
};
|
|
};
|
|
};
|
|
};
|
|
return $RV;
|
|
};
|
|
|
|
function get_argus_config(){
|
|
$RV = array();
|
|
$RV['app_is_running'] = process_is_running( '/srv/mojo/mojo' );
|
|
if( !$RV['app_is_running'] ) $RV['app_is_running'] = process_is_running( '/srv/mojo/mojo_d' );
|
|
$RV['webui_is_running'] = process_is_running( array ( 'nginx', 'master' ), 'ag_nginx' );
|
|
return $RV;
|
|
};
|
|
|
|
function serialize_current_application_config(){
|
|
$DATA = array(
|
|
'server'=>array( 'id'=>1, 'identity'=>'3638eedfe4370b4b53285ae524b88343', 'site_id'=>1 ),
|
|
'nodes'=>array(),
|
|
'resources'=>array(),
|
|
'sensors'=>array(),
|
|
'cameras'=>array(),
|
|
'modules'=>array(),
|
|
'filters'=>array(),
|
|
'system_props'=>array(),
|
|
'users'=>array()
|
|
);
|
|
$fid = fopen( '/srv/mojo/server.conf', "r" );
|
|
if( $fid !== false ){
|
|
while( !feof( $fid ) ){
|
|
$CL = trim( fgets( $fid ) );
|
|
$CL = str_replace( ' ', '', $CL );
|
|
$CL = str_replace( '"', '', $CL );
|
|
$CL = str_replace( ';', '', $CL );
|
|
$TArr = explode('=', $CL );
|
|
if( stripos( $CL, 'IDENTITY' ) !== false ){
|
|
$DATA['server']['identity'] = $TArr[1];
|
|
};
|
|
if( stripos( $CL, 'SITE_ID' ) !== false ){
|
|
$DATA['server']['site_id'] = $TArr[1];
|
|
};
|
|
};
|
|
fclose( $fid );
|
|
};
|
|
$DB = new SQLite_DB('/srv/www/web/lib/controller/data/mysqlitedb.db');
|
|
$QS = "SELECT * FROM `nodes`";
|
|
$R = $DB->Query( $QS );
|
|
foreach( $R as $T ){
|
|
$CN = array();
|
|
foreach( $T as $key=>$val ) $CN[$key] = $val;
|
|
$DATA['nodes'][] = $CN;
|
|
};
|
|
$QS = "SELECT * FROM `resources`";
|
|
$R = $DB->Query( $QS );
|
|
foreach( $R as $T ){
|
|
$CN = array();
|
|
foreach( $T as $key=>$val ) $CN[$key] = $val;
|
|
switch( $CN['type'] ){
|
|
case 1:{
|
|
$QS = "SELECT * FROM `uart_settings` WHERE `id`={$CN['aux_id']}";
|
|
$J = $DB->Query( $QS );
|
|
$PP = array();
|
|
foreach( $J[0] as $l=>$v ) $PP[$l]=$v;
|
|
$CN['private'] = $PP;
|
|
};
|
|
};
|
|
$DATA['resources'][] = $CN;
|
|
};
|
|
$QS = "SELECT * FROM `cameras`";
|
|
$R = $DB->Query( $QS );
|
|
foreach( $R as $T ){
|
|
$CN = array();
|
|
foreach( $T as $key=>$val ) $CN[$key] = $val;
|
|
$QS = "SELECT * FROM `media_streams` WHERE `source_id`={$CN['id']}";
|
|
$CN['streams'] = $DB->Query( $QS );
|
|
$DATA['cameras'][] = $CN;
|
|
};
|
|
$QS = "SELECT * FROM `lrf_sensors`";
|
|
$R = $DB->Query( $QS );
|
|
foreach( $R as $T ){
|
|
$CN = array();
|
|
foreach( $T as $key=>$val ) $CN[$key] = $val;
|
|
$QS = "SELECT * FROM `lrf_detection_profiles` WHERE `aux_id`={$CN['id']}";
|
|
$CN['private'] = $DB->Query( $QS );
|
|
$DATA['sensors'][] = $CN;
|
|
};
|
|
$QS = "SELECT * FROM `modules`";
|
|
$R = $DB->Query( $QS );
|
|
foreach( $R as $T ){
|
|
$CN = array();
|
|
foreach( $T as $key=>$val ) $CN[$key] = $val;
|
|
switch( $CN['type'] ){
|
|
case 8:{
|
|
$PP = array();
|
|
$QS = "SELECT * FROM `module_eve` WHERE `id`={$CN['aux_id']}";
|
|
$K = $DB->Query( $QS );
|
|
$PP = $K[0];
|
|
$CN['private'] = $PP;
|
|
break;
|
|
};
|
|
};
|
|
$DATA['modules'][] = $CN;
|
|
};
|
|
$QS = "SELECT * FROM `target_filters`";
|
|
$R = $DB->Query( $QS );
|
|
foreach( $R as $T ){
|
|
$CN = array();
|
|
foreach( $T as $key=>$val ) $CN[$key] = $val;
|
|
switch( $CN['type'] ){
|
|
case 1:{
|
|
$PP = array();
|
|
$QS = "SELECT * FROM `target_filter_train` WHERE `id`={$CN['aux_id']}";
|
|
$K = $DB->Query( $QS );
|
|
$PP = $K[0];
|
|
$CN['private'] = $PP;
|
|
break;
|
|
};
|
|
};
|
|
$DATA['filters'][] = $CN;
|
|
};
|
|
$QS = "SELECT * FROM `system_props`";
|
|
$R = $DB->Query( $QS );
|
|
$DATA['system_props'] = $R[0];
|
|
$QS = "SELECT * FROM `users`";
|
|
$R = $DB->Query( $QS );
|
|
foreach( $R as $T ){
|
|
if( $T['id'] > 1 ){
|
|
$CN = array();
|
|
foreach( $T as $key=>$val ) $CN[$key] = $val;
|
|
$DATA['users'][] = $CN;
|
|
};
|
|
};
|
|
unset( $DB );
|
|
return json_encode( $DATA );
|
|
};
|
|
|
|
function apply_application_config( $A, $DBF ){
|
|
$DB = new SQLite_DB( $DBF );
|
|
$DATA = json_decode( $A, true );
|
|
foreach( $DATA as $Key=>$Val ){
|
|
switch( $Key ){
|
|
case 'users':{
|
|
foreach( $Val as $T ){
|
|
$flds = array();
|
|
$values = array();
|
|
$updates = array();
|
|
foreach( $T as $A=>$B ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
$QS = "INSERT INTO `users`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
};
|
|
break;
|
|
};
|
|
case 'server':{
|
|
foreach( $Val as $A=>$B ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
$QS = "INSERT INTO `servers`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
break;
|
|
};
|
|
case 'nodes':{
|
|
foreach( $Val as $T ){
|
|
$flds = array();
|
|
$values = array();
|
|
$updates = array();
|
|
foreach( $T as $A=>$B ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
$QS = "INSERT INTO `nodes`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
};
|
|
break;
|
|
};
|
|
case 'resources':{
|
|
foreach( $Val as $T ){
|
|
$flds = array();
|
|
$values = array();
|
|
$updates = array();
|
|
foreach( $T as $A=>$B ){
|
|
if( $A!='private' ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
};
|
|
$QS = "INSERT INTO `resources`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
switch( $T['type'] ){
|
|
case 1:{
|
|
$flds = array();
|
|
$values = array();
|
|
$updates = array();
|
|
foreach( $T['private'] as $A=>$B ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
$QS = "INSERT INTO `uart_settings`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
break;
|
|
};
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case 'sensors':{
|
|
foreach( $Val as $T ){
|
|
$flds = array();
|
|
$values = array();
|
|
$updates = array();
|
|
foreach( $T as $A=>$B ){
|
|
if( $A!='private' ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
};
|
|
$QS = "INSERT INTO `lrf_sensors`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
if( count( $T['private'] ) ){
|
|
foreach( $T['private'] as $P ){
|
|
foreach( $P as $A=>$B ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
$QS = "INSERT INTO `lrf_detection_profiles`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
};
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case 'cameras':{
|
|
foreach( $Val as $T ){
|
|
$flds = array();
|
|
$values = array();
|
|
$updates = array();
|
|
foreach( $T as $A=>$B ){
|
|
if( $A!='streams' ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
};
|
|
$QS = "INSERT INTO `cameras`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
foreach( $T['streams'] as $S ){
|
|
$flds = array();
|
|
$values = array();
|
|
$updates = array();
|
|
foreach( $S as $A=>$B ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
$QS = "INSERT INTO `media_streams`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case 'modules':{
|
|
foreach( $Val as $T ){
|
|
$flds = array();
|
|
$values = array();
|
|
$updates = array();
|
|
foreach( $T as $A=>$B ){
|
|
if( $A!='private' ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
};
|
|
$QS = "INSERT INTO `modules`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
switch( $T['type'] ){
|
|
case 8:{
|
|
$flds = array();
|
|
$values = array();
|
|
$updates = array();
|
|
foreach( $T['private'] as $A=>$B ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
$QS = "INSERT INTO `module_eve`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
break;
|
|
};
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case 'filters':{
|
|
foreach( $Val as $T ){
|
|
$flds = array();
|
|
$values = array();
|
|
$updates = array();
|
|
foreach( $T as $A=>$B ){
|
|
if( $A!='private' ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
};
|
|
$QS = "INSERT INTO `target_filters`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
switch( $T['type'] ){
|
|
case 1:{
|
|
$flds = array();
|
|
$values = array();
|
|
$updates = array();
|
|
foreach( $T['private'] as $A=>$B ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
$QS = "INSERT INTO `target_filter_train`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
break;
|
|
};
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
case 'system_props':{
|
|
$flds = array();
|
|
$values = array();
|
|
$updates = array();
|
|
foreach( $Val as $A=>$B ){
|
|
$flds[] = "`{$A}`";
|
|
$values[]="'{$B}'";
|
|
if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
|
|
};
|
|
$QS = "INSERT INTO `system_props`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
|
|
$DB->Query( $QS );
|
|
break;
|
|
};
|
|
};
|
|
};
|
|
unset( $DB );
|
|
};
|
|
|
|
function set_factory_settings(){
|
|
stop_BACKEND_APPLICATION();
|
|
$R = `rm -f -R /srv/www/web/lib/controller/data/uploads/*`;//*/
|
|
$R = `rm -f -R /srv/www/web/lib/controller/data/device_draws/*`;//*/
|
|
$R = `rm -f -R /srv/www/web/lib/controller/data/videos/*`;//*/
|
|
$SRC = '/srv/www/distro/lib/data/mysqlitedb.db';
|
|
$DST = '/srv/www/web/lib/controller/data/mysqlitedb.db';
|
|
$R = `rm -f $DST`;
|
|
$C = `cp $SRC $DST`;
|
|
$SRC = '/srv/www/distro/lib/data/reports.db';
|
|
$DST = '/srv/www/web/lib/controller/data/reports.db';
|
|
$R = `rm -f $DST`;
|
|
$C = `cp $SRC $DST`;
|
|
start_BACKEND_APPLICATION();
|
|
};
|
|
|
|
function clear_history(){
|
|
stop_BACKEND_APPLICATION();
|
|
$CMD = `rm -R /srv/www/web/lib/controller/data/reports`;
|
|
$CMD = `mkdir /srv/www/web/lib/controller/data/reports`;
|
|
$CMD = `chown www-data:www-data /srv/www/web/lib/controller/data/reports`;
|
|
$CMD = `chmod 775 /srv/www/web/lib/controller/data/reports`;
|
|
start_BACKEND_APPLICATION();
|
|
};
|
|
|
|
function reload_modem_data( $MID ){
|
|
global $SYS_CONFIG;
|
|
$MANU = json_decode( `mmcli -m $MID --output-json`, true );
|
|
$LOLO = json_decode( `mmcli -m $MID --location-status --output-json`, true );
|
|
if( isset( $LOLO['modem'] ) ){
|
|
$MANU['modem']['location'] = $LOLO['modem']['location'];
|
|
};
|
|
$CD = $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID];
|
|
|
|
$SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID] = $MANU['modem'];
|
|
if( isset( $CD['REGISTER_AT_BOOT'] )){
|
|
$SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['REGISTER_AT_BOOT'] = $CD['REGISTER_AT_BOOT'];
|
|
} else $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['REGISTER_AT_BOOT'] = false;
|
|
if( isset( $CD['CONNECT_AT_BOOT'] )){
|
|
$SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CONNECT_AT_BOOT'] = $CD['CONNECT_AT_BOOT'];
|
|
} else $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CONNECT_AT_BOOT'] = false;
|
|
if( isset( $CD['CUSTOM_APN'] ) ){
|
|
$SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CUSTOM_APN'] = $CD['CUSTOM_APN'];
|
|
} else $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CUSTOM_APN'] = '';
|
|
if( isset( $CD['CUSTOM_APN_USER'] ) ){
|
|
$SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CUSTOM_APN_USER'] = $CD['CUSTOM_APN_USER'];
|
|
} else $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CUSTOM_APN_USER'] = '';
|
|
if( isset( $CD['CUSTOM_APN_PASSWORD'] ) ){
|
|
$SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CUSTOM_APN_PASSWORD'] = $CD['CUSTOM_APN_PASSWORD'];
|
|
} else $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CUSTOM_APN_PASSWORD'] = '';
|
|
};
|
|
|
|
function get_NetworkManager_Profiles(){
|
|
global $WEB_USER_SUDO_PWD;
|
|
$CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli -c no -e no -t -s connection";
|
|
$LL = `$CMD`;
|
|
$TArr = explode( "\n", $LL );
|
|
$Data = array();
|
|
foreach( $TArr as $T ){
|
|
$ZZ = explode( ':',trim( $T ) );
|
|
if( count( $ZZ ) > 3 ){
|
|
$CC = array();
|
|
$CC['name']=$ZZ[0];
|
|
$CC['uuid']=$ZZ[1];
|
|
$CC['type']=$ZZ[2];
|
|
$CC['device'] = $ZZ[3];
|
|
$CC['managed'] = true;
|
|
if( strlen( $CC['device'] ) < 3 ) {
|
|
$CC['managed'] = false;
|
|
};
|
|
$Data[] = $CC;
|
|
};
|
|
};
|
|
return $Data;
|
|
};
|
|
|
|
function get_NetworkManager_Device( $DN ){
|
|
global $WEB_USER_SUDO_PWD;
|
|
$CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli -c no -e no -t -s device show {$DN}";
|
|
$LL = `$CMD`;
|
|
$TArr = explode( "\n", $LL );
|
|
$Data = array();
|
|
foreach( $TArr as $T ){
|
|
$ZZ = explode( ':',trim( $T ) );
|
|
if( strpos( $ZZ[0], '[' ) !== false ){
|
|
$TA = explode( '[', $ZZ[0] );
|
|
if( array_key_exists($TA[0],$Data) ){
|
|
$Data[$TA[0]][]=$ZZ[1];
|
|
} else {
|
|
$Data[$TA[0]]=array();
|
|
$Data[$TA[0]][0]=$ZZ[1];
|
|
};
|
|
} else {
|
|
$Data[$ZZ[0]] = $ZZ[1];
|
|
};
|
|
};
|
|
return $Data;
|
|
};
|
|
|
|
function get_NetworkManager_Connection( $CN ){
|
|
global $WEB_USER_SUDO_PWD;
|
|
$CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli -c no -e no -t -s connection show {$CN}";
|
|
$LL = `$CMD`;
|
|
$TArr = explode( "\n", $LL );
|
|
$Data = array();
|
|
foreach( $TArr as $T ){
|
|
$ZZ = explode( ':',trim( $T ) );
|
|
if( strpos( $ZZ[0], '[' ) !== false ){
|
|
$TA = explode( '[', $ZZ[0] );
|
|
if( array_key_exists($TA[0],$Data) ){
|
|
$Data[$TA[0]][]=$ZZ[1];
|
|
} else {
|
|
$Data[$TA[0]]=array();
|
|
$Data[$TA[0]][0]=$ZZ[1];
|
|
};
|
|
} else {
|
|
$Data[$ZZ[0]] = $ZZ[1];
|
|
};
|
|
};
|
|
return $Data;
|
|
};
|
|
|
|
function create_NetworkManager_Profile( $type, $name, $add_params='' ){
|
|
global $WEB_USER_SUDO_PWD;
|
|
$CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli connection add save yes +connection.type {$type} +connection.id \"{$name}\" ";
|
|
if( is_array( $add_params ) ){
|
|
if( count( $add_params ) ){
|
|
foreach( $add_params as $Key=>$Val ){
|
|
$CMD .= "+{$Key} \"{$Value}\" ";
|
|
};
|
|
};
|
|
};
|
|
$LL = `$CMD`;
|
|
if( strpos( $LL, 'successful' ) !== false ) return true;
|
|
return false;
|
|
};
|
|
|
|
function delete_NetworkManager_Profile( $name ){
|
|
global $WEB_USER_SUDO_PWD;
|
|
$CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli connection down \"{$name}\" ";
|
|
$LL = `$CMD`;
|
|
if( strpos( $LL, 'successful' ) === false ) return false;
|
|
$CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli connection delete \"{$name}\" ";
|
|
$LL = `$CMD`;
|
|
if( strpos( $LL, 'successful' ) !== false ) return true;
|
|
return false;
|
|
};
|
|
|
|
function connect_NetworkManager_Device( $dev ){
|
|
global $WEB_USER_SUDO_PWD;
|
|
$CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli device connect \"{$dev}\" ";
|
|
$LL = `$CMD`;
|
|
if( strpos( $LL, 'successful' ) !== false ) return true;
|
|
return false;
|
|
};
|
|
|
|
function disconnect_NetworkManager_Device( $dev ){
|
|
global $WEB_USER_SUDO_PWD;
|
|
$CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli device disconnect \"{$dev}\" ";
|
|
$LL = `$CMD`;
|
|
if( strpos( $LL, 'successful' ) !== false ) return true;
|
|
return false;
|
|
};
|
|
|
|
function activate_NetworkManager_Profile( $name ){
|
|
global $WEB_USER_SUDO_PWD;
|
|
$CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli connection up \"{$name}\" ";
|
|
$LL = `$CMD`;
|
|
if( strpos( $LL, 'successful' ) !== false ) return true;
|
|
return false;
|
|
};
|
|
|
|
function deactivate_NetworkManager_Profile( $name ){
|
|
global $WEB_USER_SUDO_PWD;
|
|
$CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli connection down \"{$name}\" ";
|
|
$LL = `$CMD`;
|
|
if( strpos( $LL, 'successful' ) !== false ) return true;
|
|
return false;
|
|
};
|
|
|
|
?>
|