MDL-67118 auth_ldap: paged results functions deprecated php74 and up

Starting with php74 the following functions are deprecated:
- ldap_control_paged_result()
- ldap_control_paged_result_response()

Starting with php73, ldap servercontrols were included. One of those
servercontrols, LDAP_CONTROL_PAGEDRESULTS, is the one in charge of
controlling paged results.

So, we are going to add some conditional code here:

1) if php < 7.3, use old paged result functions.
2) if php >= 7.3, switch to LDAP_CONTROL_PAGEDRESULTS servercontrol.

With a TODO about removing 1) in Moodle 4.1, once php73 becomes required.
This commit is contained in:
Eloy Lafuente (stronk7) 2019-11-26 23:43:18 +01:00
parent fd126006b0
commit 988f9bf5b5

View file

@ -691,6 +691,7 @@ class auth_plugin_ldap extends auth_plugin_base {
//// ////
// prepare some data we'll need // prepare some data we'll need
$filter = '(&('.$this->config->user_attribute.'=*)'.$this->config->objectclass.')'; $filter = '(&('.$this->config->user_attribute.'=*)'.$this->config->objectclass.')';
$servercontrols = array();
$contexts = explode(';', $this->config->contexts); $contexts = explode(';', $this->config->contexts);
@ -708,25 +709,59 @@ class auth_plugin_ldap extends auth_plugin_base {
do { do {
if ($ldappagedresults) { if ($ldappagedresults) {
// TODO: Remove the old branch of code once PHP 7.3.0 becomes required (Moodle 4.1).
if (version_compare(PHP_VERSION, '7.3.0', '<')) {
// Before 7.3, use this function that was deprecated in PHP 7.4.
ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldapcookie); ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldapcookie);
} else {
// PHP 7.3 and up, use server controls.
$servercontrols = array(array(
'oid' => LDAP_CONTROL_PAGEDRESULTS, 'value' => array(
'size' => $this->config->pagesize, 'cookie' => $ldapcookie)));
}
} }
if ($this->config->search_sub) { if ($this->config->search_sub) {
// Use ldap_search to find first user from subtree. // Use ldap_search to find first user from subtree.
// TODO: Remove the old branch of code once PHP 7.3.0 becomes required (Moodle 4.1).
if (version_compare(PHP_VERSION, '7.3.0', '<')) {
$ldapresult = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute)); $ldapresult = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute));
} else {
$ldapresult = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute),
0, -1, -1, LDAP_DEREF_NEVER, $servercontrols);
}
} else { } else {
// Search only in this context. // Search only in this context.
// TODO: Remove the old branch of code once PHP 7.3.0 becomes required (Moodle 4.1).
if (version_compare(PHP_VERSION, '7.3.0', '<')) {
$ldapresult = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute)); $ldapresult = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute));
} else {
$ldapresult = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute),
0, -1, -1, LDAP_DEREF_NEVER, $servercontrols);
}
} }
if (!$ldapresult) { if (!$ldapresult) {
continue; continue;
} }
if ($ldappagedresults) { if ($ldappagedresults) {
// Get next server cookie to know if we'll need to continue searching.
$ldapcookie = '';
// TODO: Remove the old branch of code once PHP 7.3.0 becomes required (Moodle 4.1).
if (version_compare(PHP_VERSION, '7.3.0', '<')) {
// Before 7.3, use this function that was deprecated in PHP 7.4.
$pagedresp = ldap_control_paged_result_response($ldapconnection, $ldapresult, $ldapcookie); $pagedresp = ldap_control_paged_result_response($ldapconnection, $ldapresult, $ldapcookie);
// Function ldap_control_paged_result_response() does not overwrite $ldapcookie if it fails, by // Function ldap_control_paged_result_response() does not overwrite $ldapcookie if it fails, by
// setting this to null we avoid an infinite loop. // setting this to null we avoid an infinite loop.
if ($pagedresp === false) { if ($pagedresp === false) {
$ldapcookie = null; $ldapcookie = null;
} }
} else {
// Get next cookie from controls.
ldap_parse_result($ldapconnection, $ldapresult, $errcode, $matcheddn,
$errmsg, $referrals, $controls);
if (isset($controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'])) {
$ldapcookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'];
}
}
} }
if ($entry = @ldap_first_entry($ldapconnection, $ldapresult)) { if ($entry = @ldap_first_entry($ldapconnection, $ldapresult)) {
do { do {
@ -1504,6 +1539,7 @@ class auth_plugin_ldap extends auth_plugin_base {
if ($filter == '*') { if ($filter == '*') {
$filter = '(&('.$this->config->user_attribute.'=*)'.$this->config->objectclass.')'; $filter = '(&('.$this->config->user_attribute.'=*)'.$this->config->objectclass.')';
} }
$servercontrols = array();
$contexts = explode(';', $this->config->contexts); $contexts = explode(';', $this->config->contexts);
if (!empty($this->config->create_context)) { if (!empty($this->config->create_context)) {
@ -1520,20 +1556,54 @@ class auth_plugin_ldap extends auth_plugin_base {
do { do {
if ($ldap_pagedresults) { if ($ldap_pagedresults) {
// TODO: Remove the old branch of code once PHP 7.3.0 becomes required (Moodle 4.1).
if (version_compare(PHP_VERSION, '7.3.0', '<')) {
// Before 7.3, use this function that was deprecated in PHP 7.4.
ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldap_cookie); ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldap_cookie);
} else {
// PHP 7.3 and up, use server controls.
$servercontrols = array(array(
'oid' => LDAP_CONTROL_PAGEDRESULTS, 'value' => array(
'size' => $this->config->pagesize, 'cookie' => $ldap_cookie)));
}
} }
if ($this->config->search_sub) { if ($this->config->search_sub) {
// Use ldap_search to find first user from subtree. // Use ldap_search to find first user from subtree.
// TODO: Remove the old branch of code once PHP 7.3.0 becomes required (Moodle 4.1).
if (version_compare(PHP_VERSION, '7.3.0', '<')) {
$ldap_result = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute)); $ldap_result = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute));
} else {
$ldap_result = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute),
0, -1, -1, LDAP_DEREF_NEVER, $servercontrols);
}
} else { } else {
// Search only in this context. // Search only in this context.
// TODO: Remove the old branch of code once PHP 7.3.0 becomes required (Moodle 4.1).
if (version_compare(PHP_VERSION, '7.3.0', '<')) {
$ldap_result = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute)); $ldap_result = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute));
} else {
$ldap_result = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute),
0, -1, -1, LDAP_DEREF_NEVER, $servercontrols);
}
} }
if(!$ldap_result) { if(!$ldap_result) {
continue; continue;
} }
if ($ldap_pagedresults) { if ($ldap_pagedresults) {
// Get next server cookie to know if we'll need to continue searching.
$ldap_cookie = '';
// TODO: Remove the old branch of code once PHP 7.3.0 becomes required (Moodle 4.1).
if (version_compare(PHP_VERSION, '7.3.0', '<')) {
// Before 7.3, use this function that was deprecated in PHP 7.4.
ldap_control_paged_result_response($ldapconnection, $ldap_result, $ldap_cookie); ldap_control_paged_result_response($ldapconnection, $ldap_result, $ldap_cookie);
} else {
// Get next cookie from controls.
ldap_parse_result($ldapconnection, $ldap_result, $errcode, $matcheddn,
$errmsg, $referrals, $controls);
if (isset($controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'])) {
$ldap_cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'];
}
}
} }
$users = ldap_get_entries_moodle($ldapconnection, $ldap_result); $users = ldap_get_entries_moodle($ldapconnection, $ldap_result);
// Add found users to list. // Add found users to list.