<?php
header("Connection: Keep-alive");
header("Access-control-allow-origin: *");
header('X-Frame-Options: SAMEORIGIN');
header("Access-Control-Allow-Credentials: true");
header('Access-Control-Allow-Methods: POST');
header("Access-Control-Allow-Headers: Content-Type, Accept, X-Requested-With, X-Authorization");
header('X-XSS-Protection: 1; mode=block');
header('Content-type: application/json');
header('Accept: application/json');
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// echo 'success';return false;
if(strtoupper($_SERVER['REQUEST_METHOD'])=='POST'){
	require_once('inc/dbcon.php');
	require_once('inc/function.php');
	$now=date('Y-m-d H:i:s');
	if(isJson(file_get_contents("php://input"))){
		$formData = json_decode(file_get_contents("php://input"));
	}else{
		$formData = json_decode(toJson(file_get_contents("php://input")));
	}
	$headers = apache_request_headers();
	$auth = $headers['x-authorization'];
	if($formData->p && $auth){
		if($auth=='getAuth'){
			if($formData->p=='login' || $formData->p=='sign-up' || $formData->p=='reset-pass'){
				require_once('api-controller/'.$formData->p.'.php');
			}else{
				$jdata = array('status'=>0,'msg'=>"403 Forbidden");
				header("HTTP/1.0 403 Forbidden");
			}
		}elseif($auth=='noAuth'){
			if($formData->p=='static'){
				$qp=mysqli_query($connect, "select title,contents from static_page where page='".$formData->t."'");
				if(!$qp){
					$jdata=array('status'=>0,'msg'=>mysqli_error($connect));
					header("HTTP/1.0 403 Forbidden");
				}else{
					if(mysqli_num_rows($qp)){
						$bp=mysqli_fetch_assoc($qp);
						$jdata=array('status'=>1,'data'=>$bp);
					}else{
						$jdata=array('status'=>0,'msg'=>'Not Found');
						header("HTTP/1.0 404 Not Found");
					}
				}
			}else{
				$jdata = array('status'=>0,'msg'=>"403 Forbidden");
				header("HTTP/1.0 403 Forbidden");
			}
		}else{
			$cekAuth = json_decode(cekAuth($connect, $auth));
			if($cekAuth->status==0){
				$jdata=array('status'=>0,'msg'=>$cekAuth->msg);
				header('HTTP/1.0 '.$cekAuth->code);
			}else{
				if(is_file('api-controller/'.$formData->p.'.php')){
					$userData = $cekAuth->user;
					require_once('api-controller/'.$formData->p.'.php');
					$jdata = array('status'=>$data['status'],'result'=>$data);
				}else{
					$jdata = array('status'=>0,'msg'=>"404 Request not found");
					header("HTTP/1.0 404 Not Found");
				}
			}
		}
	}else{
		$jdata = array('status'=>0,'msg'=>"403 Forbidden",$a=>$headers);
	}
	echo json_encode($jdata);
}
