Skip to content

Commit

Permalink
Merge branch 'MDL-63887_master' of git://github.com/markn86/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
snake committed Dec 12, 2018
2 parents f9479eb + 43dcb95 commit 84e8e17
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions auth/ldap/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,46 +698,51 @@ function sync_users($do_updates=true) {
array_push($contexts, $this->config->create_context);
}

$ldap_pagedresults = ldap_paged_results_supported($this->config->ldap_version, $ldapconnection);
$ldap_cookie = '';
$ldappagedresults = ldap_paged_results_supported($this->config->ldap_version, $ldapconnection);
$ldapcookie = '';
foreach ($contexts as $context) {
$context = trim($context);
if (empty($context)) {
continue;
}

do {
if ($ldap_pagedresults) {
ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldap_cookie);
if ($ldappagedresults) {
ldap_control_paged_result($ldapconnection, $this->config->pagesize, true, $ldapcookie);
}
if ($this->config->search_sub) {
// Use ldap_search to find first user from subtree.
$ldap_result = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute));
$ldapresult = ldap_search($ldapconnection, $context, $filter, array($this->config->user_attribute));
} else {
// Search only in this context.
$ldap_result = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute));
$ldapresult = ldap_list($ldapconnection, $context, $filter, array($this->config->user_attribute));
}
if(!$ldap_result) {
if (!$ldapresult) {
continue;
}
if ($ldap_pagedresults) {
ldap_control_paged_result_response($ldapconnection, $ldap_result, $ldap_cookie);
if ($ldappagedresults) {
$pagedresp = ldap_control_paged_result_response($ldapconnection, $ldapresult, $ldapcookie);
// Function ldap_control_paged_result_response() does not overwrite $ldapcookie if it fails, by
// setting this to null we avoid an infinite loop.
if ($pagedresp === false) {
$ldapcookie = null;
}
}
if ($entry = @ldap_first_entry($ldapconnection, $ldap_result)) {
if ($entry = @ldap_first_entry($ldapconnection, $ldapresult)) {
do {
$value = ldap_get_values_len($ldapconnection, $entry, $this->config->user_attribute);
$value = core_text::convert($value[0], $this->config->ldapencoding, 'utf-8');
$value = trim($value);
$this->ldap_bulk_insert($value);
} while ($entry = ldap_next_entry($ldapconnection, $entry));
}
unset($ldap_result); // Free mem.
} while ($ldap_pagedresults && $ldap_cookie !== null && $ldap_cookie != '');
unset($ldapresult); // Free mem.
} while ($ldappagedresults && $ldapcookie !== null && $ldapcookie != '');
}

// If LDAP paged results were used, the current connection must be completely
// closed and a new one created, to work without paged results from here on.
if ($ldap_pagedresults) {
if ($ldappagedresults) {
$this->ldap_close(true);
$ldapconnection = $this->ldap_connect();
}
Expand Down

0 comments on commit 84e8e17

Please sign in to comment.