Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LDAP AD
<?php
$ldapconn = ldap_connect('domain.com')
or die("Could not connect to LDAP server.");
$un = 'domain\\username';
$pw = 'password';
$ldapbind = ldap_bind($ldapconn, $un, $pw);
if (!$ldapbind) {
die("Error binding");
}
$filter = '(CN=*)';
$sr = ldap_search($ldapconn, 'CN=Users,DC=domain,DC=com', $filter);
if (!$sr) {
die("search error: ".ldap_error($ldapconn));
}
$info = ldap_get_entries($ldapconn, $sr);
$props = [];
foreach ($info as $obj) {
if (!is_array($obj['objectclass']) || !in_array('user', $obj['objectclass'])) {
continue;
}
print $obj['cn'][0]."\n";
if (isset($obj['samaccountname'])) {
print "\t".$obj['samaccountname'][0]."\n";
}
if (isset($obj['mail'])) {
print "\t".$obj['mail'][0]."\n";
}
if (isset($obj['wwwhomepage'])) {
print "\t".$obj['wwwhomepage'][0]."\n";
}
if (isset($obj['objectguid'])) {
print "\t".bin2hex($obj['objectguid'][0])."\n";
}
if (isset($obj['memberof'])) {
print "\t".implode(" , ", $obj['memberof'])."\n";
}
//print $obj['objectclass'][0] . " | ";
print "\n";
foreach ($obj as $k=>$v) {
if (is_string($k)) {
$props[$k] = $k;
}
if (!is_array($v)) {
$v = array($v);
}
}
print "\n\n";
print json_encode($props, JSON_PRETTY_PRINT);
//{
// "objectclass": "objectclass",
// "cn": "cn",
// "sn": "sn",
// "givenname": "givenname",
// "distinguishedname": "distinguishedname",
// "instancetype": "instancetype",
// "whencreated": "whencreated",
// "whenchanged": "whenchanged",
// "displayname": "displayname",
// "usncreated": "usncreated",
// "memberof": "memberof",
// "usnchanged": "usnchanged",
// "wwwhomepage": "wwwhomepage",
// "name": "name",
// "objectguid": "objectguid",
// "useraccountcontrol": "useraccountcontrol",
// "badpwdcount": "badpwdcount",
// "codepage": "codepage",
// "countrycode": "countrycode",
// "badpasswordtime": "badpasswordtime",
// "lastlogoff": "lastlogoff",
// "lastlogon": "lastlogon",
// "pwdlastset": "pwdlastset",
// "primarygroupid": "primarygroupid",
// "objectsid": "objectsid",
// "accountexpires": "accountexpires",
// "logoncount": "logoncount",
// "samaccountname": "samaccountname",
// "samaccounttype": "samaccounttype",
// "lockouttime": "lockouttime",
// "objectcategory": "objectcategory",
// "dscorepropagationdata": "dscorepropagationdata",
// "lastlogontimestamp": "lastlogontimestamp",
// "mail": "mail",
// "count": "count",
// "dn": "dn",
// "userprincipalname": "userprincipalname",
// "description": "description",
// "admincount": "admincount",
// "iscriticalsystemobject": "iscriticalsystemobject",
// "showinadvancedviewonly": "showinadvancedviewonly",
// "serviceprincipalname": "serviceprincipalname",
// "msds-supportedencryptiontypes": "msds-supportedencryptiontypes"
//}
objectclass
cn
sn
givenname
distinguishedname
instancetype
whencreated
whenchanged
displayname
usncreated
memberof
usnchanged
wwwhomepage
name
objectguid
useraccountcontrol
badpwdcount
codepage
countrycode
badpasswordtime
lastlogoff
lastlogon
pwdlastset
primarygroupid
objectsid
accountexpires
logoncount
samaccountname
samaccounttype
lockouttime
objectcategory
dscorepropagationdata
lastlogontimestamp
mail
count
dn
userprincipalname
description
admincount
iscriticalsystemobject
showinadvancedviewonly
serviceprincipalname
msds-supportedencryptiontypes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment