Skip to content

Instantly share code, notes, and snippets.

@stojg
Created January 8, 2013 01:07
Show Gist options
  • Save stojg/4480195 to your computer and use it in GitHub Desktop.
Save stojg/4480195 to your computer and use it in GitHub Desktop.
<?php
$host = 'ldap://localhost';
$username = 'DOMAIN\\username';
$password = 'password';
$dn = "OU=Users,DC=company,DC=co,DC=nz";
if(!$argv[1]) {
$searchFilter = '(employeeID=*)';
} else {
$searchFilter = '(employeeID='.$argv[1].')';
}
// Connect to ldap host
$ldap = ldap_connect($host);
if(!$ldap) {
echo '[-] Cant connect to server "'.$host.'"';
exit(1);
}
echo '[+] Connected to '.$host.PHP_EOL;
if(!ldap_bind($ldap, $username, $password)) {
echo '[-] Cant bind to server "'.$host.'", '.ldap_error($ldap).PHP_EOL;
exit(1);
}
echo '[+] Binded to '.$host.PHP_EOL;
$resultResource = ldap_search($ldap, $dn, $searchFilter, array('uid', 'employeeID','cn', 'lastlogontimestamp'), 0, 10);
if(!$resultResource) {
echo '[-] Error: '.ldap_error($ldap).PHP_EOL;
exit(1);
}
ldap_sort($ldap, $resultResource, 'lastlogontimestamp');
echo 'Done the search for '.$searchFilter.PHP_EOL;
// Get the result from the search result
$items = ldap_get_entries($ldap, $resultResource);
foreach($items as $item) {
if(!$item['dn']) { continue; }
echo 'cn: '.$item['cn'][0];
$cid = $item['employeeid'][0];
echo ' | '.$cid.' ';
echo PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment