Large company had many remote offices with a decent number of users. All offices are connected to the same network with a common domain, each office was defined in Active Directory (hereinafter referred to as AD) as Organization Unit (OU), in which users were already established
It was necessary to give users the opportunity to quickly and without much effort get the contact details of the necessary employee from AD, and system administrators to free from the routine of editing a text file that played the role of an address book.
There were no ready-made suitable options for solving the task, so I had to do everything with my own hands and head.
To begin with, first you need to decide what to use, it’s simple – the final guide should be accessible to all users of the domain through a browser. The first thing that comes to mind is PHP in conjunction with ldap, and we will use them. The biggest advantage of using PHP, I consider its relative simplicity, is that anyone who understands the system administrator at least a little can make the necessary changes to the code, if necessary, without particularly straining.
So, let’s begin. First, let’s set domain connection parameters:
$srv ="SERVER"; $srv_domain ="DOMAIN.COM"; $srv_login ="USERNAME@".$srv_domain; $srv_password ="PASSWORD";
The next point is to determine in which OU we will search for users. We will do this by intercepting the values from $ _GET [‘place’]. For example, if the user navigates to server / index.php? Place = first, then the $ place variable will be assigned the value first.
$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";
The $ doscript variable is needed in order to store the value – whether we determined the OU in which we will search for users or not. If there are no matches listed in the “switch-case”, then $ doscript = false, the main part of the script will not be executed, and the start page “main_table.html” will be displayed (I’ll tell you about it at the very end).
If we defined OU, then we proceed with further actions: we begin to make the user’s manual page:
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/ '>
We include styles for a more pleasant appearance (yes, they could be connected as a css file, however, some versions of IE do not want to perceive the styles set in this way, so you have to write directly to the script):
<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>
The styles are over, now we’ll write the tab title and draw a convenient link to return to the main page:
<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> ";
We determine the search filters by AD, and get data about the OU:
$filter ="(&(objectcategory=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))"; /
$filter2 ="(objectCategory=OrganizationalUnit)"; //
$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 plasedraw the top of the page:
Next, we get and process user data in a cycle, in order to hide some (for example, service) accounts, we simply write “hide” in the “room” field in the user’s details in AD, such users will not be displayed in the directory:
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 = "-"; $UserIpPhone = $info[$i]["ipphone"][0]; //ip phone if (!$UserIpPhone) $UserIpPhone = "-"; $UserMobile = $info[$i]["mobile"][0]; //mobile if (!$UserMobile) $UserMobile = "-";
By the way, if you need to get the value of another attribute, then remember (this is important):
in the request we pass the attribute name in lower case, otherwise it will not work.
And we paste the received data into the table:
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>"; } echo "</table>";
Next, we close the connection via ldap, or display a message about the impossibility of connecting to the server:
ldap_close($ds); } else echo "<h4>Unable to connect to LDAP server</h4>"; echo '<br><br><br></body></html>';}
The file “main_table.html” internally represents a simple html page with links, and looks like this:
<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>
If my code helps someone – I will be glad!
You can also freely edit it as you wish (improve / degrade) and distribute it by any methods.
Thanks for attention!