Here's a bit that I use to obtain full names for smtp addresses. The names have been changed to protect the innocent. We use the network id for alias, directory name and for smtp address. This code strips off the domain and returns the full name which is then converted into a mailto link in the calling routine. I'm a newbie at php and most of the ldap access was simply copied and pasted from the examples. I have accessed Exchange before in Shell Script and Python using OpenLDAP calls so that's probably my strength - knowing what the field mappings are. I think that it might speed things up if I put the connect and binding in globals and only handled the retrieval in the body, but I tried to build in a bit of fault tolerance by specifying more than one server after wondering why things hung and then discovering that the server had been rebooted - oops! I may end up dumping the fault tolerance in favor of performance after the bind succeeds.
<?php
// Done this way to allow the code to
// be easily moved from the lab to
// production
$domain = "foo.bar.com";
$ldapservers = array ('server'.$domain, 'server2'.$domain);
$fields = array("cn");
function getusername($email) {
global $ldapservers,$domain,$fields;
// Don't even bother if the email address
// is not in the domain
$pos = strpos($email, $domain);
if ($pos === false) {
return "".$email;
}
// I don't know why the trim is needed, but
// there was a trailing space after the
// netid before
$uid = trim("uid=".str_replace("@".$domain,"",$email));
foreach ( $ldapservers as $ldapserver ) {
$ds = ldap_connect($ldapserver);
if ( $ds ) {
$r=ldap_bind($ds);
$sr=ldap_search($ds,"cn=Recipients,ou=COMPANY,o=ORGANIZATION",$uid,$fields);
$info = ldap_get_entries($ds, $sr);
if ($info["count"] > 0) {
return $info[0]["cn"][0];
} else {
return "error - ".$email;
}
}
}
return $email;
}
?>