Skip to content

Instantly share code, notes, and snippets.

@varnav
Last active January 7, 2024 10:24
Show Gist options
  • Save varnav/4100018 to your computer and use it in GitHub Desktop.
Save varnav/4100018 to your computer and use it in GitHub Desktop.
Web table listing active directory users
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Список сотрудников</title>
<style>
a {text-decoration : none}
a:link {color : #000000}
a:visited {color : #000000}
a:active {color : #000000}
a:hover {color : #0000ff}
table {
width: 100%; /* Ширина таблицы */
background: white; /* Цвет фона таблицы */
border-spacing: 1px; /* Расстояние между ячейками */
}
td, th {
padding: 5px; /* Поля вокруг текста */
}
</style>
</head>
<body>
<?php
ob_start();
// ===
// This PHP script is used to display list of Active Directory users on a webpage, grabbing it via LDAP connection.
// Evgeny Varnavsky 2016
// https://gist.github.com/varnav/4100018/
// LGPL license
// ===
setlocale(LC_ALL, 'ru_RU.UTF-8');
$time = strftime('%H:%M %e %B %Y %Z');
echo "Актуальность: $time";
// Turn on when debugging
// error_reporting(E_ALL ^ E_NOTICE);
// Get sort_by variable from URL
$sort_by=$_GET['sort_by'];
// Replacing special characters for LDAP IO
// see: RFC2254
function ldap_quote($str) {
return str_replace(
array( '\\', ' ', '*', '(', ')' ),
array( '\\5c', '\\20', '\\2a', '\\28', '\\29' ),
$str
);
for ($i = 0; $i<strlen($str); $i++) {
$char = substr($str, $i, 1);
if (ord($char)<32) {
$hex = dechex(ord($char));
if (strlen($hex) == 1) $hex = '0' . $hex;
$str = str_replace($char, '\\' . $hex, $str);
}
}
}
// Get URL of current page
$pagedisplayname = $_SERVER['PHP_SELF'];
if (!$sort_by)
{
// Setting default sorting by displayname
$sort_by = "displayname";
}
echo '<TABLE><tr>';
echo "<th id='telephonenumber'>&nbsp;<a href='$pagedisplayname?sort_by=telephonenumber'>Внут.</a></th>
<th id='displayname'>&nbsp;<a href='$pagedisplayname?sort_by=displayname'>Имя</a></th>
<th id='mobile'>&nbsp;<a href='$pagedisplayname?sort_by=mobile'>Моб.</a></th>
<th id='title'>&nbsp;<a href='$pagedisplayname?sort_by=title'>Должность</a></th>
<th id='department'>&nbsp;<a href='$pagedisplayname?sort_by=department'>Отдел</a></th>";
echo '</tr>';
//LDAP login, password and server settings
$ldaphost='dc.contoso.com';
$ldapport=389;
$ldapuser='ldapreader';
$ldappass='P@ssw0rd';
$ldapbase='DC=contoso,DC=com';
// List of attributes to grab
$ldapattr = array("cn","displayname","samaccountdisplayname", "mail", "mobile", "telephonenumber", "description", "title", "department");
// Filter output by:
// OPTION
// Only users with set telephone numbers
//$ldapfltr = "(&(objectClass=user)(telephoneNumber=*))";
// Only users
$ldapfltr = '(objectClass=user)';
// Only objects (not only users but contacts too) with set mobile and telephone numbers
// $ldapfltr = "(|(mobile=*)(telephoneNumber=*))";
// No filter
// $ldapfltr = '()';
// Search filter reference:
// http://msdn.microsoft.com/en-us/library/aa746475(VS.85).aspx
// http://support.microsoft.com/kb/555638
// Attribute reference
// Use lowercase for attribute displaynames
// https://fsuid.fsu.edu/admin/lib/WinADLDAPAttributes.html
// http://computerperformance.co.uk/Logon/LDAP_attributes_active_directory.htm
// Connect to LDAP server
$ldapconn = ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost");
// Recommended settings for AD domain controllers
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldapconn, LDAP_OPT_SIZELIMIT, 2000);
// OPTION: Access credentials. Regular user is ok!
// Log to LDAP server
ldap_bind($ldapconn,$ldapuser,$ldappass) or die("Can not bind as $ldapuser");
// Grab data with sorting
$ldapsrch = ldap_search($ldapconn, $ldapbase, $ldapfltr, $ldapattr);
ldap_sort ($ldapconn , $ldapsrch , "$sort_by");
$ldaprecs = ldap_get_entries($ldapconn, $ldapsrch);
// echo 'Пользователей получено из каталога Active Directory: <b>' . $ldaprecs['count'] . '</b><br>';
if ($ldaprecs['count'] < 1)
{
echo '<p>No users fetched from ldap. Check settings.';
}
else
// Fill variables with data
{
for ($i=0; $i < $ldaprecs['count']; $i++)
{
$mobile = $ldaprecs[$i]['mobile'][0];
$telephonenumber = $ldaprecs[$i]['telephonenumber'][0];
$description = $ldaprecs[$i]['description'][0];
$displayname = $ldaprecs[$i]['displayname'][0];
$title = $ldaprecs[$i]['title'][0];
$department = $ldaprecs[$i]['department'][0];
$mail = $ldaprecs[$i]['mail'][0];
// Populate table, but with some filtering
if (!($cn[0] == "~")&&!($title==""))
{
// Iterate row color
if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{$bgcolor='#f1f1f1';}
echo "<tr >";
echo "<td style='background: $bgcolor' headers='telephonenumber'>&nbsp;$telephonenumber</td>";
echo "<td style='background: $bgcolor' headers='displayname'>&nbsp;<a href=mailto:$mail>$displayname</a></td>";
echo "<td style='background: $bgcolor' headers='mobile'>&nbsp;$mobile</td>";
echo "<td style='background: $bgcolor' headers='title'>&nbsp;$title</td>";
echo "<td style='background: $bgcolor' headers='department'>&nbsp;$department</td>";
echo "</tr>";
}
}
}
echo '</table>';
ob_end_flush();
ldap_unbind($ldapconn);
?>
@llsrinull
Copy link

its fetch only 1000 users

@Orm1server
Copy link

Am i missing how to search in this? I have the file bound to my LDAP but I cant figure out how i am to submit a query to search with

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment