<?php
if(count(get_included_files()) ==1) exit("Direct access not permitted.");
header("Content-type: application/json");
if(isset($_POST['ikdata'])){
if (!isset($_SESSION)) session_start();
$db=pDatabase::getInstance();
$datafungsi=[
	"kirimpesan"=>"kirimpesan",
];
$data=$_POST['ikdata'];
$ex=false;
if(isset($datafungsi[$data[0]])) $ex=$datafungsi[$data[0]]($data);
echo $ex;

function kirimpesan($data){
	$db=pDatabase::getInstance();
	if(isset($_SESSION['registrasi']) && $_SESSION['registrasi']==1)  return 3;
	
	$secret = '6LdcxigTAAAAAC0UEUBxqD0n3dog7oB6EKGOzvLR';
    $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$data['cpc']."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
	
	if ($response['success']==false) {
		return 2;
	
	}
	
	$status=true;
	$db->begin();
	
	$nama=strip_tags($data['nama']);
	$alamat=strip_tags($data['alamat']);
	$telp=strip_tags($data['telp']);
	$email=strip_tags($data['email']);
	$pesan=strip_tags($data['pesan']);
	
	$datain=[
		"nama"=>$nama,
		"alamat"=>$alamat,
		"telp"=>$telp,
		"email"=>$email,
		"pesan"=>$pesan
	];	
	
	$rs=$db->insert("tb_hubungi_kami",$datain);
	
	if($rs==1){
		$dataset=$db->dataSet();
		$psn="Ada kiriman pesan dari <br/>Nama : $nama<br/>Alamat: $alamat <br/>Telp: $telp <br/>Email: $email <br/>Pesan : $pesan";
		$query="insert into tb_kirim_email (tanggal,email,subject,pesan) values (NOW(),?,?,?)";
		$rsjob=$db->query($query,array($dataset['adminemail'],"Ada pesan baru dari $nama",$psn),1);
		
		$_SESSION['kirimpesan']=1;
		//shell_exec("nohup python /home/bnsp/py/mail.py $rsjob > /dev/null 2>&1 &");
		
	}else{
		$status=false;
	}
	
	if($status){
		$db->commit();
		return true;
	}else{
		$db->rollback();
		return false;
	}
	return $rs;
}

?>