abook_ldap_server.php 9.8 KB

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