abook_ldap_server.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. /*****************************************************************/
  29. /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
  30. /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
  31. /*** + Base level indent should begin at left margin, as ***/
  32. /*** the first line of the class definition below. ***/
  33. /*** + All identation should consist of four space blocks ***/
  34. /*** + Tab characters are evil. ***/
  35. /*** + all comments should use "slash-star ... star-slash" ***/
  36. /*** style -- no pound characters, no slash-slash style ***/
  37. /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
  38. /*** ALWAYS USE { AND } CHARACTERS!!! ***/
  39. /*** + Please use ' instead of ", when possible. Note " ***/
  40. /*** should always be used in _( ) function calls. ***/
  41. /*** Thank you for your help making the SM code more readable. ***/
  42. /*****************************************************************/
  43. class abook_ldap_server extends addressbook_backend {
  44. var $btype = 'remote';
  45. var $bname = 'ldap_server';
  46. // Parameters changed by class
  47. var $sname = 'LDAP'; // Service name
  48. var $server = ''; // LDAP server name
  49. var $port = 389; // LDAP server port
  50. var $basedn = ''; // LDAP base DN
  51. var $charset = 'utf-8'; // LDAP server charset
  52. var $linkid = false; // PHP LDAP link ID
  53. var $bound = false; // True if LDAP server is bound
  54. var $maxrows = 250; // Max rows in result
  55. var $timeout = 30; // Timeout for LDAP operations (in seconds)
  56. // Constructor. Connects to database
  57. function abook_ldap_server($param) {
  58. if(!function_exists('ldap_connect')) {
  59. $this->set_error('LDAP support missing from PHP');
  60. return;
  61. }
  62. if(is_array($param)) {
  63. $this->server = $param['host'];
  64. $this->basedn = $param['base'];
  65. if(!empty($param['port']))
  66. $this->port = $param['port'];
  67. if(!empty($param['charset']))
  68. $this->charset = strtolower($param['charset']);
  69. if(isset($param['maxrows']))
  70. $this->maxrows = $param['maxrows'];
  71. if(isset($param['timeout']))
  72. $this->timeout = $param['timeout'];
  73. if(empty($param['name']))
  74. $this->sname = 'LDAP: ' . $param['host'];
  75. else
  76. $this->sname = $param['name'];
  77. $this->open(true);
  78. } else {
  79. $this->set_error('Invalid argument to constructor');
  80. }
  81. }
  82. // Open the LDAP server. New connection if $new is true
  83. function open($new = false) {
  84. $this->error = '';
  85. // Connection is already open
  86. if($this->linkid != false && !$new)
  87. return true;
  88. $this->linkid = @ldap_connect($this->server, $this->port);
  89. if(!$this->linkid)
  90. if(function_exists('ldap_error'))
  91. return $this->set_error(ldap_error($this->linkid));
  92. else
  93. return $this->set_error('ldap_connect failed');
  94. if(!@ldap_bind($this->linkid))
  95. if(function_exists('ldap_error'))
  96. return $this->set_error(ldap_error($this->linkid));
  97. else
  98. return $this->set_error('ldap_bind failed');
  99. $this->bound = true;
  100. return true;
  101. }
  102. // Encode iso8859-1 string to the charset used by this LDAP server
  103. function charset_encode($str) {
  104. if($this->charset == 'utf-8') {
  105. if(function_exists('utf8_encode'))
  106. return utf8_encode($str);
  107. else
  108. return $str;
  109. } else {
  110. return $str;
  111. }
  112. }
  113. // Decode from charset used by this LDAP server to iso8859-1
  114. function charset_decode($str) {
  115. if($this->charset == 'utf-8') {
  116. if(function_exists('utf8_decode'))
  117. return utf8_decode($str);
  118. else
  119. return $str;
  120. } else {
  121. return $str;
  122. }
  123. }
  124. // ========================== Public ========================
  125. // Search the LDAP server
  126. function search($expr) {
  127. // To be replaced by advanded search expression parsing
  128. if(is_array($expr)) return false;
  129. // Encode the expression
  130. $expr = $this->charset_encode($expr);
  131. if(strstr($expr, "*") === false)
  132. $expr = "*$expr*";
  133. $expression = "cn=$expr";
  134. // Make sure connection is there
  135. if(!$this->open())
  136. return false;
  137. // Do the search. Use improved ldap_search() if PHP version is
  138. // 4.0.2 or newer.
  139. if(sqCheckPHPVersion(4, 0, 2)) {
  140. $sret = @ldap_search($this->linkid, $this->basedn, $expression,
  141. array('dn', 'o', 'ou', 'sn', 'givenname',
  142. 'cn', 'mail', 'telephonenumber'),
  143. 0, $this->maxrows, $this->timeout);
  144. } else {
  145. $sret = @ldap_search($this->linkid, $this->basedn, $expression,
  146. array('dn', 'o', 'ou', 'sn', 'givenname',
  147. 'cn', 'mail', 'telephonenumber'));
  148. }
  149. // Should get error from server using the ldap_error() function,
  150. // but it only exist in the PHP LDAP documentation.
  151. if(!$sret)
  152. if(function_exists('ldap_error'))
  153. return $this->set_error(ldap_error($this->linkid));
  154. else
  155. return $this->set_error('ldap_search failed');
  156. if(@ldap_count_entries($this->linkid, $sret) <= 0)
  157. return array();
  158. // Get results
  159. $ret = array();
  160. $returned_rows = 0;
  161. $res = @ldap_get_entries($this->linkid, $sret);
  162. for($i = 0 ; $i < $res['count'] ; $i++) {
  163. $row = $res[$i];
  164. // Extract data common for all e-mail addresses
  165. // of an object. Use only the first name
  166. $nickname = $this->charset_decode($row['dn']);
  167. $fullname = $this->charset_decode($row['cn'][0]);
  168. if(empty($row['telephonenumber'][0])) $phone = '';
  169. else $phone = $this->charset_decode($row['telephonenumber'][0]);
  170. if(!empty($row['ou'][0]))
  171. $label = $this->charset_decode($row['ou'][0]);
  172. else if(!empty($row['o'][0]))
  173. $label = $this->charset_decode($row['o'][0]);
  174. else
  175. $label = '';
  176. if(empty($row['givenname'][0])) $firstname = '';
  177. else $firstname = $this->charset_decode($row['givenname'][0]);
  178. if(empty($row['sn'][0])) $surname = '';
  179. else $surname = $this->charset_decode($row['sn'][0]);
  180. // Add one row to result for each e-mail address
  181. if(isset($row['mail']['count'])) {
  182. for($j = 0 ; $j < $row['mail']['count'] ; $j++) {
  183. array_push($ret, array('nickname' => $nickname,
  184. 'name' => $fullname,
  185. 'firstname' => $firstname,
  186. 'lastname' => $surname,
  187. 'email' => $row['mail'][$j],
  188. 'label' => $label,
  189. 'phone' => $phone,
  190. 'backend' => $this->bnum,
  191. 'source' => &$this->sname));
  192. // Limit number of hits
  193. $returned_rows++;
  194. if(($returned_rows >= $this->maxrows) &&
  195. ($this->maxrows > 0) ) {
  196. ldap_free_result($sret);
  197. return $ret;
  198. }
  199. } // for($j ...)
  200. } // isset($row['mail']['count'])
  201. }
  202. ldap_free_result($sret);
  203. return $ret;
  204. } // end search()
  205. // If you run a tiny LDAP server and you want the "List All" button
  206. // to show EVERYONE, then uncomment this tiny block of code:
  207. //
  208. // function list_addr() {
  209. // return $this->search("*");
  210. // }
  211. //
  212. // Careful with this -- it could get quite large for big sites.
  213. }
  214. ?>