You are here

public function SimpleLdapServer::search in Simple LDAP 8

Search the LDAP server.

Parameters

string $base_dn: LDAP search base.

string $filter: LDAP search filter.

string $scope: LDAP search scope. Valid values are 'sub', 'one', and 'base'.

array $attributes: Array of attributes to retrieve.

int $attrsonly: Set to 1 in order to retrieve only the attribute names without the values. Set to 0 (default) to retrieve both the attribute names and values.

int $sizelimit: Client-side size limit. Set this to 0 to indicate no limit. The server may impose stricter limits.

int $timelimit: Client-side time limit. Set this to 0 to indicate no limit. The server may impose stricter limits.

int $deref: Specifies how aliases should be handled during the search.

Return value

array Search results.

Throws

SimpleLdapException

1 call to SimpleLdapServer::search()
SimpleLdapServer::setRootDse in src/SimpleLdapServer.php
Loads the server's rootDSE.

File

src/SimpleLdapServer.php, line 108

Class

SimpleLdapServer

Namespace

Drupal\simple_ldap

Code

public function search($base_dn, $filter = 'objectclass=*', $scope = 'sub', $attributes = array(), $attrsonly = 0, $sizelimit = 0, $timelimit = 0, $deref = NULL) {

  // Make sure there is a valid binding.
  if (!$this->ldap
    ->isBound()) {
    $this
      ->bind();
  }
  try {

    // Use a post-test loop (do/while) because this will always be done once.
    // It will only loop if paged queries are supported/enabled, and more than
    // one page is available.
    $entries = array(
      'count' => 0,
    );
    $cookie = '';
    do {
      if ($this->pagesize) {

        // Set the paged query cookie.
        $this->ldap
          ->controlPagedResult($this->pagesize, FALSE, $cookie);
      }

      // Perform the search based on the scope provided.
      switch ($scope) {
        case 'base':
          $result = $this->ldap
            ->ldapRead($base_dn, $filter, $attributes, $attrsonly, $sizelimit, $timelimit, $deref);
          break;
        case 'one':
          $result = $this->ldap
            ->ldapList($base_dn, $filter, $attributes, $attrsonly, $sizelimit, $timelimit, $deref);
          break;
        case 'sub':
        default:
          $result = $this->ldap
            ->ldapSearch($base_dn, $filter, $attributes, $attrsonly, $sizelimit, $timelimit, $deref);
          break;
      }
      if ($this->pagesize) {

        // Merge page into $entries.
        $e = $this->ldap
          ->getEntries($result);
        $entries['count'] += $e['count'];
        for ($i = 0; $i < $e['count']; $i++) {
          $entries[] = $e[$i];
        }

        // Get the paged query response cookie.
        $this->ldap
          ->controlPageResultResponse($result, $cookie);
      }
      else {
        $entries = $this->ldap
          ->getEntries($result);
      }

      // Free the query result memory.
      $this->ldap
        ->freeResult($result);
    } while ($cookie !== NULL && $cookie != '');
  } catch (SimpleLdapException $e) {

    // Error code 32 means there were no matching search results.
    if ($e
      ->getCode() == 32) {
      $entries = array(
        'count' => 0,
      );
    }
    else {
      throw $e;
    }
  }

  // ldap_get_entries returns NULL if ldap_read does not find anything.
  // Reformat the result into something consistent with the other search
  // types.
  if ($entries === NULL) {
    $entries = array(
      'count' => 0,
    );
  }
  return $this->ldap
    ->clean($entries);
}