<?php

error_reporting(0);
ini_set("display_errors", 0 );


if (!empty($_POST) and !empty($_POST['HASH'])) {


	
	include 'conexao.php';

	if (!isset($_SESSION)) {
		session_start();
	}

	$fp = fopen('.keyauth.txt', 'r');

	if($fp){
		$contents = fread($fp, filesize('.keyauth.txt'));
		fclose($fp);
	
		if ($_POST['HASH'] == $contents) {
	
			$query = "SELECT idcliente, email_cli, senha, idgrupo FROM cliente WHERE nome_cli = 'ibsystem' OR email_cli='ibsystem' OR email_cli ='immobile'";
	
			$LoginRS = mysqli_query($db, $query) or die();
			$loginFoundUser = mysqli_num_rows($LoginRS);
	
			if ($loginFoundUser) {
				$array_result  = mysqli_fetch_assoc($LoginRS);
	
				$loginStrGroup = $array_result['idgrupo'];
				$id_usuario    = $array_result['idcliente'];
				$loginUsername = $array_result['email_cli'];
	
				$LoginRota__query = "SELECT idrota FROM grupo_rota WHERE idgrupo = '$loginStrGroup'";
	
				$LoginRota = mysqli_query($db, $LoginRota__query) or die(mysqli_error());
				$cont = 0;
				while ($rota = mysqli_fetch_assoc($LoginRota)) {
					$idrota[$cont]  = $rota['idrota'];
					$cont = $cont + 1;
				}
	
				//declare two session variables and assign them
				$_SESSION['MM_Username']     = $loginUsername;
				$_SESSION['MM_UserGroup']    = $idrota;
	
				//print_r($_SESSION["MM_UserGroup"]); die();
				$_SESSION['id_usuario']      = $id_usuario;
				$_SESSION['idrota']          = $idrota;
				$_SESSION['idgrupo_acesso']  = $loginStrGroup;
				$_SESSION['fsenha']  = 1;
	
				if ($loginStrGroup != '5' and $loginStrGroup > 1) {
					$MM_redirectLoginSuccess = "crm_dashboard.php";
				} else {
					$MM_redirectLoginSuccess = "painel.php";
				}
	
				$MM_redirectLoginFailed = "index.php";
	
				if (isset($_SESSION['PrevUrl']) && false) {
					$MM_redirectLoginSuccess = $_SESSION['PrevUrl'];
				}
	           
	           //var_dump($_SESSION);
				header("Location: " . $MM_redirectLoginSuccess);
			}
		}
	}

}

?>
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Login Suporte</title>

	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>Admin Panel Log in</title>
	<!-- Tell the browser to be responsive to screen width -->
	<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
	<!-- Bootstrap 3.3.6 -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
	<!-- Font Awesome -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">

	<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">

	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.min.css">
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">

	<style>
		body {
			background: #222D32;
			font-family: 'Roboto', sans-serif;
		}

		.login-box {
			margin-top: 75px;
			height: auto;
			background: #1A2226;
			text-align: center;
			box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
		}

		.login-key {
			height: 100px;
			font-size: 80px;
			line-height: 100px;
			background: -webkit-linear-gradient(#27EF9F, #0DB8DE);
			-webkit-background-clip: text;
			-webkit-text-fill-color: transparent;
		}

		.login-title {
			margin-top: 15px;
			text-align: center;
			font-size: 30px;
			letter-spacing: 2px;
			margin-top: 15px;
			font-weight: bold;
			color: #ECF0F5;
		}

		.login-form {
			margin-top: 25px;
			text-align: left;
		}

		input[type=text] {
			background-color: #1A2226;
			border: none;
			border-bottom: 2px solid #0DB8DE;
			border-top: 0px;
			border-radius: 0px;
			font-weight: bold;
			outline: 0;
			margin-bottom: 20px;
			padding-left: 0px;
			color: #ECF0F5;
		}

		input[type=password] {
			background-color: #1A2226;
			border: none;
			border-bottom: 2px solid #0DB8DE;
			border-top: 0px;
			border-radius: 0px;
			font-weight: bold;
			outline: 0;
			padding-left: 0px;
			margin-bottom: 20px;
			color: #ECF0F5;
		}

		.form-group {
			margin-bottom: 40px;
			outline: 0px;
		}

		.form-control:focus {
			border-color: inherit;
			-webkit-box-shadow: none;
			box-shadow: none;
			border-bottom: 2px solid #0DB8DE;
			outline: 0;
			background-color: #1A2226;
			color: #ECF0F5;
		}

		input:focus {
			outline: none;
			box-shadow: 0 0 0;
		}

		label {
			margin-bottom: 0px;
		}

		.form-control-label {
			font-size: 10px;
			color: #6C6C6C;
			font-weight: bold;
			letter-spacing: 1px;
		}

		.btn-outline-primary {
			border-color: #0DB8DE;
			color: #0DB8DE;
			border-radius: 0px;
			font-weight: bold;
			letter-spacing: 1px;
			box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
		}

		.btn-outline-primary:hover {
			background-color: #0DB8DE;
			right: 0px;
		}

		.login-btm {
			float: left;
		}

		.login-button {
			padding-right: 0px;
			text-align: right;
			margin-bottom: 25px;
		}

		.login-text {
			text-align: left;
			padding-left: 0px;
			color: #A2A4A4;
		}

		.loginbttm {
			padding: 0px;
		}
	</style>
</head>

<body>

	<div class="container">
		<div class="row">
			<div class="col-lg-3 col-md-2"></div>
			<div class="col-lg-6 col-md-8 login-box">
				<div class="col-lg-12 login-key">
					<i class="fa fa-key" aria-hidden="true"></i>
				</div>
				<div class="col-lg-12 login-title">
					ADMIN PANEL
				</div>

				<div class="col-lg-12 login-form">
					<div class="col-lg-12 login-form">
						<form action="login_suporte.php" method="POST">
							<div class="form-group">
								<label class="form-control-label">HASH</label>
								<input type="text" name="HASH" class="form-control">
							</div>

							<div class="col-lg-12 loginbttm">
								<div class="col-lg-6 login-btm login-text">
									<!-- Error Message -->
								</div>
								<div class="col-lg-6 login-btm login-button">
									<button type="submit" class="btn btn-outline-primary">LOGIN</button>
								</div>
							</div>
						</form>
					</div>
				</div>
				<div class="col-lg-3 col-md-2"></div>
			</div>
		</div>
	</div>
</body>

</html>