Skip to content

Core\Ldap\Entries::references

$entries->references(): array<tainted string>

Returns the URLs of other servers that the directory named for a search, as an array of text.

A directory can keep some of its parts on other servers. When a search reaches such a part, the server does not send its entries. It sends a URL instead, called a reference. references returns these URLs in the order the server sent them. Read them after the loop, because the list is only complete then. Most searches stay on one server, and then the array is empty.

Good to know: Novis never connects to these servers by itself. Every URL is tainted, because the server sent it. Check a URL before you use it, for example to open another connection.

Return value

Returns array<tainted string>. The URLs in the order the server sent them. The list is complete after the loop ends, and it is empty when the server named no other server.

Examples

The examples below run with this configuration file.

nvs.toml
# 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"]

List the other servers

<?nvs
// Shows how to list the other servers that the directory names for a search.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
// This search reads the entries directly below the top of the directory.
var $entries = $corp->search(Core\Ldap\Filter::present('objectClass'), {
scope: Core\Ldap\Connection::Scope::OneLevel,
select: ['cn'],
});
int $count = 0;
foreach ($entries as Core\Ldap\Entry $entry) {
$count = $count + 1;
}
// Some parts of the directory are on other servers. The server sends a URL for each part.
// Novis does not connect to them. This prints three URLs.
foreach ($entries->references() as tainted string $url) {
echo $url, "\n";
}
Output
ldaps://example.test/CN=Configuration,DC=example,DC=test??base
ldaps://example.test/DC=DomainDnsZones,DC=example,DC=test??base
ldaps://example.test/DC=ForestDnsZones,DC=example,DC=test??base

Most searches name no server

<?nvs
// Shows that `references` is an empty array when the search stays on one server.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $people = $corp->search(Core\Ldap\Filter::equals('department', 'Sales'), {
base: 'OU=Company,DC=example,DC=test',
select: ['cn'],
sort: 'cn',
});
foreach ($people as Core\Ldap\Entry $person) {
echo $person->string('cn') ?? '', "\n";
}
// Everything below `OU=Company` is on this server. This prints 0.
echo Core\Arr::count($people->references()), "\n";
Output
Anna Berg
Carla Diaz
0

Read them after the loop

<?nvs
// Shows how to tell the user that a search did not find everything on this server.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $entries = $corp->search(Core\Ldap\Filter::equals('objectClass', 'container'), {
select: ['cn'],
});
uint $found = 0;
foreach ($entries as Core\Ldap\Entry $entry) {
$found = $found + 1;
}
echo $found > 0 ? "Found some containers.\n" : "Found no containers.\n";
// Read `references` after the loop. Then the list is complete.
// This search starts at the top of the directory, so the list is not empty.
uint $elsewhere = Core\Arr::count($entries->references());
if ($elsewhere > 0) {
echo 'Some entries are in ', $elsewhere, " other parts of the directory.\n";
} else {
echo "Every entry is on this server.\n";
}
Output
Found some containers.
Some entries are in 3 other parts of the directory.