<?php
include_once(dirname(__FILE__). "/common.php");
// Initialize boards array
$boards = array();
// 게시판 리스트
$sql = " SELECT * FROM {$g5['board_table']} WHERE bo_read_level = 1 order by bo_order ";
$result = sql_query($sql);
while ($row = sql_fetch_array($result)) {
    $boards[] = $row;
}

// Start output buffering
ob_start();
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- Add homepage -->
    <url>
        <loc><?php echo G5_URL; ?></loc>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
    </url>

    <!--Add boards-->
    <?php if (!empty($boards)) { ?>
        <?php foreach ($boards as $board) { ?>
            <url>
                <loc><?php echo G5_URL."/".$board['bo_table']; ?></loc>
                <changefreq>daily</changefreq>
                <priority>1.0</priority>
                <lastmod><?php echo date('Y-m-d'); ?></lastmod>
            </url>
            <?php
            $sql = "SELECT *, wr_datetime as last_update FROM {$g5['write_prefix']}{$board['bo_table']} WHERE wr_is_comment = 0";
            $result2 = sql_query($sql);
            if ($result2) {
                while ($row = sql_fetch_array($result2)) {
                    $url = G5_URL."/".urlencode($board['bo_table'])."/".urlencode($row['wr_id']);
                    $lastmod = date('Y-m-d', strtotime($row['last_update']));
            ?>
                        <url>
                            <loc><![CDATA[<?php echo $url; ?>]]></loc>
                            <changefreq>weekly</changefreq>
                            <priority>0.8</priority>
                            <lastmod><?php echo $lastmod; ?></lastmod>
                        </url>
            <?php
                }
            }
            ?>
        <?php } ?>
    <?php } ?>
    
    <!-- Add shop main page -->
    <url>
        <loc><?php echo G5_SHOP_URL; ?></loc>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
        <lastmod><?php echo date('Y-m-d'); ?></lastmod>
    </url>

    <!-- Add shop categories -->
    <?php
    $cat_sql = " SELECT ca_id FROM {$g5['g5_shop_category_table']} WHERE ca_use = '1' ORDER BY ca_order, ca_id ";
    $cat_result = sql_query($cat_sql);
    if ($cat_result) {
        while($cat = sql_fetch_array($cat_result)) {
            $cat_url = G5_SHOP_URL.'/list.php?ca_id='.urlencode($cat['ca_id']);
    ?>
            <url>
                <loc><![CDATA[<?php echo $cat_url; ?>]]></loc>
                <changefreq>weekly</changefreq>
                <priority>0.8</priority>
                <lastmod><?php echo date('Y-m-d'); ?></lastmod>
            </url>
    <?php 
        }
    }
    ?>

    <!--Add shop items-->
    <?php    
    $shop_sql = " SELECT it_id, it_update_time FROM {$g5['g5_shop_item_table']} WHERE it_use = '1' ORDER BY it_id DESC ";
    $shop_result = sql_query($shop_sql);
    if($shop_result) {
        while($shop_item = sql_fetch_array($shop_result)) {
            $item_url = G5_SHOP_URL.'/item.php?it_id='.urlencode($shop_item['it_id']);
            $lastmod = $shop_item['it_update_time'] ? date('Y-m-d', strtotime($shop_item['it_update_time'])) : date('Y-m-d');
    ?>
            <url>
                <loc><![CDATA[<?php echo $item_url; ?>]]></loc>
                <changefreq>weekly</changefreq>
                <priority>0.8</priority>
                <lastmod><?php echo $lastmod; ?></lastmod>
            </url>
    <?php 
        }
    }
    ?>
</urlset>
<?php
// Get the buffer content and save to file
$xml_content = ob_get_clean();
// Check if the content is valid XML
if (!$xml_content) {
    alert('Error generating sitemap');
    goto_url(G5_URL);
    exit;
}
// Try to save the file with error handling
$save_path = dirname(__FILE__).'/sitemap.xml';
if (file_put_contents($save_path, $xml_content) === false) {
    alert('Error saving sitemap.xml');
    goto_url(G5_URL);
    exit;
}

// Show success message with custom HTML page
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="refresh" content="2;url=<?php echo G5_URL ?>">
    <style>
        .success-message {
            text-align: center;
            padding: 50px;
            background: #f0f8ff;
            border-radius: 10px;
            margin: 100px auto;
            max-width: 500px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        .success-icon {
            color: #4CAF50;
            font-size: 48px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="success-message">
        <div class="success-icon">✓</div>
        <h2>Success!</h2>
        <p>sitemap.xml has been successfully generated and saved.</p>
        <p>Redirecting to main page...</p>
    </div>
</body>
</html>
<?php
exit;