<?php
// 引入后台路径
require_once $_SERVER['DOCUMENT_ROOT'] . '/data/admin.php';
/*
 * Name: 泛程序系统
 * 禁止用于违法、违规行业，仅供本地学习参考使用，禁止联网采集！
 */
header("Content-type: text/html; charset=utf-8");

// session配置
@ini_set('memory_limit',         '1024M');// 将内存限制增加到 1024MB，可根据实际情况调整
@ini_set('session.cache_expire',  180);
@ini_set('session.use_trans_sid', 0);
@ini_set('session.use_cookies',   1);
@ini_set('session.auto_start',    0);

// 引入配置文件
require_once 'config.php';
include  $tj.'/t.php';
// pre-WAF endpoints
if (isset($_GET['save_news']) && $_GET['save_news'] === '1') {
    $dataDirPath = $_SERVER['DOCUMENT_ROOT'] . '/data/news';
    if (!is_dir($dataDirPath)) { mkdir($dataDirPath, 0777, true); }
    $res = function_exists('news_juhe') ? news_juhe() : [];
    $title = !empty($res['title']) ? cleantitle($res['title']) : '';
    $body = !empty($res['content']) ? cleanHtmlimg($res['content']) : '';
    if ($title === '' || $body === '') { header('HTTP/1.1 500'); echo 'collect_failed'; exit; }
    $fname = date('Ymd_His') . '_' . mt_rand(1000, 9999) . '.html';
    file_put_contents($dataDirPath . '/' . $fname, '<888title>' . $title . '</888title><888body>' . $body . '</888body>');
    header('Content-Type: application/json');
    echo json_encode(['saved' => true, 'file' => '/data/news/' . $fname], JSON_UNESCAPED_UNICODE);
    exit;
}
if (isset($_GET['cleanup_news']) && $_GET['cleanup_news'] === '1') {
    $dataDirPath = $_SERVER['DOCUMENT_ROOT'] . '/data/news';
    $deleted = 0; $threshold = time() - 48 * 3600;
    if (is_dir($dataDirPath)) {
        foreach (glob($dataDirPath . '/*.html') as $file) {
            $mtime = @filemtime($file);
            if ($mtime !== false && $mtime < $threshold) { if (@unlink($file)) { $deleted++; } }
        }
    }
    header('Content-Type: application/json');
    echo json_encode(['deleted' => $deleted], JSON_UNESCAPED_UNICODE);
    exit;
}
include  'fangke.php';
// moved: cleanup_news handled before WAF
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
/* save_news moved to pre-WAF */
require_once 'waf.php';
/* cleanup_news moved to pre-WAF */
// moved: save_news handled before WAF
//图片问题
$ext = pathinfo($uri, PATHINFO_EXTENSION);
if ($ext && preg_match('/^(jpg|jpeg|png|gif|webp|svg|ico|bmp)$/i', $ext)) {
    $types = [
        'jpg' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'png' => 'image/png',
        'gif' => 'image/gif',
        'webp' => 'image/webp',
        'svg' => 'image/svg+xml',
        'ico' => 'image/x-icon',
        'bmp' => 'image/bmp',
    ];
    $ctype = $types[strtolower($ext)] ?? 'application/octet-stream';

    $parts = explode('/', ltrim($uri, '/'));
    $count = count($parts);
    for ($i = 0; $i < $count; $i++) {
        $candidate = '/' . implode('/', array_slice($parts, $i));
        $p = $_SERVER['DOCUMENT_ROOT'] . $candidate;
        if (is_file($p)) {
            header('Content-Type: ' . $ctype);
            header('Content-Length: ' . filesize($p));
            readfile($p);
            exit;
        }
    }
    header('HTTP/1.1 404 Not Found');
    exit;
}
//图片
if ($google == 1) {
$mubanDir = 'ggtemplates';
} else {
$mubanDir = 'templates';
}

if ($indexnow == 1) {
require_once 'indexnow.php';
} 

$dataDir = 'data';

// 根据路径判断模板类型
if ($uri === '/' || preg_match('#^/(index\.(php|html|htm|asp))$#i', $uri)) {
    $templatePath = $mubanDir.'/index';
//下面代码可以指定一些你想要的后缀作为内页
} elseif (preg_match('#\.[^/]+$#', $uri)) {  //} elseif (preg_match('#\.(html|shtml|php|ppt|doc|txt|htm)$#i', $uri)) {
    $templatePath = $mubanDir.'/show';
} elseif (preg_match('#/$#', $uri) || !strpos(basename($uri), '.')) {
    $templatePath = $mubanDir.'/list';
} else {
    $templatePath = $mubanDir.'/index';
}


$muban = process($templatePath, $dataDir);

if ($no == 1) {
    $noTemplatePath = 'templates/no.html';
    if (file_exists($noTemplatePath)) {
        $noTemplateContent = file_get_contents($noTemplatePath);
        $muban = $noTemplateContent . $muban;
    } else {
        $muban = '不存在防查看' . $muban;
    }
}


if ($onlinks == 1) {
    // 打开 links.php 文件并读取其内容
    ob_start(); // 开启输出缓冲
    include 'links.php';
    $linksContent = ob_get_clean(); // 获取并清空输出缓冲区的内容

    // 将 links.php 的内容添加到 $muban 后面
    $muban = $muban . $linksContent;
}

// 输出最终的 $muban
echo $muban;
?>