<?php
	//aktifkan session
	if(!isset($_SESSION)){session_start();}

	//open connection
	include ('connect-DB.php');

	//ambil informasi 
	$fuserx=isset($_POST['f_user']) ? $_POST['f_user'] : "";
	$fpassx=isset($_POST['f_pass']) ? $_POST['f_pass'] : "";
	$kuncix=isset($_POST['kunci'] ) ? $_POST['kunci']  : "";

//$fuserx='hendro';
//$fpassx='hendro123';

	if ($fuserx==""){
	$fuserx=isset($_SESSION['f_user'])	?$_SESSION['f_user']:"";
	$fpassx=isset($_SESSION['f_pass']) ?$_SESSION['f_pass']:"";
	$kuncix=isset($_SESSION['kunci'])  ?$_SESSION['kunci']:"";
	}

	//parameter utk search, "like binary" utk sensitif case, sumber:http://stackoverflow.com/questions/5629111/how-can-i-make-sql-case-sensitive-string-comparison-on-mysql
	$wherex  ="1=1"; 
	$wherex .= !empty($fuserx)	? " and user = '$fuserx'"			: "user=''"; 
	$wherex .= !empty($fpassx)	? " and password like binary '$fpassx'"	: "and password=''";

	//cek status kunci
	//if ($kuncix=="repas"){
	//	//echo "send ke email".$fuserx;
	//	//panggil halaman index
	//	header("location:regisForgot.php");
	//}
	//if ($kuncix=="new"){
	//	//echo "Buat user baru:".$fuserx;
	//	//panggil halaman index
	//	header("location:regis.php");
	//}
	//if ($kuncix=="kirim"){
		$qstring = "select * from apl_user where $wherex";
		$rs = mysqli_query($conn_login,$qstring);
		//$row = mysqli_fetch_row($rs);
		//$result["total"] = $row[0];
		//echo $row[0];
		//if ($row[0]==0){
		//if (mysqli_num_rows($rs) == 0){
		//	//logout
		//	header("location:keluar.php");
		//}else{
		//	header("location:menuutama.php");
		//}

		//$wherex=$qstring;
		//include("LogCekVar.php");
		//die();


		//$qstring = sprintf("SELECT apl_user.*,pogl.KETERANGAN FROM apl_user left join pogl on trim(apl_user.digitke2)=trim(pogl.digitke2) WHERE apl_user.user='%s' AND apl_user.password like binary '%s'",
		//	mysqli_real_escape_string($conn_mysql,$fuserx),
		//	mysqli_real_escape_string($conn_mysql,$fpassx));		
		//$rs = mysqli_query($conn_mysql,$qstring);//query the database for entries containing the term
		if (mysqli_num_rows($rs) == 0) {
    //if ($row[0]==0){
			$_SESSION["f_user"]="";
			$_SESSION["f_pass"]="";
			$_SESSION['f_fullname']="";
			
			//20170816, dibawah ini di ganti dgn kluar.php
			//session_destroy();
			////panggil halaman index
			//header("location:index.php");				
			header("location:keluar.php");
		}
		else
		{
			$row = mysqli_fetch_assoc($rs);
			$_SESSION['f_user']     = $row ['USER'];
			$_SESSION['f_pass']     = $row ['PASSWORD'];
			$_SESSION['f_fullname']	= $row['FULLNAME'];
			$_SESSION['f_email']		= $row['EMAIL'];
			if ($row ['CONFIRMKEY']!="") {
				//panggil halaman confirm
				header("location:regisConfirm.php");
			}else{
				//Info user yang di save ke cookies
				//$_SESSION['f_user']				= $row ['USER'];
				//$_SESSION['f_pass']				= $row ['PASSWORD'];
				//$_SESSION['f_fullname']		= $row ['FULLNAME'];
				//$_SESSION['f_email']			= $row ['EMAIL'];
				$_SESSION['f_pakai']			= "OK";
				$_SESSION['f_level']			= $row ['LEVEL'];
				$_SESSION['f_kodekantor']	= $row ['KODE_KANTOR'];
				$_SESSION['f_keterangan'] = $row ['KETERANGAN'];
				$_SESSION['f_fullname']		= $row ['FULLNAME'];
				$_SESSION['f_masuk']			= date('Y-m-d H:i:s');

				//mengatur session time-out (sementara tdk di gunakan karena harus atur per halaman)
				//$_SESSION['timeout'] = time();
				//if ($_SESSION['timeout'] + 10 * 60 < time()) {
				//	 // session timed out
				//} else {
				//	 // session ok
				//}

				//$_SESSION['f_uptgl'] = $row ['UPTGL'];
				//$_SESSION['f_iplokal'] = $_SERVER['REMOTE_ADDR']

				//simpan info variable yang terkirim dari page, post, session sebelumnya
				$upuserx=$_SESSION['f_user'];
				$nameprog=basename($_SERVER['PHP_SELF']);
				include("LogCekVar.php");

				session_write_close();

				//header("location:menuUtama.php");

				if (!isset($_SESSION['actual_link'])) {
					//panggil halaman menu utama
					header("location:menuUtama.php");
				}else{
					//baca alamat yg akan di baca sblm login tadi (sementara hanya di toner.php dan tonerFunc.php
					header("location:".$_SESSION['actual_link']);
				}

			}
		}
	//}
?>  
