Location: PHPKode > scripts > LDAP 2 LDIF > ldap-2-ldif/ldap2ldif.class.php
<?php
/***********************************************************************
* filename: ldap2ldif.class.php 
* purpose: utility class to export data from LDAP servers to LDIF format
* author: vedanta barooah
* email: vedanta dot barooah at gmail dot com
* date: Apr 27 2005 
* License: PHP 
************************************************************************/ 

class ldap2ldif{
	var $server 					= 	NULL;
	var $port					=	NULL;
	var $username					=	NULL;
	var $password					=	NULL;
	var $con					=	NULL;
	var $ldapbind					=	NULL;
	var $base_dn					=	NULL;
	var $requested_attributes			=	array();
	var $search_result				=	NULL;
	var $num_results				=	NULL;
	/* class info */
	var $classname					=	'ldap2ldif';
	var $version					=	'1.0';
	var $author					=	'Vedanta Barooah';
	var $author_email				=	'vedanta dot barooah at gmail dot com';

	/**
	* Class constructor
	*/
	function ldap2ldif($serverName='localhost',$serverPort='389'){
		$this->server=$serverName;
		$this->port=$serverPort;					
	}
	/**
	* Initialize and connect to a LDAP Server
	*/
	function init($ldap_username=NULL,$ldap_password=NULL){
		$this->username=$ldap_username;
		$this->password=$ldap_password;
		/* connect to the server */
		$this->con=@ldap_connect($this->server,$this->port);
		if(!$this->con) return false;
		else{
			if($this->username!=''){ /* this is non-anonymous so authenticate */
				if($this->ldapbind=@ldap_bind($this->con,$this->username,$this->password))return true;
				else return false;
			}
			else{ /* i am nobody, but still i want stuff from the ldap server */
				if($this->ldapbind=@ldap_bind($this->con))return true;
				else return false;
			}
		}
	}
	
	/**
	* Search the LDAP Directory Tree
	*/
	function search($baseDN,$filter="(objectClass=*)",$required_attributes=NULL,$limitRecords=0){
		$this->base_dn=$baseDN;
		$this->requested_attributes=$required_attributes;
		if(!$this->requested_attributes){
			$search_result=@ldap_search($this->con,$this->base_dn,$filter);
		}else{
			if(is_array($this->requested_attributes))
				$search_result=@ldap_search(
				$this->con,$this->base_dn,$filter,$this->requested_attributes,0,$limitRecords
				);
		}
		$this->search_result=$search_result;
		$this->num_results=@ldap_count_entries($this->con,$this->search_result);
		return $this->num_results;
	}

	
	/**
	* Write data to file
	* @access private
	*/
	function createFile($data=NULL,$filename){
		if(!$data)
			return false;
		else{
			if(!$fh=@fopen($filename,'w'))	return false;
			else if(!@fwrite($fh,$data)) return false;
			@fclose($fh);
		}
	}

	/**
	* Export LDAP data to LDIF (LDAP Data Interchange Format)
	*/
	function export($ldif_file_name=NULL){
		if(!@ldap_count_entries($this->con,$this->search_result)){
			return false;
		}
		$result=@ldap_get_entries($this->con,$this->search_result);
		$ldif_str="# LDIF generated by $this->classname ($this->version)\n";
		$ldif_str.="# Author: $this->author ($this->author_email)\n";
		$ldif_str.="# Server: $this->server\n";
		$ldif_str.="# Total Entries: "hide@address.com($this->con,$this->search_result)."\n";
		$ldif_str.="\n";
		for($idx=0,$ec=1;$idx<count($result)-1;$idx++,$ec++){
			$ldif_str.="# LDIF Entry ".$ec.": ".$result[$idx]['dn']."\n";	
			$ldif_str.="dn:".$result[$idx]['dn']."\n";
			for($j=0;$j<$result[$idx]["count"];$j++)for($i=0;$i<$result[$idx][$result[$idx][$j]]["count"];$i++)
			$ldif_str.=$result[$idx][$j] .": ". $result[$idx][$result[$idx][$j]][$i]."\n";
			$ldif_str.="\n";
		}
		if(!$ldif_file_name){
			return $ldif_str;
		}else{
		/* write the output to the specified file */
			if (!$this->createFile($ldif_str,$ldif_file_name)) return true;
			else return false;
		}
	}
	
	/**
	* Close LDAP Connection
	*/
	function close(){
		return @ldap_close($this->con);
	}
}

?>
Return current item: LDAP 2 LDIF