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

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