Source for file Entry.php
Documentation is available at Entry.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +--------------------------------------------------------------------------+
// +--------------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +--------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or |
// | modify it under the terms of the GNU Lesser General Public |
// | License as published by the Free Software Foundation; either |
// | version 2.1 of the License, or (at your option) any later version. |
// | This library is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
// | Lesser General Public License for more details. |
// | You should have received a copy of the GNU Lesser General Public |
// | License along with this library; if not, write to the Free Software |
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
// +--------------------------------------------------------------------------+
// | Authors: Tarjej Huse |
// +--------------------------------------------------------------------------+
// $Id: Entry.php,v 1.15.2.2 2004/03/09 10:13:26 jw Exp $
* This class represents an LDAP entry
* @version $Revision: 1.15.2.2 $
* Array of the attributes
* Array of attributes to be deleted upon update()
var $_delAttrs = array ();
* Array of attributes to be modified upon update()
var $_modAttrs = array ();
* Array of attributes to be added upon update()
var $_addAttrs = array ();
* The distinguished name of the entry
* Value of old DN if DN has changed
* Array of errors for debugging class
var $updateCheck = array ('newdn' => false ,
); // since the entry is not changed before the update();
* Net_LDAP_Schema object TO BE REMOVED
* @param - link - ldap_resource_link, dn = string entry dn, attributes - array entry attributes array.
$this->_set_attributes ($attributes);
$this->updateCheck['newEntry'] = true;
* Set the reasourcelink to the ldapserver.
* @param resource LDAP link
function _set_link (&$link)
* sets the internal array of the entrys attributes.
function _set_attributes ($attributes= array ())
$this->_attrs = $attributes;
// this is the sign that the entry exists in the first place:
$this->updateCheck['newEntry'] = false;
* removes [count] entries from the array.
* remove all the count elements in the array:
* Used before ldap_modify, ldap_add
* @return array Cleaned array of attributes
for ($i=0; $i < $this->_attrs['count'] ; $i++ ) {
$attr = $this->_attrs[$i];
if ($this->_attrs[$attr]['count'] == 1 ) {
$attributes[$this->_attrs[$i]] = $this->_attrs[$attr][0 ];
$attributes[$attr] = $this->_attrs[$attr];
unset ($attributes[ $attr ]['count']);
* returns an assosiative array of all the attributes in the array
* attributes - returns an assosiative array of all the attributes in the array
* of the form array ('attributename'=>'singelvalue' , 'attribute'=>array('multiple','values'))
* @return array Array of attributes and values.
return $this->_clean_entry ();
* Add one or more attribute to the entry
* The values given will be added to the values which already exist for the given attributes.
* $entry->add ( array('sn'=>'huse',objectclass=>array(top,posixAccount)))
* @param array Array of attributes
* @return mixed Net_Ldap_Error if error, else true.
function add($attr = array ())
if (!isset ($this->_attrs['count'])) {
$this->_attrs['count'] = 0;
return $this->raiseError (" Net_LDAP::add : the parameter supplied is not an array, $attr" , 1000 );
/* if you passed an empty array, that is your problem! */
foreach ($attr as $k => $v ) {
// empty entrys should not be added to the entry.
return $this->raiseError ("Possible malformed array as parameter to Net_LDAP::add().");
$this->_attrs[$k]['count']++;
$this->_attrs[$k][0 ] = $v;
$this->_attrs[$k]['count'] = 1;
$this->_attrs[$this->_attrs['count']] = $k;
$this->_attrs['count']++;
if (empty ($this->_addAttrs[$k])) {
$this->_addAttrs[$k] = array ();
* Set or get the DN for the object
* If a new dn is supplied, this will move the object when running $obj->update();
$this->_olddn = $this->_dn;
$this->updateCheck['newdn'] = true;
* check if a certain attribute exists in the directory
* @param string attribute name.
* get_value get the values for a attribute
* returns either an array or a string
* possible values for option:
* alloptions - returns an array with the values + a countfield.
* i.e.: array (count=>1, 'sn'=>'huse');
* single - returns the, first value in the array as a string.
* @param $attr string attribute name
function get_value($attr = '', $options = '')
if ($options == 'single') {
return $this->_attrs[$attr][0 ];
return $this->_attrs[$attr];
$value = $this->_attrs[$attr];
if (!$options == 'alloptions') {
* add/delete/modify attributes
* this function tries to do all the things that replace(),delete() and add() does on an object.
* array ( 'attribute' => newval, 'delattribute' => '', newattrivute => newval);
* Note: You cannot use this function to modify parts of an attribute. You must modify the whole attribute.
* You may call the function many times before running $entry->update();
* @param array attributes to be modified
* @return mixed errorObject if failure, true if success.
function modify($attrs = array ()) {
return $this->raiseError ("You did not supply an array as expected",1000 );
foreach ($attrs as $k => $v) {
// empty values are deleted (ldap v3 handling is in update() )
if ($v == '' && $this->exists($k)) {
$this->_delAttrs[$k] = '';
/* existing attributes are modified*/
$this->_modAttrs[$k] = $v;
$this->_modAttrs[$k][0 ] = $v;
/* new ones are created */
// an empty array is deleted...
$this->_delAttrs[$k] = '';
$this->_addAttrs[$k] = $v;
// dont't add empty attributes
if ($v != null ) $this->_addAttrs[$k][0 ] = $v;
* replace a certain attributes value
* replace - replace a certain attributes value
* $entry->replace(array('uid'=>array('tarjei')));
* @param array attributes to be replaced
* @return mixed error if failure, true if sucess.
function replace($attrs = array () )
foreach ($attrs as $k => $v) {
$this->_attrs[$k]['count'] = count($v);
$this->_modAttrs[$k] = $v;
$this->_attrs[$k]['count'] = 1;
$this->_attrs[$k][0 ] = $v;
$this->_modAttrs[$k][0 ] = $v;
return $this->raiseError (" Attribute $k does not exist" ,16 ); // 16 = no such attribute exists.
* Use this function to delete certain attributes from an object.
* @param - array of attributes to be deleted
* @return mixed Net_Ldap_Error if failure, true if success.
function delete($attrs = array ())
foreach ($attrs as $k => $v) {
// if v is a null, then remove the whole attribute, else only the value.
unset ($this->_attrs[$k]);
$this->_delAttrs[$k] = "";
// else we remove only the correct value.
for ($i = 0; $i< $this->_attrs[$k]['count']; $i++ ) {
if ($this->_attrs[$k][$i] == $v ) {
unset ($this->_attrs[$k][$i]);
$this->_delAttrs[$k] = $v;
$this->raiseError ("You tried to delete a nonexisting attribute!",16 );
* update the Entry in LDAP
* After modifying an object, you must run update() to
* make the updates on the ldap server. Before that, they only exists in the object.
* @return mixed Net_LDAP_Error object on failure or true on success
function update ($ldapObject = null )
if ($ldapObject == null && $this->_link == null ) {
$this->raiseError ("No link to database");
if ($ldapObject != null ) {
$this->_link = & $ldapObject->_link;
if ($this->updateCheck['newdn'] && !$this->updateCheck['newEntry']) {
if (@ldap_get_option ( $this->_link, LDAP_OPT_PROTOCOL_VERSION , $version) && $version != 3 ) {
return $this->raiseError ("Moving or renaming an dn is only supported in LDAP V3!", 80 );
$newparent = ldap_explode_dn ($this->_dn, 0 );
unset ($newparent['count']);
$newparent = join(',', $newparent);
if (!@ldap_rename ($this->_link, $this->_olddn, $relativeDn, $newparent, true )) {
return $this->raiseError ("DN not renamed: " . ldap_error ($this->_link), ldap_errno ($this->_link));
if ($this->updateCheck['newEntry']) {
//print "<br>"; print_r($this->_clean_entry());
if (!@ldap_add ($this->_link, $this->dn(), $this->_clean_entry ()) ) {
return $this->raiseError ("Entry" . $this->dn() . " not added!" .
ldap_error ($this->_link), ldap_errno ($this->_link));
$this->_error['first'] = $this->_modAttrs;
$this->_error['count'] = count($this->_modAttrs);
if (( count($this->_modAttrs)>0 ) &&
!ldap_modify ($this->_link, $this->dn(), $this->_modAttrs))
return $this->raiseError ("Entry " . $this->dn() . " not modified(attribs not modified): " .
ldap_error ($this->_link),ldap_errno ($this->_link));
// attributes to be deleted
if (( count($this->_delAttrs) > 0 ))
// in ldap v3 we need to supply the old attribute values for deleting
if (@ldap_get_option ( $this->_link, LDAP_OPT_PROTOCOL_VERSION , $version) && $version == 3 ) {
foreach ( $this->_delAttrs as $k => $v ) {
if ( $v == '' && $this->exists($k) ) {
$this->_delAttrs[$k] = $this->get_value( $k );
if ( !ldap_mod_del ($this->_link, $this->dn(), $this->_delAttrs) ) {
return $this->raiseError ("Entry " . $this->dn() . " not modified (attributes not deleted): " .
ldap_error ($this->_link),ldap_errno ($this->_link));
if ((count($this->_addAttrs)) > 0 && !ldap_modify ($this->_link, $this->dn(), $this->_addAttrs)) {
return $this->raiseError ( "Entry " . $this->dn() . " not modified (attributes not added): " .
ldap_error ($this->_link),ldap_errno ($this->_link));
Documentation generated on Mon, 11 Mar 2019 14:26:18 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|