<?php

include_once __DIR__ . '/secrets.php';
include 'db_connect.php';

$json = file_get_contents('php://input');
$data = json_decode($json, true);

$key = $data['key'];
$userid = $data['userid'];

$security_key = decrypData($key);

if ($security_key === false) {
    echo json_encode(['error' => 'Invalid encryption data']);
    exit;
}

if ($security_key == SECURITY_KEY_PLAIN && strlen($userid) > 0) {

    $con = dbConnect();
    $url = 'https://app.bpluspro.xyz/social/';

    // ================= USER DATA =================
    $sql = "SELECT * FROM page_table WHERE userid = '$userid'";
    $output = mysqli_query($con, $sql);

    if ($output && mysqli_num_rows($output) > 0) {

        $row = mysqli_fetch_assoc($output);

        /* ===============================
           FINAL RESPONSE
        =================================*/
        $temp = [
            'id' => $row['id'],
            'userid' => $row['userid'],
            'name' => $row['name'],
            'image' => $url . $row['image'],
            'bpay' => $row['bpay'],
        ];

        echo json_encode($temp, JSON_UNESCAPED_SLASHES);

    } else {
        echo json_encode(['error' => 'No data found']);
    }

    mysqli_close($con);

} else {
    echo json_encode(['error' => 'Invalid key']);
}

?>
