abook_ldap_server.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. ** abook_ldap_server.php
  4. **
  5. ** Address book backend for LDAP server
  6. **
  7. ** An array with the following elements must be passed to
  8. ** the class constructor (elements marked ? are optional):
  9. **
  10. ** host => LDAP server hostname/IP-address
  11. ** base => LDAP server root (base dn). Empty string allowed.
  12. ** ? port => LDAP server TCP port number (default: 389)
  13. ** ? charset => LDAP server charset (default: utf-8)
  14. ** ? name => Name for LDAP server (default "LDAP: hostname")
  15. ** Used to tag the result data
  16. ** ? maxrows => Maximum # of rows in search result
  17. **
  18. ** NOTE. This class should not be used directly. Use the
  19. ** "AddressBook" class instead.
  20. **/
  21. class abook_ldap_server extends addressbook_backend {
  22. var $btype = "remote";
  23. var $bname = "ldap_server";
  24. // Parameters changed by class
  25. var $sname = "LDAP"; // Service name
  26. var $server = ""; // LDAP server name
  27. var $port = 389; // LDAP server port
  28. var $basedn = ""; // LDAP base DN
  29. var $charset = "utf-8"; // LDAP server charset
  30. var $linkid = false; // PHP LDAP link ID
  31. var $bound = false; // True if LDAP server is bound
  32. var $maxrows = 250; // Max rows in result
  33. // Constructor. Connects to database
  34. function abook_ldap_server($param) {
  35. if(!function_exists("ldap_connect")) {
  36. $this->set_error("LDAP support missing from PHP");
  37. return;
  38. }
  39. if(is_array($param)) {
  40. $this->server = $param["host"];
  41. $this->basedn = $param["base"];
  42. if(!empty($param["port"]))
  43. $this->port = $param["port"];
  44. if(!empty($param["charset"]))
  45. $this->charset = strtolower($param["charset"]);
  46. if(isset($param["maxrows"]))
  47. $this->maxrows = $param["maxrows"];
  48. if(empty($param["name"]))
  49. $this->sname = "LDAP: ".$param["host"];
  50. else
  51. $this->sname = $param["name"];
  52. $this->open(true);
  53. } else {
  54. $this->set_error("Invalid argument to constructor");
  55. }
  56. }
  57. // Open the LDAP server. New connection if $new is true
  58. function open($new = false) {
  59. $this->error = "";
  60. // Connection is already open
  61. if($this->linkid != false && !$new)
  62. return true;
  63. $this->linkid = @ldap_connect($this->server, $this->port);
  64. if(!$this->linkid)
  65. if(function_exists("ldap_error"))
  66. return $this->set_error(ldap_error($this->linkid));
  67. else
  68. return $this->set_error("ldap_connect failed");
  69. if(!@ldap_bind($this->linkid))
  70. if(function_exists("ldap_error"))
  71. return $this->set_error(ldap_error($this->linkid));
  72. else
  73. return $this->set_error("ldap_bind failed");
  74. $this->bound = true;
  75. return true;
  76. }
  77. // Encode iso8859-1 string to the charset used by this LDAP server
  78. function charset_encode($str) {
  79. if($this->charset == "utf-8") {
  80. if(function_exists("utf8_encode"))
  81. return utf8_encode($str);
  82. else
  83. return $str;
  84. } else {
  85. return $str;
  86. }
  87. }
  88. // Decode from charset used by this LDAP server to iso8859-1
  89. function charset_decode($str) {
  90. if($this->charset == "utf-8") {
  91. if(function_exists("utf8_decode"))
  92. return utf8_decode($str);
  93. else
  94. return $str;
  95. } else {
  96. return $str;
  97. }
  98. }
  99. // ========================== Public ========================
  100. // Search the LDAP server
  101. function search($expr) {
  102. // To be replaced by advanded search expression parsing
  103. if(is_array($expr)) return false;
  104. // Encode the expression
  105. $expr = $this->charset_encode($expr);
  106. if(!ereg("\*", $expr))
  107. $expr = "*$expr*";
  108. $expression = "cn=$expr";
  109. // Make sure connection is there
  110. if(!$this->open())
  111. return false;
  112. // Do the search
  113. $sret = @ldap_search($this->linkid, $this->basedn, $expression,
  114. array("dn", "o", "ou", "sn", "givenname",
  115. "cn", "mail", "telephonenumber"));
  116. // Should get error from server using the ldap_error() function,
  117. // but it only exist in the PHP LDAP documentation.
  118. if(!$sret)
  119. if(function_exists("ldap_error"))
  120. return $this->set_error(ldap_error($this->linkid));
  121. else
  122. return $this->set_error("ldap_search failed");
  123. if(@ldap_count_entries($this->linkid, $sret) <= 0)
  124. return array();
  125. // Get results
  126. $ret = array();
  127. $returned_rows = 0;
  128. $res = @ldap_get_entries($this->linkid, $sret);
  129. for($i = 0 ; $i < $res["count"] ; $i++) {
  130. $row = $res[$i];
  131. // Extract data common for all e-mail addresses
  132. // of an object. Use only the first name
  133. $nickname = $this->charset_decode($row["dn"]);
  134. $fullname = $this->charset_decode($row["cn"][0]);
  135. if(empty($row["telephonenumber"][0])) $phone = "";
  136. else $phone = $this->charset_decode($row["telephonenumber"][0]);
  137. if(!empty($row["ou"][0]))
  138. $label = $this->charset_decode($row["ou"][0]);
  139. else if(!empty($row["o"][0]))
  140. $label = $this->charset_decode($row["o"][0]);
  141. else
  142. $label = "";
  143. if(empty($row["givenname"][0])) $firstname = "";
  144. else $firstname = $this->charset_decode($row["givenname"][0]);
  145. if(empty($row["sn"][0])) $surname = "";
  146. else $surname = $this->charset_decode($row["sn"][0]);
  147. // Add one row to result for each e-mail address
  148. for($j = 0 ; $j < $row["mail"]["count"] ; $j++) {
  149. array_push($ret, array("nickname" => $nickname,
  150. "name" => $fullname,
  151. "firstname" => $firstname,
  152. "lastname" => $surname,
  153. "email" => $row["mail"][$j],
  154. "label" => $label,
  155. "phone" => $phone,
  156. "backend" => $this->bnum,
  157. "source" => &$this->sname));
  158. // Limit number of hits
  159. $returned_rows++;
  160. if(($returned_rows >= $this->maxrows) &&
  161. ($this->maxrows > 0) ) {
  162. ldap_free_result($sret);
  163. return $ret;
  164. }
  165. }
  166. }
  167. ldap_free_result($sret);
  168. return $ret;
  169. } // end search()
  170. }
  171. ?>