abook_ldap_server.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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(is_array($param)) {
  36. $this->server = $param["host"];
  37. $this->basedn = $param["base"];
  38. if(!empty($param["port"]))
  39. $this->port = $param["port"];
  40. if(!empty($param["charset"]))
  41. $this->charset = strtolower($param["charset"]);
  42. if(isset($param["maxrows"]))
  43. $this->maxrows = $param["maxrows"];
  44. if(empty($param["name"]))
  45. $this->sname = "LDAP: ".$param["host"];
  46. else
  47. $this->sname = $param["name"];
  48. $this->open(true);
  49. } else {
  50. $this->set_error(_("Invalid argument to constructor"));
  51. }
  52. }
  53. // Open the LDAP server. New connection if $new is true
  54. function open($new = false) {
  55. $this->error = "";
  56. // Connection is already open
  57. if($this->linkid != false && !$new)
  58. return true;
  59. $this->linkid = @ldap_connect($this->server, $this->port);
  60. if(!$this->linkid)
  61. if(function_exists("ldap_error"))
  62. return $this->set_error(ldap_error($this->linkid));
  63. else
  64. return $this->set_error("ldap_connect failed");
  65. if(!@ldap_bind($this->linkid))
  66. if(function_exists("ldap_error"))
  67. return $this->set_error(ldap_error($this->linkid));
  68. else
  69. return $this->set_error("ldap_bind failed");
  70. $this->bound = true;
  71. return true;
  72. }
  73. // Encode iso8859-1 string to the charset used by this LDAP server
  74. function charset_encode($str) {
  75. if($this->charset == "utf-8") {
  76. if(function_exists("utf8_encode"))
  77. return utf8_encode($str);
  78. else
  79. return $str;
  80. } else {
  81. return $str;
  82. }
  83. }
  84. // Decode from charset used by this LDAP server to iso8859-1
  85. function charset_decode($str) {
  86. if($this->charset == "utf-8") {
  87. if(function_exists("utf8_decode"))
  88. return utf8_decode($str);
  89. else
  90. return $str;
  91. } else {
  92. return $str;
  93. }
  94. }
  95. // ========================== Public ========================
  96. // Search the LDAP server
  97. function search($expr) {
  98. // To be replaced by advanded search expression parsing
  99. if(is_array($expr)) return false;
  100. // Encode the expression
  101. $expr = $this->charset_encode($expr);
  102. if(!ereg("\*", $expr))
  103. $expr = "*$expr*";
  104. $expression = "cn=$expr";
  105. // Make sure connection is there
  106. if(!$this->open())
  107. return false;
  108. // Do the search
  109. $sret = @ldap_search($this->linkid, $this->basedn, $expression,
  110. array("dn", "o", "ou", "sn", "givenname",
  111. "cn", "mail", "telephonenumber"));
  112. // Should get error from server using the ldap_error() function,
  113. // but it only exist in the PHP LDAP documentation.
  114. if(!$sret)
  115. if(function_exists("ldap_error"))
  116. return $this->set_error(ldap_error($this->linkid));
  117. else
  118. return $this->set_error("ldap_search failed");
  119. if(@ldap_count_entries($this->linkid, $sret) <= 0)
  120. return array();
  121. // Get results
  122. $ret = array();
  123. $returned_rows = 0;
  124. $res = @ldap_get_entries($this->linkid, $sret);
  125. for($i = 0 ; $i < $res["count"] ; $i++) {
  126. $row = $res[$i];
  127. // Extract data common for all e-mail addresses
  128. // of an object. Use only the first name
  129. $nickname = $this->charset_decode($row["dn"]);
  130. $fullname = $this->charset_decode($row["cn"][0]);
  131. if(empty($row["telephonenumber"][0])) $phone = "";
  132. else $phone = $this->charset_decode($row["telephonenumber"][0]);
  133. if(!empty($row["ou"][0]))
  134. $label = $this->charset_decode($row["ou"][0]);
  135. else if(!empty($row["o"][0]))
  136. $label = $this->charset_decode($row["o"][0]);
  137. else
  138. $label = "";
  139. if(empty($row["givenname"][0])) $firstname = "";
  140. else $firstname = $this->charset_decode($row["givenname"][0]);
  141. if(empty($row["sn"][0])) $surname = "";
  142. else $surname = $this->charset_decode($row["sn"][0]);
  143. // Add one row to result for each e-mail address
  144. for($j = 0 ; $j < $row["mail"]["count"] ; $j++) {
  145. array_push($ret, array("nickname" => $nickname,
  146. "name" => $fullname,
  147. "firstname" => $firstname,
  148. "lastname" => $surname,
  149. "email" => $row["mail"][$j],
  150. "label" => $label,
  151. "phone" => $phone,
  152. "backend" => $this->bnum,
  153. "source" => &$this->sname));
  154. // Limit number of hits
  155. $returned_rows++;
  156. if(($returned_rows >= $this->maxrows) &&
  157. ($this->maxrows > 0) ) {
  158. ldap_free_result($sret);
  159. return $ret;
  160. }
  161. }
  162. }
  163. ldap_free_result($sret);
  164. return $ret;
  165. } // end search()
  166. }
  167. ?>