From d79983e4777cf3569f60cdd5002601edb810310e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come@opensides.be>
Date: Tue, 17 Jul 2018 17:16:08 +0200
Subject: [PATCH] :sparkles: feat(management) Improve size limit handling

* Avoid parsing error text, use errno instead
* Reset sizelimit on each page so that incomplete warning only shows up
   when the list actually is incomplete
* Do not show dialog by default only warning and configure button

issue #5135

Cherry-picked in #5943
---
 include/class_filterLDAP.inc    |  3 ++-
 include/class_ldap.inc          | 25 +++++++++++++++++++++++++
 include/class_ldapSizeLimit.inc |  2 +-
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/include/class_filterLDAP.inc b/include/class_filterLDAP.inc
index a887dd0a6..5e677b4bb 100644
--- a/include/class_filterLDAP.inc
+++ b/include/class_filterLDAP.inc
@@ -122,6 +122,7 @@ class filterLDAP
     // Do search for every base
     $result = array();
     $limit_exceeded = FALSE;
+    $ui->getSizeLimitHandler()->setLimitExceeded(FALSE);
 
     foreach ($bases as $base => $dnFilters) {
       // Break if the size limit is exceeded
@@ -140,7 +141,7 @@ class filterLDAP
       $ldap->search('(&'.$filter.$dnFilter.')', $attributes, $scope);
 
       // Check for size limit exceeded messages for GUI feedback
-      if (preg_match("/size limit/i", $ldap->get_error())) {
+      if ($ldap->hitSizeLimit()) {
         $ui->getSizeLimitHandler()->setLimitExceeded();
         $limit_exceeded = TRUE;
       }
diff --git a/include/class_ldap.inc b/include/class_ldap.inc
index 038a697d0..eb0ebb989 100644
--- a/include/class_ldap.inc
+++ b/include/class_ldap.inc
@@ -902,6 +902,31 @@ class LDAP
     }
   }
 
+  /*!
+   * \brief Get the errno
+   *
+   * Must be run right after the ldap request
+   */
+  function get_errno()
+  {
+    if ($this->error == 'Success') {
+      return 0;
+    } else {
+      return ldap_errno($this->cid);
+    }
+  }
+
+  /*!
+   * \brief Check if the search hit the size limit
+   *
+   * Must be run right after the search
+   */
+  function hitSizeLimit()
+  {
+    /* LDAP_SIZELIMIT_EXCEEDED 0x04 */
+    return ($this->get_errno() == 0x04);
+  }
+
   function get_credentials($url, $referrals = NULL)
   {
     $ret    = array();
diff --git a/include/class_ldapSizeLimit.inc b/include/class_ldapSizeLimit.inc
index 01fe97de4..32d3666fb 100644
--- a/include/class_ldapSizeLimit.inc
+++ b/include/class_ldapSizeLimit.inc
@@ -44,7 +44,7 @@ class ldapSizeLimit
     global $config;
 
     $this->sizeLimit  = $config->get_cfg_value('LDAPSIZELIMIT', 200);
-    $this->ignore     = preg_match('/true/i', $config->get_cfg_value('LDAPSIZEIGNORE'));
+    $this->ignore     = preg_match('/true/i', $config->get_cfg_value('LDAPSIZEIGNORE', 'TRUE'));
   }
 
   function getSizeLimit()
-- 
GitLab