Installer/Updater
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.

1329 lines
39 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <?php
  2. require_once( '/srv/scripts/SQLite_Class.php');
  3. $WEB_USER_SUDO_PWD='ujpQFE/<\'Deq+kK5=tL>8~';
  4. $WEB_USER_SUDO_PWD='terrarium';
  5. if( file_exists( '/srv/etc/system.json' ) ){
  6. $SYS_CONFIG = json_decode( file_get_contents( '/srv/etc/system.json' ), true );
  7. } else {
  8. $SYS_CONFIG = array();
  9. };
  10. function save_system_config(){
  11. global $SYS_CONFIG;
  12. $FD = fopen( '/srv/etc/system.json', "w+" );
  13. if( $FD ){
  14. fprintf( $FD, json_encode( $SYS_CONFIG ) );
  15. fclose( $FD );
  16. };
  17. };
  18. function reload_system_config(){
  19. global $SYS_CONFIG;
  20. if( file_exists( '/srv/etc/system.json' ) ){
  21. $SYS_CONFIG = json_decode( file_get_contents( '/srv/etc/system.json' ), true );
  22. } else {
  23. $SYS_CONFIG = array();
  24. };
  25. };
  26. function out_log( $str ){
  27. $FD = fopen( '/srv/tmp/task_log.var', "a+" );
  28. if( $FD ){
  29. fprintf( $FD, "%s\n", trim($str) );
  30. fclose( $FD );
  31. };
  32. };
  33. function run_command( $CMD ){
  34. $FD = @popen( $CMD, "r" );
  35. $Out = array();
  36. if( $FD ){
  37. while (($buffer = fgets( $FD ) ) !== false ){
  38. //out_log( $buffer );
  39. $Out[] = $buffer;
  40. };
  41. if( !feof( $FD ) ){
  42. out_log( "Error: unexpected fgets() fail" );
  43. }
  44. pclose($FD);
  45. };
  46. };
  47. function run_root_command( $CMD ){
  48. $sodo_user_pwd = 'terrarium';
  49. $EXEC = "echo \"{$sodo_user_pwd}\" | sudo -S {$CMD}";
  50. $FD = @popen( $EXEC, "r" );
  51. $Out = array();
  52. if( $FD ){
  53. while (($buffer = fgets( $FD ) ) !== false ){
  54. //out_log( $buffer );
  55. $Out[] = $buffer;
  56. };
  57. if( !feof( $FD ) ){
  58. out_log( "Error: unexpected fgets() fail" );
  59. }
  60. pclose($FD);
  61. };
  62. return $Out;
  63. };
  64. function nmcli_list_gsm_connections(){
  65. $O = run_root_command( "nmcli -c no -s -t connection show" );
  66. $Out = array();
  67. foreach( $O as $CL ){
  68. $T = explode( ':', trim( $CL ) );
  69. if( $T[2] == 'gsm' ) $Out[]=$T[0];
  70. };
  71. return $Out;
  72. };
  73. function nmcli_get_connection_info( $CN ){
  74. $O = run_root_command( "nmcli -c no -s -t connection show {$CN}" );
  75. $Out = array();
  76. foreach( $O as $CL ){
  77. $T = explode( ':', trim( $CL ) );
  78. $D = explode( '.', $T[0] );
  79. $Out[$D[0]][$D[1]] = $T[1];
  80. };
  81. return $Out;
  82. };
  83. function nmcli_create_connection( $name, $type ){
  84. $O = run_root_command( "nmcli -c no -s -t add con-name \"{$name}\" type {$type} ifname \"*\"" );
  85. };
  86. //sudo nmcli c mod mycon connection.autoconnect yes
  87. function create_cron_task( $TID, $Params = '' ){
  88. if( file_exists( '/srv/etc/cron.json' ) ) return false;
  89. @unlink( '/srv/tmp/task_log.var' );
  90. $Data['operation'] = intval( $TID );
  91. if( $Params!= '' ){
  92. $Data['parameter'] = $Params;
  93. };
  94. $FD = fopen( '/srv/etc/cron_.json', "w+" );
  95. if( $FD ){
  96. fprintf( $FD, json_encode( $Data ) );
  97. fclose( $FD );
  98. rename( '/srv/etc/cron_.json', '/srv/etc/cron.json' );
  99. return true;
  100. };
  101. return false;
  102. };
  103. function parse_samba_config( $file='' ){
  104. if( $file == '' ) $file = '/etc/samba/smb.conf';
  105. $Data = file_get_contents( $file );
  106. $TArr = explode( "\n", $Data );
  107. $mumu_array = array();
  108. $cSection = 'no_section';
  109. foreach( $TArr as $T ){
  110. $CL = trim( $T );
  111. if( strlen( $CL ) ){
  112. $CArr = explode( '#', $CL );
  113. if( count( $CArr ) > 1 ) $CL = $CArr[0];
  114. $CArr = explode( ';', $CL );
  115. if( count( $CArr ) > 1 ) $CL = $CArr[0];
  116. if( strlen( $CL ) ){
  117. if( ( stripos( $CL, '[' ) !== false ) && ( stripos( $CL, '[' ) !== false ) ) {
  118. $cSection = str_replace(']','',str_replace('[','',$CL));
  119. $mumu_array[$cSection] = array();
  120. } else {
  121. $TArr = explode( '=', $CL );
  122. if( count( $TArr ) == 2 ){
  123. $Key = strtolower( trim( $TArr[0] ));
  124. $Val = trim( $TArr[1] );
  125. $mumu_array[$cSection][$Key] = $Val;
  126. };
  127. };
  128. };
  129. };
  130. };
  131. return $mumu_array;
  132. };
  133. function validate_samba_config( $file='', $dist='' ){
  134. $rv = false;
  135. if( $file == '' ) $file = '/etc/samba/smb.conf';
  136. if( $dist == '' ) $dist = '/srv/etc/samba.json';
  137. $Actual = parse_samba_config( $file );
  138. //print_r( $Actual );
  139. $Params = json_decode( file_get_contents( $dist ), true );
  140. //print_r( $Params );
  141. foreach( $Actual as $CS=>$MEMS ){
  142. if( isset( $Params[$CS] ) )
  143. foreach( $MEMS as $CK=>$CV )
  144. if( isset( $Params[$CS][$CK] ))
  145. if($Params[$CS][$CK]!= $CV ) {
  146. echo "{$CS}:{$CK} does not match {$Params[$CS][$CK]}!={$CV}\n";
  147. return false;
  148. };
  149. };
  150. return true;
  151. };
  152. function restart_samba(){
  153. $CMD = 'service samba restart';
  154. $R = `{$CMD}`;
  155. if( stripos( $R, 'not found' ) !== false ){
  156. $CMD = '/etc/init.d/samba-ad-dc restart';
  157. $R = `{$CMD}`;
  158. if( stripos( $R, 'not found' ) !== false ){
  159. $CMD = '/etc/init.d/nmbd restart';
  160. $R = `{$CMD}`;
  161. $CMD = '/etc/init.d/smbd restart';
  162. $R = `{$CMD}`;
  163. };
  164. };
  165. };
  166. function get_nif_config( ){
  167. $CMD ='ip -j a';
  168. $Data = json_decode(`{$CMD}`, true );
  169. return $Data;
  170. };
  171. function set_nginx_certificate( $cert_file ){
  172. };
  173. function sync_time(){
  174. };
  175. function get_network_config(){
  176. $Data = file_get_contents( '/etc/network/interfaces' );
  177. $TArr = explode("\n", $Data );
  178. $IFS = array();
  179. $CI = '';
  180. foreach( $TArr as $T ){
  181. $CL = trim( $T );
  182. $JJ = explode( '#', $CL );
  183. if( is_array( $JJ ) ) if( strlen( $JJ[0] ) == 0 ) continue;
  184. if( stripos( $CL, 'iface') !== false ){
  185. $JJ = explode(' ', $CL );
  186. $CI = $JJ[1];
  187. if( !isset( $IFS[$JJ[1]] ) ) $IFS[$JJ[1]] = array();
  188. if( !isset( $IFS[$JJ[1]]['flags'] ) ){
  189. $IFS[$JJ[1]]['flags'] = array();
  190. $IFS[$JJ[1]]['flags']['hotplug'] = 0;
  191. $IFS[$JJ[1]]['flags']['auto'] = 0;
  192. };
  193. $IFS[$JJ[1]]['net'] = array();
  194. $IFS[$JJ[1]]['fam'] = $JJ[2];
  195. $IFS[$JJ[1]]['mode'] = $JJ[3];
  196. } else if( stripos( $CL, 'auto') !== false ){
  197. $JJ = explode( ' ', $CL );
  198. if( count( $JJ ) > 2 ){
  199. for( $i = 1; $i < count( $JJ ); $i++){
  200. if( !isset( $IFS[$JJ[$i]] ) ) $IFS[$JJ[1]] = array();
  201. if( !isset( $IFS[$JJ[$i]]['flags'] ) ){
  202. $IFS[$JJ[$i]]['flags'] = array();
  203. $IFS[$JJ[$i]]['flags']['hotplug'] = 0;
  204. $IFS[$JJ[$i]]['flags']['auto'] = 0;
  205. };
  206. $IFS[$JJ[$i]]['flags']['auto'] = 1;
  207. };
  208. } else {
  209. if( !isset( $IFS[$JJ[1]] ) ) $IFS[$JJ[1]] = array();
  210. if( !isset( $IFS[$JJ[1]]['flags'] ) ){
  211. $IFS[$JJ[1]]['flags'] = array();
  212. $IFS[$JJ[1]]['flags']['hotplug'] = 0;
  213. $IFS[$JJ[1]]['flags']['auto'] = 0;
  214. };
  215. $IFS[$JJ[1]]['flags']['auto'] = 1;
  216. };
  217. } else if( stripos( $CL, 'allow-hotplug' ) !== false ){
  218. $JJ = explode( ' ', $CL );
  219. if( count( $JJ ) > 2 ){
  220. for( $i = 1; $i < count( $JJ ); $i++){
  221. if( !isset( $IFS[$JJ[1]] ) ) $IFS[$JJ[1]] = array();
  222. if( !isset( $IFS[$JJ[1]]['flags'] ) ){
  223. $IFS[$JJ[$i]]['flags'] = array();
  224. $IFS[$JJ[$i]]['flags']['hotplug'] = 0;
  225. $IFS[$JJ[$i]]['flags']['auto'] = 0;
  226. };
  227. $IFS[$JJ[$i]]['flags']['hotplug'] = 1;
  228. };
  229. } else {
  230. if( !isset( $IFS[$JJ[1]] ) ) $IFS[$JJ[1]] = array();
  231. if( !isset( $IFS[$JJ[1]]['flags'] ) ){
  232. $IFS[$JJ[1]]['flags'] = array();
  233. $IFS[$JJ[1]]['flags']['hotplug'] = 0;
  234. $IFS[$JJ[1]]['flags']['auto'] = 0;
  235. };
  236. $IFS[$JJ[1]]['flags']['hotplug'] = 1;
  237. };
  238. } else if( $CI!= '' ) {
  239. $JJ = explode(' ', $CL );
  240. $IFS[$CI]['net'][$JJ[0]] = $JJ[1];
  241. };
  242. };
  243. return $IFS;
  244. };
  245. function get_NetGateway( $ADDR, $NM ){
  246. $TArr = explode( '.', $ADDR );
  247. $ba = 0xFFFFFFFF;
  248. foreach( $TArr as $T ) {
  249. $ba = ( $ba << 8 );
  250. $ba |= intval( $T );
  251. };
  252. $ba &= 0xFFFFFFFF;
  253. $TArr = explode( '.', $NM );
  254. $bn = 0;
  255. foreach( $TArr as $T ) {
  256. $bn = ( $bn << 8 );
  257. $bn += intval( $T );
  258. };
  259. $bc = ( $ba & $bn ) + 1;
  260. $byteArray = unpack("C*", pack("L",$bc ) );
  261. return sprintf( "%d.%d.%d.%d", $byteArray[4],$byteArray[3],$byteArray[2],$byteArray[1]);
  262. };
  263. function get_Network( $ADDR, $NM ){
  264. $TArr = explode( '.', $ADDR );
  265. $ba = 0xFFFFFFFF;
  266. foreach( $TArr as $T ) {
  267. $ba = ( $ba << 8 );
  268. $ba |= intval( $T );
  269. };
  270. $ba &= 0xFFFFFFFF;
  271. $TArr = explode( '.', $NM );
  272. $bn = 0;
  273. foreach( $TArr as $T ) {
  274. $bn = ( $bn << 8 );
  275. $bn += intval( $T );
  276. };
  277. $bc = ( $ba & $bn );
  278. $byteArray = unpack("C*", pack("L",$bc ) );
  279. return sprintf( "%d.%d.%d.%d", $byteArray[4],$byteArray[3],$byteArray[2],$byteArray[1]);
  280. };
  281. function get_NetBroadcast( $ADDR, $NM ){
  282. $TArr = explode( '.', $ADDR );
  283. $ba = 0xFFFFFFFF;
  284. foreach( $TArr as $T ) {
  285. $ba = ( $ba << 8 );
  286. $ba |= intval( $T );
  287. };
  288. $ba &= 0xFFFFFFFF;
  289. $TArr = explode( '.', $NM );
  290. $bn = 0;
  291. foreach( $TArr as $T ) {
  292. $bn = ( $bn << 8 );
  293. $bn += intval( $T );
  294. };
  295. $bc = ( $ba & $bn ) | ( ~ $bn );
  296. $byteArray = unpack("C*", pack("L",$bc ) );
  297. return sprintf( "%d.%d.%d.%d", $byteArray[4],$byteArray[3],$byteArray[2],$byteArray[1]);
  298. };
  299. function build_network_config( $NCFG, $FNAM = '' ){
  300. if( !is_array( $NCFG ) ) return;
  301. if( $FNAM == '' )$FNAM = '/etc/network/interfaces';
  302. $FD = fopen( $FNAM, "w+" );
  303. if( $FD ){
  304. foreach( $NCFG as $Key=>$NIF ){
  305. if( $NIF['flags']['auto'] ) fprintf( $FD, "auto %s\n", $Key );
  306. if( $NIF['flags']['hotplug'] ) fprintf( $FD, "allow-hotplug %s\n", $Key );
  307. fprintf( $FD, "iface %s %s %s\n", $Key, $NIF['fam'], $NIF['mode'] );
  308. if( $NIF['mode'] == 'static' && count( $NIF['net'] ) ){
  309. foreach( $NIF['net'] as $L=>$V ) fprintf( $FD, "\t%s %s\n", $L, $V );
  310. /*
  311. address 192.168.2.236
  312. netmask 255.255.255.0
  313. gateway 192.168.2.254
  314. dns-domain sweet.home
  315. dns-nameservers 192.168.2.254
  316. */
  317. };
  318. };
  319. fclose( $FD );
  320. };
  321. };
  322. function get_dhcpd_config(){
  323. $Data = file_get_contents( '/etc/dnsmasq.conf' );
  324. $TArr = explode("\n", $Data );
  325. $IFS = array();
  326. $IFS['dns'] = array();
  327. $IFS['nifs'] = array();
  328. $CANIF = '';
  329. foreach( $TArr as $T ){
  330. $CL = trim( $T );
  331. $JJ = explode( '#', $CL );
  332. if( is_array( $JJ ) ) if( strlen( $JJ[0] ) == 0 ) continue;
  333. //echo $CL . "\n";
  334. if( stripos( $CL, 'interface') !== false ){
  335. $JJ = explode('=', $CL );
  336. $CANIF = trim( $JJ[1] );
  337. $IFS['nifs'][$CANIF] = array();
  338. $IFS['nifs'][$CANIF]['range'] = array();
  339. $IFS['nifs'][$CANIF]['range']['start'] = '';
  340. $IFS['nifs'][$CANIF]['range']['end'] = '';
  341. $IFS['nifs'][$CANIF]['range']['netmask'] = '';
  342. $IFS['nifs'][$CANIF]['range']['lease'] = '';
  343. };
  344. if( stripos( $CL, 'server') !== false ){
  345. $JJ = explode('=', $CL );
  346. $IFS['dns'][] = $JJ[1];
  347. };
  348. if( stripos( $CL, 'dhcp-range') !== false ){
  349. $JJ = explode( '=', $CL );
  350. $BB = explode( ',', $JJ[1] );
  351. $IFS['nifs'][$CANIF]['range']['start'] = $BB[0];
  352. $IFS['nifs'][$CANIF]['range']['end'] = $BB[1];
  353. if( count( $BB ) == 4 ){
  354. $IFS['nifs'][$CANIF]['range']['netmask'] = $BB[2];
  355. $IFS['nifs'][$CANIF]['range']['lease'] = $BB[3];
  356. } else {
  357. $IFS['nifs'][$CANIF]['range']['lease'] = $BB[2];
  358. };
  359. };
  360. };
  361. return $IFS;
  362. };
  363. function list_tz(){
  364. $Data = `timedatectl list-timezones`;
  365. return explode( "\n", $Data );
  366. };
  367. function get_tz_nfo(){
  368. $Data = `timedatectl show`;
  369. $TArr = explode( "\n", $Data );
  370. $RData = array();
  371. foreach( $TArr as $T ){
  372. $CL = trim( $T );
  373. if( stripos( $CL, 'NTPSynchronized' ) !== false ){
  374. $BArr = explode( "=", $CL );
  375. $RData['SYNC'] = $BArr[1];
  376. } else {
  377. if( stripos( $CL, 'CanNTP' ) !== false ){
  378. $BArr = explode( "=", $CL );
  379. $RData['NTA'] = $BArr[1];
  380. } else {
  381. if( stripos( $CL, 'Timezone' ) !== false ){
  382. $BArr = explode( "=", $CL );
  383. $RData['TZ'] = $BArr[1];
  384. };
  385. if( stripos( $CL, 'LocalRTC' ) !== false ){
  386. $BArr = explode( "=", $CL );
  387. $RData['RTC'] = $BArr[1];
  388. };
  389. if( stripos( $CL, 'TimeUSec' ) !== false ){
  390. $BArr = explode( "=", $CL );
  391. $RData['TIME'] = $BArr[1];
  392. };
  393. if( stripos( $CL, 'NTP' ) !== false ){
  394. $BArr = explode( "=", $CL );
  395. $RData['NTP'] = $BArr[1];
  396. };
  397. };
  398. };
  399. };
  400. return $RData;
  401. };
  402. function build_dhcp_config( $CFG, $FNAM='' ){
  403. if( !is_array( $CFG ) ) return;
  404. if( $FNAM == '' )$FNAM = '/etc/dnsmasq.conf';
  405. $FD = fopen( $FNAM, "w+" );
  406. if( $FD ){
  407. foreach( $CFG['nifs'] as $Key=>$NIF ){
  408. fprintf( $FD, "interface=%s\n", $Key );
  409. if( $NIF['netmask']!=''){
  410. fprintf( $FD, "dhcp-range=%s,%s,%s,%s\n", $NIF['range']['start'],$NIF['range']['end'],$NIF['range']['netmask'],$NIF['range']['lease'] );
  411. } else {
  412. fprintf( $FD, "dhcp-range=%s,%s,%s\n", $NIF['range']['start'],$NIF['range']['end'],$NIF['range']['netmask'],$NIF['range']['lease'] );
  413. };
  414. };
  415. foreach( $CFG['dns'] as $DR ){
  416. fprintf( $FD, "server=%s\n", $DR );
  417. };
  418. fclose( $FD );
  419. };
  420. };
  421. function get_wifi_ap_configuration(){
  422. $Data = file_get_contents( '/etc/hostapd/hostapd.conf' );
  423. $TArr = explode("\n", $Data );
  424. $IFS = array();
  425. foreach( $TArr as $T ){
  426. $CL = trim( $T );
  427. $JJ = explode( '#', $CL );
  428. if( is_array( $JJ ) ) if( strlen( $JJ[0] ) == 0 ) continue;
  429. $JJ = explode('=', $CL );
  430. $IFS[$JJ[0]]=trim( $JJ[1] );
  431. };
  432. return $IFS;
  433. };
  434. function build_wifi_ap_configuration( $CFG, $FNAM='' ){
  435. if( !is_array( $CFG ) ) return;
  436. if( $FNAM == '' )$FNAM = '/etc/hostapd/hostapd.conf';
  437. $FD = fopen( $FNAM, "w+" );
  438. if( $FD ){
  439. foreach( $CFG as $Key=>$Val ) fprintf( $FD, "%s=%s\n", $Key, $Val );
  440. fclose( $FD );
  441. };
  442. };
  443. function scan_WIFI_networks( $Dev = '' ){
  444. if( $Dev == '' ) $Dev = 'wlan0';
  445. $JAJA = `iw dev {$Dev} scan | grep SSID`;
  446. };
  447. function apply_NGINX_AUTH( $ROOTDIR, $AFILE ){
  448. $Data = file_get_contents( '/etc/nginx/sites-available/default' );
  449. $TArr = explode( "\n", $Data );
  450. $ACTIVE = false;
  451. $FD = fopen( '/etc/nginx/sites-available/default.tmp', "w+");
  452. if( $FD ){
  453. foreach( $TArr as $T ){
  454. $JU = explode( '#', $T );
  455. $CL = trim( $JU[0] );
  456. if( $CL == '' ) continue;
  457. if( $ACTIVE ){
  458. if( stripos( $CL, 'auth_basic' ) === false ) fprintf( $FD, "%s\n", $CL );
  459. } else fprintf( $FD, "%s\n", $CL );
  460. if( stripos( $CL, 'root' ) !== false ) {
  461. $BArr = explode( " ", $CL );
  462. $CArr = explode( ';', $BArr[1] );
  463. $ACTIVE = ( $CArr[0] == $ROOTDIR );
  464. if( $ACTIVE ){
  465. fprintf( $FD, "auth_basic \"Platform's Configuration\";\n" );
  466. fprintf( $FD, "auth_basic_user_file %s;\n", $AFILE );
  467. };
  468. };
  469. };
  470. fclose( $FD );
  471. unlink( '/etc/nginx/sites-available/default' );
  472. rename( '/etc/nginx/sites-available/default.tmp', '/etc/nginx/sites-available/default' );
  473. };
  474. };
  475. function get_SERVICE_STATUS( $ServName ){
  476. $Data = `systemctl status {$ServName}`;
  477. $TArr = explode( "\n", $Data );
  478. $RD = array();
  479. foreach( $TArr as $T ){
  480. $CL = trim( $T );
  481. if( stripos( $CL, 'loaded' ) !== false ){
  482. $BArr = explode( ';', $CL );
  483. if( count( $BArr ) > 1 ){
  484. if( stripos( $BArr[1], 'enabled' )!==false ) {
  485. $RD['ENA'] = true;
  486. } else {
  487. $RD['ENA'] = false;
  488. };
  489. };
  490. };
  491. if( stripos( $CL, 'active' ) !== false ){
  492. if( stripos( $CL, 'inactive' )!==false ) {
  493. $RD['WORKS'] = false;
  494. } else {
  495. $RD['WORKS'] = true;
  496. };
  497. };
  498. };
  499. return $RD;
  500. };
  501. function remove_NGINX_AUTH( $ROOTDIR ){
  502. $Data = file_get_contents( '/etc/nginx/sites-available/default' );
  503. $TArr = explode( "\n", $Data );
  504. $ACTIVE = false;
  505. $FD = fopen( '/etc/nginx/sites-available/default.tmp', "w+");
  506. if( $FD ){
  507. foreach( $TArr as $T ){
  508. $JU = explode( '#', $T );
  509. $CL = trim( $JU[0] );
  510. if( $CL == '' ) continue;
  511. if( $ACTIVE ){
  512. if( stripos( $CL, 'auth_basic' ) === false ) fprintf( $FD, "%s\n", $CL );
  513. } else fprintf( $FD, "%s\n", $CL );
  514. if( stripos( $CL, 'root' ) !== false ) {
  515. $BArr = explode( " ", $CL );
  516. $CArr = explode( ';', $BArr[1] );
  517. $ACTIVE = ( $CArr[0] == $ROOTDIR );
  518. };
  519. };
  520. fclose( $FD );
  521. unlink( '/etc/nginx/sites-available/default' );
  522. rename( '/etc/nginx/sites-available/default.tmp', '/etc/nginx/sites-available/default' );
  523. };
  524. };
  525. function set_NGINX_APP_ENABLE(){
  526. global $SYS_CONFIG;
  527. $Data = file_get_contents( '/etc/nginx/sites-available/default' );
  528. $TArr = explode( "\n", $Data );
  529. $BLOCKED = false;
  530. $FD = fopen( '/etc/nginx/sites-available/default.tmp', "w+");
  531. if( $FD ){
  532. foreach( $TArr as $T ){
  533. $JU = explode( '#', $T );
  534. $CL = trim( $JU[0] );
  535. if( $CL == '' ) continue;
  536. if( stripos( $CL, 'server' ) === false ) {
  537. if( !$BLOCKED ) fprintf( $FD, "%s\n", $CL );
  538. };
  539. if( stripos( $CL, 'root' ) !== false ) {
  540. $BArr = explode( " ", $CL );
  541. $CArr = explode( ';', $BArr[1] );
  542. $BLOCKED = ( $CArr[0] == '/srv/www/distro' ) && ( stripos( $CL, '8080' ) === false );
  543. if( !$BLOCKED ){
  544. fprintf( $FD, "server {\n %s\nserver_name _;\n", $CL );
  545. };
  546. };
  547. };
  548. //Output App Web
  549. fprintf( $FD, "server {\nroot /srv/www/web; listen 80 default_server; listen [::]:80 default_server;\n" );
  550. if( $SYS_CONFIG['NGINX_CERTIFICATE_INSTALLED'] ){
  551. if( $SYS_CONFIG['NGINX_CERTIFICATE_ENABLED'] ){
  552. fprintf( $FD, "listen 443 ssl default_server;listen [::]:443 ssl default_server;\n" );
  553. fprintf( $FD, "include /srv/etc/nginx/self-signed.conf;\n" );
  554. fprintf( $FD, "include /srv/etc/nginx/ssl-params.conf;\n" );
  555. };
  556. };
  557. fprintf( $FD, "index index.html index.htm index.nginx-debian.html index.php;\n" );
  558. fprintf( $FD, "server_name _;\n" );
  559. fprintf( $FD, 'location / { try_files $uri $uri/ =404; index index.html index.php; }'."\n" );
  560. fprintf( $FD, 'location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php-fpm.sock;}'."\n" );
  561. fprintf( $FD, 'location ~* /jsapi_dynamic/.+\.js$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php-fpm.sock; }'."\n" );
  562. 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" );
  563. fprintf( $FD, "location ~ /\.ht {deny all;}\n}\n" );
  564. fclose( $FD );
  565. unlink( '/etc/nginx/sites-available/default' );
  566. rename( '/etc/nginx/sites-available/default.tmp', '/etc/nginx/sites-available/default' );
  567. };
  568. };
  569. function set_NGINX_APP_DISABLE(){
  570. global $SYS_CONFIG;
  571. $Data = file_get_contents( '/etc/nginx/sites-available/default' );
  572. $TArr = explode( "\n", $Data );
  573. $BLOCKED = false;
  574. $FD = fopen( '/etc/nginx/sites-available/default.tmp', "w+");
  575. if( $FD ){
  576. foreach( $TArr as $T ){
  577. $JU = explode( '#', $T );
  578. $CL = trim( $JU[0] );
  579. if( $CL == '' ) continue;
  580. if( stripos( $CL, 'server' ) === false ) {
  581. if( !$BLOCKED ) fprintf( $FD, "%s\n", $CL );
  582. };
  583. if( stripos( $CL, 'root' ) !== false ) {
  584. $BArr = explode( " ", $CL );
  585. $CArr = explode( ';', $BArr[1] );
  586. $BLOCKED = ( $CArr[0] == '/srv/www/web' );
  587. if( !$BLOCKED ){
  588. fprintf( $FD, "server {\n %s\nserver_name _;\n", $CL );
  589. };
  590. };
  591. };
  592. //Output App Web
  593. fprintf( $FD, "server {\nroot /srv/www/distro; listen 80 default_server; listen [::]:80 default_server;\n" );
  594. if( $SYS_CONFIG['NGINX_CERTIFICATE_INSTALLED'] ){
  595. };
  596. fprintf( $FD, "index index.html index.htm index.nginx-debian.html index.php;\n" );
  597. fprintf( $FD, "server_name _;\n" );
  598. fprintf( $FD, 'location / { try_files $uri $uri/ =404; index index.html index.php; }'."\n" );
  599. fprintf( $FD, 'location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php-fpm.sock;}'."\n" );
  600. fprintf( $FD, "location ~ /\.ht {deny all;}\n}\n" );
  601. fclose( $FD );
  602. unlink( '/etc/nginx/sites-available/default' );
  603. rename( '/etc/nginx/sites-available/default.tmp', '/etc/nginx/sites-available/default' );
  604. };
  605. };
  606. function process_is_running( $PName = '', $NArgs = '' ){
  607. $GS = '';
  608. if( is_array( $PName ) ){
  609. foreach( $PName as $P ) $GS .= "| grep \"{$P}\" ";
  610. if( is_array( $NArgs ) ){
  611. foreach( $NArgs as $N ) $GS .= "| grep -v \"{$N}\" ";
  612. } else {
  613. if( $NArgs!= '' ){
  614. $GS .= "| grep -v \"{$NArgs}\" ";
  615. };
  616. };
  617. } else {
  618. if( $PName == '' ) return false;
  619. $GS = "| grep \"{$PName}\" ";
  620. if( is_array( $NArgs ) ){
  621. foreach( $NArgs as $N ) $GS .= "| grep -v \"{$N}\"";
  622. } else {
  623. if( $NArgs != '' ){
  624. $GS .= "| grep -v \"{$NArgs}\" ";
  625. };
  626. };
  627. };
  628. $JOJO = `ps -A -o args {$GS} 2>&1 `;
  629. if( $JOJO == '' ) return false;
  630. $TArr = explode( "\n", $JOJO );
  631. foreach( $TArr as $T ){
  632. $CC = trim( $T );
  633. if( $CC == '' ) continue;
  634. if( ( stripos( $T, 'ps' ) === false ) && ( stripos( $T, 'grep' ) === false ) && ( stripos( $T, 'sshd' ) === false ) ) return true;
  635. };
  636. return false;
  637. };
  638. function stop_BACKEND_APPLICATION(){
  639. if( process_is_running('starProcDaemon') ){
  640. $L = `killall starProcDaemon`;
  641. sleep( 1 );
  642. };
  643. if( process_is_running('mojo_d') ){
  644. $L = `killall mojo_d`;
  645. sleep( 4 );
  646. };
  647. if( process_is_running('mojo') ){
  648. $L = `killall mojo`;
  649. sleep( 4 );
  650. };
  651. /// Shutdown Argus Web UI
  652. if( process_is_running( array( 'nginx', 'master process' ), 'ag_nginx' ) ){
  653. $L = `systemctl stop nginx.service`;
  654. sleep( 1 );
  655. };
  656. // Fix unclosed Databases
  657. if( file_exists( '/srv/www/web/lib/controller/data/mysqlitedb.db' ) ){
  658. $DATABASE = new SQLite_DB( '/srv/www/web/lib/controller/data/mysqlitedb.db');
  659. unset( $DATABASE );
  660. };
  661. if( file_exists( '/srv/www/web/lib/controller/data/reports.db' ) ){
  662. $DATABASE = new SQLite_DB( '/srv/www/web/lib/controller/data/reports.db');
  663. unset( $DATABASE );
  664. };
  665. };
  666. function start_BACKEND_APPLICATION(){
  667. $CWD = getcwd();
  668. chdir( '/srv/start' );
  669. $L = `./starProcDaemon`;
  670. sleep( 1 );
  671. chdir( $CWD );
  672. if( !process_is_running( array( 'nginx', 'master process' ), 'ag_nginx' ) ){
  673. $L = `systemctl start nginx.service`;
  674. sleep( 1 );
  675. };
  676. };
  677. function verify_CERT( $ROOT, $CHEK ){
  678. $DADA = trim( `/usr/bin/openssl verify -verbose -CAfile {$ROOT} {$CHEK}` );
  679. $TArr = explode( ':', $DADA );
  680. return ( stripos( $TArr[1],'ok' ) !== false );
  681. };
  682. function verify_CERT_KEY( $CERT, $KEY ){
  683. $L = `/usr/bin/openssl rsa -in {$KEY} -check -noout`;
  684. if( stripos( $L, 'RSA key ok' ) !== false ){
  685. $A = `openssl x509 -noout -modulus -in {$CERT}`;
  686. $B = `openssl rsa -noout -modulus -in {$KEY}`;
  687. if( $A == $B ) return true;
  688. };
  689. return false;
  690. };
  691. function get_embed_data_CERT( $CERT ){
  692. $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`;
  693. $TArr = explode( "\n", $DADA );
  694. $RD = array();
  695. $RD['subject'] = array();
  696. $RD['ip'] = array();
  697. foreach( $TArr as $T ){
  698. $CL = trim( $T );
  699. if( stripos( $CL, 'Subject:')!== false ){
  700. $BArr = explode( ':', $CL );
  701. $RR = explode( ',', $BArr[1] );
  702. foreach( $RR as $R ){
  703. $ZZ = explode( '=', $R );
  704. $RD['subject'][trim( $ZZ[0])]=trim($ZZ[1]);
  705. };
  706. };
  707. if( stripos( $CL, 'IP Address' )!== false ){
  708. $BArr = explode( ',', $CL );
  709. foreach( $BArr as $B ){
  710. $ZZ = explode( ':', $B );
  711. $RD['ip'][] = trim( $ZZ[1] );
  712. };
  713. };
  714. };
  715. return $RD;
  716. };
  717. function list_files( $Path, $Filter = '' ){
  718. $RetVal = array();
  719. if( is_dir( $Path ) ){
  720. $DD = opendir( $Path );
  721. if( $DD !== false ){
  722. while(( $file = readdir($DD)) !== false ){
  723. if( $Filter == '' && $file!= '.' && $file!= '..' ){
  724. $RetVal[] = $file;
  725. } else {
  726. if( preg_match( $Filter, $file ) ) $RetVal[] = $file;
  727. };
  728. };
  729. closedir($DD);
  730. };
  731. };
  732. return $RetVal;
  733. };
  734. function list_AP_NEARBY( $DEV ){
  735. $RV = array();
  736. $L = `sudo ip link set {$DEV} up`;
  737. $DATA = `sudo iw dev {$DEV} scan | grep SSID`;
  738. $L = `sudo ip link set {$DEV} down`;
  739. if( $DATA!= '' ){
  740. $TArr = explode( "\n", $DATA );
  741. foreach( $TArr as $T ){
  742. $CL = trim( $T );
  743. if( $CL!='' ){
  744. $TBarr = explode( ':', $CL );
  745. if( isset( $TBarr[1] ) ){
  746. $RV[] = $TBarr[1];
  747. };
  748. };
  749. };
  750. };
  751. return $RV;
  752. };
  753. function get_argus_config(){
  754. $RV = array();
  755. $RV['app_is_running'] = process_is_running( '/srv/mojo/mojo' );
  756. if( !$RV['app_is_running'] ) $RV['app_is_running'] = process_is_running( '/srv/mojo/mojo_d' );
  757. $RV['webui_is_running'] = process_is_running( array ( 'nginx', 'master' ), 'ag_nginx' );
  758. return $RV;
  759. };
  760. function serialize_current_application_config(){
  761. $DATA = array(
  762. 'server'=>array( 'id'=>1, 'identity'=>'3638eedfe4370b4b53285ae524b88343', 'site_id'=>1 ),
  763. 'nodes'=>array(),
  764. 'resources'=>array(),
  765. 'sensors'=>array(),
  766. 'cameras'=>array(),
  767. 'modules'=>array(),
  768. 'filters'=>array(),
  769. 'system_props'=>array(),
  770. 'users'=>array()
  771. );
  772. $fid = fopen( '/srv/mojo/server.conf', "r" );
  773. if( $fid !== false ){
  774. while( !feof( $fid ) ){
  775. $CL = trim( fgets( $fid ) );
  776. $CL = str_replace( ' ', '', $CL );
  777. $CL = str_replace( '"', '', $CL );
  778. $CL = str_replace( ';', '', $CL );
  779. $TArr = explode('=', $CL );
  780. if( stripos( $CL, 'IDENTITY' ) !== false ){
  781. $DATA['server']['identity'] = $TArr[1];
  782. };
  783. if( stripos( $CL, 'SITE_ID' ) !== false ){
  784. $DATA['server']['site_id'] = $TArr[1];
  785. };
  786. };
  787. fclose( $fid );
  788. };
  789. $DB = new SQLite_DB('/srv/www/web/lib/controller/data/mysqlitedb.db');
  790. $QS = "SELECT * FROM `nodes`";
  791. $R = $DB->Query( $QS );
  792. foreach( $R as $T ){
  793. $CN = array();
  794. foreach( $T as $key=>$val ) $CN[$key] = $val;
  795. $DATA['nodes'][] = $CN;
  796. };
  797. $QS = "SELECT * FROM `resources`";
  798. $R = $DB->Query( $QS );
  799. foreach( $R as $T ){
  800. $CN = array();
  801. foreach( $T as $key=>$val ) $CN[$key] = $val;
  802. switch( $CN['type'] ){
  803. case 1:{
  804. $QS = "SELECT * FROM `uart_settings` WHERE `id`={$CN['aux_id']}";
  805. $J = $DB->Query( $QS );
  806. $PP = array();
  807. foreach( $J[0] as $l=>$v ) $PP[$l]=$v;
  808. $CN['private'] = $PP;
  809. };
  810. };
  811. $DATA['resources'][] = $CN;
  812. };
  813. $QS = "SELECT * FROM `cameras`";
  814. $R = $DB->Query( $QS );
  815. foreach( $R as $T ){
  816. $CN = array();
  817. foreach( $T as $key=>$val ) $CN[$key] = $val;
  818. $QS = "SELECT * FROM `media_streams` WHERE `source_id`={$CN['id']}";
  819. $CN['streams'] = $DB->Query( $QS );
  820. $DATA['cameras'][] = $CN;
  821. };
  822. $QS = "SELECT * FROM `lrf_sensors`";
  823. $R = $DB->Query( $QS );
  824. foreach( $R as $T ){
  825. $CN = array();
  826. foreach( $T as $key=>$val ) $CN[$key] = $val;
  827. $QS = "SELECT * FROM `lrf_detection_profiles` WHERE `aux_id`={$CN['id']}";
  828. $CN['private'] = $DB->Query( $QS );
  829. $DATA['sensors'][] = $CN;
  830. };
  831. $QS = "SELECT * FROM `modules`";
  832. $R = $DB->Query( $QS );
  833. foreach( $R as $T ){
  834. $CN = array();
  835. foreach( $T as $key=>$val ) $CN[$key] = $val;
  836. switch( $CN['type'] ){
  837. case 8:{
  838. $PP = array();
  839. $QS = "SELECT * FROM `module_eve` WHERE `id`={$CN['aux_id']}";
  840. $K = $DB->Query( $QS );
  841. $PP = $K[0];
  842. $CN['private'] = $PP;
  843. break;
  844. };
  845. };
  846. $DATA['modules'][] = $CN;
  847. };
  848. $QS = "SELECT * FROM `target_filters`";
  849. $R = $DB->Query( $QS );
  850. foreach( $R as $T ){
  851. $CN = array();
  852. foreach( $T as $key=>$val ) $CN[$key] = $val;
  853. switch( $CN['type'] ){
  854. case 1:{
  855. $PP = array();
  856. $QS = "SELECT * FROM `target_filter_train` WHERE `id`={$CN['aux_id']}";
  857. $K = $DB->Query( $QS );
  858. $PP = $K[0];
  859. $CN['private'] = $PP;
  860. break;
  861. };
  862. };
  863. $DATA['filters'][] = $CN;
  864. };
  865. $QS = "SELECT * FROM `system_props`";
  866. $R = $DB->Query( $QS );
  867. $DATA['system_props'] = $R[0];
  868. $QS = "SELECT * FROM `users`";
  869. $R = $DB->Query( $QS );
  870. foreach( $R as $T ){
  871. if( $T['id'] > 1 ){
  872. $CN = array();
  873. foreach( $T as $key=>$val ) $CN[$key] = $val;
  874. $DATA['users'][] = $CN;
  875. };
  876. };
  877. unset( $DB );
  878. return json_encode( $DATA );
  879. };
  880. function apply_application_config( $A, $DBF ){
  881. $DB = new SQLite_DB( $DBF );
  882. $DATA = json_decode( $A, true );
  883. foreach( $DATA as $Key=>$Val ){
  884. switch( $Key ){
  885. case 'users':{
  886. foreach( $Val as $T ){
  887. $flds = array();
  888. $values = array();
  889. $updates = array();
  890. foreach( $T as $A=>$B ){
  891. $flds[] = "`{$A}`";
  892. $values[]="'{$B}'";
  893. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  894. };
  895. $QS = "INSERT INTO `users`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  896. $DB->Query( $QS );
  897. };
  898. break;
  899. };
  900. case 'server':{
  901. foreach( $Val as $A=>$B ){
  902. $flds[] = "`{$A}`";
  903. $values[]="'{$B}'";
  904. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  905. };
  906. $QS = "INSERT INTO `servers`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  907. $DB->Query( $QS );
  908. break;
  909. };
  910. case 'nodes':{
  911. foreach( $Val as $T ){
  912. $flds = array();
  913. $values = array();
  914. $updates = array();
  915. foreach( $T as $A=>$B ){
  916. $flds[] = "`{$A}`";
  917. $values[]="'{$B}'";
  918. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  919. };
  920. $QS = "INSERT INTO `nodes`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  921. $DB->Query( $QS );
  922. };
  923. break;
  924. };
  925. case 'resources':{
  926. foreach( $Val as $T ){
  927. $flds = array();
  928. $values = array();
  929. $updates = array();
  930. foreach( $T as $A=>$B ){
  931. if( $A!='private' ){
  932. $flds[] = "`{$A}`";
  933. $values[]="'{$B}'";
  934. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  935. };
  936. };
  937. $QS = "INSERT INTO `resources`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  938. $DB->Query( $QS );
  939. switch( $T['type'] ){
  940. case 1:{
  941. $flds = array();
  942. $values = array();
  943. $updates = array();
  944. foreach( $T['private'] as $A=>$B ){
  945. $flds[] = "`{$A}`";
  946. $values[]="'{$B}'";
  947. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  948. };
  949. $QS = "INSERT INTO `uart_settings`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  950. $DB->Query( $QS );
  951. break;
  952. };
  953. };
  954. };
  955. break;
  956. };
  957. case 'sensors':{
  958. foreach( $Val as $T ){
  959. $flds = array();
  960. $values = array();
  961. $updates = array();
  962. foreach( $T as $A=>$B ){
  963. if( $A!='private' ){
  964. $flds[] = "`{$A}`";
  965. $values[]="'{$B}'";
  966. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  967. };
  968. };
  969. $QS = "INSERT INTO `lrf_sensors`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  970. $DB->Query( $QS );
  971. if( count( $T['private'] ) ){
  972. foreach( $T['private'] as $P ){
  973. foreach( $P as $A=>$B ){
  974. $flds[] = "`{$A}`";
  975. $values[]="'{$B}'";
  976. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  977. };
  978. $QS = "INSERT INTO `lrf_detection_profiles`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  979. $DB->Query( $QS );
  980. };
  981. };
  982. };
  983. break;
  984. };
  985. case 'cameras':{
  986. foreach( $Val as $T ){
  987. $flds = array();
  988. $values = array();
  989. $updates = array();
  990. foreach( $T as $A=>$B ){
  991. if( $A!='streams' ){
  992. $flds[] = "`{$A}`";
  993. $values[]="'{$B}'";
  994. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  995. };
  996. };
  997. $QS = "INSERT INTO `cameras`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  998. $DB->Query( $QS );
  999. foreach( $T['streams'] as $S ){
  1000. $flds = array();
  1001. $values = array();
  1002. $updates = array();
  1003. foreach( $S as $A=>$B ){
  1004. $flds[] = "`{$A}`";
  1005. $values[]="'{$B}'";
  1006. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  1007. };
  1008. $QS = "INSERT INTO `media_streams`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  1009. $DB->Query( $QS );
  1010. };
  1011. };
  1012. break;
  1013. };
  1014. case 'modules':{
  1015. foreach( $Val as $T ){
  1016. $flds = array();
  1017. $values = array();
  1018. $updates = array();
  1019. foreach( $T as $A=>$B ){
  1020. if( $A!='private' ){
  1021. $flds[] = "`{$A}`";
  1022. $values[]="'{$B}'";
  1023. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  1024. };
  1025. };
  1026. $QS = "INSERT INTO `modules`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  1027. $DB->Query( $QS );
  1028. switch( $T['type'] ){
  1029. case 8:{
  1030. $flds = array();
  1031. $values = array();
  1032. $updates = array();
  1033. foreach( $T['private'] as $A=>$B ){
  1034. $flds[] = "`{$A}`";
  1035. $values[]="'{$B}'";
  1036. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  1037. };
  1038. $QS = "INSERT INTO `module_eve`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  1039. $DB->Query( $QS );
  1040. break;
  1041. };
  1042. };
  1043. };
  1044. break;
  1045. };
  1046. case 'filters':{
  1047. foreach( $Val as $T ){
  1048. $flds = array();
  1049. $values = array();
  1050. $updates = array();
  1051. foreach( $T as $A=>$B ){
  1052. if( $A!='private' ){
  1053. $flds[] = "`{$A}`";
  1054. $values[]="'{$B}'";
  1055. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  1056. };
  1057. };
  1058. $QS = "INSERT INTO `target_filters`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  1059. $DB->Query( $QS );
  1060. switch( $T['type'] ){
  1061. case 1:{
  1062. $flds = array();
  1063. $values = array();
  1064. $updates = array();
  1065. foreach( $T['private'] as $A=>$B ){
  1066. $flds[] = "`{$A}`";
  1067. $values[]="'{$B}'";
  1068. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  1069. };
  1070. $QS = "INSERT INTO `target_filter_train`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  1071. $DB->Query( $QS );
  1072. break;
  1073. };
  1074. };
  1075. };
  1076. break;
  1077. };
  1078. case 'system_props':{
  1079. $flds = array();
  1080. $values = array();
  1081. $updates = array();
  1082. foreach( $Val as $A=>$B ){
  1083. $flds[] = "`{$A}`";
  1084. $values[]="'{$B}'";
  1085. if( $A!='id' ) $updates[] = "`{$A}`='{$B}'";
  1086. };
  1087. $QS = "INSERT INTO `system_props`(" . implode( ',', $flds ).") VALUES (".implode( ',', $values ).") ON CONFLICT(`id`) DO UPDATE SET ".implode( ',', $updates );
  1088. $DB->Query( $QS );
  1089. break;
  1090. };
  1091. };
  1092. };
  1093. unset( $DB );
  1094. };
  1095. function set_factory_settings(){
  1096. stop_BACKEND_APPLICATION();
  1097. $R = `rm -f -R /srv/www/web/lib/controller/data/uploads/*`;//*/
  1098. $R = `rm -f -R /srv/www/web/lib/controller/data/device_draws/*`;//*/
  1099. $R = `rm -f -R /srv/www/web/lib/controller/data/videos/*`;//*/
  1100. $SRC = '/srv/www/distro/lib/data/mysqlitedb.db';
  1101. $DST = '/srv/www/web/lib/controller/data/mysqlitedb.db';
  1102. $R = `rm -f $DST`;
  1103. $C = `cp $SRC $DST`;
  1104. $SRC = '/srv/www/distro/lib/data/reports.db';
  1105. $DST = '/srv/www/web/lib/controller/data/reports.db';
  1106. $R = `rm -f $DST`;
  1107. $C = `cp $SRC $DST`;
  1108. start_BACKEND_APPLICATION();
  1109. };
  1110. function clear_history(){
  1111. stop_BACKEND_APPLICATION();
  1112. $CMD = `rm -R /srv/www/web/lib/controller/data/reports`;
  1113. $CMD = `mkdir /srv/www/web/lib/controller/data/reports`;
  1114. $CMD = `chown www-data:www-data /srv/www/web/lib/controller/data/reports`;
  1115. $CMD = `chmod 775 /srv/www/web/lib/controller/data/reports`;
  1116. start_BACKEND_APPLICATION();
  1117. };
  1118. function reload_modem_data( $MID ){
  1119. global $SYS_CONFIG;
  1120. $MANU = json_decode( `mmcli -m $MID --output-json`, true );
  1121. $LOLO = json_decode( `mmcli -m $MID --location-status --output-json`, true );
  1122. if( isset( $LOLO['modem'] ) ){
  1123. $MANU['modem']['location'] = $LOLO['modem']['location'];
  1124. };
  1125. $CD = $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID];
  1126. $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID] = $MANU['modem'];
  1127. if( isset( $CD['REGISTER_AT_BOOT'] )){
  1128. $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['REGISTER_AT_BOOT'] = $CD['REGISTER_AT_BOOT'];
  1129. } else $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['REGISTER_AT_BOOT'] = false;
  1130. if( isset( $CD['CONNECT_AT_BOOT'] )){
  1131. $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CONNECT_AT_BOOT'] = $CD['CONNECT_AT_BOOT'];
  1132. } else $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CONNECT_AT_BOOT'] = false;
  1133. if( isset( $CD['CUSTOM_APN'] ) ){
  1134. $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CUSTOM_APN'] = $CD['CUSTOM_APN'];
  1135. } else $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CUSTOM_APN'] = '';
  1136. if( isset( $CD['CUSTOM_APN_USER'] ) ){
  1137. $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CUSTOM_APN_USER'] = $CD['CUSTOM_APN_USER'];
  1138. } else $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CUSTOM_APN_USER'] = '';
  1139. if( isset( $CD['CUSTOM_APN_PASSWORD'] ) ){
  1140. $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CUSTOM_APN_PASSWORD'] = $CD['CUSTOM_APN_PASSWORD'];
  1141. } else $SYS_CONFIG['MODEM_CONFIG']['DEVICES'][$MID]['CUSTOM_APN_PASSWORD'] = '';
  1142. };
  1143. function get_NetworkManager_Profiles(){
  1144. global $WEB_USER_SUDO_PWD;
  1145. $CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli -c no -e no -t -s connection";
  1146. $LL = `$CMD`;
  1147. $TArr = explode( "\n", $LL );
  1148. $Data = array();
  1149. foreach( $TArr as $T ){
  1150. $ZZ = explode( ':',trim( $T ) );
  1151. if( count( $ZZ ) > 3 ){
  1152. $CC = array();
  1153. $CC['name']=$ZZ[0];
  1154. $CC['uuid']=$ZZ[1];
  1155. $CC['type']=$ZZ[2];
  1156. $CC['device'] = $ZZ[3];
  1157. $CC['managed'] = true;
  1158. if( strlen( $CC['device'] ) < 3 ) {
  1159. $CC['managed'] = false;
  1160. };
  1161. $Data[] = $CC;
  1162. };
  1163. };
  1164. return $Data;
  1165. };
  1166. function get_NetworkManager_Device( $DN ){
  1167. global $WEB_USER_SUDO_PWD;
  1168. $CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli -c no -e no -t -s device show {$DN}";
  1169. $LL = `$CMD`;
  1170. $TArr = explode( "\n", $LL );
  1171. $Data = array();
  1172. foreach( $TArr as $T ){
  1173. $ZZ = explode( ':',trim( $T ) );
  1174. if( strpos( $ZZ[0], '[' ) !== false ){
  1175. $TA = explode( '[', $ZZ[0] );
  1176. if( array_key_exists($TA[0],$Data) ){
  1177. $Data[$TA[0]][]=$ZZ[1];
  1178. } else {
  1179. $Data[$TA[0]]=array();
  1180. $Data[$TA[0]][0]=$ZZ[1];
  1181. };
  1182. } else {
  1183. $Data[$ZZ[0]] = $ZZ[1];
  1184. };
  1185. };
  1186. return $Data;
  1187. };
  1188. function get_NetworkManager_Connection( $CN ){
  1189. global $WEB_USER_SUDO_PWD;
  1190. $CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli -c no -e no -t -s connection show {$CN}";
  1191. $LL = `$CMD`;
  1192. $TArr = explode( "\n", $LL );
  1193. $Data = array();
  1194. foreach( $TArr as $T ){
  1195. $ZZ = explode( ':',trim( $T ) );
  1196. if( strpos( $ZZ[0], '[' ) !== false ){
  1197. $TA = explode( '[', $ZZ[0] );
  1198. if( array_key_exists($TA[0],$Data) ){
  1199. $Data[$TA[0]][]=$ZZ[1];
  1200. } else {
  1201. $Data[$TA[0]]=array();
  1202. $Data[$TA[0]][0]=$ZZ[1];
  1203. };
  1204. } else {
  1205. $Data[$ZZ[0]] = $ZZ[1];
  1206. };
  1207. };
  1208. return $Data;
  1209. };
  1210. function create_NetworkManager_Profile( $type, $name, $add_params='' ){
  1211. global $WEB_USER_SUDO_PWD;
  1212. $CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli connection add save yes +connection.type {$type} +connection.id \"{$name}\" ";
  1213. if( is_array( $add_params ) ){
  1214. if( count( $add_params ) ){
  1215. foreach( $add_params as $Key=>$Val ){
  1216. $CMD .= "+{$Key} \"{$Value}\" ";
  1217. };
  1218. };
  1219. };
  1220. $LL = `$CMD`;
  1221. if( strpos( $LL, 'successful' ) !== false ) return true;
  1222. return false;
  1223. };
  1224. function delete_NetworkManager_Profile( $name ){
  1225. global $WEB_USER_SUDO_PWD;
  1226. $CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli connection down \"{$name}\" ";
  1227. $LL = `$CMD`;
  1228. if( strpos( $LL, 'successful' ) === false ) return false;
  1229. $CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli connection delete \"{$name}\" ";
  1230. $LL = `$CMD`;
  1231. if( strpos( $LL, 'successful' ) !== false ) return true;
  1232. return false;
  1233. };
  1234. function connect_NetworkManager_Device( $dev ){
  1235. global $WEB_USER_SUDO_PWD;
  1236. $CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli device connect \"{$dev}\" ";
  1237. $LL = `$CMD`;
  1238. if( strpos( $LL, 'successful' ) !== false ) return true;
  1239. return false;
  1240. };
  1241. function disconnect_NetworkManager_Device( $dev ){
  1242. global $WEB_USER_SUDO_PWD;
  1243. $CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli device disconnect \"{$dev}\" ";
  1244. $LL = `$CMD`;
  1245. if( strpos( $LL, 'successful' ) !== false ) return true;
  1246. return false;
  1247. };
  1248. function activate_NetworkManager_Profile( $name ){
  1249. global $WEB_USER_SUDO_PWD;
  1250. $CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli connection up \"{$name}\" ";
  1251. $LL = `$CMD`;
  1252. if( strpos( $LL, 'successful' ) !== false ) return true;
  1253. return false;
  1254. };
  1255. function deactivate_NetworkManager_Profile( $name ){
  1256. global $WEB_USER_SUDO_PWD;
  1257. $CMD = "echo \"{$WEB_USER_SUDO_PWD}\" | sudo -S nmcli connection down \"{$name}\" ";
  1258. $LL = `$CMD`;
  1259. if( strpos( $LL, 'successful' ) !== false ) return true;
  1260. return false;
  1261. };
  1262. ?>