Net_LDAP
[ class tree: Net_LDAP ] [ index: Net_LDAP ] [ all elements ]

Source for file Search.php

Documentation is available at Search.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +--------------------------------------------------------------------------+
  4. // | Net_LDAP                                                                 |
  5. // +--------------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                    |
  7. // +--------------------------------------------------------------------------+
  8. // | This library is free software; you can redistribute it and/or            |
  9. // | modify it under the terms of the GNU Lesser General Public               |
  10. // | License as published by the Free Software Foundation; either             |
  11. // | version 2.1 of the License, or (at your option) any later version.       |
  12. // |                                                                          |
  13. // | This library is distributed in the hope that it will be useful,          |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        |
  16. // | Lesser General Public License for more details.                          |
  17. // |                                                                          |
  18. // | You should have received a copy of the GNU Lesser General Public         |
  19. // | License along with this library; if not, write to the Free Software      |
  20. // | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA |
  21. // +--------------------------------------------------------------------------+
  22. // | Authors: Tarjej Huse                                                     |
  23. // +--------------------------------------------------------------------------+
  24. //
  25. // $Id: Search.php,v 1.17 2007/02/22 08:06:10 beni Exp $
  26.  
  27. /**
  28.  * Result set of an LDAP search
  29.  *
  30.  * @author  Tarjei Huse
  31.  * @author  Benedikt Hallinger <beni@php.net>
  32.  * @version $Revision: 1.17 $
  33.  * @package Net_LDAP
  34.  */
  35. class Net_LDAP_Search extends PEAR
  36. {
  37.     /**
  38.      * Search result identifier
  39.      *
  40.      * @access private
  41.      * @var resource 
  42.      */
  43.     var $_search;
  44.  
  45.     /**
  46.      * LDAP resource link
  47.      *
  48.      * @access private
  49.      * @var resource 
  50.      */
  51.     var $_link;
  52.  
  53.     /**
  54.      * Net_LDAP object
  55.      *
  56.      * A reference of the Net_LDAP object for passing to Net_LDAP_Entry
  57.      *
  58.      * @access private
  59.      * @var object Net_LDAP 
  60.      */
  61.     var $_ldap;
  62.  
  63.     /**
  64.      * Result entry identifier
  65.      *
  66.      * @access private
  67.      * @var resource 
  68.      */
  69.     var $_entry = null;
  70.  
  71.     /**
  72.      * The errorcode the search got
  73.      *
  74.      * Some errorcodes might be of interest, but might not be best handled as errors.
  75.      * examples: 4 - LDAP_SIZELIMIT_EXCEEDED - indicates a huge search.
  76.      *               Incomplete results are returned. If you just want to check if there's anything in the search.
  77.      *               than this is a point to handle.
  78.      *           32 - no such object - search here returns a count of 0.
  79.      *
  80.      * @access private
  81.      * @var int 
  82.      */
  83.     var $_errorCode = 0; // if not set - sucess!
  84.     
  85.     /**
  86.     * What attributes we searched for
  87.     *
  88.     * The $attributes array contains the names of the searched attributes and gets
  89.     * passed from $Net_LDAP->search() so the Net_LDAP_Search object can tell
  90.     * what attributes was searched for ({@link _searchedAttrs())}
  91.     *
  92.     * This variable gets set from the constructor and returned
  93.     * from {@link _searchedAttrs()}
  94.     *
  95.     * @access private
  96.     * @var array 
  97.     */
  98.     var $_searchedAttrs = array();
  99.  
  100.    /**
  101.     * Constructor
  102.     *
  103.     * @access protected
  104.     * @param resource $search         Search result identifier
  105.     * @param Net_LDAP|resource$ldap  Net_LDAP object or just a LDAP-Link resource
  106.     * @param array $attributes        (optional) Array with searched attribute names. (see {@link $_searchedAttrs})
  107.     */
  108.     function Net_LDAP_Search(&$search&$ldap$attributes = array())
  109.     {
  110.         $this->PEAR('Net_LDAP_Error');
  111.  
  112.         $this->setSearch($search);
  113.  
  114.         if (is_a($ldap'Net_LDAP')) {
  115.             $this->_ldap =$ldap;
  116.             $this->setLink($this->_ldap->getLink());
  117.         else {
  118.             $this->setLink($ldap);
  119.         }
  120.  
  121.         $this->_errorCode @ldap_errno($this->_link);
  122.  
  123.         if (is_array($attributes&& !empty($attributes)) {
  124.             $this->_searchedAttrs $attributes;
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * Returns an assosiative array of entry objects
  130.      *
  131.      * @return array Array of entry objects.
  132.      */
  133.     function entries()
  134.     {
  135.         $entries = array();
  136.  
  137.         while ($entry $this->shiftEntry()) {
  138.             $entries[$entry;
  139.         }
  140.  
  141.         return $entries;
  142.     }
  143.  
  144.     /**
  145.      * Get the next entry in the searchresult.
  146.      *
  147.      * @return Net_LDAP_Entry|false Reference to Net_LDAP_Entry object or false
  148.      */
  149.     function &shiftEntry()
  150.     {
  151.         if ($this->count(== 0 {
  152.             return false;
  153.         }
  154.  
  155.         if (is_null($this->_entry)) {
  156.             $this->_entry @ldap_first_entry($this->_link$this->_search);
  157.             $entry = new Net_LDAP_Entry($this->_ldap$this->_entry);
  158.         else {
  159.             if (!$this->_entry @ldap_next_entry($this->_link$this->_entry)) {
  160.                 return false;
  161.             }
  162.             $entry = new Net_LDAP_Entry($this->_ldap$this->_entry);
  163.         }
  164.         return $entry;
  165.     }
  166.  
  167.     /**
  168.      * alias function of shiftEntry() for perl-ldap interface
  169.      *
  170.      * @see shiftEntry()
  171.      */
  172.     function shift_entry()
  173.     {
  174.         $args func_get_args();
  175.         return call_user_func_array(array&$this'shiftEntry' )$args);
  176.     }
  177.  
  178.     /**
  179.      * Retrieve the last entry of the searchset. NOT IMPLEMENTED
  180.      *
  181.      * @return Net_LDAP_Error 
  182.      * @todo implement me!
  183.      */
  184.     function pop_entry()
  185.     {
  186.         PEAR::raiseError("Not implemented");
  187.     }
  188.  
  189.     /**
  190.     * Return entries sorted as array
  191.     *
  192.     * This returns a array with sorted entries and the values.
  193.     * Sorting is done with PHPs {@link array_multisort()}.
  194.     * This method relies on {@link as_struct()} to fetch the raw data of the entries.
  195.     *
  196.     * Please note that attribute names are case sensitive!
  197.     *
  198.     * Usage example:
  199.     * <code>
  200.     *   // to sort entries first by location, then by surename, but descending:
  201.     *   $entries = $search->sorted_as_struct(array('locality','sn'), SORT_DESC);
  202.     * </code>
  203.     *
  204.     * @param array      $attrs Array of attribute names to sort; order from left to right.
  205.     * @param int        $order Ordering direction, either constant SORT_ASC or SORT_DESC
  206.     * @return array|Net_LDAP_Error  Array with sorted entries or error
  207.     */
  208.     function sorted_as_struct($attrs = array('cn')$order = SORT_ASC)
  209.     {
  210.         /*
  211.         * Old Code, suitable and fast for single valued sorting
  212.         * This code should be used if we know that single valued sorting is desired,
  213.         * but we need some method to get that knowledge...
  214.         */
  215.         /*
  216.         $attrs = array_reverse($attrs);
  217.         foreach ($attrs as $attribute) {
  218.             if (!ldap_sort($this->_link, $this->_search, $attribute)){
  219.                 $this->raiseError("Sorting failed for Attribute " . $attribute);
  220.             }
  221.         }
  222.  
  223.         $results = ldap_get_entries($this->_link, $this->_search);
  224.  
  225.         unset($results['count']); //for tidier output
  226.         if ($order) {
  227.             return array_reverse($results);
  228.         } else {
  229.             return $results;
  230.         }*/
  231.  
  232.         /*
  233.         * New code: complete "client side" sorting
  234.         */
  235.         // first some parameterchecks
  236.         if (!is_array($attrs)) {
  237.             return PEAR::raiseError("Sorting failed: Parameterlist must be an array!");
  238.         }
  239.         if ($order != SORT_ASC && $order != SORT_DESC{
  240.             return PEAR::raiseError("Sorting failed: sorting direction not understood! (neither constant SORT_ASC nor SORT_DESC)");
  241.         }
  242.  
  243.         // fetch the entries data
  244.         $entries $this->as_struct();
  245.  
  246.         // now sort each entries attribute values
  247.         // this is neccessary because later we can only sort by one value,
  248.         // so we need the highest or lowest attribute now, depending on the
  249.         // selected ordering for that specific attribute
  250.         foreach ($entries as $dn => $entry{
  251.             foreach ($entry as $attr_name => $attr_values{
  252.                 sort($entries[$dn][$attr_name]);
  253.                 if($order == SORT_DESC{
  254.                     array_reverse($entries[$dn][$attr_name]);
  255.                 }
  256.             }
  257.         }
  258.  
  259.         // reformat entrys array for later use with array_multisort()
  260.         $to_sort = array()// <- will be a numeric array similar to ldap_get_entries
  261.         foreach ($entries as $dn => $entry_attr{
  262.             $row = array();
  263.             $row['dn'$dn;
  264.             foreach ($entry_attr as $attr_name => $attr_values{
  265.                 $row[$attr_name$attr_values;
  266.             }
  267.             $to_sort[$row;
  268.         }
  269.  
  270.         // Build columns for array_multisort()
  271.         // each requested attribute is one row
  272.         $columns = array();
  273.         foreach ($attrs as $attr_name{
  274.             foreach ($to_sort as $key => $row{
  275.                 $columns[$attr_name][$key=$to_sort[$key][$attr_name][0];
  276.             }
  277.         }
  278.  
  279.         // sort the colums with array_multisort
  280.         $sort_params '';
  281.         foreach ($attrs as $attr_name{
  282.             $sort_params .= '$columns[\''.$attr_name.'\'], '.$order.', ';
  283.         }
  284.         eval("array_multisort($sort_params \$to_sort);")// perform sorting
  285.  
  286.         return $to_sort;
  287.     }
  288.  
  289.     /**
  290.     * Return entries sorted as objects
  291.     *
  292.     * This returns a array with sorted Net_LDAP_Entry objects.
  293.     * The sorting is actually done with {@link sorted_as_struct()}.
  294.     *
  295.     * Please note that attribute names are case sensitive!
  296.     *
  297.     * Usage example:
  298.     * <code>
  299.     *   // to sort entries first by location, then by surename, but descending:
  300.     *   $entries = $search->sorted(array('locality','sn'), SORT_DESC);
  301.     * </code>
  302.     *
  303.     * @param array      $attrs Array of sort attributes to sort; order from left to right.
  304.     * @param int        $order Ordering direction, either constant SORT_ASC or SORT_DESC
  305.     * @return array|Net_LDAP_Error  Array with sorted Net_LDAP_Entries or error
  306.     */
  307.     function sorted($attrs = array('cn')$order = SORT_ASC{
  308.         $return = array();
  309.         $sorted $this->sorted_as_struct($attrs$order);
  310.         if (PEAR::isError($sorted)) {
  311.             return $sorted;
  312.         }
  313.         foreach ($sorted as $key => $row{
  314.             $entry $this->_ldap->getEntry($row['dn']$this->_searchedAttrs());
  315.             if (!PEAR::isError($entry)) {
  316.                 array_push($return$entry);
  317.             else {
  318.                 return $entry;
  319.             }
  320.         }
  321.         return $return;
  322.     }
  323.  
  324.    /**
  325.     * Return entries as array
  326.     *
  327.     * This method returns the entries and the selected attributes values as
  328.     * array.
  329.     * The first array level contains all found entries where the keys are the
  330.     * DNs of the entries. The second level arrays contian the entries attributes
  331.     * such that the keys is the lowercased name of the attribute and the values
  332.     * are stored in another indexed array. Note that the attribute values are stored
  333.     * in an array even if there is no or just one value.
  334.     *
  335.     * The array has the following structure:
  336.     * <code>
  337.     * $return = array(
  338.     *           'cn=foo,dc=example,dc=com' => array(
  339.     *                                                'sn'       => array('foo'),
  340.     *                                                'multival' => array('val1', 'val2', 'valN')
  341.     *                                             )
  342.     *           'cn=bar,dc=example,dc=com' => array(
  343.     *                                                'sn'       => array('bar'),
  344.     *                                                'multival' => array('val1', 'valN')
  345.     *                                             )
  346.     *           )
  347.     * </code>
  348.     *
  349.     * @return array      associative result array as described above
  350.     */
  351.     function as_struct()
  352.     {
  353.         $return = array();
  354.         $entries $this->entries();
  355.         foreach ($entries as $entry{
  356.             $attrs = array();
  357.             $entry_attributes $entry->attributes();
  358.             foreach ($entry_attributes as $attr_name{
  359.                 $attr_values $entry->getValue($attr_name'all');
  360.                 if (!is_array($attr_values)) {
  361.                     $attr_values = array($attr_values);
  362.                 }
  363.                 $attrs[$attr_name$attr_values;
  364.             }
  365.             $return[$entry->dn()$attrs;
  366.         }
  367.         return $return;
  368.     }
  369.  
  370.    /**
  371.     * Set the search objects resource link
  372.     *
  373.     * @access public
  374.     * @param resource $search Search result identifier
  375.     * @return void 
  376.     */
  377.     function setSearch(&$search)
  378.     {
  379.         $this->_search $search;
  380.     }
  381.  
  382.    /**
  383.     * Set the ldap ressource link
  384.     *
  385.     * @access public
  386.     * @param resource $link Link identifier
  387.     * @return void 
  388.     */
  389.     function setLink(&$link)
  390.     {
  391.         $this->_link $link;
  392.     }
  393.  
  394.    /**
  395.     * Returns the number of entries in the searchresult
  396.     *
  397.     * @return int Number of entries in search.
  398.     */
  399.     function count()
  400.     {
  401.         /* this catches the situation where OL returned errno 32 = no such object! */
  402.         if (!$this->_search{
  403.             return 0;
  404.         }
  405.         return @ldap_count_entries($this->_link$this->_search);
  406.     }
  407.  
  408.     /**
  409.      * Get the errorcode the object got in its search.
  410.      *
  411.      * @return int The ldap error number.
  412.      */
  413.     function getErrorCode()
  414.     {
  415.         return $this->_errorCode;
  416.     }
  417.  
  418.    /**
  419.     * Destructor
  420.     *
  421.     * @access protected
  422.     */
  423.     function _Net_LDAP_Search()
  424.     {
  425.         @ldap_free_result($this->_search);
  426.     }
  427.  
  428.    /**
  429.     * Closes search result
  430.     */
  431.     function done()
  432.     {
  433.         $this->_Net_LDAP_Search();
  434.     }
  435.  
  436.     /**
  437.     * Return the attribute names this search selected
  438.     *
  439.     * @return array 
  440.     * @see $_searchedAttrs
  441.     * @access private
  442.     */
  443.     function _searchedAttrs()
  444.     {
  445.         return $this->_searchedAttrs;
  446.     }
  447. }
  448.  
  449. ?>

Documentation generated on Mon, 11 Mar 2019 14:56:52 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.