Skip to content

Core\Ldap\Filter::toString

$filter->toString(): tainted string

Returns a filter as LDAP filter text, such as (&(objectClass=user)(cn=Ann)).

Use it to write a filter to a log, or to see what a filter you built will find. A search does not use this text. The server gets the filter in its own binary form, so the text is only for people to read.

A *, (, ) or \ in a value is written as \ and two hex digits, so Ann* is written as Ann\2a. That way the text shows that the * is part of the value.

Good to know: the text is tainted, because a value in the filter may come from a user. Treat it as you treat any other user input.

Return value

Returns tainted string. The text. A *, (, ) or \ in a value is written as \ and two hex digits. The text is tainted, because a value in the filter may be.

Changelog

VersionChange
0.0.6Throwable::$message is tainted string, because some error messages contain input text. The text of a Core\Ldap\Filter and of a Core\Ldap\Dn is tainted string too.

Examples

Write a filter to the log

<?nvs
// Shows how to write a filter to a log, so you can see what a search looked for.
var $filter = Core\Ldap\Filter::all(
Core\Ldap\Filter::equals('objectClass', 'user'),
Core\Ldap\Filter::equals('sAMAccountName', 'anna'),
);
// This prints search: (&(objectClass=user)(sAMAccountName=anna)).
echo 'search: ', $filter->toString(), "\n";
Output
search: (&(objectClass=user)(sAMAccountName=anna))

Escaped characters

<?nvs
// Shows how special characters in a value are written in the text.
// A `\` in Novis text is written as `\\`, so this value is a*(b)\c.
var $filter = Core\Ldap\Filter::equals('cn', "a*(b)\\c");
// `*`, `(`, `)` and `\` become `\2a`, `\28`, `\29` and `\5c`.
// This prints (cn=a\2a\28b\29\5cc).
echo $filter->toString(), "\n";
Output
(cn=a\2a\28b\29\5cc)

Read the text back

<?nvs
// Shows that `parse` reads the text from `toString` back as the same filter.
var $built = Core\Ldap\Filter::any(
Core\Ldap\Filter::present('mail'),
Core\Ldap\Filter::not(Core\Ldap\Filter::atLeast('uSNChanged', '100')),
);
// This prints (|(mail=*)(!(uSNChanged>=100))) two times.
echo $built->toString(), "\n";
echo Core\Ldap\Filter::parse('(|(mail=*)(!(uSNChanged>=100)))')->toString(), "\n";
Output
(|(mail=*)(!(uSNChanged>=100)))
(|(mail=*)(!(uSNChanged>=100)))