<?php
$athRequestPath = parse_url((string)($_SERVER['REQUEST_URI'] ?? ''), PHP_URL_PATH);
if (in_array($athRequestPath, [
    '/api/udid_callback',
    '/api/udid_callback.php',
], true)) {
    require __DIR__ . '/api/udid_callback.php';
    exit;
}

// App UDID flow: use the PHP endpoint directly so the callback host is generated
// from the current request without requiring an Nginx rewrite for .mobileconfig.
if ($athRequestPath === '/getudid_mobileconfig.php') {
    require __DIR__ . '/getudid_mobileconfig.php';
    exit;
}

if ($athRequestPath === '/file/getudid.mobileconfig') {
    header('Location: /getudid_mobileconfig.php', true, 302);
    header('Content-Length: 0');
    exit;
}

// Web UDID flow: these legacy profile URLs keep using udid_profile.php, which
// creates a rid + Challenge session and posts back to /udid_callback.php.
if (in_array($athRequestPath, [
    '/udid.mobileconfig',
    '/includes/sign/udid.mobileconfig',
    '/includes/cert/udid.mobileconfig',
], true)) {
    $athTarget = $athRequestPath === '/includes/sign/udid.mobileconfig'
        ? 'sign'
        : ($athRequestPath === '/includes/cert/udid.mobileconfig' ? 'cert' : 'index');
    header('Location: /udid_profile.php?target=' . rawurlencode($athTarget), true, 302);
    header('Content-Length: 0');
    exit;
}
?>
<!DOCTYPE html>
<html lang="zh">
<?php

include("includes/common.php"); // 加载数据库
include("admin/client.php");

global $client;

// 检查所需函数是否可用
$requiredFunctions = array('exec', 'shell_exec', 'putenv');
$missingFunctions = array();
foreach ($requiredFunctions as $function) {
    if (!function_exists($function)) {
        $missingFunctions[] = $function;
    }
}

if (!empty($missingFunctions)) {
    echo '请开放以下函数: ' . implode(', ', $missingFunctions);
    exit;
}

['config' => $config, 'error' => $error] = ath_get_site_config($client);
if ($error !== '') {
    $error = str_replace(array('\"', '\'', "\r", "\n", "\r\n"), '', $error);
    echo "<script> alert('$error'); </script>";
}
$localSiteSettings = ath_get_local_site_settings();
$apikey = $config['dumpapp_token'];
$buyURL = $config['buy'];
$UDID = $_GET['UDID'] ?? ($_POST['UDID'] ?? '');
$isHomeShowSearchCdkey = $config['is_home_show_search_cdkey'];
$homepageThemes = ath_get_homepage_theme_templates();
$style = ath_normalize_homepage_theme_id($config['selected_web_homepage_theme_id'] ?? 1, $homepageThemes);
if (isset($_GET['preview_theme'])) {
    $style = ath_normalize_homepage_theme_id($_GET['preview_theme'], $homepageThemes);
}
$homepageThemeInstallResultButtons = ath_get_homepage_theme_install_result_buttons_for_theme(
    $localSiteSettings['homepage_theme_install_result_buttons'] ?? [],
    $style,
    $homepageThemes
);
$themeFile = __DIR__ . "/index_style{$style}.php";
if (!is_file($themeFile)) {
    $style = ath_normalize_homepage_theme_id(1, $homepageThemes);
    $homepageThemeInstallResultButtons = ath_get_homepage_theme_install_result_buttons_for_theme(
        $localSiteSettings['homepage_theme_install_result_buttons'] ?? [],
        $style,
        $homepageThemes
    );
    $themeFile = __DIR__ . "/index_style{$style}.php";
}
include $themeFile;
?>
