<?php
/**
 * 友情链接 · 站内中转页
 * 左上角固定返回扩展中心，正文内嵌目标网站。
 */
$id = (int) input('id');
$link = $id > 0 ? DB::one('SELECT * FROM ' . DB::table('links') . ' WHERE id = ? AND status = 1', [$id]) : null;
if (!$link) {
    flash('友情链接不存在或已下架', 'err');
    redirect(page_url('links'));
}

$target = trim((string) ($link['url'] ?? ''));
if (!preg_match('#^https?://#i', $target)) {
    flash('该友情链接地址无效', 'err');
    redirect(page_url('links'));
}

$name = trim((string) ($link['name'] ?? ''));
if ($name === '') {
    $name = parse_url($target, PHP_URL_HOST) ?: '友情链接';
}
$siteName = setting('site_name', '木漏茶室');
?><!DOCTYPE html>
<html lang="<?= e(km_html_lang()) ?>" data-theme="<?= e(setting('theme_mode', 'light')) ?>">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title><?= e($name) ?> · <?= e($siteName) ?></title>
<meta name="referrer" content="strict-origin-when-cross-origin">
<style>
  :root{color-scheme:light dark}
  *{box-sizing:border-box}
  html,body{height:100%;margin:0;background:#0e1216;overflow:hidden}
  .linkout-bar{
    position:fixed;z-index:20;top:0;left:0;right:0;
    height:calc(54px + env(safe-area-inset-top));
    padding:env(safe-area-inset-top) 12px 0;
    display:flex;align-items:center;gap:10px;
    background:rgba(16,21,27,.94);
    border-bottom:1px solid rgba(255,255,255,.14);
    backdrop-filter:blur(12px);-webkit-backdrop-filter:blur(12px);
    font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif;
  }
  .linkout-back{
    flex:0 0 auto;display:inline-flex;align-items:center;gap:6px;
    height:34px;padding:0 13px;border-radius:999px;
    background:#fff;color:#14202b;text-decoration:none;
    font-size:13px;font-weight:700;line-height:1;
    box-shadow:0 4px 14px rgba(0,0,0,.28);white-space:nowrap;
  }
  .linkout-back:hover{background:#eaf2f8}
  .linkout-back:active{transform:translateY(1px)}
  .linkout-name{
    flex:1 1 auto;min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;
    color:#fff;font-size:13px;font-weight:600;letter-spacing:.02em;
  }
  .linkout-name small{display:block;margin-top:2px;color:rgba(255,255,255,.58);font-size:10.5px;font-weight:400}
  .linkout-open{
    flex:0 0 auto;color:rgba(255,255,255,.82);text-decoration:none;
    font-size:12px;padding:7px 9px;border:1px solid rgba(255,255,255,.2);border-radius:8px;white-space:nowrap;
  }
  .linkout-open:hover{color:#fff;border-color:rgba(255,255,255,.42)}
  .linkout-frame{
    position:fixed;z-index:10;left:0;right:0;bottom:0;
    top:calc(54px + env(safe-area-inset-top));
    width:100%;height:calc(100% - 54px - env(safe-area-inset-top));
    border:0;background:#fff;
  }
  .linkout-loading{
    position:fixed;z-index:9;left:50%;top:calc(50% + 27px);transform:translate(-50%,-50%);
    color:rgba(255,255,255,.68);font-size:12px;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif;
    pointer-events:none;
  }
  @media(max-width:560px){
    .linkout-bar{gap:8px;padding-left:9px;padding-right:9px}
    .linkout-back{height:32px;padding:0 11px;font-size:12px}
    .linkout-name{font-size:12px}
    .linkout-name small{display:none}
    .linkout-open{font-size:11px;padding:6px 7px}
  }
</style>
</head>
<body>
  <div class="linkout-bar">
    <a class="linkout-back" href="<?= e(page_url('extensions')) ?>">← 返回扩展中心</a>
    <div class="linkout-name">
      <?= e($name) ?>
      <small>页面空白表示对方禁止站内嵌入，请点右上角新窗口打开</small>
    </div>
    <a class="linkout-open" href="<?= e($target) ?>" target="_blank" rel="noopener noreferrer">新窗口打开 ↗</a>
  </div>
  <div class="linkout-loading" id="linkoutLoading">正在打开 <?= e($name) ?>…</div>
  <iframe class="linkout-frame" id="linkoutFrame" src="<?= e($target) ?>" title="<?= e($name) ?>"
          allow="fullscreen; clipboard-read; clipboard-write" referrerpolicy="strict-origin-when-cross-origin"
          onload="document.getElementById('linkoutLoading').style.display='none';"></iframe>
</body>
</html>
