<?php
http://us3.php.net/ldap
$authuser = $_SERVER["SSL_CLIENT_S_DN_Email"];
$searchuser = explode( "@", $authuser );
$authuser = $searchuser[0];
echo "<h3>Affiliation determination via LDAP query test</h3>";
echo "The authenticated username is " . $authuser . "<br />";
$ds=ldap_connect("ldap.mit.edu");
if ($ds) {
$sr=ldap_search($ds, "dc=mit, dc=edu", "uid=".$authuser);
if (ldap_count_entries($ds, $sr) == 0){
echo "The entire directory was searched for (uid=" . $authuser . ") but no entry was found.<br />";
}
if (ldap_count_entries($ds, $sr) == 1){
echo "The entire directory was searched for (uid=" . $authuser . ") and one entry was found.<br />";
}
$info = ldap_get_entries($ds, $sr);
for ($i=0; $i<$info["count"]; $i++) {
echo "<br />dn is: " . $info[$i]["dn"] . "<br />";
echo "first cn entry is: " . $info[$i]["cn"][0] . "<br />";
echo "first email entry is: " . $info[$i]["mail"][0] . "<br /><hr />";
}
echo "<br />Now we're going to search specific portions of the directory to determine what type";
echo "of affliation this user has to MIT. This information can be used to make an authorization";
echo "decision, if this is appropriate for your application service. <br /><br />";
$sr=ldap_search($ds, "ou=employees, dc=mit, dc=edu", "uid=".$authuser);
if (ldap_count_entries($ds, $sr) == 0){
echo $authuser . " is not an employee.<br />";
}
if (ldap_count_entries($ds, $sr) == 1){
echo $authuser . " is an employee.<br />";
}
$sr=ldap_search($ds, "ou=students, dc=mit, dc=edu", "uid=".$authuser);
if (ldap_count_entries($ds, $sr) == 0){
echo $authuser . " is not a student.<br />";
}
if (ldap_count_entries($ds, $sr) == 1){
echo $authuser . " is a student.<br />";
}
$sr=ldap_search($ds, "ou=affiliates, dc=mit, dc=edu", "uid=".$authuser);
if (ldap_count_entries($ds, $sr) == 0){
echo $authuser . " is not an affliate.<br />";
}
if (ldap_count_entries($ds, $sr) == 1){
echo $authuser . " is an affiliate.<br />";
}
} else {
echo "<h4>Unable to connect to LDAP server</h4>";
}
?>