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.56 2008/10/23 11:02:18 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.     * Use this method, if you want to initialize a fresh entry.
  161.     *
  162.     * The method should be called statically: $entry = Net_LDAP_Entry::createFresh();
  163.     * You should put a 'objectClass' attribute into the $attrs so the directory server
  164.     * knows which object you want to create. However, you may omit this in case you
  165.     * don't want to add this entry to a directory server.
  166.     *
  167.     * The attributes parameter is as following:
  168.     * <code>
  169.     * $attrs = array( 'attribute1' => array('value1', 'value2'),
  170.     *                 'attribute2' => 'single value'
  171.     *          );
  172.     * </code>
  173.     *
  174.     * @param string $dn    DN of the Entry
  175.     * @param array  $attrs Attributes of the entry
  176.     *
  177.     * @static
  178.     * @return Net_LDAP_Entry 
  179.     */
  180.     function createFresh($dn$attrs = array())
  181.     {
  182.         if (!is_array($attrs)) {
  183.             return PEAR::raiseError("Unable to create fresh entry: Parameter \$attrs needs to be an array!");
  184.         }
  185.  
  186.         $entry = new Net_LDAP_Entry($attrs$dn);
  187.         return $entry;
  188.     }
  189.  
  190.     /**
  191.     * Get or set the distinguished name of the entry
  192.     *
  193.     * If called without an argument the current (or the new DN if set) DN gets returned.
  194.     * If you provide an DN, this entry is moved to the new location specified if a DN existed.
  195.     * If the DN was not set, the DN gets initialized. Call {@link update()} to actually create
  196.     * the new Entry in the directory.
  197.     * To fetch the current active DN after setting a new DN but before an update(), you can use
  198.     * {@link currentDN()} to retrieve the DN that is currently active.
  199.     *
  200.     * Please note that special characters (eg german umlauts) should be encoded using utf8_encode().
  201.     * You may use {@link Net_LDAP_Util::canonical_dn()} for properly encoding of the DN.
  202.     *
  203.     * @param string $dn New distinguished name
  204.     *
  205.     * @access public
  206.     * @return string|trueDistinguished name (or true if a new DN was provided)
  207.     */
  208.     function dn($dn = null)
  209.     {
  210.         if (false == is_null($dn)) {
  211.             if (is_null($this->_dn)) {
  212.                 $this->_dn $dn;
  213.             else {
  214.                 $this->_newdn $dn;
  215.             }
  216.             return true;
  217.         }
  218.         return (isset($this->_newdn$this->_newdn $this->currentDN());
  219.     }
  220.  
  221.     /**
  222.     * Renames or moves the entry
  223.     *
  224.     * This is just a convinience alias to {@link dn()}
  225.     * to make your code more meaningful.
  226.     *
  227.     * @param string $newdn The new DN
  228.     * @return true 
  229.     */
  230.     function move($newdn)
  231.     {
  232.         return $this->dn($newdn);
  233.     }
  234.  
  235.     /**
  236.     * Sets the internal attributes array
  237.     *
  238.     * This fetches the values for the attributes from the server.
  239.     * The attribute Syntax will be checked so binary attributes will be returned
  240.     * as binary values.
  241.     *
  242.     * Attributes may be passed directly via the $attributes parameter to setup this
  243.     * entry manually. This overrides attribute fetching from the server.
  244.     *
  245.     * @param array $attributes Attributes to set for this entry
  246.     *
  247.     * @access private
  248.     * @return void 
  249.     */
  250.     function _setAttributes($attributes = null)
  251.     {
  252.         /*
  253.         * fetch attributes from the server
  254.         */
  255.         if (is_null($attributes&& is_resource($this->_entry&& is_resource($this->_link)) {
  256.             // fetch schema
  257.             if (is_a($this->_ldap'Net_LDAP')) {
  258.                 $schema =$this->_ldap->schema();
  259.             }
  260.             // fetch attributes
  261.             $attributes = array();
  262.             do {
  263.                 if (empty($attr)) {
  264.                     $ber  = null;
  265.                     $attr @ldap_first_attribute($this->_link$this->_entry$ber);
  266.                 else {
  267.                     $attr @ldap_next_attribute($this->_link$this->_entry$ber);
  268.                 }
  269.                 if ($attr{
  270.                     $func 'ldap_get_values'// standard function to fetch value
  271.  
  272.                     // Try to get binary values as binary data
  273.                     if (is_a($schema'Net_LDAP_Schema')) {
  274.                         if ($schema->isBinary($attr)) {
  275.                              $func 'ldap_get_values_len';
  276.                         }
  277.                     }
  278.                     // fetch attribute value (needs error checking?)
  279.                     $attributes[$attr$func($this->_link$this->_entry$attr);
  280.                 }
  281.             while ($attr);
  282.         }
  283.  
  284.         /*
  285.         * set attribute data directly, if passed
  286.         */
  287.         if (is_array($attributes&& count($attributes> 0{
  288.             if (isset($attributes["count"]&& is_numeric($attributes["count"])) {
  289.                 unset($attributes["count"]);
  290.             }
  291.             foreach ($attributes as $k => $v{
  292.                 // attribute names should not be numeric
  293.                 if (is_numeric($k)) {
  294.                     continue;
  295.                 }
  296.                 // map generic attribute name to real one
  297.                 $this->_map[strtolower($k)$k;
  298.                 // attribute values should be in an array
  299.                 if (false == is_array($v)) {
  300.                     $v = array($v);
  301.                 }
  302.                 // remove the value count (comes from ldap server)
  303.                 if (isset($v["count"])) {
  304.                     unset($v["count"]);
  305.                 }
  306.                 $this->_attributes[$k$v;
  307.             }
  308.         }
  309.  
  310.         // save a copy for later use
  311.         $this->_original $this->_attributes;
  312.     }
  313.  
  314.     /**
  315.     * Get the values of all attributes in a hash
  316.     *
  317.     * The returned hash has the form
  318.     * <code>array('attributename' => 'single value',
  319.     *       'attributename' => array('value1', value2', value3'))</code>
  320.     *
  321.     * @access public
  322.     * @return array Hash of all attributes with their values
  323.     */
  324.     function getValues()
  325.     {
  326.         $attrs = array();
  327.         foreach ($this->_attributes as $attr => $value{
  328.             $attrs[$attr$this->getValue($attr);
  329.         }
  330.         return $attrs;
  331.     }
  332.  
  333.     /**
  334.     * Get the value of a specific attribute
  335.     *
  336.     * The first parameter is the name of the attribute
  337.     * The second parameter influences the way the value is returned:
  338.     * 'single': only the first value is returned as string
  339.     * 'all': all values including the value count are returned in an
  340.     *               array
  341.     * 'default': in all other cases an attribute value with a single value is
  342.     *            returned as string, if it has multiple values it is returned
  343.     *            as an array (without value count)
  344.     *
  345.     * @param string $attr   Attribute name
  346.     * @param string $option Option
  347.     *
  348.     * @access public
  349.     * @return string|array|PEAR_Errorstring, array or PEAR_Error
  350.     */
  351.     function getValue($attr$option = null)
  352.     {
  353.         $attr $this->_getAttrName($attr);
  354.  
  355.         if (false == array_key_exists($attr$this->_attributes)) {
  356.             return PEAR::raiseError("Unknown attribute ($attr) requested");
  357.         }
  358.  
  359.         $value $this->_attributes[$attr];
  360.  
  361.         if ($option == "single" || (count($value== 1 && $option != 'all')) {
  362.             $value array_shift($value);
  363.         }
  364.  
  365.         return $value;
  366.     }
  367.  
  368.     /**
  369.     * Alias function of getValue for perl-ldap interface
  370.     *
  371.     * @see getValue()
  372.     * @return string|array|PEAR_Error
  373.     */
  374.     function get_value()
  375.     {
  376.         $args func_get_args();
  377.         return call_user_func_array(array&$this'getValue' )$args);
  378.     }
  379.  
  380.     /**
  381.     * Returns an array of attributes names
  382.     *
  383.     * @access public
  384.     * @return array Array of attribute names
  385.     */
  386.     function attributes()
  387.     {
  388.         return array_keys($this->_attributes);
  389.     }
  390.  
  391.     /**
  392.     * Returns whether an attribute exists or not
  393.     *
  394.     * @param string $attr Attribute name
  395.     *
  396.     * @access public
  397.     * @return boolean 
  398.     */
  399.     function exists($attr)
  400.     {
  401.         $attr $this->_getAttrName($attr);
  402.         return array_key_exists($attr$this->_attributes);
  403.     }
  404.  
  405.     /**
  406.     * Adds a new attribute or a new value to an existing attribute
  407.     *
  408.     * The paramter has to be an array of the form:
  409.     * array('attributename' => 'single value',
  410.     *       'attributename' => array('value1', 'value2))
  411.     * When the attribute already exists the values will be added, else the
  412.     * attribute will be created. These changes are local to the entry and do
  413.     * not affect the entry on the server until update() is called.
  414.     *
  415.     * Note, that you can add values of attributes that you haven't selected, but if
  416.     * you do so, {@link getValue()} and {@link getValues()} will only return the
  417.     * values you added, _NOT_ all values present on the server. To avoid this, just refetch
  418.     * the entry after calling {@link update()} or select the attribute.
  419.     *
  420.     * @param array $attr Attributes to add
  421.     *
  422.     * @access public
  423.     * @return true|Net_LDAP_Error
  424.     */
  425.     function add($attr = array())
  426.     {
  427.         if (false == is_array($attr)) {
  428.             return PEAR::raiseError("Parameter must be an array");
  429.         }
  430.         foreach ($attr as $k => $v{
  431.             $k $this->_getAttrName($k);
  432.             if (false == is_array($v)) {
  433.                 // Do not add empty values
  434.                 if ($v == null{
  435.                     continue;
  436.                 else {
  437.                     $v = array($v);
  438.                 }
  439.             }
  440.             // add new values to existing attribute or add new attribute
  441.             if ($this->exists($k)) {
  442.                 $this->_attributes[$karray_unique(array_merge($this->_attributes[$k]$v));
  443.             else {
  444.                 $this->_map[strtolower($k)$k;
  445.                 $this->_attributes[$k]      $v;
  446.             }
  447.  
  448.             // save changes for update()
  449.             if (empty($this->_changes["add"][$k])) {
  450.                 $this->_changes["add"][$k= array();
  451.             }
  452.             $this->_changes["add"][$karray_merge($this->_changes["add"][$k]$v);
  453.         }
  454.         $return = true;
  455.         return $return;
  456.     }
  457.  
  458.     /**
  459.     * Deletes an whole attribute or a value or the whole entry
  460.     *
  461.     * The parameter can be one of the following:
  462.     *
  463.     * "attributename" - The attribute as a whole will be deleted
  464.     * array("attributename1", "attributename2) - All given attributes will be
  465.     *                                            deleted
  466.     * array("attributename" => "value") - The value will be deleted
  467.     * array("attributename" => array("value1", "value2") - The given values
  468.     *                                                      will be deleted
  469.     * If $attr is null or omitted , then the whole Entry will be deleted!
  470.     *
  471.     * These changes are local to the entry and do
  472.     * not affect the entry on the server until {@link update()} is called.
  473.     *
  474.     * Please note that you must select the attribute (at $ldap->search() for example)
  475.     * to be able to delete values of it, Otherwise {@link update()} will silently fail
  476.     * and remove nothing.
  477.     *
  478.     * @param string|array$attr Attributes to delete (NULL or missing to delete whole entry)
  479.     *
  480.     * @access public
  481.     * @return true 
  482.     */
  483.     function delete($attr = null)
  484.     {
  485.         if (is_null($attr)) {
  486.             $this->_delete = true;
  487.             return true;
  488.         }
  489.         if (is_string($attr)) {
  490.             $attr = array($attr);
  491.         }
  492.         // Make the assumption that attribute names cannot be numeric,
  493.         // therefore this has to be a simple list of attribute names to delete
  494.         if (is_numeric(key($attr))) {
  495.             foreach ($attr as $name{
  496.                 if (is_array($name)) {
  497.                     // someone mixed modes (list mode but specific values given!)
  498.                     $del_attr_name array_search($name$attr);
  499.                     $this->delete(array($del_attr_name => $name));
  500.                 else {
  501.                     $name $this->_getAttrName($name);
  502.                     if ($this->exists($name)) {
  503.                         $this->_changes["delete"][$name= null;
  504.                         unset($this->_attributes[$name]);
  505.                     }
  506.                 }
  507.             }
  508.         else {
  509.             // Here we have a hash with "attributename" => "value to delete"
  510.             foreach ($attr as $name => $values{
  511.                 if (is_int($name)) {
  512.                     // someone mixed modes and gave us just an attribute name
  513.                     $this->delete($values);
  514.                 else {
  515.                     // get the correct attribute name
  516.                     $name $this->_getAttrName($name);
  517.                     if ($this->exists($name)) {
  518.                         if (false == is_array($values)) {
  519.                             $values = array($values);
  520.                         }
  521.                         // save values to be deleted
  522.                         if (empty($this->_changes["delete"][$name])) {
  523.                             $this->_changes["delete"][$name= array();
  524.                         }
  525.                         $this->_changes["delete"][$name=
  526.                             array_merge($this->_changes["delete"][$name]$values);
  527.                         foreach ($values as $value{
  528.                             // find the key for the value that should be deleted
  529.                             $key array_search($value$this->_attributes[$name]);
  530.                             if (false !== $key{
  531.                                 // delete the value
  532.                                 unset($this->_attributes[$name][$key]);
  533.                             }
  534.                         }
  535.                     }
  536.                 }
  537.             }
  538.         }
  539.         $return = true;
  540.         return $return;
  541.     }
  542.  
  543.     /**
  544.     * Replaces attributes or its values
  545.     *
  546.     * The parameter has to an array of the following form:
  547.     * array("attributename" => "single value",
  548.     *       "attribute2name" => array("value1", "value2"))
  549.     * If the attribute does not yet exist it will be added instead.
  550.     * If the attribue value is null, the attribute will de deleted
  551.     *
  552.     * These changes are local to the entry and do
  553.     * not affect the entry on the server until {@link update()} is called.
  554.     *
  555.     * @param array $attr Attributes to replace
  556.     *
  557.     * @access public
  558.     * @return true|Net_LDAP_Error
  559.     */
  560.     function replace($attr = array())
  561.     {
  562.         if (false == is_array($attr)) {
  563.             return PEAR::raiseError("Parameter must be an array");
  564.         }
  565.         foreach ($attr as $k => $v{
  566.             $k $this->_getAttrName($k);
  567.             if (false == is_array($v)) {
  568.                 // delete attributes with empty values
  569.                 if ($v == null{
  570.                     $this->delete($k);
  571.                     continue;
  572.                 else {
  573.                     $v = array($v);
  574.                 }
  575.             }
  576.             // existing attributes will get replaced
  577.             if ($this->exists($k)) {
  578.                 $this->_changes["replace"][$k$v;
  579.                 $this->_attributes[$k]         $v;
  580.             else {
  581.                 // new ones just get added
  582.                 $this->add(array($k => $v));
  583.             }
  584.         }
  585.         $return = true;
  586.         return $return;
  587.     }
  588.  
  589.     /**
  590.     * Update the entry on the directory server
  591.     *
  592.     * @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
  593.     *
  594.     * @access public
  595.     * @return true|Net_LDAP_Error
  596.     * @todo Entry rename with a DN containing special characters needs testing!
  597.     */
  598.     function update($ldap = null)
  599.     {
  600.         if ($ldap{
  601.             $msg $this->setLDAP($ldap);
  602.             if (Net_LDAP::isError($msg)) {
  603.                 return PEAR::raiseError('You passed an invalid $ldap variable to update()');
  604.             }
  605.         }
  606.  
  607.         // ensure we have a valid LDAP object
  608.         $ldap =$this->getLDAP();
  609.         if (!is_a($ldap'Net_LDAP')) {
  610.             return PEAR::raiseError("The entries LDAP object is not valid");
  611.         }
  612.  
  613.         // Get and check link
  614.         $link $ldap->getLink();
  615.         if (!is_resource($link)) {
  616.             return PEAR::raiseError("Could not update entry: internal LDAP link is invalid");
  617.         }
  618.  
  619.         /*
  620.         * Delete the entry
  621.         */
  622.         if (true === $this->_delete{
  623.             return $ldap->delete($this);
  624.         }
  625.  
  626.         /*
  627.         * New entry
  628.         */
  629.         if (true === $this->_new{
  630.             $msg $ldap->add($this);
  631.             if (Net_LDAP::isError($msg)) {
  632.                 return $msg;
  633.             }
  634.             $this->_new                = false;
  635.             $this->_changes['add']     = array();
  636.             $this->_changes['delete']  = array();
  637.             $this->_changes['replace'= array();
  638.             $this->_original           $this->_attributes;
  639.  
  640.             $return = true;
  641.             return $return;
  642.         }
  643.  
  644.         /*
  645.         * Rename/move entry
  646.         */
  647.         if (false == is_null($this->_newdn)) {
  648.             if ($ldap->getLDAPVersion(!== 3{
  649.                 return PEAR::raiseError("Renaming/Moving an entry is only supported in LDAPv3");
  650.             }
  651.             // make dn relative to parent (needed for ldap rename)
  652.             $parent Net_LDAP_Util::ldap_explode_dn($this->_newdnarray('casefolding' => 'none''reverse' => false'onlyvalues' => false));
  653.             if (Net_LDAP::isError($parent)) {
  654.                 return $parent;
  655.             }
  656.             $child array_shift($parent);
  657.             // maybe the dn consist of a multivalued RDN, we must build the dn in this case
  658.             // because the $child-RDN is an array!
  659.             if (is_array($child)) {
  660.                 $child Net_LDAP_Util::canonical_dn($child);
  661.             }
  662.             $parent Net_LDAP_Util::canonical_dn($parent);
  663.  
  664.             // rename/move
  665.             if (false == @ldap_rename($link$this->_dn$child$parenttrue)) {
  666.                 return PEAR::raiseError("Entry not renamed: " .
  667.                                         @ldap_error($link)@ldap_errno($link));
  668.             }
  669.             // reflect changes to local copy
  670.             $this->_dn    $this->_newdn;
  671.             $this->_newdn = null;
  672.         }
  673.  
  674.         /*
  675.         * Carry out modifications to the entry
  676.         */
  677.         // ADD
  678.         foreach ($this->_changes["add"as $attr => $value{
  679.             // if attribute exists, add new values
  680.             if ($this->exists($attr)) {
  681.                 if (false === @ldap_mod_add($link$this->dn()array($attr => $value))) {
  682.                     return PEAR::raiseError("Could not add new values to attribute $attr" .
  683.                                             @ldap_error($link)@ldap_errno($link));
  684.                 }
  685.             else {
  686.                 // new attribute
  687.                 if (false === @ldap_modify($link$this->dn()array($attr => $value))) {
  688.                     return PEAR::raiseError("Could not add new attribute $attr" .
  689.                                             @ldap_error($link)@ldap_errno($link));
  690.                 }
  691.             }
  692.             // all went well here, I guess
  693.             unset($this->_changes["add"][$attr]);
  694.         }
  695.  
  696.         // DELETE
  697.         foreach ($this->_changes["delete"as $attr => $value{
  698.             // In LDAPv3 you need to specify the old values for deleting
  699.             if (is_null($value&& $ldap->getLDAPVersion(=== 3{
  700.                 $value $this->_original[$attr];
  701.             }
  702.             if (false === @ldap_mod_del($link$this->dn()array($attr => $value))) {
  703.                 return PEAR::raiseError("Could not delete attribute $attr" .
  704.                                         @ldap_error($link)@ldap_errno($link));
  705.             }
  706.             unset($this->_changes["delete"][$attr]);
  707.         }
  708.  
  709.         // REPLACE
  710.         foreach ($this->_changes["replace"as $attr => $value{
  711.             if (false === @ldap_modify($link$this->dn()array($attr => $value))) {
  712.                 return PEAR::raiseError("Could not replace attribute $attr values: " .
  713.                                         @ldap_error($link)@ldap_errno($link));
  714.             }
  715.             unset($this->_changes["replace"][$attr]);
  716.         }
  717.  
  718.         // all went well, so _original (server) becomes _attributes (local copy)
  719.         $this->_original $this->_attributes;
  720.  
  721.         $return = true;
  722.         return $return;
  723.     }
  724.  
  725.     /**
  726.     * Returns the right attribute name
  727.     *
  728.     * @param string $attr Name of attribute
  729.     *
  730.     * @access private
  731.     * @return string The right name of the attribute
  732.     */
  733.     function _getAttrName($attr)
  734.     {
  735.         $name strtolower($attr);
  736.         if (array_key_exists($name$this->_map)) {
  737.             $attr $this->_map[$name];
  738.         }
  739.         return $attr;
  740.     }
  741.  
  742.     /**
  743.     * Returns a reference to the LDAP-Object of this entry
  744.     *
  745.     * @access public
  746.     * @return Net_LDAP|Net_LDAP_Error  Reference to the Net_LDAP Object (the connection) or Net_LDAP_Error
  747.     */
  748.     function &getLDAP()
  749.     {
  750.         if (!is_a($this->_ldap'Net_LDAP')) {
  751.             $err = new PEAR_Error('LDAP is not a valid Net_LDAP object');
  752.             return $err;
  753.         else {
  754.             return $this->_ldap;
  755.         }
  756.     }
  757.  
  758.     /**
  759.     * Sets a reference to the LDAP-Object of this entry
  760.     *
  761.     * After setting a Net_LDAP object, calling update() will use that object for
  762.     * updating directory contents. Use this to dynamicly switch directorys.
  763.     *
  764.     * @param Net_LDAP &$ldap Net_LDAP object that this entry should be connected to
  765.     *
  766.     * @access public
  767.     * @return true|Net_LDAP_Error
  768.     */
  769.     function setLDAP(&$ldap)
  770.     {
  771.         if (!is_a($ldap'Net_LDAP')) {
  772.             return PEAR::raiseError("LDAP is not a valid Net_LDAP object");
  773.         else {
  774.             $this->_ldap =$ldap;
  775.             return true;
  776.         }
  777.     }
  778.  
  779.     /**
  780.     * Marks the entry as new.
  781.     *
  782.     * If an Entry is marked as new, it will be added to the directory when
  783.     * calling {@link update()}. This method is mainly intendet for internal
  784.     * Net_LDAP package usage, so if you use it, use it with care.
  785.     *
  786.     * @access private
  787.     * @param boolean $mark Value to set, defaults to "true"
  788.     */
  789.     function _markAsNew($mark = true)
  790.     {
  791.         $this->_new ($mark)? true : false;
  792.     }
  793.  
  794.     /**
  795.     * Applies a regular expression onto a single- or multivalued attribute (like preg_match())
  796.     *
  797.     * This method behaves like PHPs preg_match() but with some exceptions.
  798.     * If you want to retrieve match information, then you MUST pass the
  799.     * $matches parameter via reference! otherwise you will get no matches.
  800.     * Since it is possible to have multi valued attributes the $matches
  801.     * array will have a additionally numerical dimension (one for each value):
  802.     * <code>
  803.     * $matches = array(
  804.     *         0 => array (usual preg_match() returnarray),
  805.     *         1 => array (usual preg_match() returnarray)
  806.     *     )
  807.     * </code>
  808.     * Please note, that $matches will be initialized to an empty array inside.
  809.     *
  810.     * Usage example:
  811.     * <code>
  812.     * $result = $entry->preg_match('/089(\d+)/', 'telephoneNumber', &$matches);
  813.     * if ( $result === true ){
  814.     *     echo "First match: ".$matches[0][1];   // Match of value 1, content of first bracket
  815.     * } else {
  816.     *     if ( Net_LDAP::isError($result) ) {
  817.     *         echo "Error: ".$result->getMessage();
  818.     *     } else {
  819.     *         echo "No match found.";
  820.     *     }
  821.     * }
  822.     * </code>
  823.     *
  824.     * Please note that it is important to test for an Net_LDAP_Error, because objects are
  825.     * evaluating to true by default, thus if a error occured, and you only check using "==" then
  826.     * you get misleading results. Use the "identical" (===) operator to test for matches to
  827.     * avoid this as shown above.
  828.     *
  829.     * @param string $regex     The regular expression
  830.     * @param string $attr_name The attribute to search in
  831.     * @param array  $matches   (optional, PASS BY REFERENCE!) Array to store matches in
  832.     *
  833.     * @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
  834.     */
  835.     function preg_match($regex$attr_name$matches = array())
  836.     {
  837.         $matches = array();
  838.  
  839.         // fetch attribute values
  840.         $attr $this->getValue($attr_name'all');
  841.         if (Net_LDAP::isError($attr)) {
  842.             return $attr;
  843.         else {
  844.             unset($attr['count']);
  845.         }
  846.  
  847.         // perform preg_match() on all values
  848.         $match = false;
  849.         foreach ($attr as $thisvalue{
  850.             $matches_int = array();
  851.             if (preg_match($regex$thisvalue$matches_int)) {
  852.                 $match = true;
  853.                 array_push($matches$matches_int)// store matches in reference
  854.             }
  855.         }
  856.         return $match;
  857.     }
  858.  
  859.     /**
  860.     * Is this entry going to be deleted once update() is called?
  861.     *
  862.     * @return boolean 
  863.     */
  864.     function willBeDeleted()
  865.     {
  866.         return $this->_delete;
  867.     }
  868.  
  869.     /**
  870.     * Is this entry going to be moved once update() is called?
  871.     *
  872.     * @return boolean 
  873.     */
  874.     function willBeMoved()
  875.     {
  876.         return ($this->dn(!== $this->currentDN());
  877.     }
  878.  
  879.     /**
  880.     * Returns always the original DN
  881.     *
  882.     * If an entry will be moved but {@link update()} was not called,
  883.     * {@link dn()} will return the new DN. This method however, returns
  884.     * always the current active DN.
  885.     *
  886.     * @return string 
  887.     */
  888.     function currentDN()
  889.     {
  890.         return $this->_dn;
  891.     }
  892.  
  893.     /**
  894.     * Returns the attribute changes to be carried out once update() is called
  895.     *
  896.     * @return array 
  897.     */
  898.     function getChanges()
  899.     {
  900.         return $this->_changes;
  901.     }
  902. }
  903. ?>

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