<?php
    // update the user's chat status

    $ip = htmlentities(strip_tags($_POST['ip']));
    $chat_status = htmlentities(strip_tags($_POST['chat_status']));
    $device = htmlentities(strip_tags($_POST['device']));
    $last_visited = htmlentities(strip_tags($_POST['last_visited']));
    $user = htmlentities(strip_tags($_POST['user']));

    $file = '../ChatRoom/userChats/'.$ip.'.json';
    // create new file if it doesn't exist
    if (!file_exists($file)) {
        $data = [];
        $data['messages'] = [];
        $data['ip'] = $ip;
        $d = date("Y-m-d h:i:s A", time());
        $data['chat_start_date'] = $d;
        $data['last_updated'] = $d;
    } else {
        $data = json_decode(file_get_contents($file), true);
    }

    $data['chat_status'] = $chat_status;
    $data['user'] = $user;
    $data['last_visited'] = $last_visited;
    $data['device'] = $device;
    $json = json_encode($data, JSON_PRETTY_PRINT);
    file_put_contents($file, $json);
    echo $json;

?>