LDAP.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. ldap_connect() // establish connection to server
  4. |
  5. ldap_bind() // anonymous or authenticated "login"
  6. |
  7. do something like search or update the directory
  8. and display the results
  9. |
  10. ldap_close() // "logout"
  11. */
  12. class LDAP {
  13. public static function getInstance($ldap = null) {
  14. static $_instance;
  15. if (!is_array($_instance)) {
  16. $_instance = array();
  17. }
  18. $ldapConfName = 'default_ldap';
  19. if (is_numeric($ldap) && $ldap > 0) {
  20. $ldapConfName = "ldap_{$ldap}";
  21. }
  22. if (!array_key_exists($ldapConfName, $_instance)) {
  23. $_instance[$ldapConfName] = null;
  24. Lib::loadClass('Config');
  25. $conf = Config::getConfFile($ldapConfName);
  26. if ($conf) {
  27. $version = V::get('version', 3, $conf);
  28. $host = V::get('host', '', $conf);
  29. $port = V::get('port', '', $conf);
  30. $user = V::get('user', '', $conf);
  31. $pass = V::get('pass', '', $conf);
  32. $base_dn = V::get('base_dn', '', $conf);
  33. if ($base_dn && $host) {
  34. if ($port && $host) {
  35. $host .= ":{$port}";
  36. }
  37. $clientClass = 'Core_Client_Ldap';
  38. Lib::loadClass($clientClass);
  39. if (class_exists($clientClass)) {
  40. $_instance[$ldapConfName] = new $clientClass($host, $user, $pass, $base_dn, $version);
  41. }
  42. } else {
  43. trigger_error("Config file for ldap {$ldapConfName} not set: base_dn, host!", E_USER_WARNING);
  44. }
  45. } else {
  46. trigger_error("Config file for ldap {$ldapConfName} not exists!", E_USER_WARNING);
  47. }
  48. }
  49. return $_instance[$ldapConfName];
  50. }
  51. public static function TEST_DUMP_1($ldap, $res) {
  52. $info = $ldap->get_entries($res);
  53. echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">info: ';print_r($info);echo'</pre>';
  54. echo'<pre style="overflow:auto;border:1px solid orange;">';
  55. // print number of entries found
  56. echo "Number of entries found: " . $ldap->count_entries($res) . "\n";
  57. // iterate over array and print data for each entry
  58. for ($i=0; $i < $info['count']; $i++) {
  59. echo"\n".'-------------------------';
  60. echo"\n".'dn: <em>('.$info[$i]['dn'].')</em>';
  61. for ($j=0; $j < $info[$i]['count']; $j++) {
  62. $k2 = $info[$i][$j];
  63. echo"\n".'<b>'.$k2.'</b>: ';
  64. for ($k=0; $k < $info[$i][$k2]['count']; $k++) {
  65. echo '"'.$info[$i][$k2][$k].'" ';
  66. }
  67. }
  68. }
  69. echo'</pre>';
  70. }
  71. public static function TEST_DUMP($ldap, $res) {
  72. $ldap = self::getInstance();
  73. echo'<pre style="overflow:auto;border:1px solid orange;max-height:400px;">';
  74. // print number of entries found
  75. echo "Number of entries found: " . $ldap->count_entries($res) . "\n";
  76. $entry = $ldap->first_entry($res);
  77. while ($entry) {
  78. $dn = $ldap->get_dn($entry);
  79. echo "<b>$dn</b>\n";
  80. $attrs = $ldap->get_attributes($entry);
  81. for ( $i=0; $i < $attrs['count']; $i++) {
  82. echo "$attrs[$i]: ";
  83. for ( $j=0; $j < $attrs[$attrs[$i]]['count']; $j++ ) {
  84. echo $attrs[$attrs[$i]][$j] . " ";
  85. }
  86. echo "\n";
  87. }
  88. echo "\n";
  89. $entry = $ldap->next_entry($entry);
  90. }
  91. $ldap->free_result($res);
  92. echo'</pre>';
  93. }
  94. }