From ca09db9aa9e53908973d6e19f0519b76b8ed23b7 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Sat, 8 May 2021 16:39:59 +0200 Subject: [PATCH] fix(ldapselectfield): compatibility with PHP 8 Signed-off-by: Thierry Bugier --- inc/field/ldapselectfield.class.php | 49 ++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/inc/field/ldapselectfield.class.php b/inc/field/ldapselectfield.class.php index 049051463..3cd71a338 100644 --- a/inc/field/ldapselectfield.class.php +++ b/inc/field/ldapselectfield.class.php @@ -144,8 +144,25 @@ public function getAvailableValues() { $id = 0; do { if (AuthLDAP::isLdapPageSizeAvailable($config_ldap)) { - // phpcs:ignore Generic.PHP.DeprecatedFunctions - ldap_control_paged_result($ds, $config_ldap->fields['pagesize'], true, $cookie); + if (version_compare(PHP_VERSION, '7.3') < 0) { + // phpcs:ignore Generic.PHP.DeprecatedFunctions + ldap_control_paged_result($ds, $config_ldap->fields['pagesize'], true, $cookie); + $result = ldap_search($ds, $config_ldap->fields['basedn'], $ldap_values->ldap_filter, $attribute); + } else { + $controls = [ + [ + 'oid' =>LDAP_CONTROL_PAGEDRESULTS, + 'iscritical' => true, + 'value' => [ + 'size' => $config_ldap->fields['pagesize'], + 'cookie' => $cookie + ] + ] + ]; + $result = ldap_search($ds, $config_ldap->fields['basedn'], $ldap_values->ldap_filter, $attribute, 0, -1, -1, LDAP_DEREF_NEVER, $controls); + ldap_parse_result($ds, $result, $errcode, $matcheddn, $errmsg, $referrals, $controls); + $cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? ''; + } } $result = ldap_search($ds, $config_ldap->fields['basedn'], $ldap_values->ldap_filter, $attribute); @@ -160,7 +177,7 @@ public function getAvailableValues() { $id++; } - if (AuthLDAP::isLdapPageSizeAvailable($config_ldap)) { + if (AuthLDAP::isLdapPageSizeAvailable($config_ldap) && version_compare(PHP_VERSION, '7.3') < 0) { // phpcs:ignore Generic.PHP.DeprecatedFunctions ldap_control_paged_result_response($ds, $result, $cookie); } @@ -235,11 +252,29 @@ public function prepareQuestionInputForSave($input) { set_error_handler('plugin_formcreator_ldap_warning_handler', E_WARNING); try { - $ds = $config_ldap->connect(); + $cookie = ''; + $ds = $config_ldap->connect(); ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); - // phpcs:ignore Generic.PHP.DeprecatedFunctions - ldap_control_paged_result($ds, 1); - $sn = ldap_search($ds, $config_ldap->fields['basedn'], $input['ldap_filter'], $attribute); + if (version_compare(PHP_VERSION, '7.3') < 0) { + // phpcs:ignore Generic.PHP.DeprecatedFunctions + ldap_control_paged_result($ds, 1); + $sn = ldap_search($ds, $config_ldap->fields['basedn'], $input['ldap_filter'], $attribute); + } else { + //since PHP 7.3, send serverctrls to ldap_search + $controls = [ + [ + 'oid' =>LDAP_CONTROL_PAGEDRESULTS, + 'iscritical' => true, + 'value' => [ + 'size' => $config_ldap->fields['pagesize'], + 'cookie' => $cookie + ] + ] + ]; + $sn = @ldap_search($ds, $config_ldap->fields['basedn'], $input['ldap_filter'], $attribute); + ldap_parse_result($ds, $sn, $errcode, $matcheddn, $errmsg, $referrals, $controls); + $cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? ''; + } ldap_get_entries($ds, $sn); } catch (Exception $e) { Session::addMessageAfterRedirect(__('Cannot recover LDAP informations!', 'formcreator'), false, ERROR);