LDAP filters are defined in RFC 2254 and can be compared to the WHERE clause in SQL select statements - they filter the data returned from some search request - in this case the entries returned from the directory server. With Net_LDAP, you may use plain strings as filters, or preferably, the Net_LDAP_Filter class which mostly releases you of the burden to escape yourself and to remember all the various special characters needed for constructing and combining filters.
Where and how to use filters is described in chapter Search.
Although you should preferably use the Net_LDAP_Filter class to construct your LDAP filters, some theory may be interesting and helpful in understanding how to construct LDAP filters and what they are capable of.
Basic LDAP filters are composed of an "[attribute][operator][value]" pair enclosed by round brackets. There are several comparison operators available: "=" (equal), ">" (greater), "<" (less), ">=" (greater or equal), "<=" (less or equal) and "=~" (phonetical similar). The exact match behavior is defined by the attribute syntax of the attribute to which the filter should apply.
Example 54-1. Some basic string filters
The value part of the basic filter construct could also include a special character: "*". The star acts as placeholder for none, one or several characters at that position. "V*lue" would therefore match against "Value", "Vlue", "VaaAaAalue" and so on. There are some special named combinations using the star, but they work exactly the same way:
Table 54-1. Special named placeholder combinations
|present||(attr=*)||Also refered to as "any". Finds any entry containing any (unless empty) value for the named attribute.|
|begins||(attr=value*)||value starts with some fixed string|
|ends||(attr=*value)||value ends with some fixed string|
|contains||(attr=*value*)||value contains some fixed string|
Basic filters can be combined using the three logical operators "&" (and), "|" (or) and "!" (not). Note, that the smallest filter component, the basic filter enclosed in round brackets, remains isolated: instead of just adding another "[attribute][operator][value]" pair into the brackets, a new bracket level is introduced that contains all filter components that should be combined. Note also, that the logical operator does stand in front of all filter components, not between them as common in programming languages.
Example 54-2. Combining string filters
As you will read below, there are some special characters inside the LDAP filter definition and thus must be escaped. These are mainly the special characters used directly by the filter syntax like braces and the logical operators. Despite those, there are some other cases which need special threatment. Nearly all of this cases are hidden through the Net_LDAP_Filter class so you should only consider using string filters if you need to or you know what you are doing. If you need a filter string, you may also use Net_LDAP_Filters toString() function after building the filter.
The filter class has two different usage models: one for constructing basic filters and another to combine them logically. This has to do with the syntax of LDAP filters you may read below.
For creating basic filter components, you need to use the create() factory method. There, you combine three items: an attribute to filter for, a matching rule for comparison and a value that is beeing compared with the servers entries. The given value is automatically escaped, so you need take care if you want to use the star placeholder. In this case, you need to pass FALSE as fourth parameter to create() which causes value to be threaten as-is. This of course also means, that you need to escape the parts of the value that may contain restricted characters yourself using Net_LDAP2_Util::escape_filter_value(). To learn what characters are restricted, refer to RFC 2254 or the documentation of Net_LDAP2_Util::escape_filter_value(); otherwise its safe to always escape.
The matching rules partly follow the basic filter matching rules described above, but are enhanced to make your life easier:
Table 54-2. create()s matching rules
|equals||One of attributes values is exactly value. Please note that case sensitiviness depends on the matching rule defined in the attributes schema syntax.|
|begins||One of attributes values must begin with value|
|ends||One of attributes values must end with value|
|contains||One of attributes values must contain value|
|present | any||The attribute can contain any value but must be existent|
|greater||The attributes value is greater than value|
|less||The attributes value is less than value|
|greaterOrEqual||The attributes value is greater or equal than value|
|lessOrEqual||The attributes value is less or equal than value|
|approx||One of attributes values sounds similar to value. The matching behavior depends on the server implementation.|
Example 54-3. Creating LDAP filters
Although the filters can be used stand alone, they can be combined to match sophisticated search requiremets. This is done by using the combine() to combine several present Net_LDAP_Filter objects using a logical operator. The execption is the not operator since it only allows one filter object to be negated.
Table 54-3. combine()s operators
|and||All filter components must evaluate to true for the combined filter to be true|
|or||At least one filter component must evaluate to true for the combined filter to be true|
|not||The result of the filter component is inversed (true becomes false and vice versa). Note that this operator only accepts one filter object.|
Example 54-4. Combining LDAP filters
You may need some advanced functionality if you have to deal with string representation of filters.
Table 54-4. Advanced methods
|parse()||Takes an filter string and parses it into a Net_LDAP_Filter object. It also verifies, that the filter syntax is correct.|
|printMe()||In PERLs interface, this method is called "print" but due to language constraints, we cannot use that name. Prints the string representation of this filter object to standard output or to an optional filehandle passed as parameter.|
|toString()||Returns the string representation of the filter object.|