<?php
include "inc/inc.koneksi.php";

function sukses_masuk($username,$pass){
	$login=mysql_query("SELECT * FROM admins WHERE username='$username' ");
	$ketemu =mysql_num_rows($login);
	$r=mysql_fetch_assoc($login);
	
	if($ketemu>0){
	    date_default_timezone_set('Asia/Jakarta');
	    
	    $status =$r['status'];
	    $jamawl =$r['jam_awal'];
	    $jamakh =$r['jam_akhir'];
	    $jam	=date('H:i:s');
	    
	    $sql    =mysql_query("SELECT * FROM admins WHERE username='$username'");
	    $r      =mysql_fetch_assoc($sql);
	    $kodecab=$r['kode_cab'];
	    
	    if($status=='DISABLE') disable();
	    
	    if(!($jamawl=='00:00:00')){
	        if($jam<$jamawl) early_time();
	        if($jam>$jamakh) over_time();
	    }
	    
	    $sql1   = mysql_query("SELECT * FROM cabang WHERE kode_cab='$kodecab'");
	    $r1     = mysql_fetch_assoc($sql1);
	    $namacab= $r1['nama_cab'];
	    
		session_start();
		include "timeout.php";
	
		$_SESSION['namauser']   = $r['username'];
		$_SESSION['kodecab']   	= $kodecab;
		$_SESSION['namacab']   	= $namacab;
		$_SESSION['namalengkap']= $r['nama_lengkap'];
		$_SESSION['passuser']   = $r['password'];
		$_SESSION['leveluser']  = $r['level'];
		
		// session timeout
		$_SESSION['login'] = 1;
		timer();
		
		$ipaddress = empty($_SERVER['HTTP_CLIENT_IP'])?(empty($_SERVER['HTTP_X_FORWARDED_FOR'])? $_SERVER['REMOTE_ADDR']:$_SERVER['HTTP_X_FORWARDED_FOR']):$_SERVER['HTTP_CLIENT_IP'];
	
		mysql_query("UPDATE admins SET lastlogin=now(),ipaddress='$ipaddress' WHERE username='$username' ");
	
		if($r['level']=="Supervisor"){
			header('location:media_spv.php?module=home');
		}else if($r['level']=="Direktur"){
			header('location:media_dir.php?module=home');
		}else if($r['level']=="Admin"){
			header('location:media_adm.php?module=home');
		}else if($r['level']=="Kepala Toko"){
			header('location:media_kpt.php?module=home');
		}else if($r['level']=="Kasir"){
			header('location:media_kas.php?module=home');
		}else if($r['level']=="Gudang"){
			header('location:media_gdg.php?module=home');
		}
	}
}

function msg(){
  echo "<link href='css/screen.css' rel='stylesheet' type='text/css'>
  <link href='css/reset.css' rel='stylesheet' type='text/css'>
  <link href='css/style_button.css' rel='stylesheet' type='text/css'>
  <center><br><br><br><br><br><br>Maaf, silahkan cek kembali <b>Username</b> dan <b>Password</b> Anda<br><br>Kesalahan $_SESSION[salah]";
  echo "<div> <a href='../index.php'><img src='images/kunci.png' height=176 width=143></a></div>";
  echo "<input type=button class='button buttonblue mediumbtn' value='KEMBALI' onclick=location.href='index.php'></a></center>";
  return false;
}

function salah_blokir($username){
  echo "<link href='css/screen.css' rel='stylesheet' type='text/css'>
  <link href='css/reset.css' rel='stylesheet' type='text/css'>
  <link href='css/style_button.css' rel='stylesheet' type='text/css'>
  <center><br><br><br><br><br><br>Maaf, Username <b>$username</b> telah <b>TERBLOKIR</b>, silahkan hubungi Administrator.";
  echo "<div> <a href='../index.php'><img src='images/kunci.png'  height=176 width=143></a></div>";
  echo "<input type=button class='button buttonblue mediumbtn' value='KEMBALI' onclick=location.href='index.php'></a></center>";
  return false;
}
function salah_username($username){
  echo "<link href='css/screen.css' rel='stylesheet' type='text/css'>
  <link href='css/reset.css' rel='stylesheet' type='text/css'>
  <link href='css/style_button.css' rel='stylesheet' type='text/css'>
  <center><br><br><br><br><br><br>Maaf, Username <b>$username</b> tidak dikenal.";
  echo "<div> <a href='index.php'><img src='images/kunci.png'  height=176 width=143></a></div>";
  echo "<input type=button class='button buttonblue mediumbtn' value='KEMBALI' onclick=location.href='index.php'></a></center>";	
  return false;
}

function salah_password(){
  echo "<link href='css/screen.css' rel='stylesheet' type='text/css'>
  <link href='css/reset.css' rel='stylesheet' type='text/css'>
  <link href='css/style_button.css' rel='stylesheet' type='text/css'>
  <center><br><br><br><br><br><br>Maaf, silahkan cek kembali <b>Password</b> Anda<br><br>Kesalahan $_SESSION[salah]";
  echo "<div> <a href='../index.php'><img src='images/kunci.png' height=176 width=143></a></div>";
  echo "<input type=button class='button buttonblue mediumbtn' value='KEMBALI' onclick=location.href='index.php'></a></center>";
   return false;
}

function blokir($username){
	$ipaddress = empty($_SERVER['HTTP_CLIENT_IP'])?(empty($_SERVER['HTTP_X_FORWARDED_FOR'])? $_SERVER['REMOTE_ADDR']:$_SERVER['HTTP_X_FORWARDED_FOR']):$_SERVER['HTTP_CLIENT_IP'];
	$sql	= "UPDATE admins SET lastlogin=now(),ipaddress='$ipaddress',blokir='Y'  WHERE username='$username'";
	mysql_query($sql);		
	session_start();
	session_destroy();
	return false;
}

function disable(){
  echo "<link href='css/screen.css' rel='stylesheet' type='text/css'>
  <link href='css/reset.css' rel='stylesheet' type='text/css'>
  <link href='css/style_button.css' rel='stylesheet' type='text/css'>
  <center><br><br><br><br><br><br>Maaf, Akun Anda saat ini berstatus DISABLE, silahkan hubungi Admin untuk memulihkan.";
  echo "<div> <a href='index.php'><img src='images/kunci.png'  height=176 width=143></a></div>";
  echo "<input type=button class='button buttonblue mediumbtn' value='KEMBALI' onclick=location.href='index.php'></a></center>";	
  return false;
}

function early_time(){
  echo "<link href='css/screen.css' rel='stylesheet' type='text/css'>
  <link href='css/reset.css' rel='stylesheet' type='text/css'>
  <link href='css/style_button.css' rel='stylesheet' type='text/css'>
  <center><br><br><br><br><br><br>Maaf, saat ini belum masuk waktu Login, silahkan coba lagi pada saat jam kerja.";
  echo "<div> <a href='index.php'><img src='images/kunci.png'  height=176 width=143></a></div>";
  echo "<input type=button class='button buttonblue mediumbtn' value='KEMBALI' onclick=location.href='index.php'></a></center>";	
  return false;
}

function over_time(){
  echo "<link href='css/screen.css' rel='stylesheet' type='text/css'>
  <link href='css/reset.css' rel='stylesheet' type='text/css'>
  <link href='css/style_button.css' rel='stylesheet' type='text/css'>
  <center><br><br><br><br><br><br>Maaf, saat ini sudah di luar waktu login, silahkan coba lagi pada saat jam kerja.";
  echo "<div> <a href='index.php'><img src='images/kunci.png'  height=176 width=143></a></div>";
  echo "<input type=button class='button buttonblue mediumbtn' value='KEMBALI' onclick=location.href='index.php'></a></center>";	
  return false;
}
?>