From d190d27c9cebe545f37570d26a36ab339c3e552b Mon Sep 17 00:00:00 2001 From: Belle Aerni Date: Sat, 20 Apr 2024 10:36:42 -0700 Subject: [PATCH] Got the robots.txt and sitemap plugins working --- phpstan.neon | 3 +- src/AntCMS/PluginController.php | 27 +++++++++++ src/AntCMS/PluginLoader.php | 26 ----------- src/Bootstrap.php | 2 +- src/Plugins/Robotstxt/Controller.php | 22 +++++++++ src/Plugins/Robotstxt/RobotstxtPlugin.php | 28 ------------ src/Plugins/Sitemap/Controller.php | 53 ++++++++++++++++++++++ src/Plugins/Sitemap/SitemapPlugin.php | 55 ----------------------- src/index.php | 27 +++-------- tests/MarkdownTest.php | 4 +- 10 files changed, 113 insertions(+), 134 deletions(-) create mode 100644 src/AntCMS/PluginController.php delete mode 100644 src/AntCMS/PluginLoader.php create mode 100644 src/Plugins/Robotstxt/Controller.php delete mode 100644 src/Plugins/Robotstxt/RobotstxtPlugin.php create mode 100644 src/Plugins/Sitemap/Controller.php delete mode 100644 src/Plugins/Sitemap/SitemapPlugin.php diff --git a/phpstan.neon b/phpstan.neon index 1ef6fd4..1d51e43 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,7 +6,8 @@ parameters: analyse: - src/Vendor - src/Cache - - src/Plugins + - src/Plugins/Admin + - src/Plugins/Profile ignoreErrors: - '#^Call to an undefined method AntCMS\\Cache\:\:get\(\)\.$#' - '#^Call to an undefined method AntCMS\\Cache\:\:purge\(\)\.$#' diff --git a/src/AntCMS/PluginController.php b/src/AntCMS/PluginController.php new file mode 100644 index 0000000..1735a49 --- /dev/null +++ b/src/AntCMS/PluginController.php @@ -0,0 +1,27 @@ += 2 && $list[0] === '.' && $list[1] === '..') { + unset($list[0]); + unset($list[1]); + } + + foreach($list as $pluginName) { + $className = "\AntCMS\\Plugins\\$pluginName\\Controller"; + if(class_exists($className)) { + new $className(); + } + } + } +} diff --git a/src/AntCMS/PluginLoader.php b/src/AntCMS/PluginLoader.php deleted file mode 100644 index 4067785..0000000 --- a/src/AntCMS/PluginLoader.php +++ /dev/null @@ -1,26 +0,0 @@ - */ - public function loadPlugins(): array - { - $plugins = []; - - $files = Tools::getFileList(antPluginPath, null, true); - - foreach ($files as $file) { - if (str_ends_with($file, "Plugin.php")) { - include_once Tools::repairFilePath($file); - $className = pathinfo($file, PATHINFO_FILENAME); - $plugins[] = new $className(); - } - } - - return $plugins; - } -} diff --git a/src/Bootstrap.php b/src/Bootstrap.php index 6315ebd..39bfa86 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -18,7 +18,7 @@ $classMapPath = AntCachePath . DIRECTORY_SEPARATOR . 'classMap.php'; $loader = new \AntCMS\AntLoader(['path' => $classMapPath]); $loader->addNamespace('AntCMS\\', __DIR__ . DIRECTORY_SEPARATOR . 'AntCMS'); -$loader->addNamespace('Plugins\\', __DIR__ . DIRECTORY_SEPARATOR . 'Plugins'); +$loader->addNamespace('AntCMS\\Plugins\\', __DIR__ . DIRECTORY_SEPARATOR . 'Plugins'); $loader->checkClassMap(); $loader->register(); diff --git a/src/Plugins/Robotstxt/Controller.php b/src/Plugins/Robotstxt/Controller.php new file mode 100644 index 0000000..e91a931 --- /dev/null +++ b/src/Plugins/Robotstxt/Controller.php @@ -0,0 +1,22 @@ +scheme; + $baseURL = Config::currentConfig('baseURL'); + + echo 'User-agent: *' . PHP_EOL; + echo 'Sitemap: ' . $protocol . '://' . Tools::repairURL($baseURL . '/sitemap.xml' . PHP_EOL); + Flight::response()->setHeader('Content-Type', 'text/plain'); + }); + } +} diff --git a/src/Plugins/Robotstxt/RobotstxtPlugin.php b/src/Plugins/Robotstxt/RobotstxtPlugin.php deleted file mode 100644 index 84b692e..0000000 --- a/src/Plugins/Robotstxt/RobotstxtPlugin.php +++ /dev/null @@ -1,28 +0,0 @@ -scheme; + $baseURL = Config::currentConfig('baseURL'); + + $pages = Pages::getPages(); + + if (extension_loaded('dom')) { + $domDocument = new \DOMDocument('1.0', 'UTF-8'); + $domDocument->formatOutput = true; + + $domElement = $domDocument->createElement('urlset'); + $domElement->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); + $domDocument->appendChild($domElement); + + $urls = []; + 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 = $domDocument->createElement('url'); + + $loc = $domDocument->createElement('loc', $protocol . '://' . Tools::repairURL($baseURL . $url['url'])); + $element->appendChild($loc); + + $lastmod = $domDocument->createElement('lastmod', $url['lastchange']); + $element->appendChild($lastmod); + + $domElement->appendChild($element); + } + + echo $domDocument->saveXML(); + Flight::response()->header('Content-Type', 'application/xml'); + } else { + Flight::halt(503, "AntCMS is unable to generate a sitemap without having the DOM extension loadded in PHP."); + } + }); + } +} diff --git a/src/Plugins/Sitemap/SitemapPlugin.php b/src/Plugins/Sitemap/SitemapPlugin.php deleted file mode 100644 index 4b34336..0000000 --- a/src/Plugins/Sitemap/SitemapPlugin.php +++ /dev/null @@ -1,55 +0,0 @@ -formatOutput = true; - - $domElement = $domDocument->createElement('urlset'); - $domElement->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); - $domDocument->appendChild($domElement); - - $urls = []; - 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 = $domDocument->createElement('url'); - - $loc = $domDocument->createElement('loc', $protocol . '://' . Tools::repairURL($baseURL . $url['url'])); - $element->appendChild($loc); - - $lastmod = $domDocument->createElement('lastmod', $url['lastchange']); - $element->appendChild($lastmod); - - $domElement->appendChild($element); - } - - header('Content-Type: application/xml'); - echo $domDocument->saveXML(); - exit; - } else { - die("AntCMS is unable to generate a sitemap without having the DOM extension loadded in PHP."); - } - } - - public function getName(): string - { - return 'Sitemap'; - } -} diff --git a/src/index.php b/src/index.php index 8cf43b6..11b70e4 100644 --- a/src/index.php +++ b/src/index.php @@ -1,5 +1,6 @@ addResponseBodyCallback([CompressionBuffer::class, 'handler']); +// HTTPS redirects if (!Flight::request()->secure && !Enviroment::isCli() && Config::currentConfig('forceHTTPS')) { Flight::redirect('https://' . Flight::request()->host . Flight::request()->url); exit; } +// Asset delivery Flight::route('GET /themes/*/assets', function () use ($antCms, $requestUri): void { $antCms->serveContent(AntDir . $requestUri); }); +/// ACME challenges for certificate renewals Flight::route('GET .well-known/acme-challenge/*', function () use ($antCms, $requestUri): void { $antCms->serveContent(AntDir . $requestUri); }); -/* -if ($antRouting->checkMatch('/sitemap.xml')) { - $antRouting->setRequestUri('/plugin/sitemap'); -} - -if ($antRouting->checkMatch('/robots.txt')) { - $antRouting->setRequestUri('/plugin/robotstxt'); -} - -if ($antRouting->checkMatch('/admin/*')) { - $antRouting->requestUriUnshift('plugin'); -} - -if ($antRouting->checkMatch('/profile/*')) { - $antRouting->requestUriUnshift('plugin'); -} - -if ($antRouting->checkMatch('/plugin/*')) { - $antRouting->routeToPlugin(); -} -*/ +// Register routes for plugins +PluginController::init(); Flight::route('GET /', function () use ($antCms): void { if (!file_exists(antUsersList)) { diff --git a/tests/MarkdownTest.php b/tests/MarkdownTest.php index 02e630a..644ab64 100644 --- a/tests/MarkdownTest.php +++ b/tests/MarkdownTest.php @@ -40,9 +40,9 @@ public function testMarkdownIsFast(): void } + /* PHP's file modified time cache is causing issues. I should look at this later public function testMarkdownCacheWorks(): void { - return; // PHP's file modified time cache is causing issues. I should look at this later $markdown = file_get_contents(antContentPath . DIRECTORY_SEPARATOR . 'index.md'); $currentConfig = Config::currentConfig(); @@ -76,5 +76,5 @@ public function testMarkdownCacheWorks(): void echo "\n Markdown rendering speed with cache: {$withCache} VS without: {$withoutCache} \n\n"; $this->assertLessThan($withoutCache, $withCache, "Cache didn't speed up rendering!"); - } + }*/ }