From ac244d74953da262aa37f688c7a2cc84509aaaee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come.chilliet@fusiondirectory.org>
Date: Tue, 29 Jun 2021 11:26:34 +0200
Subject: [PATCH] ambulance: fix(core) Prepare for PHP-8.1 compatibility

Some resources are turned into objects, others may follow, we might as
 well stop using is_resource right away.

issue #6175
---
 include/class_config.inc |  2 +-
 include/class_ldap.inc   | 21 ++++++++++++---------
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/include/class_config.inc b/include/class_config.inc
index 9318e13cb..2b1bedbc6 100644
--- a/include/class_config.inc
+++ b/include/class_config.inc
@@ -308,7 +308,7 @@ class config
   {
     global $ui;
 
-    if ($this->ldapLink === NULL || !is_resource($this->ldapLink->cid)) {
+    if (($this->ldapLink === NULL) || ($this->ldapLink->cid === FALSE)) {
       /* Build new connection */
       $this->ldapLink = LDAP::init($this->current['SERVER'], $this->current['BASE'],
           $this->current['ADMINDN'], $this->get_credentials($this->current['ADMINPASSWORD']));
diff --git a/include/class_ldap.inc b/include/class_ldap.inc
index a1fbdfdaa..6b185290c 100644
--- a/include/class_ldap.inc
+++ b/include/class_ldap.inc
@@ -37,8 +37,12 @@ class LDAP
   var $reconnect      = FALSE;
   var $tls            = FALSE;
 
-  /* connection identifier */
-  var $cid;
+  /**
+   * Connection identifier
+   *
+   * @var resource|object|false
+   */
+  var $cid            = FALSE;
 
   var $hasres         = [];
   var $sr             = [];
@@ -294,7 +298,7 @@ class LDAP
   function unbind ()
   {
     @ldap_unbind($this->cid);
-    $this->cid = NULL;
+    $this->cid = FALSE;
     logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, '', 'unbind');
   }
 
@@ -769,9 +773,8 @@ class LDAP
       $r = ldap_rename($this->cid, $source, $dest_rdn, $parent, TRUE);
       $this->error = ldap_error($this->cid);
 
-      /* Check if destination dn exists, if not the
-          server may not support this operation */
-      $r &= is_resource($this->dn_exists($dest));
+      /* Check if destination dn exists, if not the server may not support this operation */
+      $r &= $this->dn_exists($dest);
       logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->error, 'rename("'.$source.'","'.$dest.'")');
       return $r;
     } else {
@@ -1202,7 +1205,7 @@ class LDAP
 
     // Try to open the process
     $process = proc_open($cmd, $descriptorspec, $pipes);
-    if (is_resource($process)) {
+    if ($process !== FALSE) {
       // Write the password to stdin
       fclose($pipes[0]);
 
@@ -1221,10 +1224,10 @@ class LDAP
     return $res;
   }
 
-  function dn_exists ($dn)
+  function dn_exists ($dn): bool
   {
     logging::debug(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, '', 'dn_exists('.$dn.')');
-    return @ldap_read($this->cid, $dn, "(objectClass=*)", ["objectClass"]);
+    return (@ldap_read($this->cid, $dn, '(objectClass=*)', ['objectClass']) !== FALSE);
   }
 
   function parseLdif (string $str_attr): array
-- 
GitLab