<?php

session_start();

if(!$_SESSION['login']){
	header("Location: login.php");
}

?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="">
<meta name="author" content="Bogen Communications LLC">
<meta name="copyright" content="Bogen Communications LLC">
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests" />
<link rel="shortcut icon" href="">
<link rel="stylesheet" href="/css/styles.css">
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/font-awesome/css/font-awesome.min.css">
<link rel="icon" type="image/png" href="/favicon.png" />
<title><?php echo $_SESSION['device_type']; ?> (<?php echo $_SESSION['device_name']; ?>) - Firmware Update</title>
</head>
<body>
<div class="container">

<?php include 'navbar.php';?>

<h4><strong><i class="fa fa-download"></i> Firmware Update</strong>&nbsp;&nbsp;<a href="/help/<?php echo $_SESSION['device_id']; ?>/#t=<?php echo $_SESSION['help_firmware']; ?>" type="button" class="btn btn-xs btn-primary" target="_blank"><i class="fa fa-question-circle"></i></a></h4>
<div id="status"></div>
<div id="message_container"></div>
<div id="hide_button" style="display: none;">
<button type="button" class="btn btn-xs btn-primary" style="margin-bottom:10px;" name="updateFirmwareButton" id="updateFirmwareButton" onclick="confirm_firmware()"><i class="fa fa-download"></i>&nbsp;&nbsp;Update Firmware</button>
<br></div>
<button type="button" class="btn btn-xs btn-primary" style="margin-bottom:10px;" name="uploadButton" id="uploadButton" onclick="upload_button()"><i class='fa fa-upload'></i>&nbsp;&nbsp;Upload Firmware</button>
<br>
<?php if($_SESSION['standalone_mode']=='1'){ ?>
<button type="button" class="btn btn-xs btn-primary" style="margin-bottom:10px;" name="updatesButton" id="updatesButton" onclick="check_for_updates()"><i class='fa fa-download'></i>&nbsp;&nbsp;Check for Updates</button>
<br>
<?php } ?>
<button type="button" class="btn btn-xs btn-primary" style="margin-bottom:10px;" name="restoreFactorySettingsButton" id="restoreFactorySettingsButton" onclick="confirm_restore()"><i class="fa fa-rotate-left"></i>&nbsp;&nbsp;Restore Factory Settings</button>
<br>
<button type="button" class="btn btn-xs btn-primary" style="margin-bottom:10px;" name="rebootButton" id="rebootButton" onclick="confirm_reboot()"><i class="fa fa-power-off"></i>&nbsp;&nbsp;Reboot Appliance</button>
<br>
<!--  start firmware confirmation modal -->
<div class="modal fade bs-example-modal-sm modal-firmware-confirmation" id="modal-restart-server" tabindex="-1" role="dialog" aria-labelledby="modal-firmware-confirmation">
	<div class="modal-dialog">
	<div class="modal-content">
			 <div class="modal-header">
			<button type="button" class="close" onClick="clear_script_response();" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>        							
			<h4 class="modal-title"><p align="center">Update Firmware</p></h4>
			</div>
			<div align="center" class="modal-body">

				<div style="text-align:center; width:100%;">
				Are you sure you want to Update the Firmware?

				</div>
			</div>
			<div class="modal-footer">        							
			<button type="button" class="btn btn-xs btn-default" data-dismiss="modal">Close</button>
			<button  type="submit" name="updateFirmware" id="updateFirmware" onClick="update_firmware()" class="btn btn-xs btn-primary" style="margin:10px;"><i class="fa fa-download"></i>&nbsp;&nbsp;Update Firmware</button>
			</div>
	</div>  							
	</div>
	</div>
<!--  end firmware confirmation modal -->

<!--  start restore factory settings confirmation modal -->
<div class="modal fade bs-example-modal-sm modal-restore-confirmation" id="modal-restart-server" tabindex="-1" role="dialog" aria-labelledby="modal-restore-confirmation">
	<div class="modal-dialog">
	<div class="modal-content">
			 <div class="modal-header">
			<button type="button" class="close" onClick="clear_script_response();" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>        							
			<h4 class="modal-title"><p align="center">Restore Factory Settings</p></h4>
			</div>
			<div align="center" class="modal-body">

				<div style="text-align:center; width:100%;">
				This is a complete reset and all user settings will be lost.
				<br><br>
				Are you sure you want to Restore Factory Settings?
				</div>
			</div>
			<div class="modal-footer">        							
			<button type="button" class="btn btn-xs btn-default" data-dismiss="modal">Close</button>
			<button  type="submit" name="restoreSettings" id="restoreSettings" onClick="restore_default_firmware()" class="btn btn-xs btn-primary" style="margin:10px;"><i class="fa fa-rotate-left"></i>&nbsp;&nbsp;Restore Factory Settings</button>
			</div>
	</div>  							
	</div>
	</div>
<!--  end restore factory settings  confirmation modal -->

<!--  start reboot appliance confirmation modal -->
<div class="modal fade bs-example-modal-sm modal-reboot-confirmation" id="modal-restart-server" tabindex="-1" role="dialog" aria-labelledby="modal-reboot-confirmation">
<div class="modal-dialog">
<div class="modal-content">
		 <div class="modal-header">
		<button type="button" class="close" onClick="clear_script_response();" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>        							
		<h4 class="modal-title"><p align="center">Reboot Appliance</p></h4>
		</div>
		<div align="center" class="modal-body">

			<div style="text-align:center; width:100%;">
			Are you sure you want to Reboot the Appliance?

			</div>
		</div>
		<div class="modal-footer">        							
		<button type="button" class="btn btn-xs btn-default" data-dismiss="modal">Close</button>
		<button  type="submit" name="rebootAppliance" id="rebootAppliance" onClick="reboot_now()" class="btn btn-xs btn-primary" style="margin:10px;"><i class="fa fa-power-off"></i>&nbsp;&nbsp;Reboot Appliance</button>
		</div>
</div>  							
</div>
</div>
<!--  end reboot appliance confirmation modal -->

<!--  start upload modal -->
<div id="uploadModal" class="modal" tabindex="-1" role="dialog" aria-labelledby="uploadModal" data-backdrop="static" data-keyboard="false">
	<div class="modal-dialog">
	<div class="modal-content">
			 <div class="modal-header">       							
			<h4 class="modal-title"><p align="center">Upload Firmware</p></h4>
			</div>
			<form method="POST" action="/scripts.php?s=upload_firmware" class="form-horizontal" enctype="multipart/form-data">
			<input type="hidden" name="_token" value="{{ csrf_token() }}">
			<div align="center" class="modal-body">
				<div style="text-align:center; width:60%;">
					<input id="fileupload" name="fileupload" type="file" class="filestyle" data-buttonName="btn-primary" data-buttonText="&nbsp;Choose file" data-size="sm" data-placeholder="No file selected">
				</div>
			</div>
			<div class="modal-footer">        							
			<button type="button" class="btn btn-xs btn-default" data-dismiss="modal">Close</button>
			<button  type="submit" name="uploadButtonModal" id="uploadButtonModal" onClick="upload_action();this.disabled=true;forms[0].submit();" class="btn btn-xs btn-primary"><span id="upload_spinner"><i class='fa fa-upload'></i>&nbsp;&nbsp;Upload</span></button>
			</div>
		  </form>
	</div>  							
	</div>
	</div>
<!--  end upload modal -->
	
<!--start updates modal -->
 <div id="updatesModal" class="modal bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="updatesModal" data-backdrop="static" data-keyboard="false">
  <div class="modal-dialog">
    <div class="modal-content">
    	<div class="modal-header">
                    <h4 class="modal-title"><p align="center"><p align="center">Check For Firmware Updates</p></p></h4>
                </div>
        <div class="modal-body">
			<p id="updates_text" align="center"></p>
        </div>
        <div class="modal-footer">
			<button id="okButton" type="button" class="btn btn-primary" onClick="location.reload();">Ok</button>
        </div>
  </div>
</div>
</div>
 <!--end updates Modal -->

<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
</body>
</html>
<script>

// run on page load 
document.getElementById("message_container").innerHTML = '';
document.getElementById("status").innerHTML = '<i class="fa fa-refresh fa-spin fa-fw"></i> <strong>Getting Firmware Status ...</strong><br><br>';
get_firmware_status();
var statusTimer;
 
function get_firmware_status(){
	
	var xmlhttp=new XMLHttpRequest();
    	xmlhttp.onreadystatechange=function() {
        	if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				// success 
				//alert(xmlhttp.responseText);
				
				var dataArray = xmlhttp.responseText.split("|");
				var old_version = dataArray[0];
				var new_version = dataArray[1];
				
				var status = 'Current Nyquist Version: ' + old_version + '<br>New Nyquist Version: ' + new_version;
				
				document.getElementById("status").innerHTML = '';
				document.getElementById("message_container").innerHTML = '<div class="isa_success"><i class="fa fa-check"></i><div class="message_content">' + status + '<br></div></div><br>';
				
				if(new_version.includes("None")){
					document.getElementById("hide_button").style.display="none";
				} else {
					document.getElementById("hide_button").style.display="block";
				}
        	}
		}
	 	xmlhttp.open("GET","scripts.php?s=get_firmware_status",true);
   		xmlhttp.send();
}
	
function status_timer(){
	
	var xmlhttp=new XMLHttpRequest();
    	xmlhttp.onreadystatechange=function() {
        	if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				window.open("firmware.php", "_self");
        	} else {
				statusTimer = setTimeout(status_timer, 30000);
			}
		}
	 	xmlhttp.open("GET","scripts.php?s=get_firmware_status",true);
   		xmlhttp.send();
}

function confirm_firmware(){
	
	$('.modal-firmware-confirmation').modal('show');
}

function update_firmware(){
	
	$('.modal-firmware-confirmation').modal('hide');
	document.getElementById("hide_button").style.display="none";
	
	document.getElementById("message_container").innerHTML = '';
		document.getElementById("status").innerHTML = '<i class="fa fa-refresh fa-spin fa-fw"></i> <strong>Processing ... It may take a few moments to apply the update.</strong><br><br>';
	
	var xmlhttp=new XMLHttpRequest();
    	xmlhttp.onreadystatechange=function() {
			if(xmlhttp.readyState==4){
			  statusTimer = setTimeout(status_timer, 30000);
			}
		}
	 	xmlhttp.open("GET","scripts.php?s=update_firmware",true);
   		xmlhttp.send();
}

function confirm_restore(){
	
	$('.modal-restore-confirmation').modal('show');

}

function restore_default_firmware(){
	
	$('.modal-restore-confirmation').modal('hide');
	
	document.getElementById("message_container").innerHTML = '';
	document.getElementById("status").innerHTML = '<i class="fa fa-refresh fa-spin fa-fw"></i> <strong>Processing ... It may take a few moments to Restore Factory Settings.</strong><br><br>';
	  
	var xmlhttp=new XMLHttpRequest();
    	xmlhttp.onreadystatechange=function() {
        	if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				// success 
				//alert(xmlhttp.responseText);
				
				document.getElementById("status").innerHTML = '';
				document.getElementById("message_container").innerHTML = '<div class="isa_success"><i class="fa fa-check"></i><div class="message_content">' + xmlhttp.responseText + '<br></div></div><br>';
        	}
		}
	 	xmlhttp.open("GET","scripts.php?s=restore_default_firmware",true);
   		xmlhttp.send();
}

function confirm_reboot(){
	
	$('.modal-reboot-confirmation').modal('show');
}

function reboot_now(){
	
	document.getElementById("message_container").innerHTML = '';
	$('.modal-reboot-confirmation').modal('hide');
	
	document.getElementById("status").innerHTML = '<i class="fa fa-refresh fa-spin fa-fw"></i> <strong>Appliance Rebooting ...</strong><br><br>';
	
	setTimeout(function(){ 
		window.open("firmware.php", "_self");
	}, 10000);
	  
	var xmlhttp=new XMLHttpRequest();
    	xmlhttp.onreadystatechange=function() {
        	if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				// success 
				//alert(xmlhttp.responseText);
        	}
		}
	 	xmlhttp.open("GET","scripts.php?s=reboot_now",true);
   		xmlhttp.send();
}

function check_for_updates(){
	
	document.getElementById("updates_text").innerHTML = '<i class="fa fa-refresh fa-spin fa-fw"></i> Checking for Firmware Updates ...'; 
	document.getElementById('okButton').style.visibility='hidden';
	$('#updatesModal').modal('show');
	
	var xmlhttp=new XMLHttpRequest();
    	xmlhttp.onreadystatechange=function() {
        	if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				// success 
				var response = xmlhttp.responseText.trim();
				//console.log(response);
                if(response=='0'){
                    document.getElementById("updates_text").innerHTML = 'No firmware updates available.';
                    document.getElementById('okButton').style.visibility='visible';
                } else if(response=='-1'){
                    document.getElementById("updates_text").innerHTML = 'Can\'t check for firmware updates, check Internet connection and try again.';
                    document.getElementById('okButton').style.visibility='visible';
                } else if(response=='1'){
                   document.getElementById("updates_text").innerHTML = '<i class="fa fa-refresh fa-spin fa-fw"></i> Downloading Firmware Update ...';
                    download_firmware_updates();
                } else {
                    $('#updatesModal').modal('hide');
                }
        	}
		}
	 	xmlhttp.open("GET","scripts.php?s=check_for_updates",true);
   		xmlhttp.send();
}
function download_firmware_updates(){
	
	// ajax action to call api so screen does not refresh
	var xmlhttp=new XMLHttpRequest();
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			// success 
			var response = xmlhttp.responseText.trim();
			//console.log(response);
			if(response=='0'){
				document.getElementById("updates_text").innerHTML = 'Firmware Update download failed, try again.'; 
				document.getElementById('okButton').style.visibility='visible';
			} else if(response=='1'){
				document.getElementById("updates_text").innerHTML = 'Firmware Update download successful.';
				document.getElementById('okButton').style.visibility='visible'; 
			} else {
				$('#updatesModal').modal('hide');	  
			}
		}
	}
	xmlhttp.open("GET","scripts.php?s=download_firmware_updates",true);
	xmlhttp.send();
}
function upload_button(){
	$('#uploadModal').modal('show');		
}
function upload_action(){
	// show spinner
	document.getElementById("upload_spinner").innerHTML = '<i class="fa fa-refresh fa-spin fa-fw"></i>&nbsp&nbsp;Uploading ...';
}
</script>