Skip to content

Instantly share code, notes, and snippets.

@remoharsono
Created October 21, 2019 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remoharsono/55f317f84c85489d051ba0068b16fb84 to your computer and use it in GitHub Desktop.
Save remoharsono/55f317f84c85489d051ba0068b16fb84 to your computer and use it in GitHub Desktop.
My LDAP Search Implementing Pagination
<?php
define("LDAP_HOST","mycompany.co.id");
define("LDAP_PORT", "389");
define("BASE_DN", "dc=mycompany,dc=co,dc=id");
define("PAGE_SIZE", 100); // limit
define("LDAP_USER", "username@mycompany.co.id");
define("LDAP_PASSWORD", 'secret');
define("BR", "\n");
define("VL", " | ");
$arrUnlisted = array(
'OU=Computer Users',
'OU=Distribution',
'CN=Computers',
'CN=Policies',
'CN=System',
'CN=Operations',
'CN=DomainUpdates',
'CN=Topology',
'DFSR-LocalSettings',
'OU=Temporary account'
);
$ldap_connection = ldap_connect(LDAP_HOST, LDAP_PORT);
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);
$bind = ldap_bind($ldap_connection, LDAP_USER, LDAP_PASSWORD);
$filter = "(cn=*)";
$cookie = '';
$pageCounter = 1;
$counter = 1;
do {
ldap_control_paged_result($ldap_connection, PAGE_SIZE, true, $cookie);
$searchResult = ldap_search($ldap_connection, BASE_DN, $filter);
$entries = ldap_get_entries($ldap_connection, $searchResult);
foreach($entries as $entry) {
if (isset($entry['mail'])) {
$cn = $entry['cn'];
$name = $cn[0];
$mail = $entry['mail'][0];
$dn = $entry['dn'];
if (strpos($dn, ',') !== false) {
$arrDN = explode(",", $dn);
$ou = $arrDN[1];
if (!in_array($ou, $arrUnlisted)) {
echo $counter . VL . $name . VL . $dn . VL . $mail . BR;
$counter++;
}
}
}
}
$pageCounter++;
ldap_control_paged_result_response($ldap_connection, $searchResult, $cookie);
} while($cookie !== null && $cookie != '');
ldap_close($ldap_connection);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment