<?php
/**
 * 标签页
 * 
 * @version        $id:tags.php 2010-06-30 11:43:09 tianya $
 * @package        DedeX.Site
 * @license        GNU GPL v2 (/license.txt)
 */
require_once(dirname(__FILE__)."/../system/common.inc.php");
require_once(DEDEINC."/archive/taglist.class.php");
// ========== 新增：伪静态开启后将动态URL 301重定向到伪静态地址 ==========
if ($cfg_rewrite == 'Y') {
    $requestUri = $_SERVER['REQUEST_URI'];
    // 仅拦截直接访问 /apps/tags.php 的请求（避免将内部重写的伪静态请求也重定向）
    if (preg_match('#^/apps/tags\.php#i', $requestUri)) {
        $query = $_SERVER['QUERY_STRING'] ?? '';
        $tagId = '';
        $page = '';
        // 匹配查询字符串格式：/数字 或 /数字/数字
        if (preg_match('#^/(\d+)(?:/(\d+))?#', $query, $matches)) {
            $tagId = $matches[1];
            $page = isset($matches[2]) ? $matches[2] : '';
        }
        // 构建目标伪静态URL
        if ($tagId !== '') {
            $redirect_url = ($page !== '') ? "/tags/{$tagId}/{$page}" : "/tags/{$tagId}";
        } else {
            $redirect_url = "/tags";
        }
        // 发送301状态码并跳转
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: {$redirect_url}");
        exit();
    }
}
// ========== 重定向逻辑结束 ==========
//根据流量统计，限制用户浏览
if ($cfg_access == 'Y') {
    $ip = GetIP();
    $moon = time() - (24 * 60 * 60);
    $flow = $dsql->GetOne("SELECT COUNT(DISTINCT id) AS dd FROM `#@__statistics_detail` WHERE ip='$ip' AND t>='$moon' AND url_type IN (4, -1)");
    if ($flow && $flow['dd'] > $cfg_access_count) {
        http_response_code(403);
        exit();
    }
}
$PageNo = 1;
if (isset($_SERVER['QUERY_STRING'])) {
    $tag = trim($_SERVER['QUERY_STRING']);
    $tags = explode('/', $tag);
    if (isset($tags[1])) $tag = $tags[1];
    if (isset($tags[2])) $PageNo = intval($tags[2]);
} else {
    $tag = '';
}
$tag = FilterSearch(urldecode($tag));
if ($tag != addslashes($tag)) $tag = '';
if ($tag == '') {
    $dlist = new TagList($tag, 'tag.htm');
} else {
    $dlist = new TagList($tag, 'tag_list.htm');
}
$dlist->Display();
exit();
?>