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. // | Net_LDAP                                                                 |
  5. // +--------------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                    |
  7. // +--------------------------------------------------------------------------+
  8. // | This library is free software; you can redistribute it and/or            |
  9. // | modify it under the terms of the GNU Lesser General Public               |
  10. // | License as published by the Free Software Foundation; either             |
  11. // | version 2.1 of the License, or (at your option) any later version.       |
  12. // |                                                                          |
  13. // | This library is distributed in the hope that it will be useful,          |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        |
  16. // | Lesser General Public License for more details.                          |
  17. // |                                                                          |
  18. // | You should have received a copy of the GNU Lesser General Public         |
  19. // | License along with this library; if not, write to the Free Software      |
  20. // | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA |
  21. // +--------------------------------------------------------------------------+
  22. // | Authors: Tarjej Huse                                                     |
  23. // +--------------------------------------------------------------------------+
  24. //
  25. // $Id: Entry.php,v 1.15.2.2 2004/03/09 10:13:26 jw Exp $
  26.  
  27. /**
  28.  * This class represents an LDAP entry
  29.  *
  30.  * @package Net_LDAP
  31.  * @author Tarjei Huse
  32.  * @version $Revision: 1.15.2.2 $
  33.  */
  34. class Net_LDAP_Entry extends PEAR
  35. {
  36.     /**#@+
  37.      * Array of the attributes
  38.      *
  39.      * @access private
  40.      * @var array
  41.      */
  42.     var $_attrs = array();
  43.     
  44.     /**
  45.      * Array of attributes to be deleted upon update()
  46.      */    
  47.     var $_delAttrs = array();
  48.  
  49.     /**
  50.      * Array of attributes to be modified upon update()
  51.      */    
  52.     var $_modAttrs = array();
  53.  
  54.     /**
  55.      * Array of attributes to be added upon update()
  56.      */        
  57.     var $_addAttrs = array();
  58.     /**#@-*/
  59.     
  60.     /**
  61.      * The distinguished name of the entry
  62.      * 
  63.      * @access private
  64.      * @var string 
  65.      */
  66.     var $_dn '';
  67.     
  68.     /**
  69.      * LDAP resource link
  70.      * 
  71.      * @access private
  72.      * @var resource 
  73.      */
  74.     var $_link = null;
  75.     
  76.     /**
  77.      * Value of old DN if DN has changed
  78.      *
  79.      * @access private
  80.      * @var string 
  81.      */
  82.     var $_olddn '';
  83.  
  84.     /**#@+
  85.      * Array of errors for debugging class
  86.      *
  87.      * @access private
  88.      */
  89.     var $_error = array();
  90.  
  91.     /**
  92.      * updatechecks
  93.      */
  94.     var $updateCheck = array('newdn'    => false,
  95.                              'modify'   => false,
  96.                              'newEntry' => true
  97.                              )// since the entry is not changed before the update();
  98.  
  99.     /**
  100.      * Net_LDAP_Schema object TO BE REMOVED
  101.      */                             
  102.     var $_schema;
  103.     /**#@-*/
  104.     
  105.     /** Constructor
  106.      *
  107.      * @param link - ldap_resource_link, dn = string entry dn, attributes - array entry attributes array.
  108.      * @return none
  109.      ***/
  110.     function Net_LDAP_Entry($link = null$dn = null$attributes = null)
  111.     {
  112.         if (!is_null($link)) {
  113.             $this->_link $link;
  114.         }
  115.         if (!is_null($dn)) {
  116.             $this->_set_dn($dn);
  117.         }
  118.         if (is_array($attributes&& count($attributes> 0{
  119.             $this->_set_attributes($attributes);
  120.         else {
  121.             $this->updateCheck['newEntry'= true;
  122.         }
  123.     }
  124.     
  125.     /** 
  126.      * Set the reasourcelink to the ldapserver.
  127.      *
  128.      * @access private
  129.      * @param resource LDAP link
  130.      */
  131.     function _set_link(&$link
  132.     {
  133.         $this->_link $link;
  134.     }
  135.     
  136.     /**
  137.      * set the entrys DN
  138.      *
  139.      * @access private
  140.      * @param string 
  141.      */
  142.     function _set_dn($dn)
  143.     {
  144.         $this->_dn $dn;
  145.     }
  146.  
  147.     /**
  148.      * sets the internal array of the entrys attributes.
  149.      *
  150.      * @access private
  151.      * @param array 
  152.      */
  153.     function _set_attributes($attributes= array())
  154.     {
  155.         $this->_attrs $attributes;
  156.         // this is the sign that the entry exists in the first place: 
  157.         $this->updateCheck['newEntry'= false;
  158.     }
  159.  
  160.    /** 
  161.     * removes [count] entries from the array.
  162.     * 
  163.     * remove all the count elements in the array:
  164.     * Used before ldap_modify, ldap_add
  165.     * 
  166.     * @access private
  167.     * @return array Cleaned array of attributes
  168.     */
  169.     function _clean_entry()
  170.     {
  171.         $attributes = array();
  172.         
  173.         for ($i=0; $i $this->_attrs['count'$i++{
  174.         
  175.             $attr $this->_attrs[$i];
  176.         
  177.             if ($this->_attrs[$attr]['count'== 1{
  178.                 $attributes[$this->_attrs[$i]] $this->_attrs[$attr][0];
  179.             else {
  180.                 $attributes[$attr$this->_attrs[$attr];
  181.                 unset ($attributes$attr ]['count']);
  182.             }
  183.         }
  184.          
  185.         return $attributes;
  186.  
  187.     }
  188.  
  189.    /**
  190.     * returns an assosiative array of all the attributes in the array
  191.     *
  192.     * attributes -  returns an assosiative array of all the attributes in the array
  193.     * of the form array ('attributename'=>'singelvalue' , 'attribute'=>array('multiple','values'))
  194.     *
  195.     * @param none 
  196.     * @return array Array of attributes and values.
  197.     */
  198.     function attributes()
  199.     {
  200.         return $this->_clean_entry();
  201.     }
  202.  
  203.    /**
  204.     * Add one or more attribute to the entry
  205.     *
  206.     * The values given will be added to the values which already exist for the given attributes.
  207.     * usage:
  208.     * $entry->add ( array('sn'=>'huse',objectclass=>array(top,posixAccount)))
  209.     *
  210.     * @param array Array of attributes
  211.     * @return mixed Net_Ldap_Error if error, else true.
  212.     */
  213.     function add($attr = array())
  214.     {
  215.         if (!isset($this->_attrs['count'])) {
  216.             $this->_attrs['count'= 0;
  217.         }
  218.         if (!is_array($attr)) {
  219.             return $this->raiseError("Net_LDAP::add : the parameter supplied is not an array, $attr"1000);   
  220.         }
  221.         /* if you passed an empty array, that is your problem! */
  222.         if (count ($attr)==0{
  223.             return true;        
  224.         }
  225.         foreach ($attr as $k => $v {
  226.             // empty entrys should not be added to the entry.
  227.             if ($v == ''{
  228.                 continue;
  229.             }
  230.  
  231.             if ($this->exists($k)) {
  232.                 if (!is_array($this->_attrs[$k])) {
  233.                     return $this->raiseError("Possible malformed array as parameter to Net_LDAP::add().");
  234.                 }
  235.                 array_push($this->_attrs[$k],$v);
  236.                 $this->_attrs[$k]['count']++;
  237.             else {
  238.                 $this->_attrs[$k][0$v;
  239.                 $this->_attrs[$k]['count'= 1;
  240.                 $this->_attrs[$this->_attrs['count']] $k;
  241.                 $this->_attrs['count']++;
  242.             }
  243.             // Fix for bug #952
  244.             if (empty($this->_addAttrs[$k])) {
  245.                 $this->_addAttrs[$k= array();
  246.             }
  247.             if (false == is_array($v)) {
  248.                 $v = array($v);
  249.             }
  250.             foreach ($v as $value{
  251.                 array_push($this->_addAttrs[$k]$value);
  252.             }
  253.         }
  254.         return true;
  255.     }
  256.  
  257.    /**
  258.     * Set or get the DN for the object
  259.     *
  260.     * If a new dn is supplied, this will move the object when running $obj->update();
  261.     *
  262.     * @param string DN
  263.     */
  264.     function dn($newdn '')
  265.     {
  266.         if ($newdn == ''{
  267.             return $this->_dn;
  268.         }
  269.       
  270.         $this->_olddn $this->_dn;
  271.         $this->_dn $newdn;
  272.         $this->updateCheck['newdn'= true;
  273.     }
  274.    
  275.    /**
  276.     * check if a certain attribute exists in the directory
  277.     *
  278.     * @param string attribute name.
  279.     * @return boolean 
  280.     */
  281.     function exists($attr)
  282.     {
  283.         if (array_key_exists($attr$this->_attrs)) {
  284.             return true;
  285.         }   
  286.         return false;
  287.     }
  288.  
  289.    /**
  290.     * get_value get the values for a attribute
  291.     *
  292.     * returns either an array or a string
  293.     * possible values for option:
  294.     *           alloptions - returns an array with the values + a countfield.
  295.     *                       i.e.: array (count=>1, 'sn'=>'huse');
  296.     *           single - returns the, first value in the array as a string.
  297.     *
  298.     * @param $attr string attribute name
  299.     * @param $options array
  300.     */
  301.     function get_value($attr ''$options '')
  302.     {
  303.         if (array_key_exists($attr$this->_attrs)) {
  304.  
  305.             if ($options == 'single'{
  306.                 if (is_array($this->_attrs[$attr])) {
  307.                     return $this->_attrs[$attr][0];
  308.                 else {
  309.                     return $this->_attrs[$attr];
  310.                 }
  311.             }
  312.  
  313.             $value $this->_attrs[$attr];
  314.             
  315.             if (!$options == 'alloptions'{
  316.                 unset ($value['count']);
  317.             }
  318.             return $value;            
  319.         else {
  320.             return '';
  321.         }
  322.     }
  323.  
  324.     /**
  325.      * add/delete/modify attributes
  326.      *
  327.      * this function tries to do all the things that replace(),delete() and add() does on an object.
  328.      * Syntax:
  329.      * array ( 'attribute' => newval, 'delattribute' => '', newattrivute => newval);
  330.      * Note: You cannot use this function to modify parts of an attribute. You must modify the whole attribute.
  331.      * You may call the function many times before running $entry->update();
  332.      *
  333.      * @param array attributes to be modified
  334.      * @return mixed errorObject if failure, true if success.
  335.      */
  336.     function modify($attrs = array()) {
  337.     
  338.         if (!is_array($attrs|| count ($attrs< 1 {
  339.             return $this->raiseError("You did not supply an array as expected",1000);
  340.         }
  341.  
  342.         foreach ($attrs as $k => $v{
  343.             // empty values are deleted (ldap v3 handling is in update() )            
  344.             if ($v == '' && $this->exists($k)) {
  345.                 $this->_delAttrs[$k'';
  346.                 continue;
  347.             }
  348.             /* existing attributes are modified*/
  349.             if ($this->exists($k) ) {
  350.                 if (is_array($v)) {
  351.                      $this->_modAttrs[$k$v;
  352.                 else {
  353.                     $this->_modAttrs[$k][0$v;
  354.                 
  355.             else {
  356.                 /* new ones are created  */
  357.                 if (is_array($v) ) {
  358.                     // an empty array is deleted...
  359.                     if (count($v== 0 {
  360.                         $this->_delAttrs[$k'';
  361.                     else {
  362.                         $this->_addAttrs[$k$v;
  363.                     }
  364.                 else {
  365.                     // dont't add empty attributes
  366.                     if ($v != null$this->_addAttrs[$k][0$v;
  367.                 }
  368.             }        
  369.         }
  370.         return true;
  371.     }
  372.  
  373.     
  374.    /**
  375.     * replace a certain attributes value
  376.     *
  377.     * replace - replace a certain attributes value
  378.     * example:
  379.     * $entry->replace(array('uid'=>array('tarjei')));
  380.     *
  381.     * @param array attributes to be replaced
  382.     * @return mixed error if failure, true if sucess.
  383.     */
  384.     function replace($attrs = array() )
  385.     {
  386.         foreach ($attrs as $k => $v{
  387.            
  388.             if ($this->exists($k)) {
  389.                 
  390.                 if (is_array($v)) {
  391.                     $this->_attrs[$k$v;
  392.                     $this->_attrs[$k]['count'count($v);
  393.                     $this->_modAttrs[$k$v;
  394.                 else {
  395.                     $this->_attrs[$k]['count'= 1;
  396.                     $this->_attrs[$k][0$v;
  397.                     $this->_modAttrs[$k][0$v;
  398.                 }
  399.             else {
  400.                 return $this->raiseError("Attribute $k does not exist",16)// 16 = no such attribute exists.
  401.             }
  402.         }
  403.         return true;
  404.     }
  405.  
  406.    /** 
  407.     * delete attributes
  408.     *
  409.     * Use this function to delete certain attributes from an object.
  410.     *
  411.     * @param array of attributes to be deleted
  412.     * @return mixed Net_Ldap_Error if failure, true if success.
  413.     */
  414.     function delete($attrs = array())
  415.     {
  416.         foreach ($attrs as $k => $v{
  417.             
  418.             if ($this->exists ($k)) {
  419.                 // if v is a null, then remove the whole attribute, else only the value.
  420.                 if ($v == ''{
  421.                     unset($this->_attrs[$k]);
  422.                     $this->_delAttrs[$k"";                    
  423.                 // else we remove only the correct value.
  424.                 else {                
  425.                     for ($i = 0;$i$this->_attrs[$k]['count'];$i++{
  426.                         if ($this->_attrs[$k][$i== $v {
  427.                             unset ($this->_attrs[$k][$i]);
  428.                             $this->_delAttrs[$k$v;
  429.                             continue;
  430.                         }
  431.                     }                    
  432.                 }                
  433.             else {
  434.                 $this->raiseError("You tried to delete a nonexisting attribute!",16);
  435.             }
  436.         }        
  437.         return true;
  438.     }
  439.  
  440.    /**
  441.     * update the Entry in LDAP
  442.     *
  443.     * After modifying an object, you must run update() to
  444.     * make the updates on the ldap server. Before that, they only exists in the object.
  445.     *
  446.     * @param object Net_LDAP 
  447.     * @return mixed Net_LDAP_Error object on failure or true on success
  448.     */
  449.     function update ($ldapObject = null)
  450.     {
  451.         if ($ldapObject == null && $this->_link == null {
  452.             $this->raiseError("No link to database");
  453.         }
  454.  
  455.         if ($ldapObject != null{
  456.             $this->_link =$ldapObject->_link;
  457.         }
  458.  
  459.         //if it's a new 
  460.         if ($this->updateCheck['newdn'&& !$this->updateCheck['newEntry']{
  461.             if (@ldap_get_option$this->_linkLDAP_OPT_PROTOCOL_VERSION$version&& $version != 3{
  462.                 return $this->raiseError("Moving or renaming an dn is only supported in LDAP V3!"80);
  463.             }
  464.  
  465.             $newparent = ldap_explode_dn($this->_dn0);
  466.             unset($newparent['count']);
  467.             $relativeDn array_shift($newparent);            
  468.             $newparent join(','$newparent);
  469.             
  470.             if (!@ldap_rename($this->_link$this->_olddn$relativeDn$newparenttrue)) {
  471.                  return $this->raiseError("DN not renamed: " . ldap_error($this->_link)ldap_errno($this->_link));
  472.             }
  473.         }
  474.  
  475.         if ($this->updateCheck['newEntry']{
  476.            //print "<br>"; print_r($this->_clean_entry());
  477.  
  478.             if (!@ldap_add($this->_link$this->dn()$this->_clean_entry()) ) {
  479.                   return $this->raiseError("Entry" $this->dn(" not added!" 
  480.                          ldap_error($this->_link)ldap_errno($this->_link));
  481.             else {
  482.                 return true;
  483.             }
  484.         // update existing entry
  485.         else {
  486.             $this->_error['first'$this->_modAttrs;
  487.             $this->_error['count'count($this->_modAttrs)
  488.             
  489.             // modified attributes
  490.             if (( count($this->_modAttrs)>0&&
  491.                   !ldap_modify($this->_link$this->dn()$this->_modAttrs))
  492.             {
  493.                 return $this->raiseError("Entry " $this->dn(" not modified(attribs not modified): " .
  494.                                          ldap_error($this->_link),ldap_errno($this->_link));
  495.             }
  496.             
  497.             // attributes to be deleted
  498.             if (( count($this->_delAttrs> 0 ))
  499.             {
  500.                 // in ldap v3 we need to supply the old attribute values for deleting
  501.                 if (@ldap_get_option$this->_linkLDAP_OPT_PROTOCOL_VERSION$version&& $version == 3{
  502.                     foreach $this->_delAttrs as $k => $v {
  503.                         if $v == '' && $this->exists($k) ) {
  504.                             $this->_delAttrs[$k$this->get_value$k );
  505.                         }
  506.                     }
  507.                 }
  508.                 if !ldap_mod_del($this->_link$this->dn()$this->_delAttrs) ) {
  509.                     return $this->raiseError("Entry " $this->dn(" not modified (attributes not deleted): " .
  510.                                              ldap_error($this->_link),ldap_errno($this->_link));
  511.                 }
  512.             }
  513.             
  514.             // new attributes
  515.             if ((count($this->_addAttrs)) > 0 && !ldap_modify($this->_link$this->dn()$this->_addAttrs)) {
  516.                 return $this->raiseError"Entry " $this->dn(" not modified (attributes not added): " .
  517.                                           ldap_error($this->_link),ldap_errno($this->_link));
  518.             }                        
  519.             return true;
  520.         }
  521.     }
  522. }
  523.  
  524. ?>

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