You are here

public function LdapServer::pagedLdapQuery in Lightweight Directory Access Protocol (LDAP) 7.2

Same name and namespace in other branches
  1. 8.2 ldap_servers/LdapServer.class.php \LdapServer::pagedLdapQuery()
  2. 7 ldap_servers/LdapServer.class.php \LdapServer::pagedLdapQuery()

Execute a paged ldap query and return entries as one aggregated array.

$this->searchPageStart and $this->searchPageEnd should be set before calling if a particular set of pages is desired.

Parameters

array $ldap_query_params: of form: 'base_dn' => base_dn, 'filter' => filter, 'attributes' => attributes, 'attrsonly' => attrsonly, 'sizelimit' => sizelimit, 'timelimit' => timelimit, 'deref' => deref, 'scope' => scope,

(this array of parameters is primarily passed on to ldapQuery() method)

Return value

array of ldap entries or FALSE on error.

1 call to LdapServer::pagedLdapQuery()
LdapServer::search in ldap_servers/LdapServer.class.php
Perform an LDAP search.

File

ldap_servers/LdapServer.class.php, line 905
Defines server classes and related functions.

Class

LdapServer
LDAP Server Class.

Code

public function pagedLdapQuery($ldap_query_params) {
  if (!($this->searchPagination && $this->paginationEnabled)) {
    $watchdog_tokens = [
      '%basedn' => $ldap_query_params['base_dn'],
      '%filter' => $ldap_query_params['filter'],
      '%attributes' => print_r($ldap_query_params['attributes'], TRUE),
      '%errmsg' => $this
        ->errorMsg('ldap'),
      '%errno' => $this
        ->ldapErrorNumber(),
    ];
    watchdog('ldap_servers', "LDAP server pagedLdapQuery() called when functionality not available in php install or\n        not enabled in ldap server configuration.  error. basedn: %basedn| filter: %filter| attributes:\n         %attributes| errmsg: %errmsg| ldap err no: %errno|", $watchdog_tokens);
    return FALSE;
  }
  $page_token = '';
  $page = 0;
  $estimated_entries = 0;
  $aggregated_entries = [];
  $aggregated_entries_count = 0;
  $has_page_results = FALSE;
  do {
    ldap_control_paged_result($this->connection, $this->searchPageSize, TRUE, $page_token);
    $result = $this
      ->ldapQuery($ldap_query_params['scope'], $ldap_query_params);
    if ($page >= $this->searchPageStart) {
      $skipped_page = FALSE;
      if ($result && $this
        ->countEntries($result) !== FALSE) {
        $page_entries = ldap_get_entries($this->connection, $result);
        unset($page_entries['count']);
        $has_page_results = is_array($page_entries) && count($page_entries) > 0;
        $aggregated_entries = array_merge($aggregated_entries, $page_entries);
        $aggregated_entries_count = count($aggregated_entries);
      }
      elseif ($this
        ->ldapErrorNumber()) {
        $watchdog_tokens = [
          '%basedn' => $ldap_query_params['base_dn'],
          '%filter' => $ldap_query_params['filter'],
          '%attributes' => print_r($ldap_query_params['attributes'], TRUE),
          '%errmsg' => $this
            ->errorMsg('ldap'),
          '%errno' => $this
            ->ldapErrorNumber(),
        ];
        watchdog('ldap_servers', "LDAP ldap_search error. basedn: %basedn| filter: %filter| attributes:\n            %attributes| errmsg: %errmsg| ldap err no: %errno|", $watchdog_tokens);
        return FALSE;
      }
      else {
        return FALSE;
      }
    }
    else {
      $skipped_page = TRUE;
    }
    @ldap_control_paged_result_response($this->connection, $result, $page_token, $estimated_entries);
    if ($ldap_query_params['sizelimit'] && $this
      ->ldapErrorNumber() == LDAP_SIZELIMIT_EXCEEDED) {

      // False positive error thrown.  do not set result limit error when $sizelimit specified.
    }
    elseif ($this
      ->hasError()) {
      watchdog('ldap_servers', 'ldap_control_paged_result_response() function error. LDAP Error: %message, ldap_list() parameters: %query', [
        '%message' => $this
          ->errorMsg('ldap'),
        '%query' => $ldap_query_params['query_display'],
      ], WATCHDOG_ERROR);
    }
    if (isset($ldap_query_params['sizelimit']) && $ldap_query_params['sizelimit'] && $aggregated_entries_count >= $ldap_query_params['sizelimit']) {
      $discarded_entries = array_splice($aggregated_entries, $ldap_query_params['sizelimit']);
      break;
    }
    elseif ($this->searchPageEnd !== NULL && $page >= $this->searchPageEnd) {
      break;
    }
    elseif ($page_token === NULL || $page_token == '') {
      break;
    }
    $page++;
  } while ($skipped_page || $has_page_results);
  $aggregated_entries['count'] = count($aggregated_entries);
  return $aggregated_entries;
}