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

Source for file Entry.php

Documentation is available at Entry.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3.  
  4. require_once 'PEAR.php';
  5. require_once 'Util.php';
  6.  
  7. /**
  8. * Object representation of a directory entry
  9. *
  10. * This class represents a directory entry. You can add, delete, replace
  11. * attributes and their values, rename the entry, delete the entry.
  12. *
  13. @category Net
  14. @package  Net_LDAP
  15. @author   Jan Wagner <wagner@netsols.de>
  16. @author   Tarjej Huse <tarjei@bergfald.no>
  17. @license  http://www.gnu.org/copyleft/lesser.html LGPL
  18. @version  CVS: $Id: Entry.php,v 1.47 2007/10/24 06:29:32 beni Exp $
  19. @link     http://pear.php.net/package/Net_LDAP/
  20.  
  21. */
  22. class Net_LDAP_Entry extends PEAR
  23. {
  24.     /**
  25.     * Entry ressource identifier
  26.     *
  27.     * @access private
  28.     * @var ressourcee 
  29.     */
  30.     var $_entry = null;
  31.  
  32.     /**
  33.     * LDAP ressource identifier
  34.     *
  35.     * @access private
  36.     * @var ressource 
  37.     */
  38.     var $_link = null;
  39.  
  40.     /**
  41.     * Net_LDAP object
  42.     *
  43.     * This object will be used for updating and schema checking
  44.     *
  45.     * @access private
  46.     * @var object Net_LDAP 
  47.     */
  48.     var $_ldap = null;
  49.  
  50.     /**
  51.     * Distinguished name of the entry
  52.     *
  53.     * @access private
  54.     * @var string 
  55.     */
  56.     var $_dn = null;
  57.  
  58.     /**
  59.     * Attributes
  60.     *
  61.     * @access private
  62.     * @var array 
  63.     */
  64.     var $_attributes = array();
  65.  
  66.     /**
  67.     * Original attributes before any modification
  68.     *
  69.     * @access private
  70.     * @var array 
  71.     */
  72.     var $_original = array();
  73.  
  74.  
  75.     /**
  76.     * Map of attribute names
  77.     *
  78.     * @access private
  79.     * @var array 
  80.     */
  81.     var $_map = array();
  82.  
  83.  
  84.     /**
  85.     * Is this a new entry?
  86.     *
  87.     * @access private
  88.     * @var boolean 
  89.     */
  90.     var $_new = true;
  91.  
  92.     /**
  93.     * New distinguished name
  94.     *
  95.     * @access private
  96.     * @var string 
  97.     */
  98.     var $_newdn = null;
  99.  
  100.     /**
  101.     * Shall the entry be deleted?
  102.     *
  103.     * @access private
  104.     * @var boolean 
  105.     */
  106.     var $_delete = false;
  107.  
  108.     /**
  109.     * Map with changes to the entry
  110.     *
  111.     * @access private
  112.     * @var array 
  113.     */
  114.     var $_changes = array("add"     => array(),
  115.                           "delete"  => array(),
  116.                           "replace" => array()
  117.                           );
  118.     /**
  119.     * Internal Constructor
  120.     *
  121.     * Constructor of the entry. Sets up the distinguished name and the entries
  122.     * attributes.
  123.     * You should not call this method manually! Use {@link Net_LDAP_Entry::createFresh()} instead!
  124.     *
  125.     * @param Net_LDAP|ressource|array&$ldap Net_LDAP object, ldap-link ressource or array of attributes
  126.     * @param string|ressource        $entry Either a DN or a LDAP-Entry ressource
  127.     *
  128.     * @access protected
  129.     * @return none 
  130.     */
  131.     function Net_LDAP_Entry(&$ldap$entry = null)
  132.     {
  133.         $this->PEAR('Net_LDAP_Error');
  134.  
  135.         if (is_resource($entry)) {
  136.             $this->_entry &$entry;
  137.         else {
  138.             $this->_dn $entry;
  139.         }
  140.  
  141.         if (is_a($ldap'Net_LDAP')) {
  142.             $this->_ldap &$ldap;
  143.             $this->_link $ldap->getLink();
  144.         elseif (is_resource($ldap)) {
  145.             $this->_link $ldap;
  146.         elseif (is_array($ldap)) {
  147.             $this->_setAttributes($ldap);  // setup attrs manually
  148.         }
  149.  
  150.         if (is_resource($this->_entry&& is_resource($this->_link)) {
  151.             $this->_new = false;
  152.             $this->_dn  @ldap_get_dn($this->_link$this->_entry);
  153.             $this->_setAttributes();  // fetch attributes from server
  154.         }
  155.     }
  156.  
  157.     /**
  158.     * Creates a fresh entry that may be added to the directory later on
  159.     *
  160.     * The method should be called statically: $entry = Net_LDAP_Entry::createFresh();
  161.     *
  162.     * Use this method, if you want to initialize a fresh entry.
  163.     * The attributes parameter is as following:
  164.     * <code>
  165.     * $attrs = array( 'attribute1' => array('value1', 'value2'),
  166.     *                 'attribute2' => 'single value'
  167.     *          );
  168.     * </code>
  169.     *
  170.     * @param string $dn    DN of the Entry
  171.     * @param array  $attrs Attributes of the entry
  172.     *
  173.     * @static
  174.     * @return Net_LDAP_Entry 
  175.     */
  176.     function createFresh($dn$attrs = array())
  177.     {
  178.         if (!is_array($attrs)) {
  179.             return PEAR::raiseError("Unable to create fresh entry: Parameter \$attrs needs to be an array!");
  180.         }
  181.  
  182.         $entry = new Net_LDAP_Entry($attrs$dn);
  183.         return $entry;
  184.     }
  185.  
  186.     /**
  187.     * Get or set the distinguished name of the entry
  188.     *
  189.     * If called without an argument the current (or the new DN if set) DN gets returned.
  190.     * If you provide an DN, this entry is moved to the new location specified if a DN existed.
  191.     * If the DN was not set, the DN gets initialized. Call {@link update()} to actually create
  192.     * the new Entry in the directory.
  193.     *
  194.     * Please note that special characters (eg german umlauts) should be encoded using utf8_encode().
  195.     *
  196.     * @param string $dn New distinguished name
  197.     *
  198.     * @access public
  199.     * @return string|trueDistinguished name (or true if a new DN was provided)
  200.     */
  201.     function dn($dn = null)
  202.     {
  203.         if (false == is_null($dn)) {
  204.             if (is_null($this->_dn)) {
  205.                 $this->_dn $dn;
  206.             else {
  207.                 $this->_newdn $dn;
  208.             }
  209.             $return = true;
  210.             return $return;
  211.         }
  212.         return (isset($this->_newdn$this->_newdn $this->_dn);
  213.     }
  214.  
  215.     /**
  216.     * Sets the internal attributes array
  217.     *
  218.     * This fetches the values for the attributes from the server.
  219.     * The attribute Syntax will be checked so binary attributes will be returned
  220.     * as binary values.
  221.     *
  222.     * Attributes may be passed directly via the $attributes parameter to setup this
  223.     * entry manually. This overrides attribute fetching from the server.
  224.     *
  225.     * @param array $attributes Attributes to set for this entry
  226.     *
  227.     * @access private
  228.     * @return void 
  229.     */
  230.     function _setAttributes($attributes = null)
  231.     {
  232.         /*
  233.         * fetch attributes from the server
  234.         */
  235.         if (is_null($attributes&& is_resource($this->_entry&& is_resource($this->_link)) {
  236.             // fetch schema
  237.             if (is_a($this->_ldap'Net_LDAP')) {
  238.                 $schema =$this->_ldap->schema();
  239.             }
  240.             // fetch attributes
  241.             $attributes = array();
  242.             do {
  243.                 if (empty($attr)) {
  244.                     $ber  = null;
  245.                     $attr @ldap_first_attribute($this->_link$this->_entry$ber);
  246.                 else {
  247.                     $attr @ldap_next_attribute($this->_link$this->_entry$ber);
  248.                 }
  249.                 if ($attr{
  250.                     $func 'ldap_get_values'// standard function to fetch value
  251.  
  252.                     // Try to get binary values as binary data
  253.                     if (is_a($schema'Net_LDAP_Schema')) {
  254.                         if ($schema->isBinary($attr)) {
  255.                              $func 'ldap_get_values_len';
  256.                         }
  257.                     }
  258.                     // fetch attribute value (needs error checking?)
  259.                     $attributes[$attr$func($this->_link$this->_entry$attr);
  260.                 }
  261.             while ($attr);
  262.         }
  263.  
  264.         /*
  265.         * set attribute data directly, if passed
  266.         */
  267.         if (is_array($attributes&& count($attributes> 0{
  268.             if (isset($attributes["count"]&& is_numeric($attributes["count"])) {
  269.                 unset($attributes["count"]);
  270.             }
  271.             foreach ($attributes as $k => $v{
  272.                 // attribute names should not be numeric
  273.                 if (is_numeric($k)) {
  274.                     continue;
  275.                 }
  276.                 // map generic attribute name to real one
  277.                 $this->_map[strtolower($k)$k;
  278.                 // attribute values should be in an array
  279.                 if (false == is_array($v)) {
  280.                     $v = array($v);
  281.                 }
  282.                 // remove the value count (comes from ldap server)
  283.                 if (isset($v["count"])) {
  284.                     unset($v["count"]);
  285.                 }
  286.                 $this->_attributes[$k$v;
  287.             }
  288.         }
  289.  
  290.         // save a copy for later use
  291.         $this->_original $this->_attributes;
  292.     }
  293.  
  294.     /**
  295.     * Get the values of all attributes in a hash
  296.     *
  297.     * The returned hash has the form
  298.     * <code>array('attributename' => 'single value',
  299.     *       'attributename' => array('value1', value2', value3'))</code>
  300.     *
  301.     * @access public
  302.     * @return array Hash of all attributes with their values
  303.     */
  304.     function getValues()
  305.     {
  306.         $attrs = array();
  307.         foreach ($this->_attributes as $attr => $value{
  308.             $attrs[$attr$this->getValue($attr);
  309.         }
  310.         return $attrs;
  311.     }
  312.  
  313.     /**
  314.     * Get the value of a specific attribute
  315.     *
  316.     * The first parameter is the name of the attribute
  317.     * The second parameter influences the way the value is returned:
  318.     * 'single': only the first value is returned as string
  319.     * 'all': all values including the value count are returned in an
  320.     *               array
  321.     * 'default': in all other cases an attribute value with a single value is
  322.     *            returned as string, if it has multiple values it is returned
  323.     *            as an array (without value count)
  324.     *
  325.     * @param string $attr   Attribute name
  326.     * @param string $option Option
  327.     *
  328.     * @access public
  329.     * @return string|array|PEAR_Errorstring, array or PEAR_Error
  330.     */
  331.     function getValue($attr$option = null)
  332.     {
  333.         $attr $this->_getAttrName($attr);
  334.  
  335.         if (false == array_key_exists($attr$this->_attributes)) {
  336.             return PEAR::raiseError("Unknown attribute ($attr) requested");
  337.         }
  338.  
  339.         $value $this->_attributes[$attr];
  340.  
  341.         if ($option == "single" || (count($value== 1 && $option != 'all')) {
  342.             $value array_shift($value);
  343.         }
  344.  
  345.         return $value;
  346.     }
  347.  
  348.     /**
  349.     * Alias function of getValue for perl-ldap interface
  350.     *
  351.     * @see getValue()
  352.     */
  353.     function get_value()
  354.     {
  355.         $args func_get_args();
  356.         return call_user_func_array(array&$this'getValue' )$args);
  357.     }
  358.  
  359.     /**
  360.     * Returns an array of attributes names
  361.     *
  362.     * @access public
  363.     * @return array Array of attribute names
  364.     */
  365.     function attributes()
  366.     {
  367.         return array_keys($this->_attributes);
  368.     }
  369.  
  370.     /**
  371.     * Returns whether an attribute exists or not
  372.     *
  373.     * @param string $attr Attribute name
  374.     *
  375.     * @access public
  376.     * @return boolean 
  377.     */
  378.     function exists($attr)
  379.     {
  380.         $attr $this->_getAttrName($attr);
  381.         return array_key_exists($attr$this->_attributes);
  382.     }
  383.  
  384.     /**
  385.     * Adds a new attribute or a new value to an existing attribute
  386.     *
  387.     * The paramter has to be an array of the form:
  388.     * array('attributename' => 'single value',
  389.     *       'attributename' => array('value1', 'value2))
  390.     * When the attribute already exists the values will be added, else the
  391.     * attribute will be created. These changes are local to the entry and do
  392.     * not affect the entry on the server until update() is called.
  393.     *
  394.     * Note, that you can add values of attributes that you haven't selected, but if
  395.     * you do so, {@link getValue()} and {@link getValues()} will only return the
  396.     * values you added, _NOT_ all values present on the server. To avoid this, just refetch
  397.     * the entry after calling {@link update()} or select the attribute.
  398.     *
  399.     * @param array $attr Attributes to add
  400.     *
  401.     * @access public
  402.     * @return true|Net_LDAP_Error
  403.     */
  404.     function add($attr = array())
  405.     {
  406.         if (false == is_array($attr)) {
  407.             return PEAR::raiseError("Parameter must be an array");
  408.         }
  409.         foreach ($attr as $k => $v{
  410.             $k $this->_getAttrName($k);
  411.             if (false == is_array($v)) {
  412.                 // Do not add empty values
  413.                 if ($v == null{
  414.                     continue;
  415.                 else {
  416.                     $v = array($v);
  417.                 }
  418.             }
  419.             // add new values to existing attribute
  420.             if ($this->exists($k)) {
  421.                 $this->_attributes[$karray_merge($this->_attributes[$k]$v);
  422.             else {
  423.                 $this->_map[strtolower($k)$k;
  424.                 $this->_attributes[$k]      $v;
  425.             }
  426.             // save changes for update()
  427.             if (empty($this->_changes["add"][$k])) {
  428.                 $this->_changes["add"][$k= array();
  429.             }
  430.             $this->_changes["add"][$karray_merge($this->_changes["add"][$k]$v);
  431.         }
  432.         $return = true;
  433.         return $return;
  434.     }
  435.  
  436.     /**
  437.     * Deletes an whole attribute or a value or the whole entry
  438.     *
  439.     * The parameter can be one of the following:
  440.     *
  441.     * "attributename" - The attribute as a whole will be deleted
  442.     * array("attributename1", "attributename2) - All given attributes will be
  443.     *                                            deleted
  444.     * array("attributename" => "value") - The value will be deleted
  445.     * array("attributename" => array("value1", "value2") - The given values
  446.     *                                                      will be deleted
  447.     * If $attr is null or omitted , then the whole Entry will be deleted!
  448.     *
  449.     * These changes are local to the entry and do
  450.     * not affect the entry on the server until {@link update()} is called.
  451.     *
  452.     * Please note that you must select the attribute (at $ldap->search() for example)
  453.     * to be able to delete values of it, Otherwise {@link update()} will silently fail
  454.     * and remove nothing.
  455.     *
  456.     * @param string|array$attr Attributes to delete (NULL or missing to delete whole entry)
  457.     *
  458.     * @access public
  459.     * @return true 
  460.     */
  461.     function delete($attr = null)
  462.     {
  463.         if (is_null($attr)) {
  464.             $this->_delete = true;
  465.             return true;
  466.         }
  467.         if (is_string($attr)) {
  468.             $attr = array($attr);
  469.         }
  470.         // Make the assumption that attribute names cannot be numeric,
  471.         // therefore this has to be a simple list of attribute names to delete
  472.         if (is_numeric(key($attr))) {
  473.             foreach ($attr as $name{
  474.                 $name $this->_getAttrName($name);
  475.                 if ($this->exists($name)) {
  476.                     $this->_changes["delete"][$name= null;
  477.                     unset($this->_attributes[$name]);
  478.                 }
  479.             }
  480.         else {
  481.             // Here we have a hash with "attributename" => "value to delete"
  482.             foreach ($attr as $name => $values{
  483.                 // get the correct attribute name
  484.                 $name $this->_getAttrName($name);
  485.                 if ($this->exists($name)) {
  486.                     if (false == is_array($values)) {
  487.                         $values = array($values);
  488.                     }
  489.                     // save values to be deleted
  490.                     if (empty($this->_changes["delete"][$name])) {
  491.                         $this->_changes["delete"][$name= array();
  492.                     }
  493.                     $this->_changes["delete"][$name=
  494.                         array_merge($this->_changes["delete"][$name]$values);
  495.                     foreach ($values as $value{
  496.                         // find the key for the value that should be deleted
  497.                         $key array_search($value$this->_attributes[$name]);
  498.                         if (false !== $key{
  499.                             // delete the value
  500.                             unset($this->_attributes[$name][$key]);
  501.                         }
  502.                     }
  503.                 }
  504.             }
  505.         }
  506.         $return = true;
  507.         return $return;
  508.     }
  509.  
  510.     /**
  511.     * Replaces attributes or its values
  512.     *
  513.     * The parameter has to an array of the following form:
  514.     * array("attributename" => "single value",
  515.     *       "attribute2name" => array("value1", "value2"))
  516.     * If the attribute does not yet exist it will be added instead.
  517.     * If the attribue value is null, the attribute will de deleted
  518.     *
  519.     * These changes are local to the entry and do
  520.     * not affect the entry on the server until {@link update()} is called.
  521.     *
  522.     * @param array $attr Attributes to replace
  523.     *
  524.     * @access public
  525.     * @return true|Net_LDAP_Error
  526.     */
  527.     function replace($attr = array())
  528.     {
  529.         if (false == is_array($attr)) {
  530.             return PEAR::raiseError("Parameter must be an array");
  531.         }
  532.         foreach ($attr as $k => $v{
  533.             $k $this->_getAttrName($k);
  534.             if (false == is_array($v)) {
  535.                 // delete attributes with empty values
  536.                 if ($v == null{
  537.                     $this->delete($k);
  538.                     continue;
  539.                 else {
  540.                     $v = array($v);
  541.                 }
  542.             }
  543.             // existing attributes will get replaced
  544.             if ($this->exists($k)) {
  545.                 $this->_changes["replace"][$k$v;
  546.                 $this->_attributes[$k]         $v;
  547.             else {
  548.                 // new ones just get added
  549.                 $this->add(array($k => $v));
  550.             }
  551.         }
  552.         $return = true;
  553.         return $return;
  554.     }
  555.  
  556.     /**
  557.     * Update the entry on the directory server
  558.     *
  559.     * @param Net_LDAP $ldap If passed, a call to setLDAP() is issued prior update, thus switching the LDAP-server. This is for perl-ldap interface compliance
  560.     *
  561.     * @access public
  562.     * @return true|Net_LDAP_Error
  563.     * @todo Entry rename with a DN containing special characters needs testing!
  564.     */
  565.     function update($ldap = null)
  566.     {
  567.         if ($ldap{
  568.             $msg $this->setLDAP($ldap);
  569.             if (Net_LDAP::isError($msg)) {
  570.                 return PEAR::raiseError('You passed an invalid $ldap variable to update()');
  571.             }
  572.         }
  573.  
  574.         // ensure we have a valid LDAP object
  575.         $ldap =$this->getLDAP();
  576.         if (!is_a($ldap'Net_LDAP')) {
  577.             return PEAR::raiseError("The entries LDAP object is not valid");
  578.         }
  579.  
  580.         $link $ldap->getLink();
  581.  
  582.         /*
  583.         * Delete the entry
  584.         */
  585.         if (true === $this->_delete{
  586.             return $ldap->delete($this);
  587.         }
  588.  
  589.         /*
  590.         * New entry
  591.         */
  592.         if (true === $this->_new{
  593.             $msg $ldap->add($this);
  594.             if (Net_LDAP::isError($msg)) {
  595.                 return $msg;
  596.             }
  597.             $this->_new                = false;
  598.             $this->_changes['add']     = array();
  599.             $this->_changes['delete']  = array();
  600.             $this->_changes['replace'= array();
  601.             $this->_original           $this->_attributes;
  602.  
  603.             $return = true;
  604.             return $return;
  605.         }
  606.  
  607.         /*
  608.         * Rename/move entry
  609.         */
  610.         if (false == is_null($this->_newdn)) {
  611.             if ($ldap->getLDAPVersion(!== 3{
  612.                 return PEAR::raiseError("Renaming/Moving an entry is only supported in LDAPv3");
  613.             }
  614.             // make dn relative to parent (needed for ldap rename)
  615.             $parent Net_LDAP_Util::ldap_explode_dn($this->_newdnarray('casefolding' => 'none''reverse' => false'onlyvalues' => false));
  616.             if (Net_LDAP::isError($parent)) {
  617.                 return $parent;
  618.             }
  619.             $child array_shift($parent);
  620.             // maybe the dn consist of a multivalued RDN, we must build the dn in this case
  621.             // because the $child-RDN is an array!
  622.             if (is_array($child)) {
  623.                 $child Net_LDAP_Util::canonical_dn($child);
  624.             }
  625.             $parent Net_LDAP_Util::canonical_dn($parent);
  626.  
  627.             // rename
  628.             if (false == @ldap_rename($link$this->_dn$child$parenttrue)) {
  629.                 return PEAR::raiseError("Entry not renamed: " .
  630.                                         @ldap_error($link)@ldap_errno($link));
  631.             }
  632.             // reflect changes to local copy
  633.             $this->_dn    $this->_newdn;
  634.             $this->_newdn = null;
  635.         }
  636.  
  637.         /*
  638.         * Carry out modifications to the entry
  639.         */
  640.         // ADD
  641.         foreach ($this->_changes["add"as $attr => $value{
  642.             // if attribute exists, add new values
  643.             if ($this->exists($attr)) {
  644.                 if (false === @ldap_mod_add($link$this->dn()array($attr => $value))) {
  645.                     return PEAR::raiseError("Could not add new values to attribute $attr" .
  646.                                             @ldap_error($link)@ldap_errno($link));
  647.                 }
  648.             else {
  649.                 // new attribute
  650.                 if (false === @ldap_modify($link$this->dn()array($attr => $value))) {
  651.                     return PEAR::raiseError("Could not add new attribute $attr" .
  652.                                             @ldap_error($link)@ldap_errno($link));
  653.                 }
  654.             }
  655.             // all went well here, I guess
  656.             unset($this->_changes["add"][$attr]);
  657.         }
  658.  
  659.         // DELETE
  660.         foreach ($this->_changes["delete"as $attr => $value{
  661.             // In LDAPv3 you need to specify the old values for deleting
  662.             if (is_null($value&& $ldap->getLDAPVersion(=== 3{
  663.                 $value $this->_original[$attr];
  664.             }
  665.             if (false === @ldap_mod_del($link$this->dn()array($attr => $value))) {
  666.                 return PEAR::raiseError("Could not delete attribute $attr" .
  667.                                         @ldap_error($link)@ldap_errno($link));
  668.             }
  669.             unset($this->_changes["delete"][$attr]);
  670.         }
  671.  
  672.         // REPLACE
  673.         foreach ($this->_changes["replace"as $attr => $value{
  674.             if (false === @ldap_modify($link$this->dn()array($attr => $value))) {
  675.                 return PEAR::raiseError("Could not replace attribute $attr values: " .
  676.                                         @ldap_error($link)@ldap_errno($link));
  677.             }
  678.             unset($this->_changes["replace"][$attr]);
  679.         }
  680.  
  681.         // all went well, so _original (server) becomes _attributes (local copy)
  682.         $this->_original $this->_attributes;
  683.  
  684.         $return = true;
  685.         return $return;
  686.     }
  687.  
  688.     /**
  689.     * Returns the right attribute name
  690.     *
  691.     * @param string $attr Name of attribute
  692.     *
  693.     * @access private
  694.     * @return string The right name of the attribute
  695.     */
  696.     function _getAttrName($attr)
  697.     {
  698.         $name strtolower($attr);
  699.         if (array_key_exists($name$this->_map)) {
  700.             $attr $this->_map[$name];
  701.         }
  702.         return $attr;
  703.     }
  704.  
  705.     /**
  706.     * Returns a reference to the LDAP-Object of this entry
  707.     *
  708.     * @access public
  709.     * @return Net_LDAP|Net_LDAP_Error  Reference to the Net_LDAP Object (the connection) or Net_LDAP_Error
  710.     */
  711.     function &getLDAP()
  712.     {
  713.         if (!is_a($this->_ldap'Net_LDAP')) {
  714.             return PEAR::raiseError("LDAP is not a valid Net_LDAP object");
  715.         else {
  716.             return $this->_ldap;
  717.         }
  718.     }
  719.  
  720.     /**
  721.     * Sets a reference to the LDAP-Object of this entry
  722.     *
  723.     * After setting a Net_LDAP object, calling update() will use that object for
  724.     * updating directory contents. Use this to dynamicly switch directorys.
  725.     *
  726.     * @param Net_LDAP &$ldap Net_LDAP object that this entry should be connected to
  727.     *
  728.     * @access public
  729.     * @return true|Net_LDAP_Error
  730.     */
  731.     function setLDAP(&$ldap)
  732.     {
  733.         if (!is_a($ldap'Net_LDAP')) {
  734.             return PEAR::raiseError("LDAP is not a valid Net_LDAP object");
  735.         else {
  736.             $this->_ldap =$ldap;
  737.             return true;
  738.         }
  739.     }
  740.  
  741.     /**
  742.     * Applies a regular expression onto a single- or multivalued attribute (like preg_match())
  743.     *
  744.     * This method behaves like PHPs preg_match() but with some exceptions.
  745.     * If you want to retrieve match information, then you MUST pass the
  746.     * $matches parameter via reference! otherwise you will get no matches.
  747.     * Since it is possible to have multi valued attributes the $matches
  748.     * array will have a additionally numerical dimension (one for each value):
  749.     * <code>
  750.     * $matches = array(
  751.     *         0 => array (usual preg_match() returnarray),
  752.     *         1 => array (usual preg_match() returnarray)
  753.     *     )
  754.     * </code>
  755.     * Please note, that $matches will be initialized to an empty array inside.
  756.     *
  757.     * Usage example:
  758.     * <code>
  759.     * $result = $entry->preg_match('/089(\d+)/', 'telephoneNumber', &$matches);
  760.     * if ( $result === true ){
  761.     *     echo "First match: ".$matches[0][1];   // Match of value 1, content of first bracket
  762.     * } else {
  763.     *     if ( Net_LDAP::isError($result) ) {
  764.     *         echo "Error: ".$result->getMessage();
  765.     *     } else {
  766.     *         echo "No match found.";
  767.     *     }
  768.     * }
  769.     * </code>
  770.     *
  771.     * Please note that it is important to test for an Net_LDAP_Error, because objects are
  772.     * evaluating to true by default, thus if a error occured, and you only check using "==" then
  773.     * you get misleading results. Use the "identical" (===) operator to test for matches to
  774.     * avoid this as shown above.
  775.     *
  776.     * @param string $regex     The regular expression
  777.     * @param string $attr_name The attribute to search in
  778.     * @param array  $matches   (optional, PASS BY REFERENCE!) Array to store matches in
  779.     *
  780.     * @return boolean|Net_LDAP_Error TRUE, if we had a match in one of the values, otherwise false. Net_LDAP_Error in case something went wrong
  781.     */
  782.     function preg_match($regex$attr_name$matches = array())
  783.     {
  784.         $matches = array();
  785.  
  786.         // fetch attribute values
  787.         $attr $this->getValue($attr_name'all');
  788.         if (Net_LDAP::isError($attr)) {
  789.             return $attr;
  790.         else {
  791.             unset($attr['count']);
  792.         }
  793.  
  794.         // perform preg_match() on all values
  795.         $match = false;
  796.         foreach ($attr as $thisvalue{
  797.             $matches_int = array();
  798.             if (preg_match($regex$thisvalue$matches_int)) {
  799.                 $match = true;
  800.                 array_push($matches$matches_int)// store matches in reference
  801.             }
  802.         }
  803.         return $match;
  804.     }
  805. }
  806. ?>

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