Skip to content

Commit

Permalink
fix(ldapselectfield): compatibility with PHP 8
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed May 8, 2021
1 parent b9b0e28 commit ca09db9
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions inc/field/ldapselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ca09db9

Please sign in to comment.