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

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