From 7f99fbf87057e642097062eb4fdc40b0b27d4fde Mon Sep 17 00:00:00 2001
From: Thibault Dockx <thibault.dockx@fusiondirectory.org>
Date: Tue, 4 Mar 2025 17:56:24 +0000
Subject: [PATCH] :sparkles: (ldap) - reviewing ldap_read return values

Ultra basic workaround of new ldap return values.
---
 include/class_ldap.inc | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/include/class_ldap.inc b/include/class_ldap.inc
index a86fe3825..09ba93cb2 100755
--- a/include/class_ldap.inc
+++ b/include/class_ldap.inc
@@ -430,6 +430,9 @@ class LDAP
    */
   function cat ($srp, $dn, $attrs = ["*"], $filter = "(objectclass=*)")
   {
+    // Set the default error hanlder of PHP allowins @ suppression of PHP Warnings in case of DN not found. (ldap_read).
+    restore_error_handler();
+
     if ($this->hascon) {
       if ($this->reconnect) {
         $this->connect();
@@ -449,6 +452,7 @@ class LDAP
     }
   }
 
+
   /*!
    * \brief Search object from a filter
    *
@@ -463,8 +467,15 @@ class LDAP
         $this->connect();
       }
       $res  = @ldap_read($this->cid, $dn, $filter, ["objectClass"]);
-      logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'object_match_filter(dn="'.$dn.'",filter="'.$filter.'")');
-      return @ldap_count_entries($this->cid, $res);
+      // Method ldap_read can return array of ldap\result (multiple ldap instances) or FALSE. We escape both.
+      // This is cleary a quick fix for php8.2 adaptation ...
+      if (is_bool($res) || is_array($res)) {
+        logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'object_match_filter(dn="'.$dn.'",filter="'.$filter.'")');
+        return FALSE;
+      } else {
+        logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'object_match_filter(dn="'.$dn.'",filter="'.$filter.'")');
+        return @ldap_count_entries($this->cid, $res);
+      }
     } else {
       $this->error = "Could not connect to LDAP server";
       logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'object_match_filter(dn="'.$dn.'",filter="'.$filter.'")');
@@ -597,6 +608,12 @@ class LDAP
   {
     if ($this->hascon) {
       if ($this->hasres[$srp]) {
+        // Method ldap_read can return array of ldap\result (multiple ldap instances) or FALSE. We escape both.
+        // This is cleary a quick fix for php8.2 adaptation ...
+        if (is_bool($this->hasres[$srp]) || is_array($this->hasres[$srp])) {
+          logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'count()');
+          return FALSE;
+        }
         $rv = @ldap_count_entries($this->cid, $this->sr[$srp]);
         $this->error = @ldap_error($this->cid);
         logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'count()');
-- 
GitLab