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

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