<?php
/**
 * 网站sitemap.xml 文件
 * @copyright 2025-浙江引擎力营销策划有限公司
 * @author ccflow<sunshinecc1@163.com>
 * @since 2025-01-16
 */
require_once "runtime.php";
require_once "func.php";

//网站语言
$defLang = M("Setting")->def_lang;
//模版id
$tplId = M("Content_Template")->select("id")->where("name = ?", "basic")->fetchRow()->id;

$list = M("Content")->select()
    ->where("lang = ?  AND (uri != ? AND uri != ?) ", array($defLang, "404", "search"))
    ->order("level ASC, rank ASC")
    ->fetchRows();

header("Content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
echo "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";

$urlList = array();
foreach ($list as $row) {

  //把 basic 页面全部屏蔽掉
    if ($row->tpl_id == $tplId) {
        continue;
    }

    //把 禁止抓取的页面 过滤出来
    if ($row->deny_spider == 1) {
        continue;
    }

    //把没有勾 立即发布 的页面过滤出来
    if ($row->is_enabled == 0) {
        continue;
    }

    //把删掉页面过滤出来
    if ($row->status == 2) {
        continue;
    }

    //把发布时间大于当前时间的项目过滤出来
    if ($row->publish_time >= time()) {
        continue;
    }

    //如果有重定向就找重定向的链接
    if ($row->redirect) {
        $row->url = "/" . ltrim($row->redirect, "/");
    }

    // 跳过包含未编码特殊字符的URL
    if (strstr($row->url, "&")) {
        continue;
    }

    // 跳过包含javaScript的URL
    if (strstr($row->url, "javaScript;;")) {
        continue;
    }

    // 跳过包含外链的url
    if (strstr($row->url, "http://")) {
        continue;
    }

    // 跳过包含外链的url
    if (strstr($row->url, "https://")) {
        continue;
    }

    // 去重
    if ($row->url && isset($urlList[$row->url])) {
        continue;
    }
    $urlList[$row->url] = true;

    echo "<url><loc>https://" . $_SERVER["HTTP_HOST"] . $row->url . "</loc></url>";

}
echo "</urlset>";
?>
