Core\Ldap\Connection::search
$connection->search(Core\Ldap\Filter $filter, {…}): Core\Ldap\Entries$connection->search( Core\Ldap\Filter $filter, {…}, ): Core\Ldap\Entries
Finds the entries in the directory that match a filter. It returns a Core\Ldap\Entries, and a foreach loop over it gives one Core\Ldap\Entry at a time.
You build the filter with Core\Ldap\Filter, such as Filter::equals('sAMAccountName', $login). A value from a request can go into a filter as it is. The filter escapes it, so the value cannot change what the filter matches.
The options say where and how to search. base is the DN the search starts from, and scope is how far below it the search looks. select names the attributes each entry has. sort, offset and window return one sorted page of a long list, and total() then returns the size of the whole list.
The server sends the entries in pages of up to pageSize entries. The loop reads the next page only when it reaches it, so a large result never needs all of its entries in memory at once.
Good to know: the values of an entry are tainted. A search that finds more than sizeLimit entries throws Core\Ldap\LdapError inside the loop.
Parameters
$filterCore\Ldap\FilterWhich entries to return, such asFilter::equals('sAMAccountName', $login).
Options
baseCore\Ldap\Dn|stringoptionalThe DN the search starts from, as aDnor as text. Left out, it is thebaseof the[ldap]block. Text cannot betainted.scopeCore\Ldap\Connection::ScopeoptionalHow far belowbasethe search looks. The default isScope::Subtree.selectarray<string>optionalThe attributes each entry has, such as['cn', 'mail']. Left out, an entry has every attribute the connection's account can read.pageSizeintoptionalHow many entries the server sends at once. The default is 1000.sizeLimitintoptionalThe most entries the search may find. The default, 0, is the server's own limit.sortstringoptionalThe attribute the server sorts the entries by, such ascn. Left out, the entries come in the server's own order.descendingbooloptionaltruesorts from the largest value to the smallest. It needssort. The default isfalse.offsetintoptionalHow many sorted entries the window skips. It needswindow. The default is 0.windowintoptionalHow many sorted entries to return, starting afteroffset. It needssort. With{ sort: 'cn', offset: 20, window: 10 }, the search returns entries 21 to 30.total()then returns how many entries there are in all. The default, 0, returns every entry.showDeletedbooloptionaltruealso finds deleted entries. Active Directory keeps a deleted entry for some time, with most of its attributes removed.isDeleted()checks whether an entry is one of them. The account needs the right to read deleted entries. The default isfalse.
Return value
Returns Core\Ldap\Entries. A Core\Ldap\Entries. Use it in a foreach loop to get each Core\Ldap\Entry.
Errors
LogicErrorA name inselectorsortis not an attribute name, a number is out of range,window,offsetordescendingis given without the option it needs, the search has nobase, or the connection is closed.Core\Ldap\LdapErrorThe server returned an error, such asNoSuchObjectfor abasethat does not exist. A search that finds more entries thansizeLimitthrowsSizeLimitExceededin the loop.
Examples
The examples below run with this configuration file.
# These examples use the LDAP directory server on this computer at port 16636.# `tls_ca_file` is the certificate that the server's certificate is checked against.[ldap.corp]url = "ldaps://127.0.0.1:16636"base = "DC=example,DC=test"user = "CN=Reader,OU=Company,DC=example,DC=test"password = "Novis-reader-1"tls_ca_file = "../../../../../tests/db/ca.crt"timeout = "5s"
# The program may open the `corp` directory.[capabilities.ldap]connect = ["corp"]Find the people in a department
<?nvs// Shows how to find every person in one department.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
// `Filter::all` matches an entry only when every filter inside it matches.var $sales = Core\Ldap\Filter::all( Core\Ldap\Filter::equals('objectClass', 'user'), Core\Ldap\Filter::equals('department', 'Sales'),);
// `base` is where the search starts, and `select` names the attributes each entry has.// The server returns entries in no fixed order. `sort` sorts them by `cn`.var $people = $corp->search($sales, { base: 'OU=Company,DC=example,DC=test', select: ['cn', 'mail'], sort: 'cn' });
// The `foreach` loop reads one entry at a time. This prints Anna Berg and Carla Diaz.foreach ($people as Core\Ldap\Entry $person) { echo $person->string('cn') ?? '', ': ', $person->string('mail') ?? 'no address', "\n";}Anna Berg: anna.berg@example.testCarla Diaz: carla.diaz@example.testOne page of a sorted list
<?nvs// Shows how to show a long list of people one page at a time, sorted by name.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $people = Core\Ldap\Filter::equals('objectClass', 'user');
// The server sorts the entries by `cn`. `offset` skips the first two,// and `window` returns the next two.var $page = $corp->search($people, { base: 'OU=Company,DC=example,DC=test', select: ['cn'], sort: 'cn', offset: 2, window: 2,});
// This prints `Carla Diaz` and `Dora Lind`.foreach ($page as Core\Ldap\Entry $person) { echo $person->string('cn') ?? '', "\n";}
// `total()` returns how many entries there are in all, on every page together.echo 'people in all: ', $page->total() ?? 0, "\n";Carla DiazDora Lindpeople in all: 5A login from a request
<?nvs// Shows how to find a user by a login name that comes from a request.// requires: samba-ad
class Accounts{ // `Filter::equals` escapes the login, so the login cannot change the filter. public static function find(Core\Ldap\Connection $corp, tainted string $login): tainted string { var $users = $corp->search(Core\Ldap\Filter::equals('sAMAccountName', $login), { select: ['cn'] }); foreach ($users as Core\Ldap\Entry $user) { return $user->dn()->toString(); } return '(no such user)'; }}
var $corp = Core\Ldap::connect('corp');
// This prints the DN of the user `Reader`.echo Accounts::find($corp, 'Reader'), "\n";
// `*` is a wildcard in a filter that is written as text. Here it is a normal character,// so this prints `(no such user)`.echo Accounts::find($corp, '*'), "\n";CN=Reader,OU=Company,DC=example,DC=test(no such user)