abook_ldap_server.php 9.8 KB

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