<?php
/**
 * Sitemap XML 生成器 - 百度SEO优化
 * 烟台三明网络科技有限责任公司 yt3m.com
 * 
 * 使用方法：直接访问 /sitemap.xml 即可生成
 * 建议在百度搜索资源平台提交此sitemap
 */

// 防止直接访问
if (basename($_SERVER['PHP_SELF']) == 'sitemap.xml') {
    header('Content-Type: application/xml; charset=utf-8');
}

// 网站基础配置
$site_url = 'https://www.yt3m.com';
$site_name = '烟台三明网络科技';
$site_desc = '烟台三明网络专注APP开发、小程序开发、网站建设、系统定制等互联网服务';
$language = 'zh-CN';

// 最后更新时间
$lastmod = date('Y-m-d');

// 生成XML内容
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
$xml .= "\n";
$xml .= '<!-- Generated by 烟台三明网络 SEO System - ' . $lastmod . ' -->';
$xml .= "\n";
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"';
$xml .= ' xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/"';
$xml .= ' xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';
$xml .= "\n";

// 静态页面列表（优先级高）
$static_pages = array(
    array('loc' => '', 'priority' => '1.0', 'changefreq' => 'daily'),
    array('loc' => 'about.html', 'priority' => '0.8', 'changefreq' => 'weekly'),
    array('loc' => 'case.html', 'priority' => '0.9', 'changefreq' => 'daily'),
    array('loc' => 'news.html', 'priority' => '0.8', 'changefreq' => 'daily'),
    array('loc' => 'contact.html', 'priority' => '0.7', 'changefreq' => 'monthly'),
);

// 案例分类页面（可扩展）
$case_pages = array(
    array('loc' => 'case.html?type=web', 'priority' => '0.8', 'changefreq' => 'weekly'),
    array('loc' => 'case.html?type=app', 'priority' => '0.8', 'changefreq' => 'weekly'),
    array('loc' => 'case.html?type=applet', 'priority' => '0.8', 'changefreq' => 'weekly'),
    array('loc' => 'case.html?type=system', 'priority' => '0.8', 'changefreq' => 'weekly'),
);

// 合并所有页面
$all_pages = array_merge($static_pages, $case_pages);

// 生成URL条目
foreach ($all_pages as $page) {
    $loc = $site_url . '/' . $page['loc'];
    $priority = $page['priority'];
    $changefreq = $page['changefreq'];
    
    $xml .= "  <url>\n";
    $xml .= "    <loc><![CDATA[" . $loc . "]]></loc>\n";
    $xml .= "    <lastmod>" . $lastmod . "</lastmod>\n";
    $xml .= "    <changefreq>" . $changefreq . "</changefreq>\n";
    $xml .= "    <priority>" . $priority . "</priority>\n";
    $xml .= "    <mobile:mobile type=\"pc,wap\"/>\n";
    $xml .= "  </url>\n";
}

// 如果需要从数据库获取动态案例和新闻，可启用以下代码
/*
try {
    require_once dirname(__FILE__) . '/lib/load_obj.php';
    $db = Import::model();
    
    // 获取最新案例（20条）
    $cases = $db->query("SELECT id, title, updatetime FROM case_table ORDER BY updatetime DESC LIMIT 20");
    foreach ($cases as $case) {
        $loc = $site_url . '/case-' . $case['id'] . '.html';
        $lastmod = date('Y-m-d', strtotime($case['updatetime']));
        
        $xml .= "  <url>\n";
        $xml .= "    <loc><![CDATA[" . $loc . "]]></loc>\n";
        $xml .= "    <lastmod>" . $lastmod . "</lastmod>\n";
        $xml .= "    <changefreq>weekly</changefreq>\n";
        $xml .= "    <priority>0.7</priority>\n";
        $xml .= "    <mobile:mobile type=\"pc,wap\"/>\n";
        $xml .= "  </url>\n";
    }
    
    // 获取最新新闻（20条）
    $news = $db->query("SELECT id, title, updatetime FROM news_table ORDER BY updatetime DESC LIMIT 20");
    foreach ($news as $item) {
        $loc = $site_url . '/news-' . $item['id'] . '.html';
        $lastmod = date('Y-m-d', strtotime($item['updatetime']));
        
        $xml .= "  <url>\n";
        $xml .= "    <loc><![CDATA[" . $loc . "]]></loc>\n";
        $xml .= "    <lastmod>" . $lastmod . "</lastmod>\n";
        $xml .= "    <changefreq>weekly</changefreq>\n";
        $xml .= "    <priority>0.6</priority>\n";
        $xml .= "    <mobile:mobile type=\"pc,wap\"/>\n";
        $xml .= "  </url>\n";
    }
} catch (Exception $e) {
    // 忽略数据库错误，只生成静态页面
}
*/

$xml .= "</urlset>";

// 输出
echo $xml;
