<?php
session_start();
include "config.php";


 
$json_response = array();

//i only receive ajax request :D
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
	$data = array(
		'username'=>$_POST['username'],
		'password'=>md5($_POST['password'])
		);

		$dt=$db->fetch_custom_single('select sys_users.*,id_sms from sys_users left join jurusan on sys_users.kode_jurusan=jurusan.kode_jurusan where username=?',array('username' => $_POST['username']));
		$check = $db->check_exist('sys_users',$data);
		if ($check==true) {
			if ($dt->id_group!=1) {
				//check again if not admin
				$data_user = array(
				'username'=>$_POST['username'],
				'password'=>md5($_POST['password'])
				);
				$checks = $db->check_exist('sys_users',$data_user);
					if ($checks==true) {
						$_SESSION['nama_lengkap'] = $dt->first_name." ".$dt->last_name;
						$_SESSION['level']=$dt->id_group;
						$_SESSION['id_user']=$dt->id;
						$_SESSION['jurusan'] = $dt->kode_jurusan;
						$_SESSION['id_sms'] = $dt->id_sms;
						$_SESSION['logins']=1;
						$status['status'] = "good";

					} 
			} else {
				//if admin, just go on
				$_SESSION['nama_lengkap'] = $dt->first_name." ".$dt->last_name;
				$_SESSION['level']=$dt->id_group;
				$_SESSION['id_user']=$dt->id;
				$_SESSION['logins']=1;
				$status['status'] = "good";
			}

		} else {
							$status['status'] = "bad";
						}

} else {
	//hei , don't ever try if you're not ajax request, because you gonna die
	$status['status'] = "go out dude";
}


array_push($json_response, $status);
echo json_encode($json_response);


?>
