From 3c8e3121615f1e0c3c3aa3b9bee6b1eed5ba6a50 Mon Sep 17 00:00:00 2001 From: Monviech <79600909+Monviech@users.noreply.github.com> Date: Thu, 16 May 2024 08:42:43 +0200 Subject: [PATCH] www/caddy: Implement gettext() function for localization in php controllers and model (#3980) --- .../OPNsense/Caddy/Api/DiagnosticsController.php | 7 +------ .../OPNsense/Caddy/Api/ServiceController.php | 2 +- .../mvc/app/models/OPNsense/Caddy/Caddy.php | 14 +++++++------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/www/caddy/src/opnsense/mvc/app/controllers/OPNsense/Caddy/Api/DiagnosticsController.php b/www/caddy/src/opnsense/mvc/app/controllers/OPNsense/Caddy/Api/DiagnosticsController.php index d0d711f60..e2f6f5b22 100644 --- a/www/caddy/src/opnsense/mvc/app/controllers/OPNsense/Caddy/Api/DiagnosticsController.php +++ b/www/caddy/src/opnsense/mvc/app/controllers/OPNsense/Caddy/Api/DiagnosticsController.php @@ -76,16 +76,11 @@ class DiagnosticsController extends ApiMutableModelControllerBase // Decode JSON to PHP array $responseArray = json_decode($response, true); + // Since errors are handled by the caddy_diagnostics script and returned as json, check for an error key in the response if (isset($responseArray['error'])) { - // Handle the error return ["status" => "failed", "message" => $responseArray['message']]; } - // Assuming the response structure is like { "content": "actual Caddyfile content here" } - if (!isset($responseArray['content'])) { - return ["status" => "failed", "message" => "Caddyfile content not found"]; - } - // Return the response as an array which gets automatically encoded to JSON return ["status" => "success", "content" => $responseArray['content']]; } diff --git a/www/caddy/src/opnsense/mvc/app/controllers/OPNsense/Caddy/Api/ServiceController.php b/www/caddy/src/opnsense/mvc/app/controllers/OPNsense/Caddy/Api/ServiceController.php index e522dc52d..6aaa5a578 100644 --- a/www/caddy/src/opnsense/mvc/app/controllers/OPNsense/Caddy/Api/ServiceController.php +++ b/www/caddy/src/opnsense/mvc/app/controllers/OPNsense/Caddy/Api/ServiceController.php @@ -68,6 +68,6 @@ class ServiceController extends ApiMutableServiceControllerBase } // If unable to parse the expected JSON output, return a generic error message - return ["status" => "failed", "message" => "Unable to parse the validation result."]; + return ["status" => "failed", "message" => gettext("Unable to parse the validation result.")]; } } diff --git a/www/caddy/src/opnsense/mvc/app/models/OPNsense/Caddy/Caddy.php b/www/caddy/src/opnsense/mvc/app/models/OPNsense/Caddy/Caddy.php index 531391213..dd9da1a6c 100644 --- a/www/caddy/src/opnsense/mvc/app/models/OPNsense/Caddy/Caddy.php +++ b/www/caddy/src/opnsense/mvc/app/models/OPNsense/Caddy/Caddy.php @@ -37,7 +37,7 @@ class Caddy extends BaseModel { // 1. Check domain-port combinations // 2. Check subdomain-port combinations - private function checkForUniquePortCombos($items, $messages, $type = 'domain') + private function checkForUniquePortCombos($items, $messages) { $combos = []; foreach ($items as $item) { @@ -59,9 +59,9 @@ class Caddy extends BaseModel if (isset($combos[$comboKey])) { // Use dynamic $key for message referencing $messages->appendMessage(new Message( - "Duplicate entry: The combination of $type '$fromDomainOrSubdomain' and port '$port' is already used. Each $type and port pairing must be unique.", - $type === 'domain' ? $key . ".FromDomain" : $key . ".FromDomain", // Adjusted to use dynamic key - "Duplicate" . ucfirst($type) . "Port" + sprintf(gettext("Duplicate entry: The combination of '%s' and port '%s' is already used. Each combination of domain/subdomain and port must be unique."), $fromDomainOrSubdomain, $port), + $key . ".FromDomain", // Adjusted to use dynamic key + "DuplicateDomainPort" )); } else { $combos[$comboKey] = true; @@ -98,7 +98,7 @@ class Caddy extends BaseModel if (!$isValid) { $key = $subdomain->__reference; // Dynamic key based on subdomain reference $messages->appendMessage(new Message( - "Invalid subdomain configuration: '$subdomainName' does not fall under any configured wildcard domain.", + sprintf(gettext("Invalid subdomain configuration: '%s' does not fall under any configured wildcard domain."), $subdomainName), $key . ".FromDomain", // Use dynamic key for message referencing "InvalidSubdomain" )); @@ -112,9 +112,9 @@ class Caddy extends BaseModel { $messages = parent::performValidation($validateFullModel); // 1. Check domain-port combinations - $this->checkForUniquePortCombos($this->reverseproxy->reverse->iterateItems(), $messages, 'domain'); + $this->checkForUniquePortCombos($this->reverseproxy->reverse->iterateItems(), $messages); // 2. Check subdomain-port combinations - $this->checkForUniquePortCombos($this->reverseproxy->subdomain->iterateItems(), $messages, 'subdomain'); + $this->checkForUniquePortCombos($this->reverseproxy->subdomain->iterateItems(), $messages); // 3. Check that subdomains are under a wildcard or exact domain $this->checkSubdomainsAgainstDomains($this->reverseproxy->subdomain->iterateItems(), $this->reverseproxy->reverse->iterateItems(), $messages);