Core\Ldap\Entry::bytes
Returns the one value of an attribute as bytes, exactly as the server sent it.
Use bytes for a binary value, such as objectGUID, objectSid or a photo in jpegPhoto. These values are not text, so string throws a LogicError for them. bytes also works for a text attribute. It then returns the UTF-8 bytes of the text.
If the entry has no value for the attribute, the result is null. If the attribute has more than one value, bytes throws a LogicError. toArray returns every value as bytes.
For the common binary attributes there is an easier way. uuid reads objectGUID as a Core\Uuid, and sid reads objectSid as a Core\Ldap\Sid.
Good to know: the result is tainted, because anyone who can change the directory can change these bytes.
Parameters
$namestringThe attribute's name, such asmail. Upper and lower case are the same.
Return value
Returns ?tainted bytes. The value, or null when the entry has no value for the attribute.
Errors
LogicErrorThe attribute has more than one value.
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"]Read a binary value
<?nvs// Shows how to read a binary value, which `string` cannot read.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['objectGUID'] });
if ($person != null) { // `objectGUID` is 16 bytes. Its value is different in every directory, // so this example prints only its length. This prints `16`. ?tainted bytes $guid = $person->bytes('objectGUID'); if ($guid != null) { echo Core\Bytes::length($guid), "\n"; }}16Text read as bytes
<?nvs// Shows that `bytes` also reads a text value, and returns its UTF-8 bytes.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $person = $corp->read('CN=Anna Berg,OU=Company,DC=example,DC=test', { select: ['mail'] });
if ($person != null) { // `anna.berg@example.test` has 22 characters and 22 bytes. // This prints `22`. ?tainted bytes $mail = $person->bytes('mail'); if ($mail != null) { echo Core\Bytes::length($mail), "\n"; }}22A person without a photo
<?nvs// Shows how a program checks for a photo before it sends one.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $person = $corp->read('CN=Carla Diaz,OU=Company,DC=example,DC=test', { select: ['jpegPhoto'] });
if ($person != null) { // Carla Diaz has no photo, so `bytes` returns `null`. // This prints `no photo, show the default picture`. ?tainted bytes $photo = $person->bytes('jpegPhoto'); if ($photo == null) { echo "no photo, show the default picture\n"; } else { echo 'photo of ', Core\Bytes::length($photo), " bytes\n"; }}no photo, show the default picture