<?php
header('Access-Control-Allow-Origin: *');
error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE & ~E_DEPRECATED);
session_start();
require_once(__DIR__."/db/connection.php");
header('Content-Type: application/xml; charset=utf-t');
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
if (is_ssl()) { $protocol = 'https'; } else { $protocol = 'http'; }
$base_url = $protocol ."://". $_SERVER['SERVER_NAME'] . str_replace("sitemap.php","viewer/",$_SERVER['SCRIPT_NAME']);
$sql_maps = "SELECT code,date_created,friendly_url FROM sml_maps WHERE include_sitemap=1 ORDER BY id DESC";
$result_maps = $mysqli->query($sql_maps);
if ($result_maps->num_rows > 0) {
    while($row = $result_maps->fetch_assoc()) {
        $friendly_url = $row['friendly_url'];
        if(!empty($friendly_url)) {
            $loc = $base_url . htmlspecialchars($friendly_url);
        } else {
            $loc = $base_url . 'index.php?code=' . htmlspecialchars($row['code']);
        }
        $loc_xml = htmlspecialchars($loc, ENT_QUOTES | ENT_XML1, 'UTF-8');
        $lastmod = date('c', strtotime($row['date_created']));
        echo "  <url>\n";
        echo "    <loc>" . $loc_xml . "</loc>\n";
        echo "    <lastmod>" . $lastmod . "</lastmod>\n";
        echo "    <changefreq>weekly</changefreq>\n";
        echo "    <priority>0.8</priority>\n";
        echo "  </url>\n";
    }
}
$sql_markers = "SELECT m.id AS marker_id, mp.code AS map_code, mp.friendly_url, m.name
                FROM sml_markers m 
                JOIN sml_maps mp ON m.id_map = mp.id 
                WHERE m.active=1 AND m.to_validate=0 AND mp.include_sitemap=1
                ORDER BY m.id DESC";

$result_markers = $mysqli->query($sql_markers);
if ($result_markers->num_rows > 0) {
    while($row = $result_markers->fetch_assoc()) {
        if(empty($row['friendly_url'])) $row['friendly_url']=$row['map_code'];
        $loc = $base_url . 'index.php?code=' . htmlspecialchars($row['map_code']) . '&amp;m=' . $row['marker_id'];
        $loc = $base_url . $row['friendly_url'] ."--".$row['marker_id']."-".slugify(str_replace(" ","-",strtolower($row['name'])));
        $lastmod = date('c');
        $loc_xml = htmlspecialchars($loc, ENT_QUOTES | ENT_XML1, 'UTF-8');
        echo "  <url>\n";
        echo "    <loc>" . $loc_xml . "</loc>\n";
        echo "    <lastmod>" . $lastmod . "</lastmod>\n";
        echo "    <changefreq>monthly</changefreq>\n";
        echo "    <priority>0.5</priority>\n";
        echo "  </url>\n";
    }
}
echo '</urlset>';
$mysqli->close();

function is_ssl() {
    if ( isset( $_SERVER['HTTPS'] ) ) {
        if ( 'on' == strtolower( $_SERVER['HTTPS'] ) ) {
            return true;
        }
        if ( '1' == $_SERVER['HTTPS'] ) {
            return true;
        }
    } elseif ( isset( $_SERVER['SERVER_PORT'] ) && ( '443' == $_SERVER['SERVER_PORT'] ) ) {
        return true;
    }
    return false;
}

function slugify($string) {
    $string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
    $string = strtolower($string);
    $string = preg_replace('/[^a-z0-9\s-]/', '', $string);
    $string = preg_replace('/[\s-]+/', '-', $string);
    return trim($string, '-');
}
?>
