<?php
date_default_timezone_set('Asia/Jakarta');
$waktu=date('d-m-Y H:i:s');


	$sql_satker = $pdo->prepare("SELECT * FROM satker where no='1'");
	$sql_satker->execute();
	$data = $sql_satker->fetch();
		$sekolah=$data['sekolah'];
		$alamat=$data['alamat'];
		$emailsek=$data['email'];
		$passwordsek=$data['password'];
		$urlheader=$data['email_urlheader'];
		$urlheader2=$data['email_urlheader2'];
		$apiwa_fitur=$data['apiwa_fitur'];
		$apiwa_passkey=$data['apiwa_passkey'];
		$apiwa_token=$data['apiwa_token'];
	
	//Data Serial
	$sql_serial = $pdo->prepare("SELECT * FROM serial where no='1'");
	$sql_serial->execute();
	$data_sn = $sql_serial->fetch();
		$serial1=$data_sn['serial1'];
		$serial2=$data_sn['serial2'];
		$serial3=$data_sn['serial3'];
		$serial4=$data_sn['serial4'];
		$serial5=$data_sn['serial5'];
		$aktivasi=$data_sn['aktivasi'];

		//$konekweb2=gethostbyname('aplikasiaks.com');
		//if ($konekweb2!='aplikasiaks.com'){
		//$wa_tok = file_get_contents("http://aplikasiaks.com/token/w4t0k3n.php");}
		//else{$wa_tok="XYZABC12954";}
	$arrContext=array(
		"ssl"=>array(
		"verify_peer"=>false,
		"verify_peer_name"=>false,
		)
	);	
	$konekweb2=gethostbyname('aplikasiaks.com');	
	if ($konekweb2!='aplikasiaks.com'){
	$wa_tok = file_get_contents("http://aplikasiaks.com/token/w4t0k3n.php",false,stream_context_create($arrContext));}
	else{$wa_tok="qE9Rgg4LkLlJe45faht7GhfhsjgfbR6HDwRfLKJG";};
	
		
	//Tanggal Awal
	$tg1=substr($apiwa_token,8,1);
	$tg2=substr($apiwa_token,1,1);		
	$tg3=substr($apiwa_token,16,1);
	$tg4=substr($apiwa_token,11,1);
	$tg5=substr($apiwa_token,17,1);
	$tg6=substr($apiwa_token,9,1);
	$tg7=substr($apiwa_token,12,1);
	$tg8=substr($apiwa_token,5,1);
	if($tg1=="E"){$vtg1=1;}elseif($tg1=="Z"){$vtg1=2;}elseif($tg1=="B"){$vtg1=3;}elseif($tg1=="F"){$vtg1=4;}elseif($tg1=="K"){$vtg1=5;}elseif($tg1=="Y"){$vtg1=6;}elseif($tg1=="C"){$vtg1=7;}elseif($tg1=="T"){$vtg1=8;}elseif($tg1=="N"){$vtg1=9;}elseif($tg1=="M"){$vtg1=0;}else{$vtg1=0;};
	if($tg2=="E"){$vtg2=1;}elseif($tg2=="Z"){$vtg2=2;}elseif($tg2=="B"){$vtg2=3;}elseif($tg2=="F"){$vtg2=4;}elseif($tg2=="K"){$vtg2=5;}elseif($tg2=="Y"){$vtg2=6;}elseif($tg2=="C"){$vtg2=7;}elseif($tg2=="T"){$vtg2=8;}elseif($tg2=="N"){$vtg2=9;}elseif($tg2=="M"){$vtg2=0;}else{$vtg2=0;};
	if($tg3=="E"){$vtg3=1;}elseif($tg3=="Z"){$vtg3=2;}elseif($tg3=="B"){$vtg3=3;}elseif($tg3=="F"){$vtg3=4;}elseif($tg3=="K"){$vtg3=5;}elseif($tg3=="Y"){$vtg3=6;}elseif($tg3=="C"){$vtg3=7;}elseif($tg3=="T"){$vtg3=8;}elseif($tg3=="N"){$vtg3=9;}elseif($tg3=="M"){$vtg3=0;}else{$vtg3=0;};
	if($tg4=="E"){$vtg4=1;}elseif($tg4=="Z"){$vtg4=2;}elseif($tg4=="B"){$vtg4=3;}elseif($tg4=="F"){$vtg4=4;}elseif($tg4=="K"){$vtg4=5;}elseif($tg4=="Y"){$vtg4=6;}elseif($tg4=="C"){$vtg4=7;}elseif($tg4=="T"){$vtg4=8;}elseif($tg4=="N"){$vtg4=9;}elseif($tg4=="M"){$vtg4=0;}else{$vtg4=0;};
	if($tg5=="E"){$vtg5=1;}elseif($tg5=="Z"){$vtg5=2;}elseif($tg5=="B"){$vtg5=3;}elseif($tg5=="F"){$vtg5=4;}elseif($tg5=="K"){$vtg5=5;}elseif($tg5=="Y"){$vtg5=6;}elseif($tg5=="C"){$vtg5=7;}elseif($tg5=="T"){$vtg5=8;}elseif($tg5=="N"){$vtg5=9;}elseif($tg5=="M"){$vtg5=0;}else{$vtg5=0;};
	if($tg6=="E"){$vtg6=1;}elseif($tg6=="Z"){$vtg6=2;}elseif($tg6=="B"){$vtg6=3;}elseif($tg6=="F"){$vtg6=4;}elseif($tg6=="K"){$vtg6=5;}elseif($tg6=="Y"){$vtg6=6;}elseif($tg6=="C"){$vtg6=7;}elseif($tg6=="T"){$vtg6=8;}elseif($tg6=="N"){$vtg6=9;}elseif($tg6=="M"){$vtg6=0;}else{$vtg6=0;};
	if($tg7=="E"){$vtg7=1;}elseif($tg7=="Z"){$vtg7=2;}elseif($tg7=="B"){$vtg7=3;}elseif($tg7=="F"){$vtg7=4;}elseif($tg7=="K"){$vtg7=5;}elseif($tg7=="Y"){$vtg7=6;}elseif($tg7=="C"){$vtg7=7;}elseif($tg7=="T"){$vtg7=8;}elseif($tg7=="N"){$vtg7=9;}elseif($tg7=="M"){$vtg7=0;}else{$vtg7=0;};
	if($tg8=="E"){$vtg8=1;}elseif($tg8=="Z"){$vtg8=2;}elseif($tg8=="B"){$vtg8=3;}elseif($tg8=="F"){$vtg8=4;}elseif($tg8=="K"){$vtg8=5;}elseif($tg8=="Y"){$vtg8=6;}elseif($tg8=="C"){$vtg8=7;}elseif($tg8=="T"){$vtg8=8;}elseif($tg8=="N"){$vtg8=9;}elseif($tg8=="M"){$vtg8=0;}else{$vtg8=0;};
	
	$tgmul=$vtg5.$vtg6.$vtg7.$vtg8."-".$vtg3.$tg4."-".$vtg1.$vtg2;
	if(strtotime($tgmul)){$cekmul=1;}else{$cekmul=0;};
	//Jml Hari
	$hr1=substr($apiwa_token,14,1);
	$hr2=substr($apiwa_token,19,1);
	$jmhr=$hr1.$hr2;
	if($jmhr>0){$jmlhr=$jmhr;}else{$jmlhr=0;};
	
	
	
	//Validasi
	$kesatu1=substr($apiwa_token,0,1);
	$kesatu2=substr($aktivasi,$vtg1,1);
	if(substr($apiwa_token,0,1)== substr($aktivasi,$vtg1,1)){$cektok1=1;}else{$cektok1=0;};
	if(substr($apiwa_token,15,1)== substr($aktivasi,$vtg2,1)){$cektok2=1;}else{$cektok2=0;};
	if(substr($apiwa_token,10,1)== substr($aktivasi,$vtg3,1)){$cektok3=1;}else{$cektok3=0;};
	if(substr($apiwa_token,6,1)== substr($aktivasi,$vtg1,1)){$cektok4=1;}else{$cektok4=0;};
	if(substr($apiwa_token,2,1)== strtoupper(substr($wa_tok,$vtg1,1))){$cektok5=1;}else{$cektok5=0;};
	if(substr($apiwa_token,4,1)== strtoupper(substr($wa_tok,$vtg3,1))){$cektok6=1;}else{$cektok6=0;};
	if(substr($apiwa_token,7,1)== strtoupper(substr($wa_tok,$vtg3,1))){$cektok7=1;}else{$cektok7=0;};
	$ttv=($cektok1+$cektok2+$cektok3+$cektok4+$cektok5+$cektok6+$cektok7+$cekmul);			
	
	
	date_default_timezone_set('Asia/Jakarta');
	$tanggal= mktime(date("m"),date("d"),date("Y"));
	$tglnow = date("Y-m-d", $tanggal);
	
	$tgl1=$tgmul; //2021-12-17
	$tgl2=date('Y-m-d', strtotime('+'.$jmhr.' days',strtotime($tgl1)));
	$tglinfo=date('d-m-Y', strtotime('+'.$jmhr.' days',strtotime($tgl1)));
	$sisahr=date('Y-m-d', strtotime('+'.$jmhr.' days',strtotime($tgl1)));
	
	if(strtotime($tgmul)){$last1=$tgmul;}else{$last1="2000-01-01";};
	//Warning
	if($ttv==8 and $tglnow<$tgl2)
			{$infoaktok="<b class='text-success'>AKTIF s.d ".$tglinfo."</b>";}
		elseif($ttv==8 and $tglnow>$tgl2)
			{$infoaktok="<b class='text-warning'>NON AKTIF</b>";}
		elseif($ttv!=8 or $konekweb2!='aplikasiaks.com')
			{$infoaktok="<b class='text-danger'>JARINGAN INTERNET TERGANGGU ATAU TOKEN TIDAK VALID</b>";}
		if($ttv==8 and $tglnow<$tgl2){$idkeyapi=$wa_tok;}else{$idkeyapi="XYZ";}
		
		if($ttv==8 and $tglnow<$tgl2){$tkn_sts="AKTIF";}else{$tkn_sts="NON AKTIF";};
	
	$last=date_create($last1);
	$today=date_create($tglnow);
	$diff= date_diff($last,$today);
	$sel_h=$diff->d;
	$sel_m=$diff->m;
	$sel_y=$diff->y;
	$sel_t=$sel_h+($sel_m*30)+($sel_y*356);
	$sel_nl=$jmlhr-$sel_t;
	
	//sisa hr
	$last_ss=date_create($tgl2);
	$today_ss=date_create($tglnow);
	$diff_ss= date_diff($last_ss,$today_ss);
	$sel_h_ss=$diff_ss->d;
	$sel_m_ss=$diff_ss->m;
	$sel_y_ss=$diff_ss->y;
	$sel_t_ss=$sel_h_ss+($sel_m_ss*30)+($sel_y_ss*356);
	//$sel_nl=$jmlhr-$sel_t;
	if($tglnow>$tgl2 or $konekweb2=='aplikasiaks.com'){$persentkn=0;}else{$persentkn=@(($sel_t_ss/$jmhr)*100);};
	if($persentkn>80){$clr_tkn="success";}elseif($persentkn<=80 and $persentkn>40){$clr_tkn="warning";}elseif($persentkn<40){$clr_tkn="danger";};
	
	if($tglnow>$tgl2){$token="";}else{$token=$apiwa_token;};
	
	?>
	
