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.49 2008/01/08 15:07:21 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.                 if (is_array($name)) {
  475.                     // someone mixed modes (list mode but specific values given!)
  476.                     $del_attr_name array_search($name$attr);
  477.                     $this->delete(array($del_attr_name => $name));
  478.                 else {
  479.                     $name $this->_getAttrName($name);
  480.                     if ($this->exists($name)) {
  481.                         $this->_changes["delete"][$name= null;
  482.                         unset($this->_attributes[$name]);
  483.                     }
  484.                 }
  485.             }
  486.         else {
  487.             // Here we have a hash with "attributename" => "value to delete"
  488.             foreach ($attr as $name => $values{
  489.                 if (is_int($name)) {
  490.                     // someone mixed modes and gave us just an attribute name
  491.                     $this->delete($values);
  492.                 else {
  493.                     // get the correct attribute name
  494.                     $name $this->_getAttrName($name);
  495.                     if ($this->exists($name)) {
  496.                         if (false == is_array($values)) {
  497.                             $values = array($values);
  498.                         }
  499.                         // save values to be deleted
  500.                         if (empty($this->_changes["delete"][$name])) {
  501.                             $this->_changes["delete"][$name= array();
  502.                         }
  503.                         $this->_changes["delete"][$name=
  504.                             array_merge($this->_changes["delete"][$name]$values);
  505.                         foreach ($values as $value{
  506.                             // find the key for the value that should be deleted
  507.                             $key array_search($value$this->_attributes[$name]);
  508.                             if (false !== $key{
  509.                                 // delete the value
  510.                                 unset($this->_attributes[$name][$key]);
  511.                             }
  512.                         }
  513.                     }
  514.                 }
  515.             }
  516.         }
  517.         $return = true;
  518.         return $return;
  519.     }
  520.  
  521.     /**
  522.     * Replaces attributes or its values
  523.     *
  524.     * The parameter has to an array of the following form:
  525.     * array("attributename" => "single value",
  526.     *       "attribute2name" => array("value1", "value2"))
  527.     * If the attribute does not yet exist it will be added instead.
  528.     * If the attribue value is null, the attribute will de deleted
  529.     *
  530.     * These changes are local to the entry and do
  531.     * not affect the entry on the server until {@link update()} is called.
  532.     *
  533.     * @param array $attr Attributes to replace
  534.     *
  535.     * @access public
  536.     * @return true|Net_LDAP_Error
  537.     */
  538.     function replace($attr = array())
  539.     {
  540.         if (false == is_array($attr)) {
  541.             return PEAR::raiseError("Parameter must be an array");
  542.         }
  543.         foreach ($attr as $k => $v{
  544.             $k $this->_getAttrName($k);
  545.             if (false == is_array($v)) {
  546.                 // delete attributes with empty values
  547.                 if ($v == null{
  548.                     $this->delete($k);
  549.                     continue;
  550.                 else {
  551.                     $v = array($v);
  552.                 }
  553.             }
  554.             // existing attributes will get replaced
  555.             if ($this->exists($k)) {
  556.                 $this->_changes["replace"][$k$v;
  557.                 $this->_attributes[$k]         $v;
  558.             else {
  559.                 // new ones just get added
  560.                 $this->add(array($k => $v));
  561.             }
  562.         }
  563.         $return = true;
  564.         return $return;
  565.     }
  566.  
  567.     /**
  568.     * Update the entry on the directory server
  569.     *
  570.     * @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
  571.     *
  572.     * @access public
  573.     * @return true|Net_LDAP_Error
  574.     * @todo Entry rename with a DN containing special characters needs testing!
  575.     */
  576.     function update($ldap = null)
  577.     {
  578.         if ($ldap{
  579.             $msg $this->setLDAP($ldap);
  580.             if (Net_LDAP::isError($msg)) {
  581.                 return PEAR::raiseError('You passed an invalid $ldap variable to update()');
  582.             }
  583.         }
  584.  
  585.         // ensure we have a valid LDAP object
  586.         $ldap =$this->getLDAP();
  587.         if (!is_a($ldap'Net_LDAP')) {
  588.             return PEAR::raiseError("The entries LDAP object is not valid");
  589.         }
  590.  
  591.         $link $ldap->getLink();
  592.  
  593.         /*
  594.         * Delete the entry
  595.         */
  596.         if (true === $this->_delete{
  597.             return $ldap->delete($this);
  598.         }
  599.  
  600.         /*
  601.         * New entry
  602.         */
  603.         if (true === $this->_new{
  604.             $msg $ldap->add($this);
  605.             if (Net_LDAP::isError($msg)) {
  606.                 return $msg;
  607.             }
  608.             $this->_new                = false;
  609.             $this->_changes['add']     = array();
  610.             $this->_changes['delete']  = array();
  611.             $this->_changes['replace'= array();
  612.             $this->_original           $this->_attributes;
  613.  
  614.             $return = true;
  615.             return $return;
  616.         }
  617.  
  618.         /*
  619.         * Rename/move entry
  620.         */
  621.         if (false == is_null($this->_newdn)) {
  622.             if ($ldap->getLDAPVersion(!== 3{
  623.                 return PEAR::raiseError("Renaming/Moving an entry is only supported in LDAPv3");
  624.             }
  625.             // make dn relative to parent (needed for ldap rename)
  626.             $parent Net_LDAP_Util::ldap_explode_dn($this->_newdnarray('casefolding' => 'none''reverse' => false'onlyvalues' => false));
  627.             if (Net_LDAP::isError($parent)) {
  628.                 return $parent;
  629.             }
  630.             $child array_shift($parent);
  631.             // maybe the dn consist of a multivalued RDN, we must build the dn in this case
  632.             // because the $child-RDN is an array!
  633.             if (is_array($child)) {
  634.                 $child Net_LDAP_Util::canonical_dn($child);
  635.             }
  636.             $parent Net_LDAP_Util::canonical_dn($parent);
  637.  
  638.             // rename
  639.             if (false == @ldap_rename($link$this->_dn$child$parenttrue)) {
  640.                 return PEAR::raiseError("Entry not renamed: " .
  641.                                         @ldap_error($link)@ldap_errno($link));
  642.             }
  643.             // reflect changes to local copy
  644.             $this->_dn    $this->_newdn;
  645.             $this->_newdn = null;
  646.         }
  647.  
  648.         /*
  649.         * Carry out modifications to the entry
  650.         */
  651.         // ADD
  652.         foreach ($this->_changes["add"as $attr => $value{
  653.             // if attribute exists, add new values
  654.             if ($this->exists($attr)) {
  655.                 if (false === @ldap_mod_add($link$this->dn()array($attr => $value))) {
  656.                     return PEAR::raiseError("Could not add new values to attribute $attr" .
  657.                                             @ldap_error($link)@ldap_errno($link));
  658.                 }
  659.             else {
  660.                 // new attribute
  661.                 if (false === @ldap_modify($link$this->dn()array($attr => $value))) {
  662.                     return PEAR::raiseError("Could not add new attribute $attr" .
  663.                                             @ldap_error($link)@ldap_errno($link));
  664.                 }
  665.             }
  666.             // all went well here, I guess
  667.             unset($this->_changes["add"][$attr]);
  668.         }
  669.  
  670.         // DELETE
  671.         foreach ($this->_changes["delete"as $attr => $value{
  672.             // In LDAPv3 you need to specify the old values for deleting
  673.             if (is_null($value&& $ldap->getLDAPVersion(=== 3{
  674.                 $value $this->_original[$attr];
  675.             }
  676.             if (false === @ldap_mod_del($link$this->dn()array($attr => $value))) {
  677.                 return PEAR::raiseError("Could not delete attribute $attr" .
  678.                                         @ldap_error($link)@ldap_errno($link));
  679.             }
  680.             unset($this->_changes["delete"][$attr]);
  681.         }
  682.  
  683.         // REPLACE
  684.         foreach ($this->_changes["replace"as $attr => $value{
  685.             if (false === @ldap_modify($link$this->dn()array($attr => $value))) {
  686.                 return PEAR::raiseError("Could not replace attribute $attr values: " .
  687.                                         @ldap_error($link)@ldap_errno($link));
  688.             }
  689.             unset($this->_changes["replace"][$attr]);
  690.         }
  691.  
  692.         // all went well, so _original (server) becomes _attributes (local copy)
  693.         $this->_original $this->_attributes;
  694.  
  695.         $return = true;
  696.         return $return;
  697.     }
  698.  
  699.     /**
  700.     * Returns the right attribute name
  701.     *
  702.     * @param string $attr Name of attribute
  703.     *
  704.     * @access private
  705.     * @return string The right name of the attribute
  706.     */
  707.     function _getAttrName($attr)
  708.     {
  709.         $name strtolower($attr);
  710.         if (array_key_exists($name$this->_map)) {
  711.             $attr $this->_map[$name];
  712.         }
  713.         return $attr;
  714.     }
  715.  
  716.     /**
  717.     * Returns a reference to the LDAP-Object of this entry
  718.     *
  719.     * @access public
  720.     * @return Net_LDAP|Net_LDAP_Error  Reference to the Net_LDAP Object (the connection) or Net_LDAP_Error
  721.     */
  722.     function &getLDAP()
  723.     {
  724.         if (!is_a($this->_ldap'Net_LDAP')) {
  725.             return PEAR::raiseError("LDAP is not a valid Net_LDAP object");
  726.         else {
  727.             return $this->_ldap;
  728.         }
  729.     }
  730.  
  731.     /**
  732.     * Sets a reference to the LDAP-Object of this entry
  733.     *
  734.     * After setting a Net_LDAP object, calling update() will use that object for
  735.     * updating directory contents. Use this to dynamicly switch directorys.
  736.     *
  737.     * @param Net_LDAP &$ldap Net_LDAP object that this entry should be connected to
  738.     *
  739.     * @access public
  740.     * @return true|Net_LDAP_Error
  741.     */
  742.     function setLDAP(&$ldap)
  743.     {
  744.         if (!is_a($ldap'Net_LDAP')) {
  745.             return PEAR::raiseError("LDAP is not a valid Net_LDAP object");
  746.         else {
  747.             $this->_ldap =$ldap;
  748.             return true;
  749.         }
  750.     }
  751.  
  752.     /**
  753.     * Applies a regular expression onto a single- or multivalued attribute (like preg_match())
  754.     *
  755.     * This method behaves like PHPs preg_match() but with some exceptions.
  756.     * If you want to retrieve match information, then you MUST pass the
  757.     * $matches parameter via reference! otherwise you will get no matches.
  758.     * Since it is possible to have multi valued attributes the $matches
  759.     * array will have a additionally numerical dimension (one for each value):
  760.     * <code>
  761.     * $matches = array(
  762.     *         0 => array (usual preg_match() returnarray),
  763.     *         1 => array (usual preg_match() returnarray)
  764.     *     )
  765.     * </code>
  766.     * Please note, that $matches will be initialized to an empty array inside.
  767.     *
  768.     * Usage example:
  769.     * <code>
  770.     * $result = $entry->preg_match('/089(\d+)/', 'telephoneNumber', &$matches);
  771.     * if ( $result === true ){
  772.     *     echo "First match: ".$matches[0][1];   // Match of value 1, content of first bracket
  773.     * } else {
  774.     *     if ( Net_LDAP::isError($result) ) {
  775.     *         echo "Error: ".$result->getMessage();
  776.     *     } else {
  777.     *         echo "No match found.";
  778.     *     }
  779.     * }
  780.     * </code>
  781.     *
  782.     * Please note that it is important to test for an Net_LDAP_Error, because objects are
  783.     * evaluating to true by default, thus if a error occured, and you only check using "==" then
  784.     * you get misleading results. Use the "identical" (===) operator to test for matches to
  785.     * avoid this as shown above.
  786.     *
  787.     * @param string $regex     The regular expression
  788.     * @param string $attr_name The attribute to search in
  789.     * @param array  $matches   (optional, PASS BY REFERENCE!) Array to store matches in
  790.     *
  791.     * @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
  792.     */
  793.     function preg_match($regex$attr_name$matches = array())
  794.     {
  795.         $matches = array();
  796.  
  797.         // fetch attribute values
  798.         $attr $this->getValue($attr_name'all');
  799.         if (Net_LDAP::isError($attr)) {
  800.             return $attr;
  801.         else {
  802.             unset($attr['count']);
  803.         }
  804.  
  805.         // perform preg_match() on all values
  806.         $match = false;
  807.         foreach ($attr as $thisvalue{
  808.             $matches_int = array();
  809.             if (preg_match($regex$thisvalue$matches_int)) {
  810.                 $match = true;
  811.                 array_push($matches$matches_int)// store matches in reference
  812.             }
  813.         }
  814.         return $match;
  815.     }
  816.  
  817.     /**
  818.     * Is this entry going to be deleted once update() is called?
  819.     */
  820.     function willBeDeleted({
  821.         return $this->_delete;
  822.     }
  823.  
  824.     /**
  825.     * Returns the attribute changes to be carried out once update() is called
  826.     *
  827.     * @return array 
  828.     */
  829.     function getChanges({
  830.         return $this->_changes;
  831.     }
  832. }
  833. ?>

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