LDAP的使用
时间:2007-02-17 来源:PHP爱好者
下面是小弟的LDAP的一个实现,主要是针对Exchange Server所做.
PS1:当服务器返回结果太多可能会有错误
PS2:里面需要的header.inc和footer.inc其实就是标准的html<head>
尾</head>与需要的信息.
欢迎各位试验.
=======Cut Begin (scd.php3)========
<?PHP
require("header.inc");
$ldapserver = "ldap.xxx.xxx.com.cn";
$ldapid = 0;
$ldapds = "";
$ldapbindr = "";
$ldapsr = "";
$ldapservers = array();
$ldapservers['server'] [0] = "ldap.xxx.xxx.com.cn";
$ldapservers['serverdesc'][0] = "XXX Ltd.";
$ldapservers['fields'] [0] = "co,st,l,company,department,physic
aldeliveryofficename,sn,givenname,telephonenumber,postalcode,mail,titl
e";
$ldapservers['fieldsdesc'][0] = "Country,Province,City,Company,Dep
artment,Office,First Name, Last Name,Phone,Postal Code,E-Mail,Title";
$ldapservers['server'] [1] = "xxx.yyy.zzz.111";
$ldapservers['serverdesc'][1] = "YYY Ltd.";
$ldapservers['fields'] [1] = "co,st,l,company,department,physic
aldeliveryofficename,sn,givenname,telephonenumber,postalcode,mail,titl
e";
$ldapservers['fieldsdesc'][1] = "Country,Province,City,Company,Dep
artment,Office,First Name, Last Name,Phone,Postal Code,E-Mail,Title";
function FillSelect($values,$texts,$selected)
{
$counter = count($values);
if($counter==0) return;
if($selected=="") $selected="mail";
for($I=0;$I<$counter;$I++)
{
if($values[$I]==$selected) $set=" selected";
else $set="";
print("rtt<option value="".$values[$I].""".$set.">".$texts[$I]
."</option>n");
}
}
function ldapsearch()
{
Global $ldapserver,$searchtext,$searchfor,$searchat;
$ahref="";
if($searchtext!="")
$searchstr=$searchfor."=*".$searchtext."*";
else
die("Please input search textNo Matched!n");
$ldapds = ldap_connect($ldapserver);
if($ldapds == false)
die( "rConnect Error!n");
$ldapbindr=ldap_bind($ldapds);
if($ldapbindr==false)
die( "rBinding Error!n");
$ldapsr = ldap_search($ldapds,"",$searchstr);
if($ldapsr==false)
die( "rSearch Failure!n");
$ldapcount=ldap_count_entries($ldapds,$ldapsr);
if($ldapcount==0)
die("No Matched!n");
$ldapinfo = ldap_get_entries($ldapds, $ldapsr);
for($i=0;$i<$ldapcount;$i++)
{
$space=" ";
$dn =ldap_explode_dn($ldapinfo[$i]["dn"],1);
$o =$dn[3];
$ou =$dn[2];
$contury =$ldapinfo[$i]["co"][0];
$state =$ldapinfo[$i]["st"][0];
$city =$ldapinfo[$i]["l"][0];
$company =$ldapinfo[$i]["company"][0];
$department =$ldapinfo[$i]["department"][0];
$office =$ldapinfo[$i]["physicaldeliveryofficename"][0];
$sn =$ldapinfo[$i]["sn"][0];
$gn =$ldapinfo[$i]["givenname"][0];
$phone =$ldapinfo[$i]["telephonenumber"][0];
$postcode =$ldapinfo[$i]["postalcode"][0];
$mail =$ldapinfo[$i]["mail"][0];
$info =$ldapinfo[$i]["info"][0];
$title =$ldapinfo[$i]["title"][0];
$name =$sn.", ".$gn;
if($sn=="") $name=$gn;
$mod =$i % 2;
echo "t<TR>n";
echo "tt<td class=body".$mod.">".$i.$space."</td>n";
echo "tt<td class=body".$mod.">".$name.$space."</td>n";
echo "tt<td class=body".$mod."><A HREF="mailto:".$mail."">
".$mail.$space."</A></td>n";
echo "tt<td class=body".$mod.">".$phone.$space."</td>n";
echo "tt<td class=body".$mod.">".$title.$space."</td>n";
echo "tt<td class=body".$mod.">".$office.$space."</td>n";
echo "tt<td class=body".$mod.">".$department.$space."</td>n";
echo "tt<td class=body".$mod.">".$company.$space."</td>n";
echo "tt<td class=body".$mod.">".$city.$space."</td>n";
echo "tt<td class=body".$mod.">".$state.$space."</td>n";
echo "tt<td class=body".$mod.">".$contury.$space."</td>n";
echo "tt<td class=body".$mod.">".$postcode.$space."</td>n";
echo "t</tr>n";
}
ldap_free_result($ldapsr);
ldap_unbind($ldapbindr);
// ldap_close($ldapds);
}
include("scd.inc");
if($searchat!="")
$ldapserver=$searchat;
if($searchat!="")
include("scdresult.inc");
require("footer.inc");
?>
=======Cut End========
=======Cut Begin (scd.inc)=======
<form name="scdform" method="post" action="scd.php3">
<div align="center">
<table width="64%" border="0" cellspacing="0" cellpadding="1">
<tr class=body>
<th colspan="3" height="29"> Directory</th>
</tr>
<tr class=body>
<td class=body>Searching At</td>
<td class=body>
<select name="searchat">
<?
FillSelect($ldapservers['server'],$ldapservers['serverdesc'],$
searchat);
?>
</select>
</td>
</tr>
<tr class=body>
<td class=body>
<select name="searchfor">
<?
$counter=count($ldapservers);
if($searchfor=="") $searchfor="mail";
for($I=0;$I<counter;$I++)
{
if($ldapservers['fields'][$I]==$searchfor) break;
}
FillSelect(explode(",",$ldapservers['fields'][$I]),explode(","
,$ldapservers['fieldsdesc'][$I]),$searchfor);
?>
</select>
</td>
<td class=body> Contain
<input type="text" name="searchtext" value=<?echo $searchtex
t ?>>
</td>
</tr>
<tr class=body>
<td width="25%" class=body>
</td>
<td class=body>
<input type="submit" name="search" value="Search">
<input type="reset" name="Clear" value="Clear">
</td>
</tr>
</table>
</div>
</form>
=======Cut End========
=======Cut Begin (scdresult.inc)========
<table class=resultbody name="SCD Result" cellpadding="1" cellspacing=
"0" width="99%">
<tr class=resultbody>
<th width="12" height="30" class=resultbody> </th>
<th width="14%" height="30" class=resultbody>Name</th>
<th width="24%" height="30" class=resultbody>E-Mail</th>
<th width="5%" height="30" class=resultbody>Phone</th>
<th width="10%" height="30" class=resultbody>Title</th>
<th width="7%" height="30" class=resultbody>Office</th>
<th width="8%" height="30" class=resultbody>Dept</th>
<th width="8%" height="30" class=resultbody>Company</th>
<th width="10%" height="30" class=resultbody>City</th>
<th width="8%" height="30" class=resultbody>Province</th>
<th width="15%" height="30" class=resultbody>Country</th>
<th width="5%" height="30" class=resultbody>ZIP</th>
</tr>
<?
ldapsearch();
?>
</table>
=======Cut End========
php爱好 者站 http://www.phpfans.net php基础|php进阶|php模板.
PS1:当服务器返回结果太多可能会有错误
PS2:里面需要的header.inc和footer.inc其实就是标准的html<head>
尾</head>与需要的信息.
欢迎各位试验.
=======Cut Begin (scd.php3)========
<?PHP
require("header.inc");
$ldapserver = "ldap.xxx.xxx.com.cn";
$ldapid = 0;
$ldapds = "";
$ldapbindr = "";
$ldapsr = "";
$ldapservers = array();
$ldapservers['server'] [0] = "ldap.xxx.xxx.com.cn";
$ldapservers['serverdesc'][0] = "XXX Ltd.";
$ldapservers['fields'] [0] = "co,st,l,company,department,physic
aldeliveryofficename,sn,givenname,telephonenumber,postalcode,mail,titl
e";
$ldapservers['fieldsdesc'][0] = "Country,Province,City,Company,Dep
artment,Office,First Name, Last Name,Phone,Postal Code,E-Mail,Title";
$ldapservers['server'] [1] = "xxx.yyy.zzz.111";
$ldapservers['serverdesc'][1] = "YYY Ltd.";
$ldapservers['fields'] [1] = "co,st,l,company,department,physic
aldeliveryofficename,sn,givenname,telephonenumber,postalcode,mail,titl
e";
$ldapservers['fieldsdesc'][1] = "Country,Province,City,Company,Dep
artment,Office,First Name, Last Name,Phone,Postal Code,E-Mail,Title";
function FillSelect($values,$texts,$selected)
{
$counter = count($values);
if($counter==0) return;
if($selected=="") $selected="mail";
for($I=0;$I<$counter;$I++)
{
if($values[$I]==$selected) $set=" selected";
else $set="";
print("rtt<option value="".$values[$I].""".$set.">".$texts[$I]
."</option>n");
}
}
function ldapsearch()
{
Global $ldapserver,$searchtext,$searchfor,$searchat;
$ahref="";
if($searchtext!="")
$searchstr=$searchfor."=*".$searchtext."*";
else
die("Please input search textNo Matched!n");
$ldapds = ldap_connect($ldapserver);
if($ldapds == false)
die( "rConnect Error!n");
$ldapbindr=ldap_bind($ldapds);
if($ldapbindr==false)
die( "rBinding Error!n");
$ldapsr = ldap_search($ldapds,"",$searchstr);
if($ldapsr==false)
die( "rSearch Failure!n");
$ldapcount=ldap_count_entries($ldapds,$ldapsr);
if($ldapcount==0)
die("No Matched!n");
$ldapinfo = ldap_get_entries($ldapds, $ldapsr);
for($i=0;$i<$ldapcount;$i++)
{
$space=" ";
$dn =ldap_explode_dn($ldapinfo[$i]["dn"],1);
$o =$dn[3];
$ou =$dn[2];
$contury =$ldapinfo[$i]["co"][0];
$state =$ldapinfo[$i]["st"][0];
$city =$ldapinfo[$i]["l"][0];
$company =$ldapinfo[$i]["company"][0];
$department =$ldapinfo[$i]["department"][0];
$office =$ldapinfo[$i]["physicaldeliveryofficename"][0];
$sn =$ldapinfo[$i]["sn"][0];
$gn =$ldapinfo[$i]["givenname"][0];
$phone =$ldapinfo[$i]["telephonenumber"][0];
$postcode =$ldapinfo[$i]["postalcode"][0];
$mail =$ldapinfo[$i]["mail"][0];
$info =$ldapinfo[$i]["info"][0];
$title =$ldapinfo[$i]["title"][0];
$name =$sn.", ".$gn;
if($sn=="") $name=$gn;
$mod =$i % 2;
echo "t<TR>n";
echo "tt<td class=body".$mod.">".$i.$space."</td>n";
echo "tt<td class=body".$mod.">".$name.$space."</td>n";
echo "tt<td class=body".$mod."><A HREF="mailto:".$mail."">
".$mail.$space."</A></td>n";
echo "tt<td class=body".$mod.">".$phone.$space."</td>n";
echo "tt<td class=body".$mod.">".$title.$space."</td>n";
echo "tt<td class=body".$mod.">".$office.$space."</td>n";
echo "tt<td class=body".$mod.">".$department.$space."</td>n";
echo "tt<td class=body".$mod.">".$company.$space."</td>n";
echo "tt<td class=body".$mod.">".$city.$space."</td>n";
echo "tt<td class=body".$mod.">".$state.$space."</td>n";
echo "tt<td class=body".$mod.">".$contury.$space."</td>n";
echo "tt<td class=body".$mod.">".$postcode.$space."</td>n";
echo "t</tr>n";
}
ldap_free_result($ldapsr);
ldap_unbind($ldapbindr);
// ldap_close($ldapds);
}
include("scd.inc");
if($searchat!="")
$ldapserver=$searchat;
if($searchat!="")
include("scdresult.inc");
require("footer.inc");
?>
=======Cut End========
=======Cut Begin (scd.inc)=======
<form name="scdform" method="post" action="scd.php3">
<div align="center">
<table width="64%" border="0" cellspacing="0" cellpadding="1">
<tr class=body>
<th colspan="3" height="29"> Directory</th>
</tr>
<tr class=body>
<td class=body>Searching At</td>
<td class=body>
<select name="searchat">
<?
FillSelect($ldapservers['server'],$ldapservers['serverdesc'],$
searchat);
?>
</select>
</td>
</tr>
<tr class=body>
<td class=body>
<select name="searchfor">
<?
$counter=count($ldapservers);
if($searchfor=="") $searchfor="mail";
for($I=0;$I<counter;$I++)
{
if($ldapservers['fields'][$I]==$searchfor) break;
}
FillSelect(explode(",",$ldapservers['fields'][$I]),explode(","
,$ldapservers['fieldsdesc'][$I]),$searchfor);
?>
</select>
</td>
<td class=body> Contain
<input type="text" name="searchtext" value=<?echo $searchtex
t ?>>
</td>
</tr>
<tr class=body>
<td width="25%" class=body>
</td>
<td class=body>
<input type="submit" name="search" value="Search">
<input type="reset" name="Clear" value="Clear">
</td>
</tr>
</table>
</div>
</form>
=======Cut End========
=======Cut Begin (scdresult.inc)========
<table class=resultbody name="SCD Result" cellpadding="1" cellspacing=
"0" width="99%">
<tr class=resultbody>
<th width="12" height="30" class=resultbody> </th>
<th width="14%" height="30" class=resultbody>Name</th>
<th width="24%" height="30" class=resultbody>E-Mail</th>
<th width="5%" height="30" class=resultbody>Phone</th>
<th width="10%" height="30" class=resultbody>Title</th>
<th width="7%" height="30" class=resultbody>Office</th>
<th width="8%" height="30" class=resultbody>Dept</th>
<th width="8%" height="30" class=resultbody>Company</th>
<th width="10%" height="30" class=resultbody>City</th>
<th width="8%" height="30" class=resultbody>Province</th>
<th width="15%" height="30" class=resultbody>Country</th>
<th width="5%" height="30" class=resultbody>ZIP</th>
</tr>
<?
ldapsearch();
?>
</table>
=======Cut End========
php爱好 者站 http://www.phpfans.net php基础|php进阶|php模板.
相关阅读 更多 +