<?php
/**
 * dns.php — Lightweight unencrypted portal/DNS list endpoint
 *
 * Called by the app's native bridge (com.DnsCfgXX -> AppConfig.dnsUrl())
 * at: {panel_url}api/dns.php
 *
 * Unlike api.php's main handleGetDevices() response (which is AES-encrypted
 * for the primary Java-side portal import flow), this endpoint returns a
 * plain, unencrypted JSON list of the same portal/DNS entries for native
 * code that does not implement the app's decryption routine.
 */

require_once __DIR__ . '/../includes/functions.php';

header('Content-Type: application/json; charset=UTF-8');

logRequest('api/dns.php');

$rows = $db->select('dns', '*', '', '');

$portals = [];

foreach ($rows as $index => $row) {
    $url = $row['url'] ?? '';

    // Pass the base server URL as-is. The app prompts the user to log in
    // with their own username/password for Xtream Codes.
    $portals[] = [
        'id' => $index + 1,
        'name' => $row['title'] ?? '',
        'url' => $url,
        'type' => 'xtream',
    ];
}

echo json_encode([
    'success' => true,
    'portals' => $portals,
    'portal_urls' => array_column($portals, 'url'),
], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
