abook_ldap_server.php 6.6 KB

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