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

   Net_LDAP_Client class
	
DESCRIPTION 

	
SYNOPSIS

CHANGES
	0.2  renamed class to Net_LDAP_Client
	
VERSION
	0.2
	
INFORMATIONS
        Leo West <west_leo AT yahoo DOT com>
*/

include_once( "Net/LDAP/Entry.php" );

class Net_LDAP_Client
{	
	/************ @public  ***************/
	/* max entries to return in a search operation @type int */
	var $searchMaxResults= NULL;
	/* timeout in a search operation @type int */
	var $searchTimeout= NULL;
	/* last error number, may be read directly  @type int */
	var $errorNo; 
	/* last error message, may be read directly @type string */
	var $errorMsg; 

	/************ @private  ***************/
	/* ldap connection identifier */
	var $cnx = NULL; 
	/* initial basedn given by the url */
	var $baseDN = NULL; 
	/* misc identifiers used in search process */
	var $resid = NULL;
	var $eid = NULL;
	
	function Net_LDAP_Client( $ldapurl= "" ) 
	{	
		if( $ldapurl ) {
			$url = parse_url( $ldapurl );
			if( $url === false )
				return;

			if( isset($url['path']) )
				$this->baseDN = substr( $url['path'], 1 );

			if( ! isset($url['port']) )
				$url['port'] = 389;

				
			if( ! $this->connect( $url['host'], $url['port'] ) )
				return $this->Error( -1, "LDAP connect failed" );
				
			if( isset($url['username']) && isset($url['password']) ) {
				$this->bind( $url['username'], $url['password'] );
			}
		}					
	}
	
	function connect( $host, $port=389 ) 
	{
		$this->cnx = ldap_connect( $host, $port );
		return ( $this->cnx > 0);
	}
	
	function bind( $userdn, $password ) 
	{
		if( $this->cnx )
			return ldap_bind( $this->cnx, $userdn, $password );
		else
			return false;
	}
	
	function close()
	{
		if( $this->cnx ) {
			ldap_close( $this->cnx );
		}
	}

	/**
	 * Add an entry int the directory
	 * @param basedn string the start DN for this search
	 * @param filter string the LDAP query filter - by default the universal filter "objectclass=*"
	 * @param atributes array the entry attributes to return. optional, return all readable attributes by default
	 * @return the first entry found @type Net_LDAP_Entry or false if nothing found or error
	 * in case of error the message is in $this->errorMsg
	 **/
	function &search( $basedn=NULL, $filter=NULL, $attributes=NULL )
	{
		if( ! $this->cnx )
			return $this->Error( -1, "No connexion" );
		( $basedn == NULL ) && $basedn = $this->baseDN;
		( $filter == NULL ) && $filter = 'objectclass=*';
		( $attributes == NULL ) &&	$attributes = array();
						
//		echo "ldap_search( $this->cnx, $basedn, $filter, $attributes, 0, $this->searchMaxResults, $this->searchTimeout )\n";
		$this->resid = @ldap_search( $this->cnx, $basedn, $filter, $attributes, 0, $this->searchMaxResults, $this->searchTimeout );
		
		if( ! $this->resid )
			return $this->setError();
		
		$this->eid = ldap_first_entry( $this->cnx, $this->resid );
		$entry = new Net_LDAP_Entry();
		$entry->load( $this->cnx, $this->eid );
		return $entry;
	}
	
	/**
	 * return next entry from search result set
	 * @return false if no more entries or error 
	 **/
	function &next()
	{
		if( ! $this->eid )
			return $this->Error( -2, "Client::next : No result identifier" );
		
		$this->eid = ldap_next_entry( $this->cnx, $this->eid );
		if( ! $this->eid ) {
			ldap_free_result( $this->resid );
			$this->resid = $this->eid = NULL;
			return false;
		}
		
		$entry = new Net_LDAP_Entry();
		$entry->load( $this->cnx, $this->eid );
		
		return $entry;
	}
	
	/**
	 * Add an entry int the directory
	 * @param objEntry LDAPEntry object to insert
	 * @return boolean true if thing was ok, false otherwise . Error message is in $conn->errorMsg
	 **/
	function add( $objEntry ) 
	{
		if( ! is_object( $objEntry ) )
			return false;
		if( get_class( $objEntry ) !=  "Net_LDAP_Entry" )
			return false;
		$dn = $objEntry->getDN();
		if( ! $dn )
			return $this->setError( -1, "Entry has no dn in Net_LDAP_Client::add()" );
		$ret = ldap_add( $this->cnx, $dn, $objEntry->attributes );
		if( ! $ret )
			return $this->setError();
		else
			return true;
	}

	
	/**
	 * <int or boolean> Compare(  string dn, string attribute, string value )
	 * Compares an attribute value to a given DN
	 * @param dn the entry DN to compare to
	 * @param the attribute name
	 * @param the value
	 * @return boolean true if attribute matchs the entry. false otherwise, -1 if error occured
	 **/
	function compare( $dn, $attribute, $value )
	{
		$res = @ldap_compare( $this->cnx, $dn, $attribute, $value );
		if( $res == -1 )
			$this->setError();
		return $res;
	}

	/**
	 * set the internal error message to $errormsg and return false
	 *	@scope protected
	 **/
	function setError( $errorNo = NULL, $errorMsg = NULL )
	{
		if( $errorNo == NULL && $errorMsg == NULL ) {
			$this->errorNo = ldap_errno($this->cnx);
			$this->errorMsg = "Net::LDAP::Client - " . ldap_error($this->cnx);
		} else {
			$this->errorNo = $errorNo;
			$this->errorMsg = "Net::LDAP::Client - " . $errorMsg;
		}
		if( $GLOBALS['DEBUG'] )
			echo "[DEBUG] $this->errorMsg ($this->errorNo)\n";
		return false;
	}



}
// end class Net_LDAP_Client

?>
Return current item: LDAP Navigator