Skip to content

Commit

Permalink
MDL-67118 enrol_ldap: paged results functions deprecated php74 and up
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stronk7 committed Jan 16, 2020
1 parent 49d1ce3 commit ba62f54
Showing 1 changed file with 86 additions and 20 deletions.
106 changes: 86 additions & 20 deletions enrol/ldap/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ public function sync_enrolments(progress_trace $trace, $onecourse = null) {
}

$ldap_cookie = '';
$servercontrols = array();
foreach ($ldap_contexts as $ldap_context) {
$ldap_context = trim($ldap_context);
if (empty($ldap_context)) {
Expand All @@ -388,28 +389,60 @@ public function sync_enrolments(progress_trace $trace, $onecourse = null) {
$flat_records = array();
do {
if ($ldap_pagedresults) {
ldap_control_paged_result($this->ldapconnection, $this->config->pagesize, true, $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($this->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->course_search_sub) {
// Use ldap_search to find first user from subtree
$ldap_result = @ldap_search($this->ldapconnection,
$ldap_context,
$ldap_search_pattern,
$ldap_fields_wanted);
// 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($this->ldapconnection, $ldap_context,
$ldap_search_pattern, $ldap_fields_wanted);
} else {
$ldap_result = @ldap_search($this->ldapconnection, $ldap_context,
$ldap_search_pattern, $ldap_fields_wanted,
0, -1, -1, LDAP_DEREF_NEVER, $servercontrols);
}
} else {
// Search only in this context
$ldap_result = @ldap_list($this->ldapconnection,
$ldap_context,
$ldap_search_pattern,
$ldap_fields_wanted);
// 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($this->ldapconnection, $ldap_context,
$ldap_search_pattern, $ldap_fields_wanted);
} else {
$ldap_result = @ldap_list($this->ldapconnection, $ldap_context,
$ldap_search_pattern, $ldap_fields_wanted,
0, -1, -1, LDAP_DEREF_NEVER, $servercontrols);
}
}
if (!$ldap_result) {
continue; // Next
}

if ($ldap_pagedresults) {
ldap_control_paged_result_response($this->ldapconnection, $ldap_result, $ldap_cookie);
// 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($this->ldapconnection, $ldap_result, $ldap_cookie);
} else {
// Get next cookie from controls.
ldap_parse_result($this->ldapconnection, $ldap_result, $errcode, $matcheddn,
$errmsg, $referrals, $controls);
if (isset($controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'])) {
$ldap_cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'];
}
}
}

// Check and push results
Expand Down Expand Up @@ -769,32 +802,65 @@ protected function find_ext_enrolments($memberuid, $role) {
}

$ldap_cookie = '';
$servercontrols = array();
$flat_records = array();
do {
if ($ldap_pagedresults) {
ldap_control_paged_result($this->ldapconnection, $this->config->pagesize, true, $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($this->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->get_config('course_search_sub')) {
// Use ldap_search to find first user from subtree
$ldap_result = @ldap_search($this->ldapconnection,
$context,
$ldap_search_pattern,
$ldap_fields_wanted);
// 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($this->ldapconnection, $context,
$ldap_search_pattern, $ldap_fields_wanted);
} else {
$ldap_result = @ldap_search($this->ldapconnection, $context,
$ldap_search_pattern, $ldap_fields_wanted,
0, -1, -1, LDAP_DEREF_NEVER, $servercontrols);
}
} else {
// Search only in this context
$ldap_result = @ldap_list($this->ldapconnection,
$context,
$ldap_search_pattern,
$ldap_fields_wanted);
// 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($this->ldapconnection, $context,
$ldap_search_pattern, $ldap_fields_wanted);
} else {
$ldap_result = @ldap_list($this->ldapconnection, $context,
$ldap_search_pattern, $ldap_fields_wanted,
0, -1, -1, LDAP_DEREF_NEVER, $servercontrols);
}
}

if (!$ldap_result) {
continue;
}

if ($ldap_pagedresults) {
ldap_control_paged_result_response($this->ldapconnection, $ldap_result, $ldap_cookie);
// 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($this->ldapconnection, $ldap_result, $ldap_cookie);
} else {
// Get next cookie from controls.
ldap_parse_result($this->ldapconnection, $ldap_result, $errcode, $matcheddn,
$errmsg, $referrals, $controls);
if (isset($controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'])) {
$ldap_cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'];
}
}
}

// Check and push results. ldap_get_entries() already
Expand Down

0 comments on commit ba62f54

Please sign in to comment.