File: /home/u590867752/domains/himanshuartinstitute.co.in/public_html/sitemap.php
<?php
set_time_limit(120);
date_default_timezone_set("Asia/Kolkata");
$siteUrl = "https://www.himanshuartinstitute.co.in";
$rootDir = __DIR__;
$cacheDir = __DIR__ . "/SITEMAP";
$cacheXmlFile = $cacheDir . "/sitemap-dynamic-cache.xml";
$cacheJsonFile = $cacheDir . "/sitemap-status.json";
$cacheSeconds = 3600; // 1 hour
$allowedExtensions = ["html", "htm", "php"];
$excludeDirs = [
"admins",
"admin",
"baba",
"actions",
"cookies",
"newsletter",
"SITEMAP",
"search",
"codes",
"css",
"js",
"images",
"uploads",
"ckeditor",
"tcpdf",
"pdf_templates",
"shop/wp-admin",
"shop/wp-content",
"shop/wp-includes",
"video-gallery/footer-videos"
];
$excludeFiles = [
"sitemap.php",
"feed.php",
"feeds.php",
"xmlrpc.php",
"wp-config.php",
"functions.php",
"bootstrap.php"
];
$excludeNameContains = [
"slider",
"slides",
"header",
"hedr",
"footer",
"foter",
"mobs",
"mobls",
"sitemap",
"successbox",
"award-link",
"awards-lists",
"offers",
"afilations",
"check",
"test",
"demo",
"sample",
"copy",
"backup",
"old"
];
function hai_path_has_excluded_dir($relativePath, $excludeDirs){
$relativePath = trim(str_replace("\\", "/", $relativePath), "/");
foreach($excludeDirs as $dir){
$dir = trim(str_replace("\\", "/", $dir), "/");
if($relativePath === $dir || strpos($relativePath, $dir . "/") === 0){
return true;
}
}
return false;
}
function hai_is_noindex_file($filePath){
$content = @file_get_contents($filePath, false, null, 0, 30000);
if($content === false){
return false;
}
return stripos($content, "noindex") !== false;
}
function hai_make_url($siteUrl, $relativePath){
$relativePath = str_replace("\\", "/", $relativePath);
$relativePath = ltrim($relativePath, "/");
if(preg_match("#(^|/)index\.(html|htm|php)$#i", $relativePath)){
$relativePath = preg_replace("#index\.(html|htm|php)$#i", "", $relativePath);
}
$parts = explode("/", $relativePath);
$encodedParts = [];
foreach($parts as $part){
if($part !== ""){
$encodedParts[] = rawurlencode($part);
}
}
$encodedPath = implode("/", $encodedParts);
if($relativePath !== "" && substr($relativePath, -1) === "/"){
$encodedPath .= "/";
}
return rtrim($siteUrl, "/") . "/" . $encodedPath;
}
function hai_priority($relativePath){
$relativePath = str_replace("\\", "/", $relativePath);
if(preg_match("#^index\.(html|htm|php)$#i", $relativePath)){
return "1.00";
}
if(preg_match("#/index\.(html|htm|php)$#i", $relativePath)){
return "0.80";
}
return "0.70";
}
function hai_get_sitemap_urls($siteUrl, $rootDir, $allowedExtensions, $excludeDirs, $excludeFiles, $excludeNameContains){
$urls = [];
$directory = new RecursiveDirectoryIterator($rootDir, FilesystemIterator::SKIP_DOTS);
$filter = new RecursiveCallbackFilterIterator($directory, function($current) use ($rootDir, $excludeDirs){
$path = $current->getPathname();
$relativePath = substr($path, strlen($rootDir) + 1);
$relativePath = str_replace("\\", "/", $relativePath);
if($current->isDir()){
if(substr(basename($relativePath), 0, 1) === "."){
return false;
}
if(hai_path_has_excluded_dir($relativePath, $excludeDirs)){
return false;
}
}
return true;
});
$iterator = new RecursiveIteratorIterator($filter);
foreach($iterator as $file){
if(!$file->isFile()){
continue;
}
$filePath = $file->getPathname();
$relativePath = substr($filePath, strlen($rootDir) + 1);
$relativePath = str_replace("\\", "/", $relativePath);
$fileName = basename($relativePath);
$extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
if(!in_array($extension, $allowedExtensions)){
continue;
}
if(in_array($fileName, $excludeFiles)){
continue;
}
foreach($excludeNameContains as $blockedName){
if(stripos($fileName, $blockedName) !== false){
continue 2;
}
}
if(hai_is_noindex_file($filePath)){
continue;
}
$urls[] = [
"loc" => hai_make_url($siteUrl, $relativePath),
"lastmod" => gmdate("Y-m-d\TH:i:s+00:00", filemtime($filePath)),
"priority" => hai_priority($relativePath),
"file" => $relativePath,
"mtime" => filemtime($filePath)
];
}
usort($urls, function($a, $b){
return strcmp($a["loc"], $b["loc"]);
});
return $urls;
}
function hai_build_xml($urls){
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
foreach($urls as $url){
$xml .= " <url>\n";
$xml .= " <loc>" . htmlspecialchars($url["loc"], ENT_XML1, "UTF-8") . "</loc>\n";
$xml .= " <lastmod>" . $url["lastmod"] . "</lastmod>\n";
$xml .= " <changefreq>weekly</changefreq>\n";
$xml .= " <priority>" . $url["priority"] . "</priority>\n";
$xml .= " </url>\n";
}
$xml .= "</urlset>";
return $xml;
}
function hai_rebuild_sitemap($siteUrl, $rootDir, $allowedExtensions, $excludeDirs, $excludeFiles, $excludeNameContains, $cacheXmlFile, $cacheJsonFile){
$urls = hai_get_sitemap_urls($siteUrl, $rootDir, $allowedExtensions, $excludeDirs, $excludeFiles, $excludeNameContains);
$xml = hai_build_xml($urls);
$latest = null;
foreach($urls as $url){
if($latest === null || $url["mtime"] > $latest["mtime"]){
$latest = $url;
}
}
$status = [
"status" => "ok",
"site_url" => $siteUrl,
"sitemap_url" => $siteUrl . "/sitemap.php",
"total_urls" => count($urls),
"last_checked" => date("Y-m-d H:i:s"),
"latest_page" => $latest ? $latest["loc"] : "",
"latest_modified" => $latest ? date("Y-m-d H:i:s", $latest["mtime"]) : ""
];
@file_put_contents($cacheXmlFile, $xml);
@file_put_contents($cacheJsonFile, json_encode($status));
return [$xml, $status];
}
$forceRefresh = isset($_GET["refresh"]) && $_GET["refresh"] == "1";
$cacheIsFresh = file_exists($cacheXmlFile) && (time() - filemtime($cacheXmlFile) < $cacheSeconds);
if($forceRefresh || !$cacheIsFresh){
list($xml, $status) = hai_rebuild_sitemap(
$siteUrl,
$rootDir,
$allowedExtensions,
$excludeDirs,
$excludeFiles,
$excludeNameContains,
$cacheXmlFile,
$cacheJsonFile
);
}else{
$xml = file_get_contents($cacheXmlFile);
$status = file_exists($cacheJsonFile) ? json_decode(file_get_contents($cacheJsonFile), true) : [];
}
if(isset($_GET["format"]) && $_GET["format"] === "json"){
header("Content-Type: application/json; charset=utf-8");
echo json_encode($status);
exit;
}
header("Content-Type: application/xml; charset=utf-8");
echo $xml;
?>