SHARE
TWEET

Untitled

a guest Sep 4th, 2018 137 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. PHP LDAP Get user details of member which is a member of a group
  2. <?php
  3. $ldap_server = "AD_Server.domain.pri:389";
  4. $auth_user = "user@domain.pri";
  5. $auth_pass = "password";
  6.  
  7. $base_dn = "OU=IM Groups,OU=GLOBAL,DC=domain,DC=pri";
  8. $filter = "(&(objectCategory=user)(memberOf=IM-ALL_USERS))";
  9.  
  10. // connect to server
  11. if (!($connect=@ldap_connect($ldap_server))) {
  12.      die("Could not connect to ldap server");
  13. }
  14.  
  15. // bind to server
  16. if (!($bind = ldap_bind($connect, $auth_user, $auth_pass))) {
  17.      die("Unable to bind to server");
  18. }
  19.  
  20. // search active directory
  21. if (!($search = ldap_search($connect, $base_dn, $filter))) {
  22.      die("Unable to search ldap server");
  23. }
  24.  
  25. $number_returned = ldap_count_entries($connect,$search);
  26. $info = ldap_get_entries($connect, $search);
  27.  
  28. echo "The number of entries returned is ". $number_returned."<p>";
  29.  
  30. for ($i=0; $i<$info["count"]; $i++) {
  31.    echo "Name is: ". $info[$i]["givenname"][0]."<br>";
  32.    echo "Display name is: ". $info[$i]["displayname"][0]."<br>";
  33.    echo "Email is: ". $info[$i]["mail"][0]."<br>";
  34.    echo "Telephone number is: ". $info[$i]["telephonenumber"][0]."<p>";
  35. }
  36. ?>
  37.    
  38. <?php
  39.  
  40. function explode_dn($dn, $with_attributes=0)
  41. {
  42.     $result = ldap_explode_dn($dn, $with_attributes);
  43.     foreach($result as $key => $value) $result[$key] = preg_replace("/\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\1')).''", $value);
  44.     return $result;
  45. }
  46.  
  47. function get_members($group,$user,$password) {
  48.     $ldap_host = "LDAPSERVER";
  49.     $ldap_dn = "OU=some_group,OU=some_group,DC=company,DC=com";
  50.     $base_dn = "DC=company,DC=com";
  51.     $ldap_usr_dom = "@company.com";
  52.     $ldap = ldap_connect($ldap_host);
  53.  
  54.     ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION,3);
  55.     ldap_set_option($ldap, LDAP_OPT_REFERRALS,0);
  56.  
  57.     ldap_bind($ldap, $user . $ldap_usr_dom, $password);
  58.     $results = ldap_search($ldap,$ldap_dn, "cn=" . $group);
  59.     $member_list = ldap_get_entries($ldap, $results);
  60.  
  61.     $dirty = 0;
  62.     $group_member_details = array();
  63.  
  64.     foreach($member_list[0]['member'] as $member) {
  65.         if($dirty == 0) {
  66.             $dirty = 1;
  67.         } else {
  68.             $member_dn = explode_dn($member);
  69.             $member_cn = str_replace("CN=","",$member_dn[0]);
  70.             $member_search = ldap_search($ldap, $base_dn, "(CN=" . $member_cn . ")");
  71.             $member_details = ldap_get_entries($ldap, $member_search);
  72.             $group_member_details[] = array($member_details[0]['givenname'][0],$member_details[0]['sn'][0],$member_details[0]['telephonenumber'][0],$member_details[0]['othertelephone'][0]);
  73.         }
  74.     }
  75.     ldap_close($ldap);
  76.     return $group_member_details;
  77. }
  78.  
  79. // Specify the group from where to get members and a username and password with rights to query it
  80. $result = get_members("groupname","username","password");
  81.  
  82. // The following will create an XML file with the details from $group_member_details
  83. $xml = simplexml_load_string("<?xml version='1.0'?>n<AddressBook></AddressBook>");
  84. $version = $xml->addChild('version', '1');
  85.  
  86. foreach($result as $e) {
  87.     $contact = $xml->addChild('Contact');
  88.     $contact->addChild('FirstName', $e[0]);
  89.     $contact->addChild('LastName', $e[1]);
  90.     $phone = $contact->addChild('Phone');
  91.     if ($e[3] == '') {
  92.                 $phone->addChild('phonenumber', '0');
  93.         } else {
  94.                 $phone->addChild('phonenumber', $e[3]);
  95.         }
  96.     $phone->addChild('accountindex', '0');
  97.     $phone = $contact->addChild('Phone');
  98.     if ($e[2] == '') {
  99.         $phone->addChild('phonenumber', '0');
  100.     } else {
  101.         $phone->addChild('phonenumber', $e[2]);
  102.     }
  103.     $phone->addChild('accountindex', '1');
  104.     $contact->addChild('Group', '0');
  105.     $contact->addChild('PhotoUrl', 'empty');
  106. }
  107.  
  108. $xml->asXML('phonebook.xml');
  109.  
  110. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top