<?php
header('Content-Type: application/json');
require '../config.php';
if(isset($_POST['key']) && isset($_POST['sign'])) {
    $key = $_POST['key'];
    $sign = $_POST['sign'];

    $s_api = $db->query("SELECT * FROM users WHERE api_key = '$key'");
    if($s_api->num_rows == 0) die(json_encode(['result' => false,'message' => 'API Key not registered.'],true));
    $r_api = $s_api->fetch_assoc();
    $username = $r_api['username'];
    $data_user = $username;
    if($sign <> md5($r_api['api_id'].$r_api['api_key'])) die(json_encode(['result' => false,'message' => 'API Signature not valid.'],true));
    if($data_user == false) die(json_encode(['result' => false,'message' => 'User not found.']));
    if(check_ip($client_ip, $r_api['ip_static']) == false) die(json_encode(['result' => false,'message' => 'IP Anda '.$client_ip.' Tidak Dikenali'],true));
    $sess_username = $r_api['username'];
    
    die(json_encode(['result' => true,'data' => [
        'full_name' => $r_api['name'],
        'username' => $sess_username,
        'email' => $r_api['email'],
        'phone' => $r_api['phone'],
        'saldo' => (int)$r_api['saldo'],
        'level' => $r_api['level'],
        'registered' => $r_api['register_at'],
    ],'message' => 'Successfully got your account details.']));
} else {
    die(json_encode(['result' => false,'message' => 'Request not detected.'],true));
}