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.

1125 lines
32 KiB

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