Skip to content

Commit

Permalink
Got the robots.txt and sitemap plugins working
Browse files Browse the repository at this point in the history
  • Loading branch information
BelleNottelling committed Apr 20, 2024
1 parent 711c7dc commit d190d27
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 134 deletions.
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\(\)\.$#'
27 changes: 27 additions & 0 deletions src/AntCMS/PluginController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace AntCMS;

class PluginController
{
public static array $plugins = [];

/**
* Registers all plugin routes & sets up needed info for the plugin controller
*/
public static function init(): void
{
$list = scandir(antPluginPath);
if (count($list) >= 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();
}
}
}
}
26 changes: 0 additions & 26 deletions src/AntCMS/PluginLoader.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
22 changes: 22 additions & 0 deletions src/Plugins/Robotstxt/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace AntCMS\Plugins\Robotstxt;

use Flight;
use AntCMS\Config;
use AntCMS\Tools;

class Controller
{
public function __construct()
{
Flight::route("GET /robots.txt", function (): void {
$protocol = Config::currentConfig('forceHTTPS') ? 'https' : Flight::request()->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');
});
}
}
28 changes: 0 additions & 28 deletions src/Plugins/Robotstxt/RobotstxtPlugin.php

This file was deleted.

53 changes: 53 additions & 0 deletions src/Plugins/Sitemap/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace AntCMS\Plugins\Sitemap;

use Flight;
use AntCMS\Config;
use AntCMS\Tools;
use AntCMS\Pages;

class Controller
{
public function __construct()
{
Flight::route("GET /sitemap.xml", function (): void {
$protocol = Config::currentConfig('forceHTTPS') ? 'https' : Flight::request()->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.");
}
});
}
}
55 changes: 0 additions & 55 deletions src/Plugins/Sitemap/SitemapPlugin.php

This file was deleted.

27 changes: 6 additions & 21 deletions src/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use AntCMS\PluginController;
use HostByBelle\CompressionBuffer;
use AntCMS\AntCMS;
use AntCMS\Config;
Expand Down Expand Up @@ -28,40 +29,24 @@
CompressionBuffer::setUp();
Flight::response()->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)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/MarkdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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!");
}
}*/
}

0 comments on commit d190d27

Please sign in to comment.