<?php
session_start();
header('Content-type: application/json');
include_once '../db/db.php';
$response = array();

if ($_POST) {
	$user = trim($_POST['user']);
	$pass = trim($_POST['password']);
	
	$uname = htmlspecialchars(strip_tags($user), ENT_QUOTES, 'UTF-8');
	$upass = htmlspecialchars(strip_tags($pass), ENT_QUOTES, 'UTF-8');
	
	$password = hash('sha256', $upass);
	
	$sql = mysqli_query($conn, "SELECT * FROM user_acc_tbl WHERE userID='$uname' and access=1 and val=1");	
	$row = mysqli_fetch_array($sql);
	
	if(mysqli_num_rows($sql) == 1) {
		if($row['pass']== $password){
			$response['status'] = 'ok'; // log in
			$_SESSION['userID'] = $row['userID'];
		
		} else{
			$response['status'] = 'error';
			 // wrong details
			$response['message'] = '<span class="glyphicon glyphicon-info-sign"></span> &nbsp; Username or Password does not match.'; 
		}
	}
	
	else {
		$response['status'] = 'error';
		// wrong details
		$response['message'] = '<span class="glyphicon glyphicon-info-sign"></span> &nbsp; Username or Password does not match.'; 
	}
	
}

echo json_encode($response);
?>