Skip to content

Core\Ldap\Entry::strings

$entry->strings(string $name): ?array<tainted string>$entry->strings(
    string $name,
): ?array<tainted string>

Returns every value of an attribute as an array of text, such as every member of a group or every group in a person's memberOf.

The values are in the order the server sent them. That order is not fixed, so sort the array if you print it or compare it. If the entry has no value for the attribute, the result is null.

strings also works for an attribute with one value. The result is then an array with one item. If one of the values is not UTF-8 text, strings throws a LogicError.

Good to know: every value is tainted. Anyone who can change the directory can change this text, so check or escape each value before you use it in a page, a query or a command.

Parameters

  • $namestringThe attribute's name, such as mail. Upper and lower case are the same.

Return value

Returns ?array<tainted string>. The values in the order the server sent them, or null when the entry has no value for the attribute.

Errors

  • LogicErrorA value is not UTF-8 text.

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 members of a group

<?nvs
// Shows how to list every member of a group.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $group = $corp->read('CN=Employees,OU=Company,DC=example,DC=test', { select: ['member'] });
if ($group != null) {
// Each value of `member` is the DN of one member.
// The server sends them in no fixed order, so they are sorted.
array<tainted string> $members = $group->strings('member') ?? [];
// This prints five DNs. The last one is the group `Managers`.
foreach (Core\Arr::sort($members) as tainted string $member) {
echo $member, "\n";
}
}
Output
CN=Anna Berg,OU=Company,DC=example,DC=test
CN=Ben Kohl,OU=Company,DC=example,DC=test
CN=Carla Diaz,OU=Company,DC=example,DC=test
CN=Dora Lind,OU=Company,DC=example,DC=test
CN=Managers,OU=Company,DC=example,DC=test

An account in no group

<?nvs
// Shows that `strings` returns `null` when an entry has no value at all.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
array<string> $dns = [
'CN=Ben Kohl,OU=Company,DC=example,DC=test',
'CN=Reader,OU=Company,DC=example,DC=test',
];
foreach ($dns as string $dn) {
var $account = $corp->read($dn, { select: ['memberOf'] });
if ($account == null) {
continue;
}
// Ben Kohl is in two groups. The account `Reader` is in none, so `strings` returns `null`.
// This prints `Ben Kohl: 2 groups` and `Reader: no groups`.
?array<tainted string> $groups = $account->strings('memberOf');
if ($groups == null) {
echo $account->dn()->rdnValue(), ": no groups\n";
} else {
echo $account->dn()->rdnValue(), ': ', Core\Arr::count($groups), " groups\n";
}
}
Output
Ben Kohl: 2 groups
Reader: no groups

One value is an array of one

<?nvs
// Shows that `strings` also reads an attribute with only one value.
// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
var $person = $corp->read('CN=Dora Lind,OU=Company,DC=example,DC=test', { select: ['mail'] });
if ($person != null) {
// `mail` has one value, so the array has one item.
array<tainted string> $addresses = $person->strings('mail') ?? [];
// This prints `1` and then `dora.lind@example.test`.
echo Core\Arr::count($addresses), "\n";
foreach ($addresses as tainted string $address) {
echo $address, "\n";
}
}
Output
1
dora.lind@example.test