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, Benedikt Hallinger                                 |
  23. // +--------------------------------------------------------------------------+
  24. //
  25. // $Id: Search.php,v 1.19 2007/04/23 08:11:09 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.19 $
  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.             $false = false;
  153.             return $false;
  154.         }
  155.  
  156.         if (is_null($this->_entry)) {
  157.             $this->_entry @ldap_first_entry($this->_link$this->_search);
  158.             $entry = new Net_LDAP_Entry($this->_ldap$this->_entry);
  159.         else {
  160.             if (!$this->_entry @ldap_next_entry($this->_link$this->_entry)) {
  161.                 $false = false;
  162.                 return $false;
  163.             }
  164.             $entry = new Net_LDAP_Entry($this->_ldap$this->_entry);
  165.         }
  166.         return $entry;
  167.     }
  168.  
  169.     /**
  170.      * alias function of shiftEntry() for perl-ldap interface
  171.      *
  172.      * @see shiftEntry()
  173.      */
  174.     function shift_entry()
  175.     {
  176.         $args func_get_args();
  177.         return call_user_func_array(array&$this'shiftEntry' )$args);
  178.     }
  179.  
  180.     /**
  181.      * Retrieve the last entry of the searchset. NOT IMPLEMENTED
  182.      *
  183.      * @return Net_LDAP_Error 
  184.      * @todo implement me!
  185.      */
  186.     function pop_entry()
  187.     {
  188.         PEAR::raiseError("Not implemented");
  189.     }
  190.  
  191.     /**
  192.     * Return entries sorted as array
  193.     *
  194.     * This returns a array with sorted entries and the values.
  195.     * Sorting is done with PHPs {@link array_multisort()}.
  196.     * This method relies on {@link as_struct()} to fetch the raw data of the entries.
  197.     *
  198.     * Please note that attribute names are case sensitive!
  199.     *
  200.     * Usage example:
  201.     * <code>
  202.     *   // to sort entries first by location, then by surename, but descending:
  203.     *   $entries = $search->sorted_as_struct(array('locality','sn'), SORT_DESC);
  204.     * </code>
  205.     *
  206.     * @param array      $attrs Array of attribute names to sort; order from left to right.
  207.     * @param int        $order Ordering direction, either constant SORT_ASC or SORT_DESC
  208.     * @return array|Net_LDAP_Error  Array with sorted entries or error
  209.     */
  210.     function sorted_as_struct($attrs = array('cn')$order = SORT_ASC)
  211.     {
  212.         /*
  213.         * Old Code, suitable and fast for single valued sorting
  214.         * This code should be used if we know that single valued sorting is desired,
  215.         * but we need some method to get that knowledge...
  216.         */
  217.         /*
  218.         $attrs = array_reverse($attrs);
  219.         foreach ($attrs as $attribute) {
  220.             if (!ldap_sort($this->_link, $this->_search, $attribute)){
  221.                 $this->raiseError("Sorting failed for Attribute " . $attribute);
  222.             }
  223.         }
  224.  
  225.         $results = ldap_get_entries($this->_link, $this->_search);
  226.  
  227.         unset($results['count']); //for tidier output
  228.         if ($order) {
  229.             return array_reverse($results);
  230.         } else {
  231.             return $results;
  232.         }*/
  233.  
  234.         /*
  235.         * New code: complete "client side" sorting
  236.         */
  237.         // first some parameterchecks
  238.         if (!is_array($attrs)) {
  239.             return PEAR::raiseError("Sorting failed: Parameterlist must be an array!");
  240.         }
  241.         if ($order != SORT_ASC && $order != SORT_DESC{
  242.             return PEAR::raiseError("Sorting failed: sorting direction not understood! (neither constant SORT_ASC nor SORT_DESC)");
  243.         }
  244.  
  245.         // fetch the entries data
  246.         $entries $this->as_struct();
  247.  
  248.         // now sort each entries attribute values
  249.         // this is neccessary because later we can only sort by one value,
  250.         // so we need the highest or lowest attribute now, depending on the
  251.         // selected ordering for that specific attribute
  252.         foreach ($entries as $dn => $entry{
  253.             foreach ($entry as $attr_name => $attr_values{
  254.                 sort($entries[$dn][$attr_name]);
  255.                 if($order == SORT_DESC{
  256.                     array_reverse($entries[$dn][$attr_name]);
  257.                 }
  258.             }
  259.         }
  260.  
  261.         // reformat entrys array for later use with array_multisort()
  262.         $to_sort = array()// <- will be a numeric array similar to ldap_get_entries
  263.         foreach ($entries as $dn => $entry_attr{
  264.             $row = array();
  265.             $row['dn'$dn;
  266.             foreach ($entry_attr as $attr_name => $attr_values{
  267.                 $row[$attr_name$attr_values;
  268.             }
  269.             $to_sort[$row;
  270.         }
  271.  
  272.         // Build columns for array_multisort()
  273.         // each requested attribute is one row
  274.         $columns = array();
  275.         foreach ($attrs as $attr_name{
  276.             foreach ($to_sort as $key => $row{
  277.                 $columns[$attr_name][$key=$to_sort[$key][$attr_name][0];
  278.             }
  279.         }
  280.  
  281.         // sort the colums with array_multisort
  282.         $sort_params '';
  283.         foreach ($attrs as $attr_name{
  284.             $sort_params .= '$columns[\''.$attr_name.'\'], '.$order.', ';
  285.         }
  286.         eval("array_multisort($sort_params \$to_sort);")// perform sorting
  287.  
  288.         return $to_sort;
  289.     }
  290.  
  291.     /**
  292.     * Return entries sorted as objects
  293.     *
  294.     * This returns a array with sorted Net_LDAP_Entry objects.
  295.     * The sorting is actually done with {@link sorted_as_struct()}.
  296.     *
  297.     * Please note that attribute names are case sensitive!
  298.     *
  299.     * Usage example:
  300.     * <code>
  301.     *   // to sort entries first by location, then by surename, but descending:
  302.     *   $entries = $search->sorted(array('locality','sn'), SORT_DESC);
  303.     * </code>
  304.     *
  305.     * @param array      $attrs Array of sort attributes to sort; order from left to right.
  306.     * @param int        $order Ordering direction, either constant SORT_ASC or SORT_DESC
  307.     * @return array|Net_LDAP_Error  Array with sorted Net_LDAP_Entries or error
  308.     */
  309.     function sorted($attrs = array('cn')$order = SORT_ASC{
  310.         $return = array();
  311.         $sorted $this->sorted_as_struct($attrs$order);
  312.         if (PEAR::isError($sorted)) {
  313.             return $sorted;
  314.         }
  315.         foreach ($sorted as $key => $row{
  316.             $entry $this->_ldap->getEntry($row['dn']$this->_searchedAttrs());
  317.             if (!PEAR::isError($entry)) {
  318.                 array_push($return$entry);
  319.             else {
  320.                 return $entry;
  321.             }
  322.         }
  323.         return $return;
  324.     }
  325.  
  326.    /**
  327.     * Return entries as array
  328.     *
  329.     * This method returns the entries and the selected attributes values as
  330.     * array.
  331.     * The first array level contains all found entries where the keys are the
  332.     * DNs of the entries. The second level arrays contian the entries attributes
  333.     * such that the keys is the lowercased name of the attribute and the values
  334.     * are stored in another indexed array. Note that the attribute values are stored
  335.     * in an array even if there is no or just one value.
  336.     *
  337.     * The array has the following structure:
  338.     * <code>
  339.     * $return = array(
  340.     *           'cn=foo,dc=example,dc=com' => array(
  341.     *                                                'sn'       => array('foo'),
  342.     *                                                'multival' => array('val1', 'val2', 'valN')
  343.     *                                             )
  344.     *           'cn=bar,dc=example,dc=com' => array(
  345.     *                                                'sn'       => array('bar'),
  346.     *                                                'multival' => array('val1', 'valN')
  347.     *                                             )
  348.     *           )
  349.     * </code>
  350.     *
  351.     * @return array      associative result array as described above
  352.     */
  353.     function as_struct()
  354.     {
  355.         $return = array();
  356.         $entries $this->entries();
  357.         foreach ($entries as $entry{
  358.             $attrs = array();
  359.             $entry_attributes $entry->attributes();
  360.             foreach ($entry_attributes as $attr_name{
  361.                 $attr_values $entry->getValue($attr_name'all');
  362.                 if (!is_array($attr_values)) {
  363.                     $attr_values = array($attr_values);
  364.                 }
  365.                 $attrs[$attr_name$attr_values;
  366.             }
  367.             $return[$entry->dn()$attrs;
  368.         }
  369.         return $return;
  370.     }
  371.  
  372.    /**
  373.     * Set the search objects resource link
  374.     *
  375.     * @access public
  376.     * @param resource $search Search result identifier
  377.     * @return void 
  378.     */
  379.     function setSearch(&$search)
  380.     {
  381.         $this->_search $search;
  382.     }
  383.  
  384.    /**
  385.     * Set the ldap ressource link
  386.     *
  387.     * @access public
  388.     * @param resource $link Link identifier
  389.     * @return void 
  390.     */
  391.     function setLink(&$link)
  392.     {
  393.         $this->_link $link;
  394.     }
  395.  
  396.    /**
  397.     * Returns the number of entries in the searchresult
  398.     *
  399.     * @return int Number of entries in search.
  400.     */
  401.     function count()
  402.     {
  403.         /* this catches the situation where OL returned errno 32 = no such object! */
  404.         if (!$this->_search{
  405.             return 0;
  406.         }
  407.         return @ldap_count_entries($this->_link$this->_search);
  408.     }
  409.  
  410.     /**
  411.      * Get the errorcode the object got in its search.
  412.      *
  413.      * @return int The ldap error number.
  414.      */
  415.     function getErrorCode()
  416.     {
  417.         return $this->_errorCode;
  418.     }
  419.  
  420.    /**
  421.     * Destructor
  422.     *
  423.     * @access protected
  424.     */
  425.     function _Net_LDAP_Search()
  426.     {
  427.         @ldap_free_result($this->_search);
  428.     }
  429.  
  430.    /**
  431.     * Closes search result
  432.     */
  433.     function done()
  434.     {
  435.         $this->_Net_LDAP_Search();
  436.     }
  437.  
  438.     /**
  439.     * Return the attribute names this search selected
  440.     *
  441.     * @return array 
  442.     * @see $_searchedAttrs
  443.     * @access private
  444.     */
  445.     function _searchedAttrs()
  446.     {
  447.         return $this->_searchedAttrs;
  448.     }
  449. }
  450.  
  451. ?>

Documentation generated on Mon, 11 Mar 2019 15:01:40 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.