Jelajahi Sumber

Added a sitemap generator

Belle Aerni 2 tahun lalu
induk
melakukan
eaca96fd6b
3 mengubah file dengan 59 tambahan dan 3 penghapusan
  1. 0 3
      src/.htaccess
  2. 54 0
      src/Plugins/Sitemap/SitemapPlugin.php
  3. 5 0
      src/index.php

+ 0 - 3
src/.htaccess

@@ -8,7 +8,4 @@ RewriteRule ^Themes/[^/]+/Assets/.+$ - [L]
 # Allow access to the robots.txt file
 RewriteRule ^robots\.txt$ - [L]
 
-# Allow access to the sitemap.xml file
-RewriteRule ^sitemap\.xml$ - [L]
-
 RewriteRule ^(.+)$ index.php [L,QSA]

+ 54 - 0
src/Plugins/Sitemap/SitemapPlugin.php

@@ -0,0 +1,54 @@
+<?php
+
+use AntCMS\AntPlugin;
+use AntCMS\AntPages;
+use AntCMS\AntConfig;
+use AntCMS\AntTools;
+
+class SitemapPlugin extends AntPlugin
+{
+    public function handlePluginRoute(array $route)
+    {
+        $protocol = AntConfig::currentConfig('forceHTTPS') ? 'https' : 'http';
+        $baseURL = AntConfig::currentConfig('baseURL');
+
+        $pages = AntPages::getPages();
+
+        if (extension_loaded('dom')) {
+            $doc = new DOMDocument();
+            $doc->formatOutput = true;
+
+            $root = $doc->createElement('urlset');
+            $root->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
+            $doc->appendChild($root);
+
+            $urls = array();
+            foreach ($pages as $key => $value) {
+                $urls[$key]['url'] = $value['functionalPagePath'];
+                $urls[$key]['lastchange'] = date('Y-m-d', filemtime($value['fullPagePath']));
+            }
+
+            foreach ($urls as $url) {
+                $element = $doc->createElement('url');
+
+                $loc = $doc->createElement('loc', $protocol . '://' . AntTools::repairURL($baseURL . $url['url']));
+                $element->appendChild($loc);
+
+                $lastmod = $doc->createElement('lastmod', $url['lastchange']);
+                $element->appendChild($lastmod);
+
+                $root->appendChild($element);
+            }
+
+            header('Content-Type: application/xml');
+            echo $doc->saveXML();
+            exit;
+        } else {
+            die("AntCMS is unable to generate a sitemap without having the DOM extension loadded in PHP.");
+        }
+    }
+    public function getName()
+    {
+        return 'Sitemap';
+    }
+}

+ 5 - 0
src/index.php

@@ -54,6 +54,11 @@ if ($segments[0] === 'themes' && $segments[2] === 'assets') {
     exit;
 }
 
+if($segments[0] == 'sitemap.xml'){
+    $segments[0] = 'plugin';
+    $segments[1] = 'sitemap';
+}
+
 if ($segments[0] === 'plugin') {
     $pluginName = $segments[1];
     $pluginLoader = new AntPluginLoader();