LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   php connect "active directory" script (https://www.linuxquestions.org/questions/linux-software-2/php-connect-active-directory-script-441404/)

paul_mat 05-03-2006 08:01 PM

php connect "active directory" script
 
hi there,

i'm just wondering if someone out there has writtern a basic php script that will connect to active directory using ldap? and maybe search it?

i'm looking for maybe a combination of this

http://www.weberdev.com/get_example-4132.html

and this

<?php
if ($name_submitted != "" && $passwd_submitted != "") {

$ldap_host = "ldap://someserver.rhodes.edu";
$base_dn = "CN=Users,DC=rhodes, DC=edu";

if ($search_submitted == "") {
$search_value = $name_submitted;
} else {
$search_value = $search_submitted;
}

$filter = "(CN=$search_value)";
$ldap_user = "CN=$name_submitted, CN=Users, DC=rhodes, DC=edu";
$ldap_pass = $passwd_submitted;

$connect = ldap_connect( $ldap_host, $ldap_port)
or exit("Could not connect to LDAP server");

// required to search AD, according to note in PHP manual notes
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);

$bind = ldap_bind($connect, $ldap_user, $ldap_pass)
or exit("Could not bind to $ldap_host");

echo "Successful bind to $ldap_host with $bind<br><br>\n";

$read = ldap_search($connect, $base_dn, $filter)
or exit("Unable to search ldap server");

$info = ldap_get_entries($connect, $read);
echo $info["count"]." entries returned for $filter<br><br>\n";

$ii=0;
for ($i=0; $ii<$info[$i]["count"]; $ii++){
$data = $info[$i][$ii];
if ($data == "memberof") {
$total_memberof = (count($info[$i][$data]));
echo "Total memberof entries returned: $total_memberof<br><br>\n";
$total = 0;
$total = count($info[$i][$data]);
$jj=0;
for ($jj=0; $jj<$total; $jj++) {
if ($info[$i][$data][$jj] == "CN=STAFF,OU=Security
Groups,OU=Groups,DC=rhodes,DC=edu") {
echo "<b>Got Staff Match</b> ";
$user_type = "staff";
} elseif (($info[$i][$data][$jj] == "CN=FACULTY,OU=Security
Groups,OU=Groups,DC=rhodes,DC=edu") && $user_type == "") {
echo "<b>Got Faculty Match</b> ";
$user_type = "faculty";
} elseif (($info[$i][$data][$jj] == "CN=Students,OU=Security
Groups,OU=Groups,DC=rhodes,DC=edu") && $user_type == "") {
echo "<b>Got Students Match</b> ";
$user_type = "student";
}
echo $i." ".$ii." ".$jj."
".$data.":&nbsp;&nbsp;".$info[$i][$data][$jj]."<br>\n";
}
}

}

ldap_unbind($connect);

echo "<br><br><b>User Type is: ";

switch ($user_type) {
case "staff":
echo "STAFF";
break;
case "faculty":
echo "FACULTY";
break;
case "student":
echo "STUDENT";
break;
default:
echo "UNKNOWN";
break;
}

echo "</b><br><br>\n";

echo "<br><br><a href=\"index.php\">Search again</a><br><br>\n";

} else {

echo "<html><head></head><body>\n";
echo "<form action=\"index.php\" method=\"POST\">\n";
echo "AD User Name: <input type=\"text\"
name=\"name_submitted\"><br>\n";
echo "AD Password: <input type=\"password\"
name=\"passwd_submitted\"><br>\n";
echo "Search User Name: <input type=\"text\"
name=\"search_submitted\"><br>\n";
echo "<input type=\"submit\" value=\"Submit\">\n";
echo "</form>\n";
echo "</body></html>\n";

}
?>

so I want a username/password inside the file that connects to the Active Directory server and then I just want a search box that i can search the displayName area and return with mail area.


All times are GMT -5. The time now is 09:05 AM.