abook_ldap_server.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * abook_ldap_server.php
  4. *
  5. * Copyright (c) 1999-2003 The SquirrelMail Project 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. }
  53. if(!empty($param['charset'])) {
  54. $this->charset = strtolower($param['charset']);
  55. }
  56. if(isset($param['maxrows'])) {
  57. $this->maxrows = $param['maxrows'];
  58. }
  59. if(isset($param['timeout'])) {
  60. $this->timeout = $param['timeout'];
  61. }
  62. if(empty($param['name'])) {
  63. $this->sname = 'LDAP: ' . $param['host'];
  64. }
  65. else {
  66. $this->sname = $param['name'];
  67. }
  68. $this->open(true);
  69. } else {
  70. $this->set_error('Invalid argument to constructor');
  71. }
  72. }
  73. /* Open the LDAP server. New connection if $new is true */
  74. function open($new = false) {
  75. $this->error = '';
  76. /* Connection is already open */
  77. if($this->linkid != false && !$new) {
  78. return true;
  79. }
  80. $this->linkid = @ldap_connect($this->server, $this->port);
  81. if(!$this->linkid) {
  82. if(function_exists('ldap_error')) {
  83. return $this->set_error(ldap_error($this->linkid));
  84. } else {
  85. return $this->set_error('ldap_connect failed');
  86. }
  87. }
  88. if(!@ldap_bind($this->linkid)) {
  89. if(function_exists('ldap_error')) {
  90. return $this->set_error(ldap_error($this->linkid));
  91. } else {
  92. return $this->set_error('ldap_bind failed');
  93. }
  94. }
  95. $this->bound = true;
  96. return true;
  97. }
  98. /* Encode iso8859-1 string to the charset used by this LDAP server */
  99. function charset_encode($str) {
  100. if($this->charset == 'utf-8') {
  101. if(function_exists('utf8_encode')) {
  102. return utf8_encode($str);
  103. } else {
  104. return $str;
  105. }
  106. } else {
  107. return $str;
  108. }
  109. }
  110. /* Decode from charset used by this LDAP server to iso8859-1 */
  111. function charset_decode($str) {
  112. if($this->charset == 'utf-8') {
  113. if(function_exists('utf8_decode')) {
  114. return utf8_decode($str);
  115. } else {
  116. return $str;
  117. }
  118. } else {
  119. return $str;
  120. }
  121. }
  122. /* ========================== Public ======================== */
  123. /* Search the LDAP server */
  124. function search($expr) {
  125. /* To be replaced by advanded search expression parsing */
  126. if(is_array($expr)) return false;
  127. /* Encode the expression */
  128. $expr = $this->charset_encode($expr);
  129. if(strstr($expr, '*') === false) {
  130. $expr = "*$expr*";
  131. }
  132. $expression = "cn=$expr";
  133. /* Make sure connection is there */
  134. if(!$this->open()) {
  135. return false;
  136. }
  137. $sret = @ldap_search($this->linkid, $this->basedn, $expression,
  138. array('dn', 'o', 'ou', 'sn', 'givenname',
  139. 'cn', 'mail', 'telephonenumber'),
  140. 0, $this->maxrows, $this->timeout);
  141. /* Should get error from server using the ldap_error() function,
  142. * but it only exist in the PHP LDAP documentation. */
  143. if(!$sret) {
  144. if(function_exists('ldap_error')) {
  145. return $this->set_error(ldap_error($this->linkid));
  146. } else {
  147. return $this->set_error('ldap_search failed');
  148. }
  149. }
  150. if(@ldap_count_entries($this->linkid, $sret) <= 0) {
  151. return array();
  152. }
  153. /* Get results */
  154. $ret = array();
  155. $returned_rows = 0;
  156. $res = @ldap_get_entries($this->linkid, $sret);
  157. for($i = 0 ; $i < $res['count'] ; $i++) {
  158. $row = $res[$i];
  159. /* Extract data common for all e-mail addresses
  160. * of an object. Use only the first name */
  161. $nickname = $this->charset_decode($row['dn']);
  162. $fullname = $this->charset_decode($row['cn'][0]);
  163. if(empty($row['telephonenumber'][0])) {
  164. $phone = '';
  165. } else {
  166. $phone = $this->charset_decode($row['telephonenumber'][0]);
  167. }
  168. if(!empty($row['ou'][0])) {
  169. $label = $this->charset_decode($row['ou'][0]);
  170. }
  171. else if(!empty($row['o'][0])) {
  172. $label = $this->charset_decode($row['o'][0]);
  173. } else {
  174. $label = '';
  175. }
  176. if(empty($row['givenname'][0])) {
  177. $firstname = '';
  178. } else {
  179. $firstname = $this->charset_decode($row['givenname'][0]);
  180. }
  181. if(empty($row['sn'][0])) {
  182. $surname = '';
  183. } else {
  184. $surname = $this->charset_decode($row['sn'][0]);
  185. }
  186. /* Add one row to result for each e-mail address */
  187. if(isset($row['mail']['count'])) {
  188. for($j = 0 ; $j < $row['mail']['count'] ; $j++) {
  189. array_push($ret, array('nickname' => $nickname,
  190. 'name' => $fullname,
  191. 'firstname' => $firstname,
  192. 'lastname' => $surname,
  193. 'email' => $row['mail'][$j],
  194. 'label' => $label,
  195. 'phone' => $phone,
  196. 'backend' => $this->bnum,
  197. 'source' => &$this->sname));
  198. // Limit number of hits
  199. $returned_rows++;
  200. if(($returned_rows >= $this->maxrows) &&
  201. ($this->maxrows > 0) ) {
  202. ldap_free_result($sret);
  203. return $ret;
  204. }
  205. } // for($j ...)
  206. } // isset($row['mail']['count'])
  207. }
  208. ldap_free_result($sret);
  209. return $ret;
  210. } /* end search() */
  211. /* If you run a tiny LDAP server and you want the "List All" button
  212. * to show EVERYONE, then uncomment this tiny block of code:
  213. *
  214. * function list_addr() {
  215. * return $this->search('*');
  216. * }
  217. *
  218. * Careful with this -- it could get quite large for big sites. */
  219. }
  220. ?>