<?php

session_start();

if (!$_SESSION['login']) {

	if ($_REQUEST['s'] == 'get_log_text') {
		echo 0;
	} else {
		header("Location: login.php");
	}
}
if ($_REQUEST['s'] == 'get_firmware_status') {

	$script_path = '/usr/local/bin/get_firmware_status';
	$status = shell_exec($script_path);
	echo $status;
}
if ($_REQUEST['s'] == 'update_firmware') {

	$script_path = 'sudo /opt/bogen/nyq_update';
	$status = shell_exec($script_path);
	echo $status;
}
if ($_REQUEST['s'] == 'restore_default_firmware') {

	$script_path = 'sudo /usr/local/bin/restore_default_firmware';
	$status = shell_exec($script_path);
	echo $status;
}
if ($_REQUEST['s'] == 'reboot_now') {

	$script_path = '/usr/local/bin/reboot_now';
	$run_script = shell_exec($script_path);
}
if ($_REQUEST['s'] == 'restart_application') {

	$script_path = ' sudo /usr/local/bin/restart_application no_update';
	$run_script = shell_exec($script_path);
}
if ($_REQUEST['s'] == 'update_configuration') {

	$script_path = ' sudo /usr/local/bin/update_configuration';
	$status = shell_exec($script_path);
	echo $status;
}
if ($_REQUEST['s'] == 'check_for_updates') {

	$script_path = '/usr/local/bin/check-for-firmware-updates';
	$status = shell_exec($script_path);
	echo $status;
}
if ($_REQUEST['s'] == 'download_firmware_updates') {

	$script_path = '/usr/local/bin/download-firmware-updates';
	$status = shell_exec($script_path);
	echo $status;
}
if ($_REQUEST['s'] == 'set_volume') {
	$volume = $_REQUEST['v'];
	$script_path = '/usr/local/bin/NyquistVolume set ' . $volume;
	$status = shell_exec($script_path);
	echo $status;
}
if ($_REQUEST['s'] == 'get_volume') {
	$script_path = '/usr/local/bin/NyquistVolume get';
	$status = shell_exec($script_path);
	echo $status;
}
if ($_REQUEST['s'] == 'get_volume_control') {
	$stream = $_REQUEST['d'];
	$script_path = '/usr/local/bin/stream_volume_controller get ' . $stream;
	$status = shell_exec($script_path);
	echo $status;
}
if ($_REQUEST['s'] == 'set_volume_control') {
	$volume = $_REQUEST['v'];
	$stream = $_REQUEST['d'];
	$script_path = '/usr/local/bin/stream_volume_controller set ' . $stream . ' ' . $volume;
	$status = shell_exec($script_path);
	echo $status;
}
if ($_REQUEST['s'] == 'upload_firmware') {

	$name = $_FILES['fileupload']['name'];
	$error = $_FILES['fileupload']['error'];
	$tmp_name = $_FILES['fileupload']['tmp_name'];
	$type = $_FILES['fileupload']['type'];
	$size = $_FILES['fileupload']['size'];

	$uploaddir = '/opt/bogen/incoming/';
	$uploadfile = $uploaddir . basename($_FILES['fileupload']['name']);

	// remove existing files
	$files = glob('/opt/bogen/incoming/*');
	foreach ($files as $file) {
		if (is_file($file)) {
			unlink($file);
		}
	}

	// check for parenthesis
	if (strpos($name, '(') === false && strpos($name, ')') === false) {
		// no match
		// upload file
		if (move_uploaded_file($_FILES['fileupload']['tmp_name'], $uploadfile)) {
			// echo 'success';
			header('Location:firmware.php');
		} else {
			// echo "failure<br>";
			// echo "error: " . $error . "<br>";
			header('Location:firmware.php');
		}
		// error_reporting(E_ALL);
		// echo '<pre>';
		// print_r($_FILES);
		// echo '</pre>';
		// phpinfo();
	} else {
		// match
		header('Location:firmware.php');
	}
}
if ($_REQUEST['s'] == 'get_log_files') {

	$files = shell_exec('find /var/log/ -group adm -type f -maxdepth 1');

	$file_array = explode("\n", $files);
	sort($file_array);

	$html =  '<option value="">Choose...</option>';
	foreach ($file_array as $path) {
		$file = substr($path, 9);
		if ($file != '') {
			$html .=  '<option value="' . $file . '">' . $file . '</option>';
		}
	}
	echo $html;
}
if ($_REQUEST['s'] == 'get_log_text') {
	if (!$_SESSION['login']) {
	} else {
		$file_name = $_REQUEST['f'];
		$cat = 'cat';
		$check_for_gz = substr($file_name, -2);
		if ($check_for_gz == 'gz') {
			$cat = 'zcat';
		} else {
			$cat = 'cat';
		}
		$path = '/var/log/' . $file_name;
		if ($file_name == 'dmesg') {
			system('dmesg');
		} else {
			system($cat . ' ' . $path);
		}
	}
}
if ($_REQUEST['s'] == 'save_config_changes') {

	$dcs_text = $_REQUEST['d'];
	$available_text = $_REQUEST['a'];
	$keys_text = $_REQUEST['k'];

	$dcsArray = json_decode($dcs_text);
	$availableArray = json_decode($available_text);
	$keysArray = json_decode($keys_text);

	$xml = '<SerializableClass Type="CANconfiguration" Version="0.1">' . "\n";
	$xml .= "\t" . '<Collection Name="PortsConfiguration">' . "\n";

	foreach ($keysArray as $key) {
		$total = 0;
		$xml .= "\t" . "\t" . '<Class Type="PortConfiguration" Version="0.1">' . "\n";
		$xml .= "\t" . "\t" . "\t" . '<Member Name="Port">' . $key . '</Member>' . "\n";
		$xml .= "\t" . "\t" . "\t" . '<Member Name="Privacy"></Member>' . "\n";
		$xml .= "\t" . "\t" . "\t" . '<Collection Name="DCSentries">' . "\n";

		foreach ($dcsArray as $dcs) {
			if ($dcs[2] == $key && count($dcs > 0)) {
				$xml .= "\t" . "\t" . "\t" . "\t" . '<Class Type="DCSentry" Version="0.1">' . "\n";
				$xml .= "\t" . "\t" . "\t" . "\t" . "\t" . '<Member Name="SerialNumber">' . $dcs[0] . '</Member>' . "\n";
				$xml .= "\t" . "\t" . "\t" . "\t" . "\t" . '<Member Name="DeviceNumber">' . $dcs[1] . '</Member>' . "\n";
				$xml .= "\t" . "\t" . "\t" . "\t" . '</Class>' . "\n";
				$total++;
			}
		}

		$xml .= "\t" . "\t" . "\t" . '</Collection>' . "\n";

		$xml .= "\t" . "\t" . '</Class>' . "\n";
	}

	$xml .= "\t" . '</Collection>' . "\n";

	if (count($availableArray) > 0) {

		$xml .= "\t" . '<Collection Name="DCSentries">' . "\n";

		foreach ($availableArray as $available) {
			$xml .= "\t" . "\t" . '<Class Type="DCSentry" Version="0.1">' . "\n";
			$xml .= "\t" . "\t" . "\t" . '<Member Name="SerialNumber">' . $available[0] . '</Member>' . "\n";
			$xml .= "\t" . "\t" . "\t" . '<Member Name="DeviceNumber">' . $available[1] . '</Member>' . "\n";
			$xml .= "\t" . "\t" . '</Class>' . "\n";
		}

		$xml .= "\t" .  '</Collection>' . "\n";
	}

	$xml .= '</SerializableClass>' . "\n";

	//echo $xml; 

	// write xml file to server
	$canconfig = fopen("/etc/nyquist/canconfig.xml", "w");

	if (!$canconfig) {
		// error
	} else {
		// complete write
		fwrite($canconfig, $xml);
		fclose($canconfig);
	}

	$_SESSION['saved'] = 'yes';

	$script_path = 'sudo /usr/local/bin/transfer_device_config ToServer';
	$status = shell_exec($script_path);
	$script_path = 'sudo systemctl reload appliance';
	$status = shell_exec($script_path);
	echo $status;
}
if ($_REQUEST['s'] == 'save_settings_to_server') {
	$script_path = 'sudo /usr/local/bin/transfer_device_config ToServer';
	$status = shell_exec($script_path);
	echo $status;
}
if ($_REQUEST['s'] == 'switch_standalone') {

	if (file_exists('/etc/nyquist/standalone.config')) {
		$script_path = 'sudo mv /etc/nyquist/standalone.config  /etc/nyquist/standalone.config-backup';
		$run_script = shell_exec($script_path);

		$script_path2 = 'sudo /usr/local/bin/update_configuration';
		$run_script2 = shell_exec($script_path2);

		echo 1;
	} else {
		$script_path = 'sudo mv /etc/nyquist/standalone.config-backup  /etc/nyquist/standalone.config';
		$run_script = shell_exec($script_path);

		$script_path2 = 'sudo /usr/local/bin/update_configuration';
		$run_script2 = shell_exec($script_path2);

		// get username from config file
		$config_file = fopen("/etc/nyquist/SIP.config", "r");
		$web_username = '';
		while (!feof($config_file)) {

			$line_text = fgets($config_file);

			if (strpos($line_text, 'WEB_USERNAME =') !== FALSE) {
				$web_username = str_replace("WEB_USERNAME = ", "", $line_text);
				$web_username = trim($web_username);
			}
		}
		fclose($config_file);

		// update log file
		$hostname_script = 'cat /etc/hostname';
		$hostname_text = shell_exec($hostname_script);
		$hostname_text_full = date("M j Y H:i:s") . ' ' . trim($hostname_text) . ' Username: ' . $web_username . PHP_EOL;

		$file = '/var/log/eula.log';

		if (file_exists('/var/log/eula.log')) {
			file_put_contents($file, $hostname_text_full, FILE_APPEND);
		} else {
			$touch_script = 'sudo touch /var/log/eula.log';
			$run_touch_script = shell_exec($touch_script);

			$chown_script = 'sudo chown www-data:adm /var/log/eula.log';
			$run_chown_script = shell_exec($chown_script);

			file_put_contents($file, $hostname_text_full, FILE_APPEND);
		}

		echo 1;
	}

	$_SESSION['login'] = false;
}
if ($_REQUEST['s'] == 'upload_presets') {

	$fileuploads = $_FILES['fileuploads'];

	if ($fileuploads['name'][0] == '') {
		// no file chosen
		$_SESSION['redirect_from_presets_upload'] = '<span style="color: #D8000C;"><i class="fa fa-times-circle"></i></span>  No preset files selected.';
		header('Location:dsp.php');
	} else {
		// preset files selected

		$_SESSION['redirect_from_presets_upload'] = '';

		//print_r($fileuploads);

		$chmod_preset = shell_exec('sudo chmod 775 /etc/nyquist/preset');
		$chown_preset = shell_exec('sudo chown root:www-data /etc/nyquist/preset');

		if (!is_dir('/run/uploads')) {
			$mkdir_uploads = shell_exec('sudo mkdir /run/uploads');
			$chown_uploads = shell_exec('sudo chown root:www-data /run/uploads');
			$chmod_uploads = shell_exec('sudo chmod 775 /run/uploads');
		}

		$validator_string = 'validate_presets ';

		if (is_array($fileuploads) && count($fileuploads['name']) > 0) {

			// exclude string
			$exclude = shell_exec('find /etc/nyquist/preset/ -group root');
			$exclude = str_replace("/etc/nyquist/preset/", "--exclude=", $exclude);
			$exclude = preg_replace('/\s+/', ' ', $exclude);
			$exclude = trim($exclude);

			for ($x = 0; $x < count($fileuploads['name']); $x++) {

				$name = $fileuploads['name'][$x];
				$type = $fileuploads['type'][$x];
				$tmp_name = $fileuploads['tmp_name'][$x];
				$error = $fileuploads['error'][$x];
				$size = $fileuploads['size'][$x];

				$uploaddir = '/run/uploads/';
				$uploadfile = $uploaddir . basename($name);
				if (move_uploaded_file($tmp_name, $uploadfile)) {
					// success
				} else {
					// failure
				}
				$validator_string  .= '"/run/uploads/' . $name . '" ';
			}

			$validator_path = $validator_string;
			$run_validator_script = shell_exec($validator_path);

			$repsoneArray = json_decode($run_validator_script);

			$response_string = '';

			foreach ($repsoneArray as $key => $reponse) {

				$description = '';
				$icon_text = '';

				if ($reponse == 'ValidationSuccess') {
					$description = 'The uploaded preset successfully validated.';
					$icon_text = '<span style="color: #4F8A10;"><i class="fa fa-check-circle"></i></span> ';
				}
				if ($reponse == 'ValidationFailed') {
					$description = 'The uploaded preset failed validation.';
					$icon_text = '<span style="color: #D8000C;"><i class="fa fa-times-circle"></i></span>  ';
				}
				if ($reponse == 'DuplicateName') {
					$description = 'The uploaded preset failed validation as preset name is duplicate.';
					$icon_text = '<span style="color: #D8000C;"><i class="fa fa-times-circle"></i></span>  ';
				}
				if ($reponse == 'DuplicateFile') {
					$description = 'The uploaded preset failed validation as preset filename is duplicate.';
					$icon_text = '<span style="color: #D8000C;"><i class="fa fa-times-circle"></i></span>  ';
				}
				if ($reponse == 'UpdatedFile') {
					$description = 'The uploaded preset failed validation as a newer copy of preset name exists.';
					$icon_text = '<span style="color: #D8000C;"><i class="fa fa-times-circle"></i></span>  ';
				}
				if ($reponse == 'InvalidName') {
					$description = 'The uploaded preset failed validation as the preset name is missing.';
					$icon_text = '<span style="color: #D8000C;"><i class="fa fa-times-circle"></i></span>  ';
				}
				if ($reponse == 'InvalidFile') {
					$description = 'The uploaded preset failed validation as the preset file is invalid.';
					$icon_text = '<span style="color: #D8000C;"><i class="fa fa-times-circle"></i></span>  ';
				}

				$preset_name = str_replace("/run/uploads/", "", $key);
				$preset_dir = '/etc/nyquist/preset/';
				$preset_file = $preset_dir . basename($preset_name);

				$response_string .= $icon_text . ' ' . $preset_name . ' - ' . $description . '<br>';

				if ($reponse == 'ValidationSuccess') {
					if (copy($key, $preset_file)) {
						// success
						unlink($key);

						$chmod = shell_exec('sudo chmod 664 ' . $preset_file);
						$chown = shell_exec('sudo chown www-data:www-data ' . $preset_file);
						$tar = shell_exec('sudo tar czPf /etc/nyquist/presets.tgz ' . $exclude . ' /etc/nyquist/preset');
						$chmod = shell_exec('sudo chmod 664 /etc/nyquist/presets.tgz');
						$chown = shell_exec('sudo chown root:www-data /etc/nyquist/presets.tgz');
					} else {
						// failure
						unlink($key);
					}
				} else {
					// failure
					unlink($key);
				}
			}

			// parse freshly uploaded file(s) 
			$parse_path = 'sudo /usr/local/bin/transfer_device_config FromUser';
			$parse_script = shell_exec($parse_path);

			if ($_SESSION['standalone_mode'] != '1') {
				// transfer script
				$transfer_path = 'sudo /usr/local/bin/transfer_device_config ToServer';
				$transfer_script = shell_exec($transfer_path);
			}

			// final repsonse to presets view
			$_SESSION['redirect_from_presets_upload'] = $response_string;
			header('Location:dsp.php');
		}
	}
}
if ($_REQUEST['s'] == 'get_preset_url') {
	if ($_REQUEST['p'] != '') {

		$connection = ssh2_connect('bogen-ssu.bogen.com', 22);
		if (!$connection) {
			//echo 1;
			// see if local file exists
			if (is_readable("/etc/nyquist/presets-overload.json")) {
				$local_file = fopen("/etc/nyquist/presets-overload.json", "r");
				$local_content = '';
				if (!$local_file) {
					echo 6;
				} else {
					while (! feof($local_file)) {
						$line = fgets($local_file);
						$local_content = $local_content . $line;
					}
					echo $local_content;
				}
				fclose($local_file);
			}
		} else {
			$password_auth = ssh2_auth_password($connection, 'ftp_bg_user', 's~jYh6g}zGwb');
			if (!$password_auth) {
				echo 2;
			} else {
				$sftp = ssh2_sftp($connection);
				if (!$sftp) {
					echo 3;
				} else {
					$stream = fopen("ssh2.sftp://$sftp/PRESET/presets-overload.json", 'r');
					if (!$stream) {
						echo 4;
					} else {
						$json_content = '';

						while (! feof($stream)) {
							$line = fgets($stream);
							$json_content = $json_content . $line;
						}

						// write presets-overload file to appliance
						if (!is_readable("/etc/nyquist/presets-overload.json")) {
							// create empty file, set permissions, set ownership
							shell_exec('sudo touch /etc/nyquist/presets-overload.json');
							shell_exec('sudo chown root:www-data /etc/nyquist/presets-overload.json');
							shell_exec('sudo chmod 664 /etc/nyquist/presets-overload.json');
						}
						$overload_file = fopen("/etc/nyquist/presets-overload.json", "w");

						if (!$overload_file) {
							echo 5;
						} else {
							// complete write
							fwrite($overload_file, $json_content);
							echo $json_content;
						}
						fclose($overload_file);
					}
				}
			}
		}
	}
}
if ($_REQUEST['s'] == 'backup_presets') {

	$chmod_preset = shell_exec('sudo chmod 775 /etc/nyquist/preset');
	$chown_preset = shell_exec('sudo chown root:www-data /etc/nyquist/preset');

	// find presets
	$exclude = shell_exec('find /etc/nyquist/preset/ -group root');
	$exclude = str_replace("/etc/nyquist/preset/", "--exclude=", $exclude);
	$exclude = preg_replace('/\s+/', ' ', $exclude);
	$exclude = trim($exclude);

	// backup presets
	$tar = shell_exec('sudo tar czPf /etc/nyquist/presets.tgz ' . $exclude . ' /etc/nyquist/preset');
	$chmod = shell_exec('sudo chmod 664 /etc/nyquist/presets.tgz');
	$chown = shell_exec('sudo chown root:www-data /etc/nyquist/presets.tgz');

	$parse_path = 'sudo /usr/local/bin/transfer_device_config FromUser';
	$parse_script = shell_exec($parse_path);

	if ($_SESSION['standalone_mode'] != '1') {
		// transfer script
		$transfer_path = 'sudo /usr/local/bin/transfer_device_config ToServer';
		$transfer_script = shell_exec($transfer_path);
	}
}
if ($_REQUEST['s'] == 'get_preset_text') {

	$path = '/etc/nyquist/preset';
	$files = array_diff(scandir($path), array('.', '..'));
	$preset_name = $_REQUEST['f'];
	$found_path = '';

	foreach ($files as $filename) {

		$filepath = '/etc/nyquist/preset/' . $filename;
		$contents = file_get_contents($filepath);
		$dataArray = json_decode($contents);
		$preset = $dataArray->preset;
		$name = $preset->Name;

		if ($name == $preset_name) {
			$filename = str_replace(" ", "\ ", $filename);
			$found_path = '/etc/nyquist/preset/' . $filename;
		}
	}
	if ($found_path != '') {
		system('cat ' . $found_path);
	}
}
if ($_REQUEST['s'] == 'save_mqtt_password') {

	$mqtt_password = $_REQUEST['p'];

	$script_path = 'sudo /usr/local/bin/update_mqtt_password "' . $mqtt_password . '"';
	$run_script = shell_exec($script_path);

	echo 1;
}
if ($_REQUEST['s'] == 'show_mqtt_password') {

	$script_path = 'cat /etc/mosquitto/mqtt_pwd';
	$run_script = shell_exec($script_path);

	echo $run_script;
}
if ($_REQUEST['s'] == 'get_relay_trigger_status') {

	$device_type = $_REQUEST['d'];

	if ($device_type == '1') {
		$script_path = 'cat /sys/class/gpio/gpio68/value';
		$run_script = shell_exec($script_path);
		echo $run_script;
	}
	if ($device_type == '2') {
		$script_path = 'cat /sys/class/gpio/gpio44/value';
		$run_script = shell_exec($script_path);
		echo $run_script;
	}
	if ($device_type == '3') {
		$script_path = 'cat /sys/class/gpio/gpio69/value';
		$run_script = shell_exec($script_path);
		echo $run_script;
	}
	if ($device_type == '4') {
		$script_path = 'cat /sys/class/gpio/gpio45/value';
		$run_script = shell_exec($script_path);
		echo $run_script;
	}
}
if ($_REQUEST['s'] == 'enable_disable_volume') {

	$data = $_REQUEST['d'];

	if ($data == '1') {
		if (file_exists('/etc/nyquist/slider_enabled')) {
		} else {
			$script_path1 = 'sudo touch /etc/nyquist/slider_enabled';
			$run_script1 = shell_exec($script_path1);

			$script_path2 = 'sudo systemctl restart appliance';
			$run_script2 = shell_exec($script_path2);

			echo 1;
		}
	} else {
		$script_path1 = 'sudo rm /etc/nyquist/slider_enabled';
		$run_script1 = shell_exec($script_path1);

		$script_path2 = 'sudo systemctl restart appliance';
		$run_script2 = shell_exec($script_path2);

		echo 0;
	}
}
if ($_REQUEST['s'] == 'file_check') {

	if (file_exists('/etc/nyquist/slider_enabled')) {
		echo 1;
	} else {
		echo 0;
	}
}
if ($_REQUEST['s'] == 'get_ntc_status_text') {

	$response = '';

	if (file_exists('/tmp/nqnetcap.log')) {
		// run script
		$script_path = 'cat /tmp/nqnetcap.log';
		$response = shell_exec($script_path);
	}

	echo $response;
}
if ($_REQUEST['s'] == 'start_ntc') {

	$data = $_REQUEST['d'];

	$decoded_data =  base64_decode($data);
	$dataArray = explode(',', $decoded_data);
	$ntc_capture_seconds = $dataArray[0];
	$ntc_options = $dataArray[1];
	$ntc_expression = $dataArray[2];

	if ($ntc_capture_seconds != '') {

		$capture_seconds_file = '/tmp/nqnetcap-seconds';

		if (file_exists($capture_seconds_file)) {
		} else {
			shell_exec('touch /tmp/nqnetcap-seconds');
		}
		$handle_seconds = fopen($capture_seconds_file, 'w') or die('Cannot open file:  ' . $capture_seconds_file);
		fwrite($handle_seconds, $ntc_capture_seconds);
		fclose($handle_seconds);
	}

	if ($ntc_options != '') {

		$ntc_options_file = '/tmp/nqnetcap-options';

		if (file_exists($ntc_options_file)) {
		} else {
			shell_exec('touch /tmp/nqnetcap-options');
		}
		$handle_options = fopen($ntc_options_file, 'w') or die('Cannot open file:  ' . $ntc_options_file);
		fwrite($handle_options, $ntc_options);
		fclose($handle_options);
	}

	if ($ntc_expression != '') {

		$ntc_expression_file = '/tmp/nqnetcap-expression';

		if (file_exists($ntc_expression_file)) {
		} else {
			shell_exec('touch /tmp/nqnetcap-expression');
		}
		$handle_expression = fopen($ntc_expression_file, 'w') or die('Cannot open file:  ' . $ntc_expression_file);
		fwrite($handle_expression, $ntc_expression);
		fclose($handle_expression);
	}

	// run script
	session_write_close();
	$script_path = 'sudo /usr/local/bin/nqnetcap';
	$response = shell_exec($script_path);  //  1:Success   0:Error message to display to user

	echo trim($response);
}
if ($_REQUEST['s'] == 'stop_ntc') {

	$ntc_stop_file = '/tmp/nqnetcap-stop';

	if (file_exists($ntc_stop_file)) {
	} else {
		shell_exec('touch /tmp/nqnetcap-stop');
	}
	$handle_stop = fopen($ntc_stop_file, 'w') or die('Cannot open file:  ' . $ntc_stop_file);
	fwrite($handle_stop, 'STOP');
	fclose($handle_stop);

	echo 1;
}
if ($_REQUEST['s'] == 'delete_pcap') {

	$pcap_file = '/var/www/html/exports/nqnetcap.pcap';

	if (file_exists($pcap_file)) {
		unlink($pcap_file);
	}

	echo 1;
}
if ($_REQUEST['s'] == 'stat_pcap') {

	$response = '';

	$pcap_file = '/var/www/html/exports/nqnetcap.pcap';

	if (file_exists($pcap_file)) {

		// run script
		$script_path = 'stat --printf="%s" ' . $pcap_file;
		$response = shell_exec($script_path);

		if ($response <= 1000) {
			$response = $response . ' bytes';
		} else if ($response <= 1000000) {
			$response = round($response / 1000) . ' KB';
		} else if ($response <= 1000000000) {
			$response = round($response / 1000000) . ' MB';
		} else if ($response <= 1000000000000) {
			$response = round($response / 1000000000) . ' GB';
		}
	}

	echo $response;
}
if ($_REQUEST['s'] == 'ntc_check') {

	$response = 0;

	$ntc_check_file = '/tmp/nqnetcap-running';

	if (file_exists($ntc_check_file)) {
		$response = 1;
	}

	echo $response;
}
