<?php
// file container where all texts are to be written
//$fileContainer = "Log".date("MjY").'.log'; //LogMay62015.log
$fileContainer = "Log".date("Ymd").'.log'; //Log20150506.log
// open the said file
$filePointer = fopen($fileContainer,"w+");
// text to be written in the file
$logMsg  ='======isi $variable bawaan grid======='."\n";
$logMsg  .="Metode  => ".$_SERVER['REQUEST_METHOD']."\n";
$logMsg  .="diakses => ".date("d-M-Y, H:m:s") ."\n";
$logMsg  .="add IP  => ".$_SERVER['REMOTE_ADDR']."\n";

//sumber => https://stackoverflow.com/questions/22111546/var-dump-output-in-a-variable
$logMsg  .="wherex   => ".(isset($wherex)?is_array($wherex)? var_export($wherex,true):$wherex:"<none>")."\n";

//$logMsg  .="wherex  => ".(isset($wherex)?is_object($wherex)? var_dump($wherex):$wherex:"<none>")."\n";
$logMsg  .="saveto  => ".(isset($savetox)?$savetox:"<none>")."\n";
$logMsg  .="sort    => ".(isset($sortx)?is_array($sortx)? implode(',',$sortx):$sortx:"<none>")."\n";
$logMsg  .="order   => ".(isset($orderx)?is_array($orderx)? implode(',',$orderx):$orderx:"<none>")."\n";
$logMsg  .="kunci   => ".(isset($kunci)?$kunci:"<none>")."\n";
$logMsg  .="kuncix  => ".(isset($kuncix)?$kuncix:"<none>")."\n";
$logMsg  .="lain2   => ".(isset($lain2)?$lain2:"<none>")."\n";
$logMsg  .="q       => ".(isset($q)?$q:"<none>")."\n";
$logMsg  .='============isi $_POST==============='."\n";


foreach ($_POST as $key => $value){
	if (is_array($_POST[$key])){
		//cek isi array, sumber => https://stackoverflow.com/questions/22111546/var-dump-output-in-a-variable
		$logMsg.='$_POST['.$key."] => ".var_export($value,true)."\n";
	}else{
		$logMsg.='$_POST['.$key."] => ".$value."\n";
	}
}
$logMsg  .='==========isi $_Session=============='."\n";
if (isset($_SESSION)){
	foreach ($_SESSION as $key => $value){
		if (is_array($_SESSION)){
			//cek isi array, sumber => https://stackoverflow.com/questions/22111546/var-dump-output-in-a-variable
			$logMsg.=	'$_SESSION['.$key."] => ".var_export($value,true)."\n";
		}else{	
			$logMsg.='$_SESSION['.$key."] => ".$value."\n";
		}
	}
}


//simpan log ke table beacukailog
if (isset($nameprog) ) {
	if (!isset($conn_mysql)){ 
	  include ('connect-DB.php');
	}
	//set waktu
	date_default_timezone_set('Asia/Jakarta');
	$ipx=$_SERVER['REMOTE_ADDR'];
	$masukx=(isset($_SESSION['f_masuk'])?$_SESSION['f_masuk']:date('Y-m-d H:i:s'));
	$keluarx=date('Y-m-d H:i:s');
	//simpan saat login/buka program/logout 
	$openprog=str_replace("func","",strtolower($nameprog));
	$upuserx =isset($_SESSION['f_user'])?$_SESSION['f_user']:'';

	//simpan info status khusus delete axbcmdelbc
	if (trim(strtolower($openprog))=="axbcmdelbc"){
		$masukx=date('Y-m-d H:i:s');
		$keluarx=$masukx;
		//var statusx => ambil dari file php sblm nya
		$qstringLog="INSERT INTO beacukailog(user      ,ip    ,masuk																				,keluar                                      ,openprog    )
														VALUES   ('$upuserx','$ipx',STR_TO_DATE('$masukx', '%Y-%m-%d %H:%i:%s'),STR_TO_DATE('$keluarx', '%Y-%m-%d %H:%i:%s') ,'$statusx' )
							ON DUPLICATE KEY UPDATE  
								keluar=STR_TO_DATE('$keluarx', '%Y-%m-%d %H:%i:%s')
						 ";
	}else{
		if (trim(strtolower($openprog))=="keluar.php"){
			$qstringLog="INSERT INTO beacukailog(user      ,ip    ,masuk																				,keluar                                      ,openprog    )
															VALUES   ('$upuserx','$ipx',STR_TO_DATE('$masukx', '%Y-%m-%d %H:%i:%s'),STR_TO_DATE('$keluarx', '%Y-%m-%d %H:%i:%s') ,'$openprog' )
								ON DUPLICATE KEY UPDATE  
									keluar=STR_TO_DATE('$keluarx', '%Y-%m-%d %H:%i:%s')
							 ";
		}else{
			$qstringLog="INSERT INTO beacukailog(user      ,ip    ,masuk                                       ,keluar,openprog  )
									VALUES               ('$upuserx','$ipx',STR_TO_DATE('$masukx', '%Y-%m-%d %H:%i:%s') ,null  ,'$openprog' )
								ON DUPLICATE KEY UPDATE  
									masuk=STR_TO_DATE('$masukx', '%Y-%m-%d %H:%i:%s'),
									openprog='$openprog',
									keluar=null;
							 ";
		}
	}
	//hanya disimpan ke table jika $qstringLog tdk kosong
	if ($qstringLog!='' and $upuserx!=''){
		$rsxxxxxxx = mysqli_query($conn_login,$qstringLog);//query the database for entries containing the term
	}
	//hanya utk cek perintah di atas saja
	//$logMsg  .=(isset($qstringLog)?$qstringLog:"<none>")."\n";
}


// below is where the log message has been written to a file.
fputs($filePointer,$logMsg);
// close the open said file after writing the text
fclose($filePointer);



?>