From 9084233540bd4ba2e4a2c86d672e37f7e21a5ab9 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 16 May 2024 11:46:37 +0400 Subject: [PATCH] =?UTF-8?q?impl=C3=A9menter=20reconnect()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nur_src/ldap/LdapConn.php | 21 +++++++++++++++++++++ nur_src/ldap/LdapSearch.php | 7 +++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/nur_src/ldap/LdapConn.php b/nur_src/ldap/LdapConn.php index 67c4b2a..f8f56f5 100644 --- a/nur_src/ldap/LdapConn.php +++ b/nur_src/ldap/LdapConn.php @@ -133,6 +133,27 @@ class LdapConn extends Parametrable implements ICloseable { $this->conn = $this->tryConnect($binddn, $password, $controls, $this->conn); } + /** + * se reconnecter, mais seulement s'il y a une erreur sur la connection + * + * @return true si la reconnexion a effectivement eu lieu + */ + function reconnect(bool $force=false): bool { + if (!$force) { + try { + $this->_search(null, [ + "attrs" => ["objectClass"], + "scope" => "base", + "suffix" => "", + ])->first(); + } catch (LdapException $e) { + $force = true; + } + } + if ($force) $this->connect(); + return $force; + } + /** @return resource */ protected function conn() { if ($this->conn === null) $this->connect(); diff --git a/nur_src/ldap/LdapSearch.php b/nur_src/ldap/LdapSearch.php index 82e4047..33d8805 100644 --- a/nur_src/ldap/LdapSearch.php +++ b/nur_src/ldap/LdapSearch.php @@ -132,6 +132,7 @@ class LdapSearch extends Parametrable implements IteratorAggregate { /** @var array */ protected $ppControls; + /** @throws LdapException */ function getIterator() { $conn = $this->conn; $args = [$conn]; @@ -161,12 +162,12 @@ class LdapSearch extends Parametrable implements IteratorAggregate { $rr = LdapException::check("search", $conn, $rr); try { - $er = ldap_first_entry($conn, $rr); + $er = LdapException::check("first_entry", $conn, ldap_first_entry($conn, $rr)); while ($er !== false) { $dn = ldap_get_dn($conn, $er); $entry = ldap_get_attributes($conn, $er); yield $dn => $entry; - $er = ldap_next_entry($conn, $er); + $er = LdapException::check("next_entry", $conn, ldap_next_entry($conn, $er)); } } catch (StopException $e) { } finally { @@ -177,6 +178,8 @@ class LdapSearch extends Parametrable implements IteratorAggregate { /** * retourner la première entrée du résultat de la recherche ou null si la * recherche ne retourne aucun résultat + * + * @throws LdapException */ function first(?string &$dn=null): ?array { $it = $this->getIterator();