Ldap.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * LDAP client.
  4. *
  5. ldap_connect() // establish connection to server
  6. |
  7. ldap_bind() // anonymous or authenticated "login"
  8. |
  9. do something like search or update the directory
  10. and display the results
  11. |
  12. ldap_close() // "logout"
  13. */
  14. class Core_Client_Ldap {
  15. private $_conn;
  16. private $_protocol_version;
  17. private $_base_dn;
  18. private $_user;
  19. private $_pass;
  20. private $_LDAP_OPT_DIAGNOSTIC_MESSAGE = 0x0032;
  21. public function __construct($host, $user, $pass, $base_dn, $version = 3) {
  22. $this->_base_dn = $base_dn;
  23. $this->_user = $user;
  24. $this->_pass = $pass;
  25. if (0) {// TODO: if base_dn not set - set from $host name
  26. if (empty($this->_base_dn)) {
  27. $serverName = $host;
  28. if (false !== ($pos = strrpos($serverName, ':'))) {
  29. $serverName = substr($serverName, 0, $pos);
  30. }
  31. if (false !== ($pos = strrpos($serverName, '/'))) {
  32. $serverName = substr($serverName, $pos + 1);
  33. }
  34. $ldapDcArr = array();
  35. $dc_arr = explode('.', $serverName);
  36. foreach ($dc_arr as $vDc) {
  37. $ldapDcArr[] = "dc={$vDc}";
  38. }
  39. $this->_base_dn = implode(',', $ldapDcArr);
  40. }
  41. }
  42. $this->_conn = ldap_connect($host);
  43. if (!is_resource($this->_conn)) {
  44. trigger_error("Error connecting to LDAP", E_USER_WARNING);
  45. }
  46. else {
  47. // use ldap 3 version
  48. $this->_protocol_version = 2;
  49. if ($version == 3) {
  50. if (ldap_set_option($this->_conn, LDAP_OPT_PROTOCOL_VERSION, 3)) {
  51. $this->_protocol_version = 3;// Using LDAP v3
  52. } else {
  53. // Failed to set version to protocol 3
  54. }
  55. }
  56. // ldap_set_option($ldap->conn, LDAP_OPT_REFERRALS, 0);
  57. // TODO:?: bind to the LDAP server if user and pass is set
  58. //if ($user && $pass) {
  59. // $this->bind($user, $pass);
  60. //}
  61. }
  62. }
  63. public function __destruct() {
  64. if (is_resource($this->_conn)) {
  65. @ ldap_close($this->_conn);
  66. }
  67. }
  68. public function isConnected() {
  69. return is_resource($this->_conn);
  70. }
  71. public function getBaseDN() {
  72. return $this->_base_dn;
  73. }
  74. public function bind($user_rdn, $pass, &$errorMsg) {
  75. $bind = ldap_bind($this->_conn, $user_rdn, $pass);
  76. if ($bind) {
  77. return true;
  78. }
  79. if (ldap_get_option($this->_conn, $this->_LDAP_OPT_DIAGNOSTIC_MESSAGE, $extended_error)) {
  80. $errorMsg = 'Error Binding to LDAP: '.$extended_error;
  81. } else {
  82. $errorMsg = 'Error Binding to LDAP: No additional information is available.';
  83. }
  84. return false;
  85. }
  86. public function bindDiradmin(&$errorMsg = '') {
  87. $bind = ldap_bind($this->_conn, "uid={$this->_user},cn=users,{$this->_base_dn}", $this->_pass);
  88. if ($bind) {
  89. return true;
  90. }
  91. if (ldap_get_option($this->_conn, $this->_LDAP_OPT_DIAGNOSTIC_MESSAGE, $extended_error)) {
  92. $errorMsg = 'Error Binding to LDAP: '.$extended_error;
  93. } else {
  94. $errorMsg = 'Error Binding to LDAP: No additional information is available.';
  95. }
  96. return false;
  97. }
  98. public function search($query, $domain = '', $attributes = array()) {
  99. $domain = ($domain)? "{$domain},{$this->_base_dn}" : $this->_base_dn;
  100. $res = ldap_search($this->_conn, $domain, $query, $attributes);
  101. if (!is_resource($res)) {
  102. trigger_error("Error LDAP search '{$query}', '{$domain}'", E_USER_WARNING);
  103. }
  104. return $res;
  105. }
  106. public function count_entries($res) {
  107. return ldap_count_entries($this->_conn, $res);
  108. }
  109. public function first_entry($res) {
  110. $entry = ldap_first_entry($this->_conn, $res);
  111. return $entry;
  112. }
  113. public function get_dn($entry) {
  114. return ldap_get_dn($this->_conn, $entry);
  115. }
  116. public function get_values($entry, $attr) {
  117. return ldap_get_values($this->_conn, $entry, $attr);
  118. }
  119. public function get_attributes($entry) {
  120. $attrs = ldap_get_attributes($this->_conn, $entry);
  121. return $attrs;
  122. }
  123. public function next_entry($entry) {
  124. $entry = ldap_next_entry($this->_conn, $entry);
  125. return $entry;
  126. }
  127. public function free_result($res) {
  128. ldap_free_result($res);
  129. }
  130. public function error() {
  131. return ldap_error($this->_conn);
  132. }
  133. public function get_entries($res) {
  134. return ldap_get_entries($this->_conn, $res);
  135. }
  136. /**
  137. * example: $attr['email'] = 'foo@bar.com';
  138. */
  139. public function mod_replace($userUid, $attr) {
  140. $result = ldap_mod_replace($this->_conn, "uid={$userUid},cn=users,{$this->_base_dn}", $attr);
  141. if (true === $result) {
  142. return true;
  143. }
  144. return false;
  145. }
  146. public function mod_add($userUid, $attr) {
  147. $result = ldap_mod_add($this->_conn, "uid={$userUid},cn=users,{$this->_base_dn}", $attr);
  148. if (true === $result) {
  149. return true;
  150. }
  151. return false;
  152. }
  153. public function groupAttrUpdate($groupUid, $attr) {
  154. $result = ldap_mod_replace($this->_conn, "cn={$groupUid},cn=groups,{$this->_base_dn}", $attr);
  155. //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">ldap_mod_replace('."cn={$groupUid},cn=groups,{$this->_base_dn}".') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($result);echo'</pre>';
  156. if (true === $result) {
  157. return true;
  158. }
  159. return false;
  160. }
  161. public function mod_del($userUid, $attr) {
  162. $result = ldap_mod_del($this->_conn, "uid={$userUid},cn=users,{$this->_base_dn}", $attr);
  163. if (true === $result) {
  164. return true;
  165. }
  166. return false;
  167. }
  168. }