<?php
	//set time indonesia
	ini_set('date.timezone', 'Asia/Jakarta');


	//mengaktifkan fungsi autoprint pdf
	require('pdf_js.php');
	class PDF_AutoPrint extends PDF_JavaScript
	{
		function AutoPrint($dialog=false)
		{
			//Open the print dialog or start printing immediately on the standard printer
			$param=($dialog ? 'true' : 'false');
			$script="print($param);";
			$this->IncludeJS($script);
		}
		function AutoPrintToPrinter($server, $printer, $dialog=false)
		{
			//Print on a shared printer (requires at least Acrobat 6)
			$script = "var pp = getPrintParams();";
			if($dialog)
				$script .= "pp.interactive = pp.constants.interactionLevel.full;";
			else
				$script .= "pp.interactive = pp.constants.interactionLevel.automatic;";
			$script .= "pp.printerName = '\\\\\\\\".$server."\\\\".$printer."';";
			$script .= "print(pp);";
			$this->IncludeJS($script);
		}
	}


	/** Include path **/
	set_include_path(get_include_path() . PATH_SEPARATOR . 'PHPExcel-1.8/Classes/');
	/** PHPExcel_IOFactory */
	include 'PHPExcel/IOFactory.php';

	//Source => http://www.infragistics.com/community/blogs/taz_abdeali/archive/2012/02/08/netadvantage-for-jquery-with-php-101.aspx
	//Cek metode
	$metode = $_SERVER['REQUEST_METHOD'];

	//di aktifkan sblm session
	$nameprog =basename($_SERVER['PHP_SELF']);
	include ('ceklogin.php');

	//aktifkan session
	if(!isset($_SESSION)){session_start();}
	$fkodekantorx=isset($_SESSION['f_kodekantor'])?$_SESSION['f_kodekantor']:"";

	//jika user diberi akses maka bisa pilih kode kantor (butuh ceklogin.php)
	$bcadd_haky=($bcadd_hakx=='disabled')?1:0;

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

	if ($metode=="POST"){
		//cek posting
		$fuserx						=isset($_POST['usernamex'])					?$_POST['usernamex']				:"";
		$ffullnamex				=isset($_POST['fullnamex'])					?$_POST['fullnamex']				:"";
		$foldpasswordx		=isset($_POST['oldpasswordx'])			?$_POST['oldpasswordx']			:"";
		$fpasswordx				=isset($_POST['passwordx'])					?$_POST['passwordx']				:"";
		$fconfirmpassword	=isset($_POST['confirmpasswordx'])	?$_POST['confirmpasswordx']	:"";
		$femailx					=isset($_POST['emailx'])						?$_POST['emailx']						:"";
		$fkodekantorx			=isset($_POST['kodekantorx'])				?$_POST['kodekantorx']			:"";
		$simpanx					=isset($_POST['simpan'])						?$_POST['simpan']						:false;
		$kuncix						=isset($_POST['kunci'])							?$_POST['kunci']						:"";
		//utk save
		$savetox=isset($_POST['saveto'])	? $_POST['saveto'] : "";
	}else{
		//cek posting
		$fuserx						=isset($_GET['usernamex'])				?$_GET['usernamex']				:"";
		$ffullnamex				=isset($_GET['fullnamex'])				?$_GET['fullnamex']				:"";
		$foldpasswordx		=isset($_GET['oldpasswordx'])			?$_GET['oldpasswordx']		:"";
		$fpasswordx				=isset($_GET['passwordx'])				?$_GET['passwordx']				:"";
		$fconfirmpassword	=isset($_GET['confirmpasswordx'])	?$_GET['confirmpasswordx']:"";
		$femailx					=isset($_GET['emailx'])						?$_GET['emailx']					:"";
		$fkodekantorx			=isset($_GET['kodekantorx'])			?$_GET['kodekantorx']			:"";
		$simpanx					=isset($_GET['simpan'])						?$_GET['simpan']					:false;
		$kuncix						=isset($_GET['kunci'])						?$_GET['kunci']						:"";
		//utk save
		$savetox=isset($_POST['saveto'])	? $_POST['saveto'] : "";
	}
	
	//include("LogCekVar.php");
  //echo json_encode(array ('isimsg'=>"test"));
	//die();

	//parameter utk search 
	$wherex ="1=1";

	//simpan - sebelumnya di cek dulu validasinya
	if (!preg_match("/^[A-Za-z0-9_!@$]{1,50}$/", $foldpasswordx))
	{
		$error_message = "<font color='red'><b>Gagal</b></font>, Password lama - harus diisi, please check and try again!";
	}else
	if (!cekoldpass($conn_login,$fuserx,$foldpasswordx)){
		$error_message = "<font color='red'><b>Gagal</b></font>, Password lama - salah!, please check and try again!";
	}else
	if (!preg_match("/^[A-Za-z0-9_!@$]{1,50}$/", $fpasswordx))
	{
		$error_message = "<font color='red'><b>Gagal</b></font>, Password - harus di isi, please check and try again!";
	}else
	if ($fpasswordx != $fconfirmpassword)
	{
		$error_message = "<font color='red'><b>Gagal</b></font>, Password dan konfirmasi Password - beda!";
	}else
	if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/", $femailx))		 
	{
		$error_message = "<font color='red'><b>Gagal</b></font>, Email harus valid, Please check and try again.";
	}else
	if ($fkodekantorx=="") //harus di buatkan sndiri connect database
	{
		$error_message = "<font color='red'><b>Gagal</b></font>, Kantor cabang harus di isi, Please check and try again.";
	}

	//jika tidak ada message error
	if (empty($error_message)){
		//update
		$qstring="update apl_user set fullname='$ffullnamex',email='$femailx',password='$fpasswordx',kode_kantor='$fkodekantorx' where user='$fuserx' and password='$foldpasswordx'";
		$rs = mysqli_query($conn_login,$qstring);
		//$wherex=$qstring;
		//include("LogCekVar.php");
		//die();
		if ($rs){
			//session
			$_SESSION['f_pass']				= $fpasswordx;
			$_SESSION['f_fullname']		= $ffullnamex;
			$_SESSION['f_email']			= $femailx;
			$_SESSION['f_kodekantor'] = $fkodekantorx;
			$isimsg="";
			$arr = array ('isimsg'=>"<font color='blue'><b>Berhasil</b></font>, ke menu utama",'berhasil'=>true);
		}else{
			$arr = array ('isimsg'=>"<font color='red'><b>Gagal</b></font>, tidak bisa Update",'berhasil'=>false);
		}
	}else{
		$arr = array ('isimsg'=>$error_message,'berhasil'=>false);
	}

	//ambil nilai variable php ke javascript
	//echo "
	//<script>
	//		kode_kantor='$fkodekantorx';
	//		//supaya combobox readonly
	//		tfcombo1=false;
	//		//cek supaya combobox disable
	//		temp='$bcadd_hakx';
	//		if (temp=='disabled'){
	//			tfcombo1status='disable';
	//		}else{
	//			tfcombo1status='enable';
	//		}
	//</script>";

	function cekoldpass($conn_login,$fuserx,$fpassx){
		$qstring="select count(*) from apl_user where user='$fuserx' and password='$fpassx'";
		$rs		= mysqli_query($conn_login,$qstring);
		$row	= mysqli_fetch_row($rs);
		if ($row[0]==0){
			return false;
		}else{
			return true;
		}
		//$wherex=$qstring;
		//include("LogCekVar.php");		
	}

	echo json_encode($arr);
	die();
?>
