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

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