/* Write a sitmap.xml */
public function writeSitemap() {
// Your domain
$domain = "https://mydomain.ch/";
// Array für error reporting
$errors = [];
$code = "";
// XML prolog
$code .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
// XML root element
$code .= "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\r\n";
// Add the URL of the homepage
$code .= "\t<url>\r\n";
$home = $domain."index.php";
$code .= "\t\t<loc>".$home."</loc>\r\n";
$fileTimeHome = filemtime("index.php");
$lastModifiedHome = date("Y-m-d", $fileTimeHome);
$code .= "\t\t<lastmod>".$lastModifiedHome."</lastmod>\r\n";
$code .= "\t</url>\r\n";
// All pages
foreach ($this->structure as $key => $out1) {
$folder = $key."/";
foreach ($out1 as $out2) {
$file = $folder.$out2['fileName'];
if (file_exists($file)) {
$code .= "\t<url>\n";
$url = $domain.$file;
$code .= "\t\t<loc>".$url."</loc>\r\n";
$fileTime = filemtime($file);
$lastModified = date("Y-m-d", $fileTime);
$code .= "\t\t<lastmod>".$lastModified."</lastmod>\r\n";
$code .= "\t</url>\r\n";
}
else {
$errors[] = "Nicht gefunden: ".$file;
}
}
}
// End XML root element
$code .= "</urlset>\r\n";
// Write Sitemap-File
$handle = fopen("sitemap.xml","w");
fwrite($handle,$code);
fclose($handle);
// Are there errors?
$nrErrors = count($errors);
if ($nrErrors > 0) {
$str = "Ich habe die Sitemap geschrieben. Leider konnte ich nicht alle Files finden:<br>";
foreach ($errors as $out3){
$str .= $out3."<br>";
}
$str .= "<br><strong>Bitte checke das Array mit der Struktur nochmals!</strong>";
return $str;
}
else {
return "Es gab keine Fehler. Habe das Sitemap-File geschrieben.";
}
}