我们创建PHP + LDAP的WEB地址目录

碰巧,(相对)大型活动有许多远程办公室,并且用户数量不错。所有办公室都通过一个公共域连接到同一网络,每个办公室都在Active Directory(以下称为AD)中定义为组织单位(OU),在其中已经建立了用户。

必须让用户有机会迅速而轻松地从AD中获取必要员工的联系信息,并且系统管理员应摆脱编辑充当地址簿角色的文本文件的例程。


没有现成的合适选项可以解决任务,因此我必须用自己的双手和头部进行所有操作。

首先,您首先需要确定要使用的内容,这很简单-最终指南应该对域中的所有用户都可通过浏览器访问。首先想到的是PHP与ldap结合使用,我们将使用它们。我认为使用PHP的最大优点是相对简单,这是任何对系统管理员了解甚少的人都可以对代码进行必要的更改(如果需要),而不会特别费力。

所以,让我们开始吧。首先,让我们设置域连接参数:

$srv ="SERVER";
$srv_domain ="DOMAIN.COM";
$srv_login ="USERNAME@".$srv_domain; 
$srv_password ="PASSWORD";

下一步是确定我们将在哪个OU中搜索用户。我们将通过截取$ _GET ['place']中的值来做到这一点。例如,如果用户进入server / index.php?Place = first,那么$ place变量将被首先分配值

$place = (@$_GET['place']);
$doscript=true;
switch($place){ 
case "first" :
	$dn ="OU=ou1,OU=DOMAIN,dc=DOMAIN,dc=COM";			
	break;
case "second":
	$dn ="OU=ou2,OU=DOMAIN,dc=DOMAIN,dc=COM";			
	break;
	//    .
default:
	$doscript=false; 
	break;
}
if (!$doscript) include "main_table.html";

$ doscript变量是必要的,以保存价值-我们是否已经确定了在OU,我们将为用户或不是搜索。如果“ switch-case”中没有列出匹配项,则$ doscript = false,脚本的主要部分将不会执行,并且将显示起始页面“ main_table.html”(我会在最后告诉您)。

如果我们定义了OU,那么我们将采取进一步的行动:开始绘制用户手册页:

else if ($doscript) {
{echo "
<!DOCTYPE html> 
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<link rel='shortcut icon' href='ico.png'>
<meta charset='windows-1251/ '>

我们提供了样式,使外观更令人愉悦(是的,它们可以作为css文件连接,但是,某些版本的IE不想感知以这种方式设置的样式,因此您必须直接编写脚本):

<style>
	*{text-align: center; font-family:tahoma; font-size:14px;}
	a{text-decoration: none; color: #000;}
	a:hover{text-decoration: underline; color: #0059FF;}
	#bold{text-decoration: none; font-weight: 600;font-size:20px;}
	#table,tr,td{border-style:solid;border-width:1px;	border-collapse:collapse;padding:5px; height:22px;border-color:#7d7d7d;}
	/*   */#table tbody tr:nth-child(odd){background: #fff;}
	/*   */   #table tbody tr:nth-child(even){background: #F7F7F7;}	
	#noborder{border-width: 0 px; border-style: none;}	
	#sp30px{text-indent: 30px;text-align: justify;}
	#smallsize{font-family:tahoma; text-indent: 5px; text-align:left; font-size:12px;}
	#top {background: #ffffff;
		text-align: center;
		left:0;
		top:0px;
		table-layout: fixed;
		border-style:solid;
		border-width:0px;
		border-collapse:collapse;
		padding:0px;
		height:22px;
		border: 0px;
		z-index: 99999;
		display:block;
		width:80px;
		opacity: 0.6;
		filter: alpha(Opacity=60);
		height:100%;
		position:fixed;}
	#top:hover{background: #afafaf;opacity: 100;filter: alpha(Opacity=100);text-decoration: none;color: #000000;}
	.smalltext{padding-top: 1px;
		padding-bottom: 1px;
		text-align: bottom;
		font-family:tahoma;
		color: #a0a0a0;
		line-height: 7px;
		font-size: 10px;}
	.smalltext:hover{color: #0000ff;}		
	.transition-rotate {position: relative;
		z-index: 2;
		margin: 0 auto;
		padding: 5px;
		text-align: center;
		max-width: 500px;
		cursor: pointer;
		transition: 0.1s linear;}
	.transition-rotate:hover {-webkit-transform: rotate(-2deg);	transform: rotate(-2deg);}
	#lineheight{
		text-align: left;
		line-height: 1px;
		text-decoration: none;
		font-weight: 600;
		font-size:20px;}
</style>

样式已结束,现在编写选项卡标题并绘制一个方便的链接以返回主页:

<title>Adressbook of «YourMegaCompanyName»</title>	
</head>
<body style='background-color:#ffffff;'>";
}
echo "
<table id='top'><tr><td id='top'>
<a href='index.php?place=main' id='top' >
<br><br><br>
<img src='back_to_main.png' alt='' border='0' width='75' height='60'/>
<p> </p></a>
</td></tr></table>
";

我们通过AD确定搜索过滤器,并获取有关OU的数据:

$filter ="(&(objectcategory=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))"; // ,  .
$filter2 ="(objectCategory=OrganizationalUnit)"; //     OU
$ds=ldap_connect($srv);   
if ($ds) { 
    $r=ldap_bind($ds,$srv_login,$srv_password);;     
	ldap_set_option($ds,LDAP_OPT_REFERRALS, 0);
	ldap_set_option($ds,LDAP_OPT_PROTOCOL_VERSION,3);
	$sr=ldap_search($ds,$dn ,$filter );   
    ldap_sort($ds,$sr, "givenname");
    $info = ldap_get_entries($ds, $sr); 
    $sr2=ldap_search($ds,$dn ,$filter2 );   
    $placeinfo = ldap_get_entries($ds, $sr2); 
$PlaceName = $placeinfo[0]["l"][0];  			// name of place
$PlaceAddres = $placeinfo[0]["street"][0];		// address of place
$PlaceMail = $placeinfo[0]["description"][0]; 	// mail of place
$PlacePhone = $placeinfo[0]["st"][0]; 		// phone of plase

接下来,绘制页面顶部:

echo"<table align='center' height = '80'>
	<td id='noborder' ><div id='lineheight'>". $PlaceName ."</div></td></tr>
	<tr><td id='noborder' >". $PlaceAddres ."</td></tr>
    </table>
<table align='center' id='table'>
	<tr><td width='35' bgcolor = #f0f0e4>  № </td>
	<td width='300' bgcolor = #f0f0e4> Name </td>
	<td width='250' bgcolor = #f0f0e4> E-mail </td>
	<td width='60' bgcolor = #f0f0e4> Phone </td>
	<td width='150' bgcolor = #f0f0e4> Mobile </td></tr>
	<tr><td></td><td>  OU </td><td>";
echo "<div class='transition-rotate'><a href=mailto:" . $PlaceMail .">" . $PlaceMail ." </a></div>";
echo "</td><td width='150'> " . $PlacePhone ." </td><td> - </td></tr>";

接下来,我们循环获取和处理用户数据,并且为了隐藏某些(例如服务)帐户,我们只需在AD用户详细信息的“房间”字段中指定“隐藏”,这样的用户将不会显示在目录中:

for ($i=0; $i<$info["count"];$i++) { 
$UserHide = $info[$i]["physicaldeliveryofficename"][0];
if ($UserHide != 'hide') {
$UserName = $info[$i]["cn"][0];                // 
$UserPosition = $info[$i]["title"][0]; 		// 
$UserMail = $info[$i]["mail"][0];			//mail
if (!$UserMail)) $UserMail = "-";                  //      AD,   
$UserIpPhone = $info[$i]["ipphone"][0];		//ip phone
	if (!$UserIpPhone) $UserIpPhone = "-";    //      AD,   
$UserMobile = $info[$i]["mobile"][0];		//mobile
	if (!$UserMobile) $UserMobile = "-";     //      AD,   

顺便说一句,如果您需要获取另一个属性的值,请记住(这一点很重要):
在请求中,我们以小写形式传递属性名称,否则它将不起作用。

然后将接收到的数据粘贴到表中:

    echo "<tr>
	<td>". $n+=1 ."</td>
	<td> ". $UserName ."<br> <div class='smalltext'>". $UserPosition ."</div></td><td>"; //	    
	if ($UserMail !='-') echo "<div class='transition-rotate'><a href=mailto:'$UserMail'>$UserMail  </a></div>";    //     e-mail     
	else echo "-"; //  e-mail -  .
 	echo "<td> ". $UserIpPhone ." </td>
 	<td> ". $UserMobile ." </td></tr>";
	}
}
echo "</table>";

接下来,我们通过ldap关闭连接,或显示有关无法连接到服务器的消息:

ldap_close($ds); 
} 
else echo "<h4>Unable to connect to LDAP server</h4>"; 
echo '<br><br><br></body></html>';}

文件“ main_table.html”在内部表示一个带有链接的简单html页面,看起来像这样:

<head>
<link rel="shortcut icon" href="ico.png"/>
<meta charset="windows-1251"/>
<title>Adressbook of «YourMegaCompanyName»</title>
</head>
<body style='background-color:#ffffff;'>
<center><a href=index.php><IMG border="none" src="logo.png"/></a></center>
<center><b>Places and offices</b></center>
<br>
<table border="0" width="450" bgcolor="#dddddd" align="center" valign="middle" CELLSPACING="0">

<tr id="space"><td></td></tr>
<tr><td align="left" id="abz"><a href="index.php?place=ou1">OU1</a></td></tr>
<tr id="space"><td></td></tr>
<tr><td align="left" id="abz"><a href="index.php?place=ou2">OU2</a></td></tr>

</table></body></html>

如果我的代码可以帮助某人-我将很高兴使用它!

您还可以根据需要自由编辑(改进/降级)并通过任何方法进行分发。

感谢您的关注!

All Articles