abook_ldap_server.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. ** abook_ldap_server.php
  4. **
  5. ** Copyright (c) 1999-2001 The Squirrelmail Development Team
  6. ** Licensed under the GNU GPL. For full terms see the file COPYING.
  7. **
  8. ** Address book backend for LDAP server
  9. **
  10. ** An array with the following elements must be passed to
  11. ** the class constructor (elements marked ? are optional):
  12. **
  13. ** host => LDAP server hostname/IP-address
  14. ** base => LDAP server root (base dn). Empty string allowed.
  15. ** ? port => LDAP server TCP port number (default: 389)
  16. ** ? charset => LDAP server charset (default: utf-8)
  17. ** ? name => Name for LDAP server (default "LDAP: hostname")
  18. ** Used to tag the result data
  19. ** ? maxrows => Maximum # of rows in search result
  20. ** ? timeout => Timeout for LDAP operations (in seconds, default: 30)
  21. ** Might not work for all LDAP libraries or servers.
  22. **
  23. ** NOTE. This class should not be used directly. Use the
  24. ** "AddressBook" class instead.
  25. **
  26. ** $Id$
  27. **/
  28. class abook_ldap_server extends addressbook_backend {
  29. var $btype = 'remote';
  30. var $bname = 'ldap_server';
  31. // Parameters changed by class
  32. var $sname = 'LDAP'; // Service name
  33. var $server = ''; // LDAP server name
  34. var $port = 389; // LDAP server port
  35. var $basedn = ''; // LDAP base DN
  36. var $charset = 'utf-8'; // LDAP server charset
  37. var $linkid = false; // PHP LDAP link ID
  38. var $bound = false; // True if LDAP server is bound
  39. var $maxrows = 250; // Max rows in result
  40. var $timeout = 30; // Timeout for LDAP operations (in seconds)
  41. // Constructor. Connects to database
  42. function abook_ldap_server($param) {
  43. if(!function_exists('ldap_connect')) {
  44. $this->set_error('LDAP support missing from PHP');
  45. return;
  46. }
  47. if(is_array($param)) {
  48. $this->server = $param['host'];
  49. $this->basedn = $param['base'];
  50. if(!empty($param['port']))
  51. $this->port = $param['port'];
  52. if(!empty($param['charset']))
  53. $this->charset = strtolower($param['charset']);
  54. if(isset($param['maxrows']))
  55. $this->maxrows = $param['maxrows'];
  56. if(isset($param['timeout']))
  57. $this->timeout = $param['timeout'];
  58. if(empty($param['name']))
  59. $this->sname = 'LDAP: ' . $param['host'];
  60. else
  61. $this->sname = $param['name'];
  62. $this->open(true);
  63. } else {
  64. $this->set_error('Invalid argument to constructor');
  65. }
  66. }
  67. // Open the LDAP server. New connection if $new is true
  68. function open($new = false) {
  69. $this->error = '';
  70. // Connection is already open
  71. if($this->linkid != false && !$new)
  72. return true;
  73. $this->linkid = @ldap_connect($this->server, $this->port);
  74. if(!$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_connect failed');
  79. if(!@ldap_bind($this->linkid))
  80. if(function_exists('ldap_error'))
  81. return $this->set_error(ldap_error($this->linkid));
  82. else
  83. return $this->set_error('ldap_bind failed');
  84. $this->bound = true;
  85. return true;
  86. }
  87. // Encode iso8859-1 string to the charset used by this LDAP server
  88. function charset_encode($str) {
  89. if($this->charset == 'utf-8') {
  90. if(function_exists('utf8_encode'))
  91. return utf8_encode($str);
  92. else
  93. return $str;
  94. } else {
  95. return $str;
  96. }
  97. }
  98. // Decode from charset used by this LDAP server to iso8859-1
  99. function charset_decode($str) {
  100. if($this->charset == 'utf-8') {
  101. if(function_exists('utf8_decode'))
  102. return utf8_decode($str);
  103. else
  104. return $str;
  105. } else {
  106. return $str;
  107. }
  108. }
  109. // ========================== Public ========================
  110. // Search the LDAP server
  111. function search($expr) {
  112. // To be replaced by advanded search expression parsing
  113. if(is_array($expr)) return false;
  114. // Encode the expression
  115. $expr = $this->charset_encode($expr);
  116. if(strstr($expr, "*") === false)
  117. $expr = "*$expr*";
  118. $expression = "cn=$expr";
  119. // Make sure connection is there
  120. if(!$this->open())
  121. return false;
  122. // Do the search. Use improved ldap_search() if PHP version is
  123. // 4.0.2 or newer.
  124. if(sqCheckPHPVersion(4, 0, 2)) {
  125. $sret = @ldap_search($this->linkid, $this->basedn, $expression,
  126. array('dn', 'o', 'ou', 'sn', 'givenname',
  127. 'cn', 'mail', 'telephonenumber'),
  128. 0, $this->maxrows, $this->timeout);
  129. } else {
  130. $sret = @ldap_search($this->linkid, $this->basedn, $expression,
  131. array('dn', 'o', 'ou', 'sn', 'givenname',
  132. 'cn', 'mail', 'telephonenumber'));
  133. }
  134. // Should get error from server using the ldap_error() function,
  135. // but it only exist in the PHP LDAP documentation.
  136. if(!$sret)
  137. if(function_exists('ldap_error'))
  138. return $this->set_error(ldap_error($this->linkid));
  139. else
  140. return $this->set_error('ldap_search failed');
  141. if(@ldap_count_entries($this->linkid, $sret) <= 0)
  142. return array();
  143. // Get results
  144. $ret = array();
  145. $returned_rows = 0;
  146. $res = @ldap_get_entries($this->linkid, $sret);
  147. for($i = 0 ; $i < $res['count'] ; $i++) {
  148. $row = $res[$i];
  149. // Extract data common for all e-mail addresses
  150. // of an object. Use only the first name
  151. $nickname = $this->charset_decode($row['dn']);
  152. $fullname = $this->charset_decode($row['cn'][0]);
  153. if(empty($row['telephonenumber'][0])) $phone = '';
  154. else $phone = $this->charset_decode($row['telephonenumber'][0]);
  155. if(!empty($row['ou'][0]))
  156. $label = $this->charset_decode($row['ou'][0]);
  157. else if(!empty($row['o'][0]))
  158. $label = $this->charset_decode($row['o'][0]);
  159. else
  160. $label = '';
  161. if(empty($row['givenname'][0])) $firstname = '';
  162. else $firstname = $this->charset_decode($row['givenname'][0]);
  163. if(empty($row['sn'][0])) $surname = '';
  164. else $surname = $this->charset_decode($row['sn'][0]);
  165. // Add one row to result for each e-mail address
  166. if(isset($row['mail']['count'])) {
  167. for($j = 0 ; $j < $row['mail']['count'] ; $j++) {
  168. array_push($ret, array('nickname' => $nickname,
  169. 'name' => $fullname,
  170. 'firstname' => $firstname,
  171. 'lastname' => $surname,
  172. 'email' => $row['mail'][$j],
  173. 'label' => $label,
  174. 'phone' => $phone,
  175. 'backend' => $this->bnum,
  176. 'source' => &$this->sname));
  177. // Limit number of hits
  178. $returned_rows++;
  179. if(($returned_rows >= $this->maxrows) &&
  180. ($this->maxrows > 0) ) {
  181. ldap_free_result($sret);
  182. return $ret;
  183. }
  184. } // for($j ...)
  185. } // isset($row['mail']['count'])
  186. }
  187. ldap_free_result($sret);
  188. return $ret;
  189. } // end search()
  190. // If you run a tiny LDAP server and you want the "List All" button
  191. // to show EVERYONE, then uncomment this tiny block of code:
  192. //
  193. // function list_addr() {
  194. // return $this->search("*");
  195. // }
  196. //
  197. // Careful with this -- it could get quite large for big sites.
  198. }
  199. ?>