Core\Ldap\Entry::string
Returns the one value of an attribute as text, such as a person's mail or department.
If the entry has no value for the attribute, the result is null. A directory does not send an attribute that has no value, so ?? is a simple way to give a default.
string reads an attribute with one value. Some attributes have several values, such as a person's memberOf. For those, string throws a LogicError, and strings returns every value. A binary value, such as objectGUID, is not text. For it, string throws a LogicError, and bytes returns it.
Good to know: the result is tainted. Anyone who can change the directory can change this text, so escape it before you write it into a web page, for example with Core\Html::escape.
Parameters
$namestringThe attribute's name, such asmail. Upper and lower case are the same.
Return value
Returns ?tainted string. The value, or null when the entry has no value for the attribute.
Errors
LogicErrorThe attribute has more than one value, so usestrings. Or the value is not UTF-8 text, so usebytes.
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"]Print a department
<?nvs// Shows how to read one text value of an entry, and how to give a default.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $person = $corp->read('CN=Carla Diaz,OU=Company,DC=example,DC=test', { select: ['department', 'title'] });
if ($person != null) { // This prints `Sales`. echo $person->string('department') ?? 'no department', "\n";
// Carla Diaz has no job title, so `string` returns `null`. // This prints `no job title`. echo $person->string('title') ?? 'no job title', "\n";}Salesno job titleSeveral values need strings
<?nvs// Shows what `string` does with an attribute that has several values.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['memberOf'] });
if ($person != null) { // Anna Berg is in two groups, so `memberOf` has two values. // `string` throws a `LogicError`, and its message names `strings`. try { echo $person->string('memberOf'), "\n"; } catch (LogicError $refusal) { echo $refusal->message, "\n"; }
// `strings` returns both values. This prints `2`. echo Core\Arr::count($person->strings('memberOf') ?? []), "\n";}Core\Ldap\Entry::string: `memberOf` has 2 values. `strings` returns all of them2Show a name on a web page
<?nvs// Shows how to put a value from the directory into HTML safely.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $person = $corp->read('CN=Ben Kohl,OU=Company,DC=example,DC=test', { select: ['givenName', 'mail'] });
if ($person != null) { // The value is `tainted`, so `Core\Html::escape` makes it safe for HTML first. Core\Html\Markup $name = Core\Html::escape($person->string('givenName') ?? 'colleague'); Core\Html\Markup $mail = Core\Html::escape($person->string('mail') ?? '');
// This prints `<p>Hello Ben, your address is ben.kohl@example.test.</p>`. echo '<p>Hello ', $name, ', your address is ', $mail, ".</p>\n";}<p>Hello Ben, your address is ben.kohl@example.test.</p>