code:
<?php function getLdapInfo($user,$password,$domain) { // Config: $ldap_server = "ldaps://<YOUR_LDAP_SERVER>"; $defaultDN = "dc=<DOMAIN_NAME>,dc=<DOMAIN_TLD>"; $filter = '(&(objectClass=user)(sAMAccountName='.$user.'))'; // Script $ad = @ldap_connect($ldap_server); ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3); ldap_set_option($ad, LDAP_OPT_REFERRALS, 0); $bd = @ldap_bind($ad,$user.'@'.$domain,$password); if (ldap_errno($ad)) { switch(ldap_errno($ad)) { case 81: # Can't connect to LDAP Server return(array( 'errorno' => -1, 'errorstr' => 'Can\'t connect to Domain Server', 'errortxt' => 'Error #'.ldap_errno($ad).': '.ldap_err2str(ldap_errno($ad)) )); case 49: # Can't authenticate return(array( 'errorno' => -2, 'errorstr' => 'Invalid Login/Password', 'errortxt' => 'Error #'.ldap_errno($ad).': '.ldap_err2str(ldap_errno($ad)) )); default: # Unknown Error return(array( 'errorno' => -99, 'errorstr' => 'Unknown Error, Please contact support', 'errortxt' => 'Error #'.ldap_errno($ad).': '.ldap_err2str(ldap_errno($ad)) )); } } $result = @ldap_search($ad, $defaultDN, $filter, array('memberOf','displayName','mail','sAMAccountName')); $entries = @ldap_get_entries($ad, $result); if (ldap_errno($ad)) { return(array( 'errorno' => -3, 'errorstr' => 'Configuration Error, Please contact support', 'errortxt' => 'Error #'.ldap_errno($ad).': '.ldap_err2str(ldap_errno($ad)) )); } $_RETURN[id] = $entries[0]["samaccountname"][0]; $_RETURN[name] = $entries[0]["displayname"][0]; $_RETURN[email] = $entries[0]["mail"][0]; $_RETURN[groups] = array(); for ($i=0; $i < $entries[0]["memberof"]["count"]; $i++) { array_push($_RETURN[groups],substr($entries[0]["memberof"][$i],3,strpos($entries[0]["memberof"][$i],',')-3)); } return($_RETURN); ldap_unbind($ad); } // Test Code print "<pre>"; print_r(getLdapInfo('<USERNAME>','<PASSWORD>','<DOMAIN_CONTROLLER')); ?>
script end
result of the scriptArray ( [id] => adambotbyl [name] => Adam Botbyl [email] => adam@locateadam.com [groups] => Array ( [0] => Web Blog [1] => Admin ) )
No comments:
Post a Comment