Skip to content

Commit ca09db9

Browse files
committed
fix(ldapselectfield): compatibility with PHP 8
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
1 parent b9b0e28 commit ca09db9

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

inc/field/ldapselectfield.class.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,25 @@ public function getAvailableValues() {
144144
$id = 0;
145145
do {
146146
if (AuthLDAP::isLdapPageSizeAvailable($config_ldap)) {
147-
// phpcs:ignore Generic.PHP.DeprecatedFunctions
148-
ldap_control_paged_result($ds, $config_ldap->fields['pagesize'], true, $cookie);
147+
if (version_compare(PHP_VERSION, '7.3') < 0) {
148+
// phpcs:ignore Generic.PHP.DeprecatedFunctions
149+
ldap_control_paged_result($ds, $config_ldap->fields['pagesize'], true, $cookie);
150+
$result = ldap_search($ds, $config_ldap->fields['basedn'], $ldap_values->ldap_filter, $attribute);
151+
} else {
152+
$controls = [
153+
[
154+
'oid' =>LDAP_CONTROL_PAGEDRESULTS,
155+
'iscritical' => true,
156+
'value' => [
157+
'size' => $config_ldap->fields['pagesize'],
158+
'cookie' => $cookie
159+
]
160+
]
161+
];
162+
$result = ldap_search($ds, $config_ldap->fields['basedn'], $ldap_values->ldap_filter, $attribute, 0, -1, -1, LDAP_DEREF_NEVER, $controls);
163+
ldap_parse_result($ds, $result, $errcode, $matcheddn, $errmsg, $referrals, $controls);
164+
$cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? '';
165+
}
149166
}
150167

151168
$result = ldap_search($ds, $config_ldap->fields['basedn'], $ldap_values->ldap_filter, $attribute);
@@ -160,7 +177,7 @@ public function getAvailableValues() {
160177
$id++;
161178
}
162179

163-
if (AuthLDAP::isLdapPageSizeAvailable($config_ldap)) {
180+
if (AuthLDAP::isLdapPageSizeAvailable($config_ldap) && version_compare(PHP_VERSION, '7.3') < 0) {
164181
// phpcs:ignore Generic.PHP.DeprecatedFunctions
165182
ldap_control_paged_result_response($ds, $result, $cookie);
166183
}
@@ -235,11 +252,29 @@ public function prepareQuestionInputForSave($input) {
235252
set_error_handler('plugin_formcreator_ldap_warning_handler', E_WARNING);
236253

237254
try {
238-
$ds = $config_ldap->connect();
255+
$cookie = '';
256+
$ds = $config_ldap->connect();
239257
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
240-
// phpcs:ignore Generic.PHP.DeprecatedFunctions
241-
ldap_control_paged_result($ds, 1);
242-
$sn = ldap_search($ds, $config_ldap->fields['basedn'], $input['ldap_filter'], $attribute);
258+
if (version_compare(PHP_VERSION, '7.3') < 0) {
259+
// phpcs:ignore Generic.PHP.DeprecatedFunctions
260+
ldap_control_paged_result($ds, 1);
261+
$sn = ldap_search($ds, $config_ldap->fields['basedn'], $input['ldap_filter'], $attribute);
262+
} else {
263+
//since PHP 7.3, send serverctrls to ldap_search
264+
$controls = [
265+
[
266+
'oid' =>LDAP_CONTROL_PAGEDRESULTS,
267+
'iscritical' => true,
268+
'value' => [
269+
'size' => $config_ldap->fields['pagesize'],
270+
'cookie' => $cookie
271+
]
272+
]
273+
];
274+
$sn = @ldap_search($ds, $config_ldap->fields['basedn'], $input['ldap_filter'], $attribute);
275+
ldap_parse_result($ds, $sn, $errcode, $matcheddn, $errmsg, $referrals, $controls);
276+
$cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? '';
277+
}
243278
ldap_get_entries($ds, $sn);
244279
} catch (Exception $e) {
245280
Session::addMessageAfterRedirect(__('Cannot recover LDAP informations!', 'formcreator'), false, ERROR);

0 commit comments

Comments
 (0)