From bfeb56c83cc0dfa8e54078225c590a6530bf74c4 Mon Sep 17 00:00:00 2001
From: m0ark <33390109+m0ark@users.noreply.github.com>
Date: Fri, 12 Mar 2021 15:58:08 +0100
Subject: [PATCH] Add Support for custom error messages (#1326)

Co-authored-by: Tim van Dijen <tim.dijen@minbzk.nl>
---
 docs/simplesamlphp-changelog.md     |  1 +
 lib/SimpleSAML/Error/ErrorCodes.php | 17 ++++++++++++-----
 lib/SimpleSAML/Locale/Translate.php | 23 ++++++++++++++++++++---
 3 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/docs/simplesamlphp-changelog.md b/docs/simplesamlphp-changelog.md
index aa7908479..37ffc7be2 100644
--- a/docs/simplesamlphp-changelog.md
+++ b/docs/simplesamlphp-changelog.md
@@ -10,6 +10,7 @@ See the upgrade notes for specific information about upgrading.
 
 Released TBD
 
+  * Restore support for custom error messages (#1326)
   * Fixed a bug in the Artifact Resolution Service (#1428)
   * Fixed compatibility with Composer pre 1.8.5 (Debian 10) (#1427)
   * Updated npm dependencies up to February 1, 2021
diff --git a/lib/SimpleSAML/Error/ErrorCodes.php b/lib/SimpleSAML/Error/ErrorCodes.php
index 783027288..cae1250a7 100644
--- a/lib/SimpleSAML/Error/ErrorCodes.php
+++ b/lib/SimpleSAML/Error/ErrorCodes.php
@@ -12,7 +12,6 @@ use SimpleSAML\Locale\Translate;
  * @author Hanne Moa, UNINETT AS. <hanne.moa@uninett.no>
  * @package SimpleSAMLphp
  */
-
 class ErrorCodes
 {
     /**
@@ -161,8 +160,12 @@ class ErrorCodes
      */
     public static function getErrorCodeTitle($errorCode)
     {
-        $errorCodeTitles = self::getAllErrorCodeTitles();
-        return $errorCodeTitles[$errorCode];
+        if (array_key_exists($errorCode, self::getAllErrorCodeTitles())) {
+            $errorCodeTitles = self::getAllErrorCodeTitles();
+            return $errorCodeTitles[$errorCode];
+        } else {
+            return Translate::addTagPrefix($errorCode, 'title_');
+        }
     }
 
 
@@ -175,8 +178,12 @@ class ErrorCodes
      */
     public static function getErrorCodeDescription($errorCode)
     {
-        $errorCodeDescriptions = self::getAllErrorCodeDescriptions();
-        return $errorCodeDescriptions[$errorCode];
+        if (array_key_exists($errorCode, self::getAllErrorCodeTitles())) {
+            $errorCodeDescriptions = self::getAllErrorCodeDescriptions();
+            return $errorCodeDescriptions[$errorCode];
+        } else {
+            return Translate::addTagPrefix($errorCode, 'descr_');
+        }
     }
 
 
diff --git a/lib/SimpleSAML/Locale/Translate.php b/lib/SimpleSAML/Locale/Translate.php
index 5db3acc1e..93d49596a 100644
--- a/lib/SimpleSAML/Locale/Translate.php
+++ b/lib/SimpleSAML/Locale/Translate.php
@@ -58,7 +58,7 @@ class Translate
      * Constructor
      *
      * @param \SimpleSAML\Configuration $configuration Configuration object
-     * @param string|null               $defaultDictionary The default dictionary where tags will come from.
+     * @param string|null $defaultDictionary The default dictionary where tags will come from.
      */
     public function __construct(Configuration $configuration, $defaultDictionary = null)
     {
@@ -230,7 +230,7 @@ class Translate
     /**
      * Mark a string for translation without translating it.
      *
-     * @param string  $tag A tag name to mark for translation.
+     * @param string $tag A tag name to mark for translation.
      *
      * @return string The tag, unchanged.
      */
@@ -356,7 +356,7 @@ class Translate
     /**
      * Include a language file from the dictionaries directory.
      *
-     * @param string                         $file File name of dictionary to include
+     * @param string $file File name of dictionary to include
      * @param \SimpleSAML\Configuration|null $otherConfig Optionally provide a different configuration object than the
      * one provided in the constructor to be used to find the directory of the dictionary. This allows to combine
      * dictionaries inside the SimpleSAMLphp main code distribution together with external dictionaries. Defaults to
@@ -541,4 +541,21 @@ class Translate
         // nothing we can use, return null so that we can set a default
         return null;
     }
+
+    /**
+     * Prefix tag
+     *
+     * @param string $tag Translation tag
+     * @param string $prefix Prefix to be added
+     *
+     * @return string Prefixed tag
+     */
+    public static function addTagPrefix(string $tag, string $prefix): string
+    {
+        $tagPos = strrpos($tag, ':');
+        // if tag contains ':' target actual tag
+        $tagPos = ($tagPos === false) ? 0 : $tagPos + 1;
+        // add prefix at $tagPos
+        return substr_replace($tag, $prefix, $tagPos, 0);
+    }
 }
-- 
GitLab