From 9754d244a7eaa53b89f2731d5d3ca1adcfd0962d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.chilliet@fusiondirectory.org>
Date: Thu, 4 Feb 2021 16:44:45 +0100
Subject: [PATCH] :sparkles: feat(templates) Add a uid mode to "a" modifier

issue #6145
---
 include/class_templateHandling.inc | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/include/class_templateHandling.inc b/include/class_templateHandling.inc
index 6da06c372..3e30a4251 100644
--- a/include/class_templateHandling.inc
+++ b/include/class_templateHandling.inc
@@ -321,15 +321,33 @@ class templateHandling
     return $fields;
   }
 
-  private static function modifierRemoveAccents ($str)
+  private static function modifierRemoveAccents (array $args, $str)
   {
+    $mode = 'ascii';
+    if (count($args) >= 1) {
+      $mode = $args[0];
+    }
+
     $str = htmlentities($str, ENT_NOQUOTES, 'UTF-8');
 
     $str = preg_replace('#&([A-za-z])(?:acute|cedil|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $str);
     // handle ligatures
     $str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str);
     // delete unhandled characters
-    return [preg_replace('#&[^;]+;#', '', $str)];
+    $str = preg_replace('#&[^;]+;#', '', $str);
+
+    if ($mode === 'ascii') {
+      return [$str];
+    } elseif ($mode === 'uid') {
+      if (strict_uid_mode()) {
+        $str = preg_replace('/[^a-z0-9_-]/', '', mb_strtolower($str, 'UTF-8'));
+      } else {
+        $str = preg_replace('/[^a-zA-Z0-9 _.-]/', '', $str);
+      }
+      return [$str];
+    } else {
+      throw new FusionDirectoryException(_('Invalid mode for "a" modifier, supported modes are "uid" and "ascii"'));
+    }
   }
 
   private static function modifierTranslit (array $args, $str)
@@ -610,7 +628,7 @@ class templateHandling
         break;
       case 'a':
         // remove accent
-        $result = static::modifierRemoveAccents($str);
+        $result = static::modifierRemoveAccents($args, $str);
         break;
       case 't':
         // translit
-- 
GitLab