<?php
// 简化版文档列表 API
header('Content-Type: application/json; charset=utf-8');

// 禁用缓存
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');

$md_dir = dirname(__FILE__) . '/md';
$version_file = dirname(dirname(__FILE__)) . '/download/version.txt';

// 获取版本号
$current_version = '4.0.1-BetaN(+AU)';
if (file_exists($version_file)) {
    $current_version = trim(file_get_contents($version_file));
}

// 获取所有 md 文件
$articles = array();

if (is_dir($md_dir)) {
    $files = glob($md_dir . '/*.md');
    if ($files !== false) {
        foreach ($files as $file) {
            $name = basename($file, '.md');
            $content = @file_get_contents($file);
            
            $title = $name;
            if ($content && preg_match('/^# (.+)$/m', $content, $matches)) {
                $title = $matches[1];
            }
            
            $articles[] = array(
                'name' => $name,
                'title' => $title,
                'desc' => ''
            );
        }
    }
}

// 输出 JSON
echo json_encode(array(
    'version' => $current_version,
    'articles' => $articles
));
?>