<?php
/**
 * Atom Feed — 旺特音响最新文章
 */
require_once __DIR__ . '/config.php';

$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']==='on') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? 'yinxiang.com';
$base = $protocol . '://' . $host;
$settings = getSettings();

$feedId = 'tag:' . $host . ',' . date('Y') . ':feed';
$updated = date(DATE_ATOM);

$stmt = getDB()->query('SELECT n.id, n.title, n.excerpt, n.content, n.created_at, nc.name AS cat_name FROM news n JOIN news_categories nc ON n.cat_id=nc.id WHERE n.is_visible=1 ORDER BY n.created_at DESC LIMIT 50');
$articles = $stmt->fetchAll();

header('Content-Type: application/atom+xml; charset=UTF-8');
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-CN">
  <id><?= e($feedId) ?></id>
  <title><?= e($settings['company_brand'] ?? '旺特音响') ?> — 新闻资讯</title>
  <subtitle>专业音响、公共广播、会议系统技术文章与产品百科</subtitle>
  <link href="<?= e($base) ?>/" rel="alternate" type="text/html"/>
  <link href="<?= e($base) ?>/feed.xml" rel="self" type="application/atom+xml"/>
  <updated><?= $updated ?></updated>
  <author>
    <name><?= e($settings['company_name'] ?? '润泰商贸') ?></name>
  </author>
  <generator uri="https://wangte.audio">旺特音响 CMS</generator>
<?php foreach ($articles as $a): 
    $url = $base . '/news-detail.php?id=' . $a['id'];
    $published = date(DATE_ATOM, strtotime($a['created_at']));
    $summary = $a['excerpt'] ?: mb_substr(strip_tags($a['content']), 0, 200, 'UTF-8');
?>
  <entry>
    <id>tag:<?= e($host) ?>,<?= substr($a['created_at'],0,10) ?>:news-<?= $a['id'] ?></id>
    <title><?= e($a['title']) ?></title>
    <link href="<?= e($url) ?>"/>
    <updated><?= $published ?></updated>
    <published><?= $published ?></published>
    <category term="<?= e($a['cat_name']) ?>"/>
    <summary type="html"><?= e($summary) ?></summary>
    <content type="html"><?= e(safeContent($a['content'])) ?></content>
  </entry>
<?php endforeach; ?>
</feed>
