Location: PHPKode > projects > LDAP Navigator > ldapnavigator-0.9.6/lib/Net/LDAP/Entry.php
<?php
/*

   Net_LDAP_Entry class
	
DESCRIPTION 

   description_here
	
SYNOPSIS

	$entry = new Net_LDAP_Entry();
	
	$entry->setDN( "cn=Leo West, ou=People, o=Acme.com" );
	$entry->addValue( "cn", "Leo West" );
	$entry->addValue( "telephonenumber", "4 733 429 04" );
	$entry->addValue( "telephonenumber", "1 234 040 33" );
	$entry->update( $ds); // update the entry in the directory
	
	// to manipulate entry for a directory
	$es = ldap_read( $ds, $sr );
	$hash = ldap_get_attributes( $ds,$es );
	$entry = new Net_LDAP_Entry( $hash );
	$entry->addValue( "o", "ACME" );
	
	
	
CHANGES

@TODO
	see charset	problems (utf-iso)
	
@VERSION   0.3
	    created : jeu,  7 jun 2001 10:04:08 GMT
  lastModified : ven, 31 aoĆ» 2001 08:59:52 GMT
        author : Leo West <west_leo AT yahoo DOT com>
		  

*/

class Net_LDAP_Entry
{	
	/********** @scope private variables */
	/* entry DN @type string */
	var $dn = NULL;
	/* entry's attributes @type hash of type 'attributeName' => array(values) */
	var $attributes = array();
	/* used to keep entry status for partial update */
	var $changes_del;
	var $changes_mod;
	var $changes_add;


	/* Net_LDAP_Entry
	 * initialize a new Entry instance
	 */
	function Net_LDAP_Entry() 
	{
		
	}
	
	function validDN( $dn )
	{
		return preg_match( '/^\S+=.+ *(, *\S+=.+)*$/U', $dn );
	}
		
	/**
	 * populate
	 * initialize the entry with a set of attributes, coming from the directory
	 * Note: the entry internal status is initialized to "no changes"
	 * @param $attributes an array of attributes as returned by ldap_get_attributes()
	 * @scope ?
	 * @return boolean always true for now
	 *
	 */
	function populate( &$attributes ) 
	{
		// reset the modification informations
		$this->changes_del = array();
		$this->changes_mod = array();
		$this->changes_add = array();

		// feed our entry with attribute in the array
		for( $i=0; $i< $attributes['count']; $i++ ) {
			$attributeName = $attributes[$i];
			unset( $attributes[$attributeName]['count'] );
			$this->attributes[$attributeName] = $attributes[$attributeName];
		}
		return true;
	}

	/**
	 * load attributes from a LDAP entry identifier
	 * method used by Client::Search() and Client::Next()
	 * @param $cnx ldap connection identifier
	 * @param $eid ldap result entry identifier
	 * @return boolean true if load is ok, false otherwise
	 */	
	function load( $cnx, $eid )
	{
//		echo "Entry:load( $cnx, $eid )\n";
		$attributes = ldap_get_attributes( $cnx, $eid );
		if( $attributes === false ) {
			return false;
		}
		$this->setDN( ldap_get_dn( $cnx, $eid ) );
		return $this->populate( $attributes );
	}
	
	/**
	 * setDN
	 * define the DN of the entry . Warning: no checks are done
	 * @param $dn string the distinguished name
	 * @return void
	 * @access public
	 */
	function setDN( $dn ) 
	{
		if( ! $this->validDN( $dn) ) {
			return false;
		} else {
			$this->dn = $dn;
			return true;
		}
	}
		

	/**
	 * getDN
	 *
	 * @return string the entry DN or NULL
	 */
	function getDN() 
	{
		return $this->dn;
	}


	/**
	 * getValues
	 *  return an array of the values for an attribute
	 *  if the attribute doesnt exists, return an empty array (not false)
	 *
	 * @param $attribute string the attribute name
	 *
	 * @return array the values of the attribute
	 */
	function getValues( $attribute ) 
	{
		if( isset( $this->attributes[$attribute]) )
			return $this->attributes[$attribute];
		else
			return array();
	}


	/**
	 *  return true if the attribute contains the specified value. 
	 *  @Example
	 *  if( $entry->hasValue( "objectclass", "person" ) )
	 *		echo "The entry is a person";
	 *
	 * @param $attribute string the attribute name
 	 * @param $value string the value to search for
	 * @return boolean true if the value was found, false otherwise
	 */
	function hasValue( $attribute, $value ) 
	{
		if( ! isset($this->attributes[$attribute]))
			return false;
			
		foreach( $this->attributes[$attribute] as $myvalue ) {
			if( $myvalue == $value )
				return true;
		}
		return false;
	}


	/**
	 * matchValue
	 * check if one of the attribute value match the given pattern 
	 *	Ex: $entry->matchValue( "cn", '/^Pierre\s+/i' ) echo "entry firstname is Pierre";
	 *
	 * @param $attribute string the attribute to compare
 	 * @param $regex string a Perl Regular expression with the pattern separators //
	 * @return boolean true if a matching value was found
	 */
	function matchValue( $attribute, $regex ) 
	{
		if( ! isset($this->attributes[$attribute]))
			return false;
		foreach( $this->attributes[$attribute] as $myvalue ) {
			if( preg_match( $regex, $myvalue) )
				return true;
		}
		
	}
	
	/**
	 * setValues
	 * set all values of an attribute, existing values being removed
	 *
	 * @param $attribute string attribute name
 	 * @param $values any the attribute values. if $values is an object, convert it to array before
	 * @return boolean true
	 */
	function setValues( $attribute, $values ) 
	{
		if( is_array( $values ) ) {
			$this->changes_add[$attribute] = $this->attributes[$attribute] = $values;
		} elseif( is_object($values) ) {
			$this->changes_add[$attribute] = $this->attributes[$attribute] = (array) $values;
		} else {
			$this->changes_add[$attribute] = $this->attributes[$attribute] = array( $values );
		}
		return true;
	}

	/**
	 * addValue
	 * add a value to an attribute
	 *
	 * @param $attribute string the attribute name
 	 * @param $value scalar the attribute value to add
	 * @return boolean true if add was ok, false otherwise
	 */
	function addValue( $attribute, $value ) 
	{
		if( ! isset($this->attributes[$attribute] ) )
			$this->attributes[$attribute] = array();
			
		$this->attributes[$attribute][] = $value;
		// for the incremental update
		$this->changes_add[$attribute][] = $value;
		return true;
	}

	/**
	 * remove
	 * remove all values of an attribute and unset it
	 *
	 * @param $attribute string the attribute name
	 * @return boolean always true
	 *
	 */
	function remove( $attribute ) 
	{
		if( isset($this->attributes[$attribute] ) ) {
			unset( $this->attributes[$attribute] );
			$this->changes_del[$attribute] = true;
		}
		return true;
	}
	
	

	/**
	 * removeValue
	 *  Remove a value from an attribute, if it exists. 
	 * if the attribute has no such value, return false
	 *
	 * @param $attribute string the attribute name
 	 * @param $value scalar string the attribute value
	 * @return boolean true if removed was ok, false otherwise
	 *
	 */
	function removeValue( $attribute, $value ) 
	{
		if( ! isset($this->attributes[$attribute]) )
			return false;
		$idx = array_search( $this->attributes[$attribute], $value );
		if( $idx === false )
			return false;
		unset( $this->attributes[$attribute][$idx] );
		$this->changes_mod[$attribute] = true;
		return true;		
	}
	
	/**
	 * size
	 * return the number of values of the given attribute
	 *
	 * @param $attribute string the attribute name
	 * @return int the value count
	 *
	 */
	function size( $attribute ) 
	{
		if( ! isset($this->attributes[$attribute] ) )
			return 0;
		else
			return count($this->attributes[$attribute]);
			
		return $count;
	}

	/**
	 * isDeleted
	 * test if the given attribute was deleted
	 * return true if it the case
	 *
	 * @param $attribute <type> <comment>
	 *
	 * @return boolean true if the entry attribute was deleted
	 *
	 */
	function isDeleted( $attribute ) 
	{
		return isset($this->changes_del[$attribute]);
	}
	
	/**
	 * isModified
	 * Test if the given entry attribute was modified 
	 *
	 * @param $attribute <type> <comment>
	 *
	 * @return boolean true if the entry attribute was modified
	 */
	function isModified( $attribute ) 
	{
		if( isset($this->changes_mod[$attribute]) 
			  || isset($this->changes_add[$attribute]) )
			return true;
		else
			return false;
	}
	

	/**
	 * exists
	 * Test if the entry has the specified attribute
	 *
	 * @param $attribute string the attribute to lookup
	 *
	 * @return boolean true if the entry contains this attribute, false otherwise
	 */
	function exists( $attribute ) 
	{
		return isset($this->attributes[$attribute] );
	}

	/**
	 * toString
	 * return entry in LDIF format, with a trailing empty line
	 * you can overridde toString() in a subclass to use another format (eg. CSV, HTML formatted)
	 * 
	 * @return string entry in LDIF format
	 */
	function toString()
	{
		$str = "dn: " . $this->getDN() . "\n";
		ksort($this->attributes);
		foreach( $this->attributes as $name => $v ) 
		{
			foreach( $this->getValues( $name ) as $value ) {
				$str .= "$name: $value\n";
			}
		}
		$str .= "\n";
		return $str;
	}
		
}
// end class Net_LDAP_Entry

?>
Return current item: LDAP Navigator