<?php

header('Content-Type: application/xml; charset=utf-8');


/* ============================================================
   AARSH INSTITUTE
   DYNAMIC XML SITEMAP
   ============================================================ */

$baseURL = 'https://aarshinstitute.com';

$rootPath = __DIR__ . '/aarsh';


/* ============================================================
   EXCLUDED DIRECTORIES
   ============================================================ */

$excludedDirectories = [

    'admin',
    'includes',
    'config',
    'private',
    'uploads'

];


/* ============================================================
   EXCLUDED FILES
   ============================================================ */

$excludedFiles = [

    'header.php',
    'footer.php',

    '404.php',
    'error.php',

    'sitemap.php'

];


/* ============================================================
   COLLECT PUBLIC PHP FILES
   ============================================================ */

$urls = [];


function scanPublicPages(
    string $directory,
    string $relativePath = ''
) {

    global
        $excludedDirectories,
        $excludedFiles,
        $urls;

    if (!is_dir($directory)) {
        return;
    }


    $items = scandir($directory);

    if ($items === false) {
        return;
    }


    foreach ($items as $item) {

        if (
            $item === '.' ||
            $item === '..'
        ) {
            continue;
        }


        $fullPath = $directory . DIRECTORY_SEPARATOR . $item;


        /* ====================================================
           DIRECTORY
        ==================================================== */

        if (is_dir($fullPath)) {

            if (
                in_array(
                    strtolower($item),
                    array_map('strtolower', $excludedDirectories),
                    true
                )
            ) {
                continue;
            }


            scanPublicPages(
                $fullPath,
                $relativePath . $item . '/'
            );

            continue;
        }


        /* ====================================================
           FILE
        ==================================================== */

        if (!is_file($fullPath)) {
            continue;
        }


        $extension = strtolower(
            pathinfo($item, PATHINFO_EXTENSION)
        );


        if ($extension !== 'php') {
            continue;
        }


        if (
            in_array(
                $item,
                $excludedFiles,
                true
            )
        ) {
            continue;
        }


        /*
        --------------------------------------------------------
        Convert file path into website URL
        --------------------------------------------------------
        */

        if ($item === 'index.php') {

            $urlPath = $relativePath;

        } else {

            $urlPath =
                $relativePath .
                $item;

        }


        $urls[] = [

            'url' =>
                $GLOBALS['baseURL'] .
                '/aarsh/' .
                ltrim($urlPath, '/'),

            'lastmod' =>
                date(
                    'Y-m-d',
                    filemtime($fullPath)
                )

        ];

    }

}


/* ============================================================
   START SCANNING
   ============================================================ */

scanPublicPages($rootPath);


/* ============================================================
   REMOVE DUPLICATES
   ============================================================ */

$unique = [];

foreach ($urls as $item) {

    $unique[$item['url']] = $item;

}

$urls = array_values($unique);


/* ============================================================
   SORT URLS
   ============================================================ */

usort(
    $urls,
    function ($a, $b) {

        return strcmp(
            $a['url'],
            $b['url']
        );

    }
);


/* ============================================================
   XML OUTPUT
   ============================================================ */

echo '<?xml version="1.0" encoding="UTF-8"?>';

echo "\n";

echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

echo "\n";


foreach ($urls as $item) {

    echo "    <url>\n";

    echo "        <loc>" .
        htmlspecialchars(
            $item['url'],
            ENT_XML1,
            'UTF-8'
        ) .
        "</loc>\n";

    echo "        <lastmod>" .
        $item['lastmod'] .
        "</lastmod>\n";

    echo "    </url>\n";

}


echo '</urlset>';