<?

$file = $_SERVER['REQUEST_URI'];
$filepath = $_SERVER['DOCUMENT_ROOT'] . $file;

if (file_exists($filepath)) {
    $content = file_get_contents($filepath);

    $content = str_replace("bewave.ru", $_SERVER["SERVER_NAME"], $content);
    $content = str_replace("", '', $content);
    $content = str_replace("www.sitemaps", 'www.sitemaps', $content);



    // Работа с поддоменами 

    if ($_SERVER["SERVER_NAME"] !== "bewave.ru") {
        if (
            strpos($_SERVER["REQUEST_URI"], "sitemap-iblock-6.xml") !== false ||
            strpos($_SERVER["REQUEST_URI"], "sitemap-iblock-12.xml") !== false ||
            strpos($_SERVER["REQUEST_URI"], "sitemap-iblock-15.xml") !== false ||
            strpos($_SERVER["REQUEST_URI"], "sitemap-iblock-16.xml") !== false

        ) {
            $file_404 = $_SERVER['DOCUMENT_ROOT'] . '/404.php';
            include($file_404);
        }


        if (strpos($content, '<url>')) {
            $ar = explode("</url>", $content);
        } else {
            $ar = explode("</sitemap>", $content);
        }
        foreach ($ar as $key => $arItem) {
            if (
                strpos($arItem, "iblock-6.xml") !== false ||
                strpos($arItem, "iblock-12.xml") !== false ||
                strpos($arItem, "iblock-15.xml") !== false ||
                strpos($arItem, "iblock-16.xml") !== false ||
                strpos($arItem, "encyclopedia") !== false ||
                strpos($arItem, "job") !== false ||
                strpos($arItem, "case") !== false ||
                strpos($arItem, "blog") !== false ||
                strpos($arItem, "technologies") !== false ||
                strpos($arItem, "about") !== false


            ) {
                unset($ar[$key]);
            }
        }

        $ar = array_values($ar);

        if (strpos($content, '<url>')) {
            $content = implode("</url>", $ar);
        } else {
            $content = implode("</sitemap>", $ar);
        }
    }

    header('Content-Type: text/xml; charset=utf-8');
    echo $content;
} else {
    $file_404 = $_SERVER['DOCUMENT_ROOT'] . '/404.php';
    if (file_exists($file_404)) {
        include($file_404);
    } else {
        header("HTTP/1.0 404 Not Found");
        echo "Страница не найдена";
    }
}
