From fb35a64842c6df4fe7760a899718b965e5d3752f Mon Sep 17 00:00:00 2001
From: Thibault Dockx <thibault.dockx@fusiondirectory.org>
Date: Mon, 10 Mar 2025 17:00:59 +0000
Subject: [PATCH] :ambulance: (filter) - modify filtering logic

Adapts filtering to php8.2
---
 include/class_ldapFilter.inc | 44 +++++++++++++++++++++++++-----------
 1 file changed, 31 insertions(+), 13 deletions(-)

diff --git a/include/class_ldapFilter.inc b/include/class_ldapFilter.inc
index d5805ce2f..93230a453 100755
--- a/include/class_ldapFilter.inc
+++ b/include/class_ldapFilter.inc
@@ -138,34 +138,52 @@ class ldapFilterLeaf extends ldapFilter implements \Stringable
 
   protected $pattern;
   protected $dnFilter = FALSE;
-
-  function __construct ($left, $operator, $right)
+  function __construct($left, $operator, $right)
   {
-    if (@strrpos((string) $left, ':dn:', -4) !== FALSE) {
-      $this->dnFilter = TRUE;
-      $left = substr((string) $left, 0, -4);
+    // Ensure $left is a string for consistency
+    $left = (string) $left;
+
+    // Check if $left ends with ':dn:' using str_ends_with()
+    if (str_ends_with($left, ':dn:')) {
+      $this->dnFilter = true;
+      $left = substr($left, 0, -4); // Remove ':dn:'
     }
+
     parent::__construct($operator, [$left, $right]);
-    if (($this->operator == '=') || ($this->operator == '~=')) {
+
+    if ($this->operator === '=' || $this->operator === '~=') {
       $prefix = '';
       $suffix = '';
-      if (preg_match('/^\\*/', (string) $this->subparts[1])) {
+
+      // Detect * at start and end using a single regex
+      if (preg_match('/^\*(.*)\*$/', (string) $this->subparts[1], $matches)) {
         $prefix = '.*';
-      }
-      if (preg_match('/\\*$/', (string) $this->subparts[1])) {
         $suffix = '.*';
+        $search = $matches[1]; // Extract inner content
+      } else {
+        $search = trim((string) $this->subparts[1], '*');
+        if (str_starts_with((string) $this->subparts[1], '*')) {
+          $prefix = '.*';
+        }
+        if (str_ends_with((string) $this->subparts[1], '*')) {
+          $suffix = '.*';
+        }
       }
-      $search = preg_replace(['/^\\*/','/\\*$/'], '', (string) $this->subparts[1]);
+
+      $escapedSearch = preg_quote($search, '/');
+
+      // Construct the pattern
       if ($this->dnFilter) {
-        $this->pattern = '/'.$left.'='.$prefix.preg_quote((string) $search, '/').$suffix.',/';
-      } elseif ($this->subparts[1] == '*') {
+        $this->pattern = "/{$left}={$prefix}{$escapedSearch}{$suffix},/";
+      } elseif ($this->subparts[1] === '*') {
         $this->pattern = '/^.*$/';
       } else {
-        $this->pattern = '/^'.$prefix.preg_quote((string) $search, '/').$suffix.'$/';
+        $this->pattern = "/^{$prefix}{$escapedSearch}{$suffix}$/";
       }
     }
   }
 
+
   function isDnFilter ()
   {
     return $this->dnFilter;
-- 
GitLab