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: Jan Wagner |
// +--------------------------------------------------------------------------+
// $Id: Entry.php,v 1.29 2007/01/10 07:38:02 beni Exp $
require_once("PEAR.php");
* Object representation of a directory entry
* This class represents a directory entry. You can add, delete, replace
* attributes and their values, rename the entry, delete the entry.
* @author Jan Wagner <wagner@netsols.de>
* @version $Revision: 1.29 $
* Entry ressource identifier
* LDAP ressource identifier
* This object will be used for updating and schema checking
* Distinguished name of the entry
var $_attributes = array ();
* Original attributes before any modification
var $_original = array ();
* Shall the entry be deleted?
* Map with changes to the entry
var $_changes = array ("add" => array (),
* Constructor of the entry. Sets up the distinguished name and the entries
* @param Net_LDAP|ressource|array$ldap Net_LDAP object, ldap-link ressource or array of attributes
* @param string|ressource $entry Either a DN or a LDAP-Entry
$this->PEAR ('Net_LDAP_Error');
if (is_a($ldap, 'Net_LDAP')) {
$this->_link = $ldap->getLink ();
$this->_setAttributes ($ldap);
$this->_dn = @ldap_get_dn ($this->_link, $this->_entry);
* Get or set the distinguished name of the entry
* If called without an argument the current (or the new DN if set) DN gets returned.
* If you provide an DN, this entry is moved to the new location specified if a DN existed.
* If the DN was not set, the DN gets initialized. Call {@link update()} to actually create
* the new Entry in the directory.
* Please note that special characters (eg german umlauts) should be encoded using utf8_encode().
* @param string $dn New distinguished name
* @return string|trueDistinguished name (or true if a new DN was provided)
return (isset ($this->_newdn) ? $this->_newdn : $this->_dn);
* Sets the internal attributes array
* This fetches the values for the attributes from the server.
* @param array $attributes
function _setAttributes ($attributes = null )
// fetch attributes from the server
if (is_a($this->_ldap, 'Net_LDAP')) {
$schema = $this->_ldap->schema();
$attr = @ldap_first_attribute ($this->_link, $this->_entry, $ber);
$attr = @ldap_next_attribute ($this->_link, $this->_entry, $ber);
$func = 'ldap_get_values'; // function to fetch value
if (is_a($schema, 'Net_LDAP_Schema')) {
// try to get binary values as binary data
$attr_s = $schema->get ('attribute', $attr);
$func = 'ldap_get_values_len';
// fetch attribute value (needs error checking?)
$attributes[$attr] = $func($this->_link, $this->_entry, $attr);
if (isset ($attributes["count"]) && is_numeric($attributes["count"])) {
unset ($attributes["count"]);
foreach ($attributes as $k => $v) {
// attribute names should not be numeric
// map generic attribute name to real one
// attribute values should be in an array
// remove the value count (comes from ldap server)
if (isset ($v["count"])) {
$this->_attributes[$k] = $v;
// save a copy for later use
$this->_original = $this->_attributes;
* Get the values of all attributes in a hash
* The returned hash has the form
* array('attributename' => 'single value',
* 'attributename' => array('value1', value2', value3'))
* @return array Hash of all attributes with their values
foreach ($this->_attributes as $attr => $value) {
* Get the value of a specific attribute
* The first parameter is the name of the attribute
* The second parameter influences the way the value is returned:
* 'single': only the first value is returned as string
* 'all': all values including the value count are returned in an
* 'default': in all other cases an attribute value with a single value is
* returned a string, if it has multiple values it is returned
* as an array (without value count)
* @param string $attr Attribute name
* @param string $option Option
* @return string|array|PEAR_Errorstring, array or PEAR_Error
function getValue($attr, $option = null )
$attr = $this->_getAttrName ($attr);
return PEAR ::raiseError (" Unknown attribute ($attr) requested" );
$value = $this->_attributes[$attr];
if ($option == "single" || (count($value) == 1 && $option != 'all')) {
* Alias function of getValue for perl-ldap interface
* Returns an array of attributes names
* @return array Array of attribute names
* Returns whether an attribute exists or not
* @param string $attr Attribute name
$attr = $this->_getAttrName ($attr);
* Adds a new attribute or a new value to an existing attribute
* The paramter has to be an array of the form:
* array('attributename' => 'single value',
* 'attributename' => array('value1', 'value2))
* When the attribute already exists the values will be added, else the
* attribute will be created. These changes are local to the entry and do
* not affect the entry on the server until update() is called.
* Note, that you can add values of attributes that you haven't selected, but if
* you do so, {@link getValue()} and {@link getValues()} will only return the
* values you added, _NOT_ all values present on the server. To avoid this, just refetch
* the entry after calling {@link update()} or select the attribute.
function add($attr = array ())
return PEAR ::raiseError ("Parameter must be an array");
foreach ($attr as $k => $v) {
$k = $this->_getAttrName ($k);
// Do not add empty values
// add new values to existing attribute
$this->_attributes[$k] = array_merge($this->_attributes[$k], $v);
$this->_attributes[$k] = $v;
// save changes for update()
if (empty ($this->_changes["add"][$k])) {
$this->_changes["add"][$k] = array ();
$this->_changes["add"][$k] = array_merge($this->_changes["add"][$k], $v);
* Deletes an attribute or a value
* The parameter can be one of the following:
* "attributename" - The attribute as a whole will be deleted
* array("attributename1", "attributename2) - All given attributes will be
* array("attributename" => "value") - The value will be deleted
* array("attributename" => array("value1", "value2") - The given values
* If $attr is null or omitted , then the whole Entry will be deleted!
* These changes are local to the entry and do
* not affect the entry on the server until {@link update()} is called.
* Please note that you must select the attribute (at $ldap->search() for example)
* to be able to delete values of it, Otherwise {@link update()} will silently fail
* @param string|array$attr
// Make the assumption that attribute names cannot be numeric,
// therefore this has to be a simple list of attribute names to delete
foreach ($attr as $name) {
$name = $this->_getAttrName ($name);
$this->_changes["delete"][$name] = null;
unset ($this->_attributes[$name]);
// Here we have a hash with "attributename" => "value to delete"
foreach ($attr as $name => $values) {
// get the correct attribute name
$name = $this->_getAttrName ($name);
$values = array ($values);
// save values to be deleted
if (empty ($this->_changes["delete"][$name])) {
$this->_changes["delete"][$name] = array ();
$this->_changes["delete"][$name] =
array_merge($this->_changes["delete"][$name], $values);
foreach ($values as $value) {
// find the key for the value that should be deleted
unset ($this->_attributes[$name][$key]);
* Replaces attributes or its values
* The parameter has to an array of the following form:
* array("attributename" => "single value",
* "attribute2name" => array("value1", "value2"))
* If the attribute does not yet exist it will be added instead.
* If the attribue value is null, the attribute will de deleted
* These changes are local to the entry and do
* not affect the entry on the server until {@link update()} is called.
return PEAR ::raiseError ("Parameter must be an array");
foreach ($attr as $k => $v) {
$k = $this->_getAttrName ($k);
// delete attributes with empty values
// existing attributes will get replaced
$this->_changes["replace"][$k] = $v;
$this->_attributes[$k] = $v;
// new ones just get added
$this->add(array ($k => $v));
* Update the entry on the directory server
* @param Net_LDAP $ldap (optional) If you provide a Net_LDAP object, be sure to PASS IT VIA REFERENCE!
if (!$ldap) { // If object is not provided, then use this entrys ldap object
if (!is_a($ldap, 'Net_LDAP')) {
return PEAR ::raiseError ("Need a Net_LDAP object as parameter");
$link = $ldap->getLink ();
if ($this->_delete === true ) {
return $ldap->delete ($this);
if ($this->_new === true ) {
$msg = $ldap->add ($this);
$this->_changes['add'] = array ();
$this->_changes['delete'] = array ();
$this->_changes['replace'] = array ();
$this->_original = $this->_attributes;
if (false == is_null($this->_newdn)) {
if ($ldap->getLDAPVersion () !== 3 ) {
return PEAR ::raiseError ("Renaming/Moving an entry is only supported in LDAPv3");
// make dn relative to parent (needed for ldap rename)
$parent = join(",", $parent);
if (false == @ldap_rename ($link, $this->_dn, $child, $parent, true )) {
return PEAR ::raiseError ("Entry not renamed: " .
@ldap_error ($link), @ldap_errno ($link));
// reflect changes to local copy
$this->_dn = $this->_newdn;
foreach ($this->_changes["add"] as $attr => $value) {
// if attribute exists, add new values
if (false === @ldap_mod_add ($link, $this->dn(), array ($attr => $value))) {
return PEAR ::raiseError (" Could not add new values to attribute $attr: " .
@ldap_error ($link), @ldap_errno ($link));
if (false === @ldap_modify ($link, $this->dn(), array ($attr => $value))) {
return PEAR ::raiseError (" Could not add new attribute $attr: " .
@ldap_error ($link), @ldap_errno ($link));
// all went well here, I guess
unset ($this->_changes["add"][$attr]);
foreach ($this->_changes["delete"] as $attr => $value) {
// In LDAPv3 you need to specify the old values for deleting
if (is_null($value) && $ldap->getLDAPVersion () === 3 ) {
$value = $this->_original[$attr];
if (false === @ldap_mod_del ($link, $this->dn(), array ($attr => $value))) {
return PEAR ::raiseError (" Could not delete attribute $attr: " .
@ldap_error ($link), @ldap_errno ($link));
unset ($this->_changes["delete"][$attr]);
foreach ($this->_changes["replace"] as $attr => $value) {
if (false === @ldap_modify ($link, $this->dn(), array ($attr => $value))) {
return PEAR ::raiseError (" Could not replace attribute $attr values: " .
@ldap_error ($link), @ldap_errno ($link));
unset ($this->_changes["replace"][$attr]);
// all went well, so _original (server) becomes _attributes (local copy)
$this->_original = $this->_attributes;
* Returns the right attribute name
* @param string $attr Name of attribute
* @return string The right name of the attribute
function _getAttrName ($attr)
$attr = $this->_map[$name];
* Copy the current entry to another place in the directory
* @param Net_LDAP $ldap Net_LDAP
* @param string $dn New distinguished name
* @param boolean $relative Is the new name relative to current parent
* @return Net_LDAP_Entry|Net_LDAP_Error Reference to the copied Net_LDAP_Entry or Net_LDAP_Error
function ©(&$ldap, $dn, $relative = false )
$dn = " $dn," . $this->dn();
// get the attribute which makes up the rdn
$parts = @ldap_explode_dn ($this->dn(), 0 );
list ($attr, $value) = explode('=', $parts[0 ]);
// remove it from the entry (not valid in copy)
$old_e = $this; // backup
$old_e->delete (array ($attr => $value));
// get the attribute which makes up the new rdn
$parts = @ldap_explode_dn ($dn, 0 );
list ($attr, $value) = explode('=', $parts[0 ]);
$old_e->add (array ($attr => $value));
$entry->add ($old_e->getValues ());
$msg = $entry->update ($ldap);
* Returns a reference to the LDAP-Object of this entry
* @return Net_LDAP|Net_LDAP_Error Reference to the Net_LDAP Object (the connection) or Net_LDAP_Error
if (!is_a($this->_ldap, 'Net_LDAP')) {
return PEAR ::raiseError ("LDAP is not a valid Net_LDAP object");
* Wrapper function for PHPs ldap_explode_dn()
* PHPs ldap_explode_dn() does not escape DNs so it will fail
* if the parameter $dn is something like <kbd>"<foobar>"</kbd> or contains
* This method ensures, that the DN is properly escaped and encoded.
* It is taken from http://php.net/ldap_explode_dn and slightly modified.
* @author DavidSmith@byu.net
* @param string $dn The DN that should be split
$result = ldap_explode_dn ( $dn, $only_values );
if (isset ($result["count"])) {
//translate hex code into ascii again
foreach( $result as $key => $value )
$result[$key] = preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $value);
Documentation generated on Mon, 11 Mar 2019 14:56:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|