<?php
$user_txt = 'user.txt';
require '../../data.php';

if (file_exists($user_txt)) {
    $fileHandle = file($user_txt, FILE_IGNORE_NEW_LINES);

    $fileHandle =array_map(function ($line) {
        return str_replace(["\r", "\n"], '', $line);
    }, $fileHandle);

        $datas = array();
        $pasaran = array();
        $total = '';
        foreach ($fileHandle as $line) {
            $curtime = date("H:i:s");
            $spas = "SELECT nama_pasaranweb FROM pasaran_web";
            $qpas = mysqli_query($data, $spas);
            $total = '';
            if(mysqli_num_rows($qpas) > 0) {
                $index = 0;
                while($fpas = mysqli_fetch_assoc($qpas)) {
                    $pasaran[] = $fpas['nama_pasaranweb'];
                    $total = mysqli_num_rows($qpas);
                    
                }
            }

            $rand = rand(0, $total);
            $formattedNumber = sprintf('%d', $rand);

            $middleIndex = ceil(strlen($line) / 2);
            $line = substr_replace($line, "***", $middleIndex, 3);
            $line = strtolower($line);
            
            $datas[] = array (
                'user' => $line,
                'pasaran' => $pasaran[$formattedNumber]
            );
        }
    
} else {
    echo 'No User.';
}

$all_user = json_encode($datas);
header('Content-Type: application/json');
echo $all_user;