abook_ldap_server.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. * @package squirrelmail
  28. */
  29. /**
  30. * Undocumented class - fixme
  31. * @package squirrelmail
  32. */
  33. class abook_ldap_server extends addressbook_backend {
  34. var $btype = 'remote';
  35. var $bname = 'ldap_server';
  36. /* Parameters changed by class */
  37. var $sname = 'LDAP'; /* Service name */
  38. var $server = ''; /* LDAP server name */
  39. var $port = 389; /* LDAP server port */
  40. var $basedn = ''; /* LDAP base DN */
  41. var $charset = 'utf-8'; /* LDAP server charset */
  42. var $linkid = false; /* PHP LDAP link ID */
  43. var $bound = false; /* True if LDAP server is bound */
  44. var $maxrows = 250; /* Max rows in result */
  45. var $timeout = 30; /* Timeout for LDAP operations (in seconds) */
  46. /* Constructor. Connects to database */
  47. function abook_ldap_server($param) {
  48. if(!function_exists('ldap_connect')) {
  49. $this->set_error('LDAP support missing from PHP');
  50. return;
  51. }
  52. if(is_array($param)) {
  53. $this->server = $param['host'];
  54. $this->basedn = $param['base'];
  55. if(!empty($param['port'])) {
  56. $this->port = $param['port'];
  57. }
  58. if(!empty($param['charset'])) {
  59. $this->charset = strtolower($param['charset']);
  60. }
  61. if(isset($param['maxrows'])) {
  62. $this->maxrows = $param['maxrows'];
  63. }
  64. if(isset($param['timeout'])) {
  65. $this->timeout = $param['timeout'];
  66. }
  67. if(empty($param['name'])) {
  68. $this->sname = 'LDAP: ' . $param['host'];
  69. }
  70. else {
  71. $this->sname = $param['name'];
  72. }
  73. $this->open(true);
  74. } else {
  75. $this->set_error('Invalid argument to constructor');
  76. }
  77. }
  78. /* Open the LDAP server. New connection if $new is true */
  79. function open($new = false) {
  80. $this->error = '';
  81. /* Connection is already open */
  82. if($this->linkid != false && !$new) {
  83. return true;
  84. }
  85. $this->linkid = @ldap_connect($this->server, $this->port);
  86. if(!$this->linkid) {
  87. if(function_exists('ldap_error')) {
  88. return $this->set_error(ldap_error($this->linkid));
  89. } else {
  90. return $this->set_error('ldap_connect failed');
  91. }
  92. }
  93. if(!@ldap_bind($this->linkid)) {
  94. if(function_exists('ldap_error')) {
  95. return $this->set_error(ldap_error($this->linkid));
  96. } else {
  97. return $this->set_error('ldap_bind failed');
  98. }
  99. }
  100. $this->bound = true;
  101. return true;
  102. }
  103. /* Encode iso8859-1 string to the charset used by this LDAP server */
  104. function charset_encode($str) {
  105. if($this->charset == 'utf-8') {
  106. if(function_exists('utf8_encode')) {
  107. return utf8_encode($str);
  108. } else {
  109. return $str;
  110. }
  111. } else {
  112. return $str;
  113. }
  114. }
  115. /* Decode from charset used by this LDAP server to iso8859-1 */
  116. function charset_decode($str) {
  117. if($this->charset == 'utf-8') {
  118. if(function_exists('utf8_decode')) {
  119. return utf8_decode($str);
  120. } else {
  121. return $str;
  122. }
  123. } else {
  124. return $str;
  125. }
  126. }
  127. /* ========================== Public ======================== */
  128. /* Search the LDAP server */
  129. function search($expr) {
  130. /* To be replaced by advanded search expression parsing */
  131. if(is_array($expr)) return false;
  132. /* Encode the expression */
  133. $expr = $this->charset_encode($expr);
  134. if(strstr($expr, '*') === false) {
  135. $expr = "*$expr*";
  136. }
  137. $expression = "cn=$expr";
  138. /* Make sure connection is there */
  139. if(!$this->open()) {
  140. return false;
  141. }
  142. $sret = @ldap_search($this->linkid, $this->basedn, $expression,
  143. array('dn', 'o', 'ou', 'sn', 'givenname',
  144. 'cn', 'mail', 'telephonenumber'),
  145. 0, $this->maxrows, $this->timeout);
  146. /* Should get error from server using the ldap_error() function,
  147. * but it only exist in the PHP LDAP documentation. */
  148. if(!$sret) {
  149. if(function_exists('ldap_error')) {
  150. return $this->set_error(ldap_error($this->linkid));
  151. } else {
  152. return $this->set_error('ldap_search failed');
  153. }
  154. }
  155. if(@ldap_count_entries($this->linkid, $sret) <= 0) {
  156. return array();
  157. }
  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])) {
  169. $phone = '';
  170. } else {
  171. $phone = $this->charset_decode($row['telephonenumber'][0]);
  172. }
  173. if(!empty($row['ou'][0])) {
  174. $label = $this->charset_decode($row['ou'][0]);
  175. }
  176. else if(!empty($row['o'][0])) {
  177. $label = $this->charset_decode($row['o'][0]);
  178. } else {
  179. $label = '';
  180. }
  181. if(empty($row['givenname'][0])) {
  182. $firstname = '';
  183. } else {
  184. $firstname = $this->charset_decode($row['givenname'][0]);
  185. }
  186. if(empty($row['sn'][0])) {
  187. $surname = '';
  188. } else {
  189. $surname = $this->charset_decode($row['sn'][0]);
  190. }
  191. /* Add one row to result for each e-mail address */
  192. if(isset($row['mail']['count'])) {
  193. for($j = 0 ; $j < $row['mail']['count'] ; $j++) {
  194. array_push($ret, array('nickname' => $nickname,
  195. 'name' => $fullname,
  196. 'firstname' => $firstname,
  197. 'lastname' => $surname,
  198. 'email' => $row['mail'][$j],
  199. 'label' => $label,
  200. 'phone' => $phone,
  201. 'backend' => $this->bnum,
  202. 'source' => &$this->sname));
  203. // Limit number of hits
  204. $returned_rows++;
  205. if(($returned_rows >= $this->maxrows) &&
  206. ($this->maxrows > 0) ) {
  207. ldap_free_result($sret);
  208. return $ret;
  209. }
  210. } // for($j ...)
  211. } // isset($row['mail']['count'])
  212. }
  213. ldap_free_result($sret);
  214. return $ret;
  215. } /* end search() */
  216. /* If you run a tiny LDAP server and you want the "List All" button
  217. * to show EVERYONE, then uncomment this tiny block of code:
  218. *
  219. * function list_addr() {
  220. * return $this->search('*');
  221. * }
  222. *
  223. * Careful with this -- it could get quite large for big sites. */
  224. }
  225. ?>