Location: PHPKode > projects > LDAP contact manager > ldapmgr/functions.php
<?php
// php ldap manager. for address book
// provides access to manage ldap objects in single context only
//gloabal stuff for these functions

require_once("siteconfig.inc");

$ctInfo = array("cn","givenname","sn","description","mail","telephoneNumber") ;

/*
   function list:
   create new entry
   modify entry
   delete entry
   search for object
*/

//generic functions

function ldaplogin() {
	global $ldapHost, $ldapPwd, $ldapUser, $ldapBase ;
	print "<br>Connecting...<br>" ;
	$ldapConn = ldap_connect ($ldapHost) ;
	if ($ldapConn) {
		print "Conect successfull...<br>" ;
		$result = ldap_bind ($ldapConn,$ldapUser,$ldapPwd);
		if ($result) {
			print "Bind successful..<br>" ;
			return $ldapConn ;
		} else {
			return False ;
		}
	} else {
		return False ;
	}
}
	
/*******************************
   create new entry from form
   form fields: firstname, surname, company, cell number, email
   
*/


function create_entry($ctInfo) {
	global $ldapBase ;
	$result = ldapLogin() ;
	if ($result <> False) {
		if ((isset($ctInfo["fname"])) && (isset($ctInfo["sname"])) && (isset($ctInfo["company"]))) {
			$newCN = "cn=".$ctInfo["fname"]." ".$ctInfo["sname"].",".$ldapBase;
			$info["objectClass"][0] = "Person" ;
			$info["objectClass"][1] = "inetOrgPerson";
			$info["givenname"] = $ctInfo["fname"] ;
			$info["cn"] = $newCN ;
			$info["sn"] = $ctInfo["sname"] ;
			if (isset ($ctInfo["email"])) { $info["mail"] = $ctInfo["email"] ; }
			if (isset ($ctInfo["company"])) { $info["description"] = $ctInfo["company"] ; }
			if (isset ($ctInfo["cellnum"])) { $info["telephoneNumber"] = $ctInfo["cellnum"] ; }
			$rst = ldap_add ($result, $newCN, $info);
			if ($rst) {
				return  "Entry ".$newCN." successfuly created";
			} else {
				return  "Add entry failed for ".$newCN." with error ".$ldap_error ;
			}
		ldap_unbind($result) ;
		}
	} else {
		return $ldap_error;
	}
	ldap_close($result);
}


/*******************************
   delete object from directory
   fields: objectCn
*/
function delete_object ($oCName) {
	global $ldapBase ;
	$result = LDAPLogin() ;
	if ($result) {
		$dn = $oCName ;
		return (ldap_delete($result,$dn)) ;
	} else {
		return $ldap_error ;
	}
}


/*******************************
   find object based on search criteria
   fields: serachAttribute, searchString
   return value: cn of object(s).
*/

function find_objects($clStr) {
	$srchResult = array();
	global $ldapHost, $ldapBase, $ctInfo ;
	//use anonymous bind for searching
	$result = ldap_connect ($ldapHost) ;
	if (! empty($clStr["sname"])) { $filter = "(sn=".$clStr["sname"].")" ; }
	if (! empty($clStr["fname"])) { $filter .= "(givenname=".$clStr["fname"].")" ; }
	if (! empty($clStr["cellnum"])) { $filter .= "(telephoneNumber=".$clStr["cellnum"].")" ; }
	if (!empty($clStr["company"])) { $filter .= "(description=".$clStr["company"].")" ; }
	if (!empty($clStr["email"])) { $filter .= "(mail=".$clStr["email"].")" ; }
 	if (empty($filter)) { $filter = "cn=*" ; }
	print "<br>Using filter ".$filter."<br>" ;
	if ($result) {
		global $srchResult ;
		$brs = ldap_bind ($result) ;
		$qry = ldap_search($result,$ldapBase,$filter,$ctInfo);
		$ctEntry = ldap_first_entry ($result, $qry) ;
		$i = 0 ;
		print "Finding Matches...<br>";
		While ($ctEntry) {
			$ctSrchInfo = ldap_get_attributes ( $result, $ctEntry) ;
			$ctSname = $ctSrchInfo["sn"][0] ;
			$ctCName = $ctSrchInfo["cn"][0] ;
			$ctCompName = $ctSrchInfo["description"][0] ;
			$ctcellNum = $ctSrchInfo["telephoneNumber"][0] ;
			$srchResult[$i] = $ctCName;
			$i++ ;
			$ctEntry = ldap_next_entry ($result, $ctEntry) ;
		}
		ldap_free_result ( $qry );
		if (empty($srchResult)) { $srchResult = False ; }
		return $srchResult ;
	}
}


function get_object ($getCn) {
	$srchResult = array();
	global $ldapHost, $ldapBase, $ctInfo ;
	//use anonymous bind for searching
	$result = ldap_connect ($ldapHost) ;
	$filter = "cn=".$getCn ;
	if ($result) {
		global $srchResult ;
		$brs = ldap_bind ($result) ;
		$qry = ldap_search($result,$ldapBase,$filter,$ctInfo);
		$ctEntry = ldap_first_entry ($result, $qry) ;
		$ctSrchInfo = ldap_get_attributes ( $result, $ctEntry) ;
		$ctSname = $ctSrchInfo["sn"][0] ;
		$ctName = $ctSrchInfo["givenName"][0] ;
		$ctCompName = $ctSrchInfo["description"][0] ;
		$ctcellNum = $ctSrchInfo["telephoneNumber"][0] ;
		$ctMail = $ctSrchInfo["mail"][0] ;
		$ctRs = Array($ctName,$ctSname,$ctCompName,$ctMail,$ctcellNum) ;
		return $ctRs ;
	} else {
		return False ;
	}

}

/* 
function to modify user info after search
*/

function update_info ($oCn,$oArr) {
	$rs = ldapLogin () ;
	if ($rs) {
		$mrs = ldap_modify ($rs,$oCn,$oArr) ;
		return $mrs ;
	}
}

?>
Return current item: LDAP contact manager