Source for file Search.php
Documentation is available at Search.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
* Result set of an LDAP search
* @author Tarjej Huse <tarjei@bergfald.no>
* @author Benedikt Hallinger <beni@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @version CVS $Id: Search.php,v 1.2 2008/03/20 09:32:39 beni Exp $
* @link http://pear.php.net/package/Net_LDAP22/
* Search result identifier
* A reference of the Net_LDAP2 object for passing to Net_LDAP2_Entry
* Result entry identifier
* The errorcode the search got
* Some errorcodes might be of interest, but might not be best handled as errors.
* examples: 4 - LDAP_SIZELIMIT_EXCEEDED - indicates a huge search.
* Incomplete results are returned. If you just want to check if there's anything in the search.
* than this is a point to handle.
* 32 - no such object - search here returns a count of 0.
* Cache for all entries already fetched from iterator interface
* What attributes we searched for
* The $attributes array contains the names of the searched attributes and gets
* passed from $Net_LDAP2->search() so the Net_LDAP2_Search object can tell
* what attributes was searched for ({@link _searchedAttrs())}
* This variable gets set from the constructor and returned
* from {@link _searchedAttrs()}
* Cache variable for storing entries fetched internally
* This currently is only used by {@link pop_entry()}
* @param resource &$search Search result identifier
* @param Net_LDAP2|resource&$ldap Net_LDAP2 object or just a LDAP-Link resource
* @param array $attributes (optional) Array with searched attribute names. (see {@link $_searchedAttrs})
public function __construct(&$search, &$ldap, $attributes = array ())
$this->PEAR ('Net_LDAP2_Error');
if (is_array($attributes) && !empty ($attributes)) {
* Returns an array of entry objects
* @return array Array of entry objects.
* Get the next entry in the searchresult.
* This will return a valid Net_LDAP2_Entry object or false, so
* you can use this method to easily iterate over the entries inside
* @return Net_LDAP2_Entry|false Reference to Net_LDAP2_Entry object or false
if ($this->count() == 0 ) {
* Alias function of shiftEntry() for perl-ldap interface
* Retrieve the next entry in the searchresult, but starting from last entry
* This is the opposite to {@link shiftEntry()} and is also very useful
* to be used inside a while loop.
* @return Net_LDAP2_Entry|false
// fetch entries into cache if not done so far
return (null === $return)? false : $return;
* Alias function of popEntry() for perl-ldap interface
* Return entries sorted as array
* This returns a array with sorted entries and the values.
* Sorting is done with PHPs {@link array_multisort()}.
* This method relies on {@link as_struct()} to fetch the raw data of the entries.
* Please note that attribute names are case sensitive!
* // to sort entries first by location, then by surename, but descending:
* $entries = $search->sorted_as_struct(array('locality','sn'), SORT_DESC);
* @param array $attrs Array of attribute names to sort; order from left to right.
* @param int $order Ordering direction, either constant SORT_ASC or SORT_DESC
* @return array|Net_LDAP2_Error Array with sorted entries or error
* Old Code, suitable and fast for single valued sorting
* This code should be used if we know that single valued sorting is desired,
* but we need some method to get that knowledge...
$attrs = array_reverse($attrs);
foreach ($attrs as $attribute) {
if (!ldap_sort($this->_link, $this->_search, $attribute)){
$this->raiseError("Sorting failed for Attribute " . $attribute);
$results = ldap_get_entries($this->_link, $this->_search);
unset($results['count']); //for tidier output
return array_reverse($results);
* New code: complete "client side" sorting
// first some parameterchecks
return PEAR ::raiseError ("Sorting failed: Parameterlist must be an array!");
if ($order != SORT_ASC && $order != SORT_DESC ) {
return PEAR ::raiseError ("Sorting failed: sorting direction not understood! (neither constant SORT_ASC nor SORT_DESC)");
// fetch the entries data
// now sort each entries attribute values
// this is neccessary because later we can only sort by one value,
// so we need the highest or lowest attribute now, depending on the
// selected ordering for that specific attribute
foreach ($entries as $dn => $entry) {
foreach ($entry as $attr_name => $attr_values) {
sort($entries[$dn][$attr_name]);
if ($order == SORT_DESC ) {
// reformat entrys array for later use with array_multisort()
$to_sort = array (); // <- will be a numeric array similar to ldap_get_entries
foreach ($entries as $dn => $entry_attr) {
foreach ($entry_attr as $attr_name => $attr_values) {
$row[$attr_name] = $attr_values;
// Build columns for array_multisort()
// each requested attribute is one row
foreach ($attrs as $attr_name) {
foreach ($to_sort as $key => $row) {
$columns[$attr_name][$key] = & $to_sort[$key][$attr_name][0 ];
// sort the colums with array_multisort, if there is something
// to sort and if we have requested sort columns
if (!empty ($to_sort) && !empty ($columns)) {
foreach ($attrs as $attr_name) {
$sort_params .= '$columns[\''. $attr_name. '\'], '. $order. ', ';
eval (" array_multisort($sort_params \$to_sort);" ); // perform sorting
* Return entries sorted as objects
* This returns a array with sorted Net_LDAP2_Entry objects.
* The sorting is actually done with {@link sorted_as_struct()}.
* Please note that attribute names are case sensitive!
* // to sort entries first by location, then by surename, but descending:
* $entries = $search->sorted(array('locality','sn'), SORT_DESC);
* @param array $attrs Array of sort attributes to sort; order from left to right.
* @param int $order Ordering direction, either constant SORT_ASC or SORT_DESC
* @return array|Net_LDAP2_Error Array with sorted Net_LDAP2_Entries or error
public function sorted($attrs = array ('cn'), $order = SORT_ASC )
if (PEAR ::isError ($sorted)) {
foreach ($sorted as $key => $row) {
if (!PEAR ::isError ($entry)) {
* Return entries as array
* This method returns the entries and the selected attributes values as
* The first array level contains all found entries where the keys are the
* DNs of the entries. The second level arrays contian the entries attributes
* such that the keys is the lowercased name of the attribute and the values
* are stored in another indexed array. Note that the attribute values are stored
* in an array even if there is no or just one value.
* The array has the following structure:
* 'cn=foo,dc=example,dc=com' => array(
* 'multival' => array('val1', 'val2', 'valN')
* 'cn=bar,dc=example,dc=com' => array(
* 'multival' => array('val1', 'valN')
* @return array associative result array as described above
foreach ($entries as $entry) {
$entry_attributes = $entry->attributes ();
foreach ($entry_attributes as $attr_name) {
$attr_values = $entry->getValue ($attr_name, 'all');
$attr_values = array ($attr_values);
$attrs[$attr_name] = $attr_values;
$return[$entry->dn ()] = $attrs;
* Set the search objects resource link
* @param resource &$search Search result identifier
* Set the ldap ressource link
* @param resource &$link Link identifier
* Returns the number of entries in the searchresult
* @return int Number of entries in search.
// this catches the situation where OL returned errno 32 = no such object!
* Get the errorcode the object got in its search.
* @return int The ldap error number.
* Return the attribute names this search selected
* Tells if this search exceeds a sizelimit
* SPL Iterator interface methods.
* This interface allows to use Net_LDAP2_Search
* objects directly inside a foreach loop!
* SPL Iterator interface: Return the current element.
* The SPL Iterator interface allows you to fetch entries inside
* a foreach() loop: <code>foreach ($search as $dn => $entry) { ...</code>
* Of course, you may call {@link current()}, {@link key()}, {@link next()},
* {@link rewind()} and {@link valid()} yourself.
* If the search throwed an error, it returns false.
* False is also returned, if the end is reached
* In case no call to next() was made, we will issue one,
* thus returning the first entry.
* @return Net_LDAP2_Entry|false
* SPL Iterator interface: Return the identifying key (DN) of the current entry.
* @return string|falseDN of the current entry; false in case no entry is returned by current()
* SPL Iterator interface: Move forward to next entry.
* After a call to {@link next()}, {@link current()} will return
* the next entry in the result set.
// if we have no entrys anymore, we add false (which is
// returned by shiftEntry()) so current() will complain.
// move on array pointer to current element.
// even if we have added all entries, this will
// ensure proper operation in case we rewind()
* SPL Iterator interface: Check if there is a current element after calls to {@link rewind()} or {@link next()}.
* Used to check if we've iterated to the end of the collection.
* @return boolean FALSE if there's nothing more to iterate over
* SPL Iterator interface: Rewind the Iterator to the first element.
* After rewinding, {@link current()} will return the first entry in the result set.
Documentation generated on Mon, 11 Mar 2019 15:17:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|