<?php

session_start();

$message = '';
$config_file = '';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
	
	$errorArray = array();
	$message = '';
	
	// get login info
	  $web_username = '';
	  $web_password = '';
	  $device_name = '';
	  $device_type = '';
	
      // load config file and read in data
      if(file_exists('/etc/nyquist/standalone.config')){
          $config_file = fopen("/etc/nyquist/standalone.config", "r");
		  $_SESSION['standalone_mode'] = '1';
      } else {
          $config_file = fopen("/etc/nyquist/SIP.config", "r");
		  $_SESSION['standalone_mode'] = '0';
      }
	  if(!$config_file){
		  // show error message
		  $message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
		  $message .= 'There was a problem reading the Configuration file.';
		  $message .= '</div></div>';
	  } else { 
	  	 // go through the config file one line at a time
		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);
			}
			if(strpos($line_text, 'WEB_PASSWORD =') !== FALSE){
				$web_password = str_replace("WEB_PASSWORD = ","",$line_text);
				$web_password = trim($web_password);
			}
			if(strpos($line_text, 'DEVICE_NAME =') !== FALSE){
				$device_name = str_replace("DEVICE_NAME = ","",$line_text);
				$device_name = trim($device_name);
			}
			if(strpos($line_text, 'DEVICE_TYPE =') !== FALSE){
				$device_type = str_replace("DEVICE_TYPE = ","",$line_text);
				$device_type = trim($device_type);
			}
		}
		fclose($config_file);
		
		if (empty($_POST["web_username"]) || empty($_POST["web_password"])) {
    		$errorArray[] = "Username and password are required.";
  		}
		
		if(($_POST["web_username"]!=$web_username || $_POST["web_password"]!=$web_password) && !empty($_POST["web_username"]) && !empty($_POST["web_password"])){
			$errorArray[] = "Invalid login.";
		}
		
	  }
	
	if(count($errorArray)>0){
		// show error messages
		$message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
		foreach($errorArray as $error){
			$message .= $error . '<br>';
		}
		$message .= '</div></div><br>';
	} else {
		$_SESSION['device_name'] = $device_name;
		 // grab the device_type name from the environmental variable if it's not in the config file
		if($device_type==''){
			$_SESSION['device_type'] = trim($_ENV['APPLIANCE_BOARD']);
			$_SESSION['device_type'] =  str_replace('"','', $_SESSION['device_type']); 
		} else {
			$_SESSION['device_type'] = $device_type;
		}
		$_SESSION['login'] = true;
		if($web_password=='bogen'){
			$_SESSION['show_login_alert'] = '1';
			$_SESSION['bogen_default_password'] = '1';
		} else {
			$_SESSION['show_login_alert'] = '0';
			$_SESSION['bogen_default_password'] = '0';
		}
		header("Location: index.php");
	}
} else {
	// get device name
	if(file_exists('/etc/nyquist/standalone.config')){
          $config_file = fopen("/etc/nyquist/standalone.config", "r");
		  $_SESSION['standalone_mode'] = '1';
      } else {
          $config_file = fopen("/etc/nyquist/SIP.config", "r");
		  $_SESSION['standalone_mode'] = '0';
	} 
	  if(!$config_file){
		  // show error message
		  $message = '<div class="isa_error"><i class="fa fa-times-circle"></i><div class="message_content">';
		  $message .= 'There was a problem reading the Configuration file.';
		  $message .= '</div></div>';
	  } else { 
	  	 // go through the config file one line at a time
		while(!feof($config_file)) {
			
			$line_text = fgets($config_file);
				
			if(strpos($line_text, 'DEVICE_NAME =') !== FALSE){
				$device_name = str_replace("DEVICE_NAME = ","",$line_text);
				$device_name = trim($device_name);
			}
			if(strpos($line_text, 'DEVICE_TYPE =') !== FALSE){
				$device_type = str_replace("DEVICE_TYPE = ","",$line_text);
				$device_type = trim($device_type);
				$_SESSION['device_type'] = $device_type;
			}
		}
		fclose($config_file);
	
		// grab the device_type name from the environmental variable if it's not in the config file
		if($device_type==''){
			$device_type = trim($_ENV['APPLIANCE_BOARD']);
			$device_type = str_replace('"','', $device_type); 
			if($device_type == 'Nyquist S810V2'){
				$device_type = 'Nyquist S1810V2';
			}
			$_SESSION['device_type'] = $device_type;
		}  
		if(file_exists('/etc/nyquist/wbcdetected')){
			$device_type = 'NQ-S1810WBC VoIP Wall Baffle Combo Spkr/Clock/Msg';
			$_SESSION['device_type'] = 'NQ-S1810WBC VoIP Wall Baffle Combo Spkr/Clock/Msg';
		}
	  }
}
?>

<!doctype html>
<html>
<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" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo $device_type; ?> (<?php echo $device_name; ?>) - Login</title>
</head>

<body><br>
 <div class="container-fluid main">
      <div class="row">
        <div class="col-md-12"> 
        <div align="center">

<div id="message_container"><?php echo $message; ?></div>

<div class="panel panel-default" id="login_panel" style="width:600px">
    <div class="panel-heading"><h3><img id="logo" src="/img/nyquist_logo.png" width="300"><br>Login</h3><h4><?php echo $device_type;?><br><?php echo $device_name;?><?php if($_SESSION['standalone_mode']=='1'){ echo '<br>(Standalone Operation Enabled)';}?></h4></div>
    <div class="panel-body">
        <form class="form-horizontal" name="loginform" id="loginform" role="form" method="POST" action="login.php">
                    <h3><input type="text" class="form-control" name="web_username" id="web_username" value="" autofocus style="width:250px;font-size:20px;" placeholder="Username"></h3>
                    <h3><input type="password" class="form-control" name="web_password" id="web_password" value="" style="width:250px;font-size:20px;" placeholder="Password"></h3>
              <br><br>
             <button class="btn btn-primary" type="submit"><i class="fa fa-sign-in"></i> Login</button>
        </form>
    </div>
</div>

</div>
</div>
</div>
</div>
<script src="/js/jquery.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
</body>
</html>
<script>
$(document).ready(function() {
	// resize the login window smaller for mobile devices
	var isMobile = navigator.userAgent.match(/(iPhone|iPad|Android)/);
	var login_panel = document.getElementById('login_panel');
	var logo = document.getElementById('logo');
	if(isMobile){
		login_panel.style.width="320px";
		logo.style.width="300px";
	}
});
<!-- clear message after 5 seconds -->
setTimeout(function(){ 
	document.getElementById("message_container").innerHTML = '';
}, 5000);

</script>
