<?php if(!isset($db)) print 'Error';

function CurL($url){
     $session = curl_init(); 
     curl_setopt($session, CURLOPT_URL, $url);
     curl_setopt($session, CURLOPT_RETURNTRANSFER, 1);
     $hasil = curl_exec($session);
     curl_close($session);
     //print_r($hasil);
     $res = json_decode($hasil, true);
     return $res;
}
$ip = $_SERVER['REMOTE_ADDR'];
$sumber =  CurL('http://www.geoplugin.net/json.gp?ip='.$_SERVER['REMOTE_ADDR']);

if (isset($_POST['login'])) {
	$post_user = $db->real_escape_string(e(@$_POST['user']));
	$post_pass = $db->real_escape_string(e(@$_POST['pass']));
	
	$check_use = $db->query("SELECT * FROM users WHERE username = '{$post_user}'");
        $data_use = $check_use->fetch_assoc();
			
	if (!$post_user || !$post_pass) {
		$_SESSION['alert'] = ['danger', 'Failed!', 'Masih ada form kosong.'];
		exit(header('location: '.base_url('auth/login')));
	} else {
		$check_user = $db->query("SELECT * FROM users WHERE username = '{$post_user}'");
		if (mysqli_num_rows($check_user) == 0) {
			$_SESSION['alert'] = ['danger', 'Failed!', 'Akun tidak terdaftar.'];
			exit(header('location: '.base_url('auth/login')));
		} else {
			$data_user = $check_user->fetch_assoc();
			if (password_verify($post_pass, $data_user['password']) === false) {
				$_SESSION['alert'] = ['danger', 'Failed!', 'Invalid Password.'];
				exit(header('location: '.base_url('auth/login')));
			} else {
				$create_token = md5(uniqid(rand(0000000,0000000)));
				$db->query("UPDATE users SET cookie_token = '{$create_token}' WHERE username = '{$post_user}'");
				setcookie('cookie_token',$create_token, time() + (86400 * 90), '/','');
			    $address = $_SERVER['REMOTE_ADDR']; 
                                $login_at = date('Y-m-d H:i:s');
				$db->query("INSERT INTO user_notifications VALUES('','{$data_user['username']}', 'Melakukan Login','{$address}','{$_SERVER['HTTP_USER_AGENT']}', '".$sumber['geoplugin_city']." ".$sumber['geoplugin_countryCode']."', '', '','{$login_at}')");
				$db->query("INSERT INTO agent VALUES('', '{$device}','".get_client_browser()."')");
                                $db->query("UPDATE users SET token = '{$create_token}' WHERE username = '{$post_user}'");
				$_SESSION['user'] = $data_user;
				$_SESSION['alert'] = ['success', 'Success!', 'Welcome, '.e($data_user['username']).'.'];
				exit(header('location: /admin/'));
			    }
		    }
	     }
      }