abook_ldap_server.php 6.7 KB

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