From a92c1ce772dfecf4df42aeb20bd894be2c413d5a Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Sat, 23 May 2026 18:10:11 -0500 Subject: [PATCH] feat: site aliases tab with per-alias offline, robots, and backend redirect Move site aliases from a comma-separated text field in Diagnostics to its own tab using Joomla's subform repeatable-table layout. Each alias entry now supports: domain, offline toggle with custom message, robots meta directive, and backend redirect to primary domain. Frontend stays on the alias domain while admin requests can be redirected to the primary. Authored-by: Moko Consulting Co-Authored-By: Claude Opus 4.6 (1M context) --- src/Extension/MokoWaaS.php | 175 ++++++++++++++++++++- src/forms/alias_entry.xml | 55 +++++++ src/language/en-GB/plg_system_mokowaas.ini | 17 +- src/language/en-US/plg_system_mokowaas.ini | 17 +- src/mokowaas.xml | 24 ++- 5 files changed, 269 insertions(+), 19 deletions(-) create mode 100644 src/forms/alias_entry.xml diff --git a/src/Extension/MokoWaaS.php b/src/Extension/MokoWaaS.php index 628bf69..9be7366 100644 --- a/src/Extension/MokoWaaS.php +++ b/src/Extension/MokoWaaS.php @@ -96,6 +96,9 @@ class MokoWaaS extends CMSPlugin // Security: HTTPS redirect (runs for all clients) $this->enforceHttps(); + // Site alias handling: offline page and backend redirect + $this->handleSiteAlias(); + // MokoWaaS API endpoints (run before routing) $mokoAction = $this->app->input->get('mokowaas', ''); @@ -880,14 +883,20 @@ class MokoWaaS extends CMSPlugin */ public function onBeforeCompileHead() { - if (!$this->app->isClient('administrator')) + $doc = $this->app->getDocument(); + + if ($doc->getType() !== 'html') { return; } - $doc = $this->app->getDocument(); + // Inject robots meta tag for alias domains (frontend only) + if ($this->app->isClient('site')) + { + $this->injectAliasRobots($doc); + } - if ($doc->getType() !== 'html') + if (!$this->app->isClient('administrator')) { return; } @@ -2559,6 +2568,142 @@ class MokoWaaS extends CMSPlugin } } + // ------------------------------------------------------------------ + // Site Alias handling + // ------------------------------------------------------------------ + + /** + * Get the alias configuration for the current request domain, if any. + * + * @return object|null Alias entry object or null if not an alias domain + * + * @since 02.01.43 + */ + protected function getCurrentAlias() + { + $currentHost = $_SERVER['HTTP_HOST'] ?? ''; + $primaryHost = parse_url(Uri::root(), PHP_URL_HOST); + + if (empty($currentHost) || strcasecmp($currentHost, $primaryHost) === 0) + { + return null; + } + + $aliases = $this->params->get('site_aliases', ''); + + if (empty($aliases)) + { + return null; + } + + // Subform returns JSON string or array + if (is_string($aliases)) + { + $aliases = json_decode($aliases, false); + } + + if (!is_array($aliases)) + { + return null; + } + + foreach ($aliases as $alias) + { + $alias = (object) $alias; + + if (isset($alias->domain) && strcasecmp(trim($alias->domain), $currentHost) === 0) + { + return $alias; + } + } + + return null; + } + + /** + * Handle site alias logic: offline page and backend redirect. + * + * Runs early in onAfterInitialise before routing occurs. + * + * @return void + * + * @since 02.01.43 + */ + protected function handleSiteAlias() + { + $alias = $this->getCurrentAlias(); + + if ($alias === null) + { + return; + } + + // Backend redirect: send admin requests to the primary domain + if (!empty($alias->redirect_backend) && $alias->redirect_backend === '1' + && $this->app->isClient('administrator')) + { + $primaryUrl = rtrim(Uri::root(), '/') . '/administrator' . Uri::getInstance()->toString(['path', 'query']); + $adminPath = str_replace(Uri::root() . 'administrator', '', Uri::getInstance()->toString(['path', 'query'])); + $primaryUrl = rtrim(Uri::root(), '/') . '/administrator' . $adminPath; + + $this->app->redirect($primaryUrl, 301); + } + + // Offline: show maintenance page for frontend requests + if (!empty($alias->offline) && $alias->offline === '1' + && $this->app->isClient('site')) + { + // Allow health API to still respond + if ($this->app->input->get('mokowaas', '') !== '') + { + return; + } + + $message = $alias->offline_message ?? 'This site is currently offline for maintenance.'; + $brandName = $this->params->get('brand_name', 'MokoWaaS'); + + header('HTTP/1.1 503 Service Unavailable'); + header('Retry-After: 3600'); + header('Content-Type: text/html; charset=utf-8'); + echo ''; + echo ''; + echo '' . htmlspecialchars($brandName) . ' - Maintenance'; + echo ''; + echo '
'; + echo '

' . htmlspecialchars($brandName) . '

'; + echo '

' . htmlspecialchars($message) . '

'; + echo '
'; + $this->app->close(); + } + } + + /** + * Inject robots meta tag for alias domains. + * + * @param \Joomla\CMS\Document\HtmlDocument $doc Document object + * + * @return void + * + * @since 02.01.43 + */ + protected function injectAliasRobots($doc) + { + $alias = $this->getCurrentAlias(); + + if ($alias === null) + { + return; + } + + $robots = $alias->robots ?? 'index, follow'; + + if ($robots !== 'index, follow') + { + $doc->setMetaData('robots', $robots); + } + } + // ------------------------------------------------------------------ // Heartbeat (called from onExtensionAfterSave) // ------------------------------------------------------------------ @@ -2591,16 +2736,30 @@ class MokoWaaS extends CMSPlugin // Register primary domain $this->sendHeartbeat($siteUrl, $siteName, $healthToken, $app); - // Register any alias domains + // Register any alias domains (subform format: array of objects with domain key) $aliases = $params->get('site_aliases', ''); if (!empty($aliases)) { - foreach (array_filter(array_map('trim', explode(',', $aliases))) as $alias) + if (is_string($aliases)) { - $aliasUrl = 'https://' . ltrim($alias, 'https://'); - $aliasUrl = rtrim($aliasUrl, '/'); - $this->sendHeartbeat($aliasUrl, $siteName . ' (' . $alias . ')', $healthToken, $app); + $aliases = json_decode($aliases, false); + } + + if (is_array($aliases)) + { + foreach ($aliases as $alias) + { + $alias = (object) $alias; + + if (!empty($alias->domain)) + { + $domain = trim($alias->domain); + $aliasUrl = 'https://' . preg_replace('#^https?://#i', '', $domain); + $aliasUrl = rtrim($aliasUrl, '/'); + $this->sendHeartbeat($aliasUrl, $siteName, $healthToken, $app); + } + } } } } diff --git a/src/forms/alias_entry.xml b/src/forms/alias_entry.xml new file mode 100644 index 0000000..1613b71 --- /dev/null +++ b/src/forms/alias_entry.xml @@ -0,0 +1,55 @@ + +
+ + + + + + + + + + + + + + + + + + diff --git a/src/language/en-GB/plg_system_mokowaas.ini b/src/language/en-GB/plg_system_mokowaas.ini index 348613a..415df26 100644 --- a/src/language/en-GB/plg_system_mokowaas.ini +++ b/src/language/en-GB/plg_system_mokowaas.ini @@ -130,5 +130,18 @@ PLG_SYSTEM_MOKOWAAS_UPLOAD_TYPES_DESC="Comma-separated list of allowed file exte PLG_SYSTEM_MOKOWAAS_UPLOAD_SIZE_LABEL="Max Upload Size (MB)" PLG_SYSTEM_MOKOWAAS_UPLOAD_SIZE_DESC="Maximum file upload size in megabytes." -PLG_SYSTEM_MOKOWAAS_SITE_ALIASES_LABEL="Site Aliases" -PLG_SYSTEM_MOKOWAAS_SITE_ALIASES_DESC="Comma-separated list of additional domains this site is accessible on (e.g. www.example.com,alias.example.com). Each alias gets its own Grafana datasource for health monitoring." +; ===== Site Aliases fieldset ===== +PLG_SYSTEM_MOKOWAAS_FIELDSET_ALIASES_LABEL="Site Aliases" +PLG_SYSTEM_MOKOWAAS_FIELDSET_ALIASES_DESC="Configure additional domains that mirror this site. Each alias can have its own offline status, robots directive, and backend redirect behavior." +PLG_SYSTEM_MOKOWAAS_SITE_ALIASES_LABEL="Domain Aliases" +PLG_SYSTEM_MOKOWAAS_SITE_ALIASES_DESC="Add domain aliases that serve as mirrors of this site. Each alias gets its own Grafana monitoring datasource." +PLG_SYSTEM_MOKOWAAS_ALIAS_DOMAIN_LABEL="Domain" +PLG_SYSTEM_MOKOWAAS_ALIAS_DOMAIN_DESC="The alias domain name (e.g. www.example.com). Do not include https:// prefix." +PLG_SYSTEM_MOKOWAAS_ALIAS_OFFLINE_LABEL="Offline" +PLG_SYSTEM_MOKOWAAS_ALIAS_OFFLINE_DESC="Show an offline maintenance page when visitors access the site through this alias domain." +PLG_SYSTEM_MOKOWAAS_ALIAS_OFFLINE_MSG_LABEL="Offline Message" +PLG_SYSTEM_MOKOWAAS_ALIAS_OFFLINE_MSG_DESC="Custom message to display when this alias is set to offline." +PLG_SYSTEM_MOKOWAAS_ALIAS_ROBOTS_LABEL="Robots" +PLG_SYSTEM_MOKOWAAS_ALIAS_ROBOTS_DESC="Meta robots directive for this alias domain. Use 'noindex, nofollow' to prevent search engines from indexing the alias." +PLG_SYSTEM_MOKOWAAS_ALIAS_REDIRECT_BACKEND_LABEL="Redirect Backend" +PLG_SYSTEM_MOKOWAAS_ALIAS_REDIRECT_BACKEND_DESC="Redirect admin panel requests on this alias to the primary domain. Frontend stays on the alias domain." diff --git a/src/language/en-US/plg_system_mokowaas.ini b/src/language/en-US/plg_system_mokowaas.ini index 28eafa4..62cfa64 100644 --- a/src/language/en-US/plg_system_mokowaas.ini +++ b/src/language/en-US/plg_system_mokowaas.ini @@ -130,5 +130,18 @@ PLG_SYSTEM_MOKOWAAS_UPLOAD_TYPES_DESC="Comma-separated list of allowed file exte PLG_SYSTEM_MOKOWAAS_UPLOAD_SIZE_LABEL="Max Upload Size (MB)" PLG_SYSTEM_MOKOWAAS_UPLOAD_SIZE_DESC="Maximum file upload size in megabytes." -PLG_SYSTEM_MOKOWAAS_SITE_ALIASES_LABEL="Site Aliases" -PLG_SYSTEM_MOKOWAAS_SITE_ALIASES_DESC="Comma-separated list of additional domains this site is accessible on (e.g. www.example.com,alias.example.com). Each alias gets its own Grafana datasource for health monitoring." +; ===== Site Aliases fieldset ===== +PLG_SYSTEM_MOKOWAAS_FIELDSET_ALIASES_LABEL="Site Aliases" +PLG_SYSTEM_MOKOWAAS_FIELDSET_ALIASES_DESC="Configure additional domains that mirror this site. Each alias can have its own offline status, robots directive, and backend redirect behavior." +PLG_SYSTEM_MOKOWAAS_SITE_ALIASES_LABEL="Domain Aliases" +PLG_SYSTEM_MOKOWAAS_SITE_ALIASES_DESC="Add domain aliases that serve as mirrors of this site. Each alias gets its own Grafana monitoring datasource." +PLG_SYSTEM_MOKOWAAS_ALIAS_DOMAIN_LABEL="Domain" +PLG_SYSTEM_MOKOWAAS_ALIAS_DOMAIN_DESC="The alias domain name (e.g. www.example.com). Do not include https:// prefix." +PLG_SYSTEM_MOKOWAAS_ALIAS_OFFLINE_LABEL="Offline" +PLG_SYSTEM_MOKOWAAS_ALIAS_OFFLINE_DESC="Show an offline maintenance page when visitors access the site through this alias domain." +PLG_SYSTEM_MOKOWAAS_ALIAS_OFFLINE_MSG_LABEL="Offline Message" +PLG_SYSTEM_MOKOWAAS_ALIAS_OFFLINE_MSG_DESC="Custom message to display when this alias is set to offline." +PLG_SYSTEM_MOKOWAAS_ALIAS_ROBOTS_LABEL="Robots" +PLG_SYSTEM_MOKOWAAS_ALIAS_ROBOTS_DESC="Meta robots directive for this alias domain. Use 'noindex, nofollow' to prevent search engines from indexing the alias." +PLG_SYSTEM_MOKOWAAS_ALIAS_REDIRECT_BACKEND_LABEL="Redirect Backend" +PLG_SYSTEM_MOKOWAAS_ALIAS_REDIRECT_BACKEND_DESC="Redirect admin panel requests on this alias to the primary domain. Frontend stays on the alias domain." diff --git a/src/mokowaas.xml b/src/mokowaas.xml index 0e00fa9..325629e 100644 --- a/src/mokowaas.xml +++ b/src/mokowaas.xml @@ -44,6 +44,7 @@ script.php Extension Field + forms payload services language @@ -268,6 +269,22 @@ description="PLG_SYSTEM_MOKOWAAS_HIDDEN_MENUS_DESC" rows="5" filter="raw" /> +
+ +
-