<?php
if(count(get_included_files()) ==1) exit("Direct access not permitted.");
if(isset($_POST['ikdata'])){
if (!isset($_SESSION)) session_start();
$db=pDatabase::getInstance();
if(!$db->ceckmodule($cl)) exit;

header("Content-type: application/json");
	$data=$_POST['ikdata'];
	$fungsi = strip_tags(stripslashes($data[0]));
	$ex=$fungsi($data);
	if($ex){
		echo $ex;
	}else{
		echo false;
	}
}else{
	echo false;
}
function gethalaman($qr,$dt,$jumlah){
	$db=pDatabase::getInstance();
	
	$rs=$db->loadAssoc($qr,$dt);
	
	$total=1;
	if(isset($rs['total'])){
		$total=ceil($rs['total']/$jumlah);
	}
	if($total==0){
		return 1;
	}else{
		return $total;
	}
}

function datacari01($data=array())
{
	$mode=0;
	$kunci=array("nama","token","ipaddress");
	$alias=array("Nama User","Token","IP Address");
	$kolom=array("t2.nama","t1.token","t1.ipaddress");
	$jenis=array();
	
	$hasil=array("kunci"=>$kunci,"kolom"=>$kolom,"alias"=>$alias,"jenis"=>$jenis);
	if(sizeof($data)>0){
		$mode=$data[1];
	}
	if($mode==0){
		return $hasil;
	}else{
		return json_encode($hasil);
	}
}

function getTabel($data){
	$db=pDatabase::getInstance();
	$cari=$data[3];
	$arrcr=explode("&", $cari);
	
	$datapush=array();
	$wh="";
	if(sizeof($arrcr)>0){
		$arrkunci0=explode("=", $arrcr[0]);
		if(sizeof($arrcr)==1 && sizeof($arrkunci0)==1){
			$cr="%$cari%";
			$wh.="AND t1.ipaddress LIKE ? ";
			array_push($datapush,$cr);
		}else{
			$datacari=$db->datacari(datacari01(),$arrcr);
			$datapush=$datacari['datapush'];
			$wh=$datacari['query'];
		}
	}
	
	if($_SESSION['profile']['level']<30){
		$wh.="and t1.iduser=? ";
		array_push($datapush,$_SESSION['profile']['id']);
	}
	
	$qt="SELECT COUNT(*) as total FROM tb_username_api as t1
		inner join tb_username as t2 on t1.iduser=t2.id
		WHERE t1.id>0 ".$wh;
		
	$total=gethalaman($qt,$datapush,$data[2]);
	array_push($datapush,$data[1],$data[2]);
	
	$query="SELECT t1.id,t1.iduser,t1.ipaddress,t1.token,t1.status,
			t2.nama as namauser,
			$total as halaman FROM tb_username_api as t1
		inner join tb_username as t2 on t1.iduser=t2.id	
		WHERE t1.id>0 ".$wh."
		ORDER BY t1.id desc LIMIT ?,?";
	$rs=$db->jsonData($query,$datapush);
	return $rs;
}

function datacariuser($data=array())
{
	$mode=0;
	$kunci=array("nama");
	$alias=array("Nama User");
	$kolom=array("t1.nama");
	$jenis=array();
	
	$hasil=array("kunci"=>$kunci,"kolom"=>$kolom,"alias"=>$alias,"jenis"=>$jenis);
	if(sizeof($data)>0){
		$mode=$data[1];
	}
	if($mode==0){
		return $hasil;
	}else{
		return json_encode($hasil);
	}
}

function getdatauser($data){
	$db=pDatabase::getInstance();
	$cari=$data['cari'];
	$arrcr=explode("&", $cari);
	
	$datapush=array();
	$wh="";
	if(sizeof($arrcr)>0){
		$arrkunci0=explode("=", $arrcr[0]);
		if(sizeof($arrcr)==1 && sizeof($arrkunci0)==1){
			$cr="%$cari%";
			$wh.="AND t1.nama LIKE ? ";
			array_push($datapush,$cr);
		}else{
			$datacari=$db->datacari(datacariuser(),$arrcr);
			$datapush=$datacari['datapush'];
			$wh=$datacari['query'];
		}
	}
	
	$posisihalaman=($data['halaman']-1)*$data['jumlah'];
	array_push($datapush,$posisihalaman,$data['jumlah']);
	
	$query="SELECT 
				SQL_CALC_FOUND_ROWS t1.id,
					t1.`id`,
					t1.`username`,t1.nama,t1.email
			FROM tb_username as t1
		WHERE t1.id>0 and t1.status=1 ".$wh."
		ORDER BY t1.nama ASC LIMIT ?,?";
	$rs=$db->loadAll($query,$datapush);
	
	$qrow="SELECT FOUND_ROWS() as totalbaris;";
	$rsrow=$db->loadAssoc($qrow);
	$jumlahhalaman=1;
	if(isset($rsrow['totalbaris'])){
		$jumlahhalaman=ceil($rsrow['totalbaris']/$data['jumlah']);
	}
	
	return json_encode(array("data"=>$rs,"totalhalaman"=>$jumlahhalaman));
}

function inputuserapi($data){
	$db=pDatabase::getInstance();
	$iduser=$data['iduser'];
	if($_SESSION['profile']['level']!=30){
		$iduser=$_SESSION['profile']['id'];
	}
	$token=$db->gentoken(64);
	$query="insert into tb_username_api (iduser,ipaddress,token,callback) values (?,?,?,?)";
	$rs=$db->query($query,array($iduser,$data['ipaddress'],$token,$data['callback']));
	return $rs;
}
function edituserapi($data){
	$db=pDatabase::getInstance();
	$wh="";
	$datapush=array($data['ipaddress'],$data['callback'],$data['id']);
	if($_SESSION['profile']['level']!=30){
		$wh.="and iduser=? ";
		array_push($datapush,$_SESSION['profile']['id']);
	}
	$query="update tb_username_api set ipaddress=?,callback=? where id=? ".$wh;
	$rs=$db->query($query,$datapush);
	return $rs;
}
function hapusData($data){
	$db=pDatabase::getInstance();
	$wh="";
	$datapush=array($data['id']);
	if($_SESSION['profile']['level']!=30){
		$wh.="and iduser=? ";
		array_push($datapush,$_SESSION['profile']['id']);
	}
	
	$query="DELETE FROM tb_username_api WHERE id=? ".$wh;
	$rs=$db->query($query,$datapush);
	return $rs;
}
function setstatus($data){
	$db=pDatabase::getInstance();
	$wh="";
	$datapush=array($data['status'],$data['id']);
	if($_SESSION['profile']['level']!=30){
		$wh.="and iduser=? ";
		array_push($datapush,$_SESSION['profile']['id']);
	}
	
	$query="update tb_username_api set status=? WHERE id=? ".$wh;
	$rs=$db->query($query,$datapush);
	return $rs;
}
?>