Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
PHP 5.4+ ldap controle paged result
Browse files Browse the repository at this point in the history
  • Loading branch information
c12simple committed Nov 13, 2014
1 parent 296744c commit d431949
Showing 1 changed file with 55 additions and 17 deletions.
72 changes: 55 additions & 17 deletions core/src/plugins/auth.ldap/class.ldapAuthDriver.php
Expand Up @@ -282,8 +282,24 @@ public function getUserEntries($login = null, $countOnly = false, $offset = -1,
if (count($conn) < 1) {
return array("count" => 0);
}
$ret = ldap_search($conn,$this->ldapDN,$filter, $expected);
//$ret = ldap_search($conn,$this->ldapDN,$filter, $expected);

$cookie = '';
if(empty($this->pageSize) || !is_numeric($this->pageSize)){
$this->pageSize = 500;
}


$allEntries = array("count" => 0);
$isSupportPagedResult = function_exists("ldap_control_paged_result") && function_exists("ldap_control_paged_result_response");

$gotAllEntries = false;
$index = 0;
do {
if ($isSupportPagedResult)
ldap_control_paged_result($this->ldapconn, $this->pageSize, true, $cookie);

$ret = ldap_search($conn, $this->ldapDN, $filter, $expected, 0, 0);
foreach ($ret as $i => $resourceResult) {
if($resourceResult === false) continue;
if ($countOnly) {
Expand All @@ -295,27 +311,49 @@ public function getUserEntries($login = null, $countOnly = false, $offset = -1,
ldap_sort($conn[$i], $resourceResult, $this->ldapUserAttr);
}
$entries = ldap_get_entries($conn[$i], $resourceResult);
$index = 0;
if (!empty($entries["count"])) {
$allEntries["count"] += $entries["count"];
unset($entries["count"]);
foreach ($entries as $entry) {
if ($offset != -1 && $index < $offset) {
$index ++; continue;
}

if (in_array(strtolower("memberof"), array_map("strtolower", $expected)) && ($this->enableMemberOf)) {
$uid = $entry["dn"];
$strldap = "(&" . $this->ldapGFilter . "(member=".$uid."))";
$this->fakeMemberOf($conn, $this->ldapGDN, $strldap, array("cn"), $entry);
// for better performance
if ((is_array($entries)) && ($offset != -1) && ($limit != -1) && (($index + $this->pageSize) < $offset)){
$index += $this->pageSize;
continue;
}

if (!empty($entries["count"])) {
$allEntries["count"] += $entries["count"];
unset($entries["count"]);
foreach ($entries as $entry) {
if ($offset != -1 && $index < $offset) {
$index++;
continue;
}

// fake memberOf
if (in_array(strtolower("memberof"), array_map("strtolower", $expected)) && ($this->enableMemberOf) && method_exists($this, "fakeMemberOf")) {
$uid = $entry["dn"];
$strldap = "(&" . $this->ldapGFilter . "(member=" . $uid . "))";
$this->fakeMemberOf($conn, $this->ldapGDN, $strldap, array("cn"), $entry);
}

$allEntries[] = $entry;
$index++;
if (($offset != -1) && ($limit!= -1) && $index > $offset + $limit)
break;
}

$allEntries[] = $entry;
$index ++;
if($limit!= -1 && $index >= $offset + $limit) break;
if(($index > $offset + $limit) && ($limit != -1) && ($offset != -1))
$gotAllEntries = true;
}
}
}
if ($isSupportPagedResult)
foreach($ret as $element){
ldap_control_paged_result_response($this->ldapconn, $element, $cookie);
}
} while (($cookie !== null && $cookie != '') && ($isSupportPagedResult) && (!$gotAllEntries));

// reset paged_result for other activities (otherwise we will experience ldap error)
if ($isSupportPagedResult)
ldap_control_paged_result($this->ldapconn, 0);

return $allEntries;
}

Expand Down

0 comments on commit d431949

Please sign in to comment.