From f26d864049591ad08e243665dce4f669e66db0b6 Mon Sep 17 00:00:00 2001
From: manu0401 <manu@netbsd.org>
Date: Tue, 15 Aug 2017 09:26:22 +0200
Subject: [PATCH] Update PHP API change gor ldapwhoami (#674)

I have been maintaining the PHP LDAP EXOP patch for a few years,
which include the ldapwhoami() function. This has finally made its
way into PHP distribution and will be available in PHP 7.3, but
with a modified prototype.

This changes adapts to this API change. While there, also update
exception handling on par with recent SimpleSAMLphp code.
---
 lib/SimpleSAML/Auth/LDAP.php | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/lib/SimpleSAML/Auth/LDAP.php b/lib/SimpleSAML/Auth/LDAP.php
index f8a56ebf5..6f9ed8dcb 100644
--- a/lib/SimpleSAML/Auth/LDAP.php
+++ b/lib/SimpleSAML/Auth/LDAP.php
@@ -723,18 +723,27 @@ class SimpleSAML_Auth_LDAP
      * ldap_exop_whoami accessor, if available. Use requested authz_id
      * otherwise.
      *
-     * ldap_exop_whoami is not yet included in PHP. For reference, the
-     * feature request: http://bugs.php.net/bug.php?id=42060
-     * And the patch against lastest PHP release:
-     * http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/databases/php-ldap/files/ldap-ctrl-exop.patch
+     * ldap_exop_whoami() has been provided as a third party patch that
+     * waited several years to get its way upstream:
+     * http://cvsweb.netbsd.org/bsdweb.cgi/pkgsrc/databases/php-ldap/files
+     * 
+     * When it was integrated into PHP repository, the function prototype
+     * was changed, The new prototype was used in third party patch for 
+     * PHP 7.0 and 7.1, hence the version test below.
      */
     public function whoami($searchBase, $searchAttributes)
     {
         $authz_id = '';
 
         if (function_exists('ldap_exop_whoami')) {
-            if (ldap_exop_whoami($this->ldap, $authz_id) !== true) {
-                throw $this->getLDAPException('LDAP whoami exop failure');
+            if (version_compare(phpversion(), '7', '<')) {
+                if (ldap_exop_whoami($this->ldap, $authz_id) !== true) {
+                    throw $this->makeException('LDAP whoami exop failure');
+                }
+            } else {
+                if (($authz_id = ldap_exop_whoami($this->ldap)) === false) {
+                    throw $this->makeException('LDAP whoami exop failure');
+                }
             }
         } else {
             $authz_id = $this->authz_id;
@@ -743,7 +752,7 @@ class SimpleSAML_Auth_LDAP
         $dn = $this->authzid_to_dn($searchBase, $searchAttributes, $authz_id);
 
         if (!isset($dn) || ($dn == '')) {
-            throw $this->getLDAPException('Cannot figure userID');
+            throw $this->makeException('Cannot figure userID');
         }
 
         return $dn;
-- 
GitLab