4 * abook_ldap_server.php
6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Address book backend for LDAP server
11 * An array with the following elements must be passed to
12 * the class constructor (elements marked ? are optional):
14 * host => LDAP server hostname/IP-address
15 * base => LDAP server root (base dn). Empty string allowed.
16 * ? port => LDAP server TCP port number (default: 389)
17 * ? charset => LDAP server charset (default: utf-8)
18 * ? name => Name for LDAP server (default "LDAP: hostname")
19 * Used to tag the result data
20 * ? maxrows => Maximum # of rows in search result
21 * ? timeout => Timeout for LDAP operations (in seconds, default: 30)
22 * Might not work for all LDAP libraries or servers.
23 * ? binddn => LDAP Bind DN.
24 * ? bindpw => LDAP Bind Password.
25 * ? protocol => LDAP Bind protocol.
27 * NOTE. This class should not be used directly. Use the
28 * "AddressBook" class instead.
31 * @package squirrelmail
35 * Undocumented class - fixme
36 * @package squirrelmail
38 class abook_ldap_server
extends addressbook_backend
{
39 var $btype = 'remote';
40 var $bname = 'ldap_server';
42 /* Parameters changed by class */
43 var $sname = 'LDAP'; /* Service name */
44 var $server = ''; /* LDAP server name */
45 var $port = 389; /* LDAP server port */
46 var $basedn = ''; /* LDAP base DN */
47 var $charset = 'utf-8'; /* LDAP server charset */
48 var $linkid = false; /* PHP LDAP link ID */
49 var $bound = false; /* True if LDAP server is bound */
50 var $maxrows = 250; /* Max rows in result */
51 var $timeout = 30; /* Timeout for LDAP operations (in seconds) */
52 var $binddn = ''; /* DN to bind to (non-anonymous bind) */
53 var $bindpw = ''; /* password to bind with (non-anonymous bind) */
54 var $protocol = ''; /* protocol used to connect to ldap server */
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');
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'];
68 if(!empty($param['charset'])) {
69 $this->charset
= strtolower($param['charset']);
71 if(isset($param['maxrows'])) {
72 $this->maxrows
= $param['maxrows'];
74 if(isset($param['timeout'])) {
75 $this->timeout
= $param['timeout'];
77 if(isset($param['binddn'])) {
78 $this->binddn
= $param['binddn'];
80 if(isset($param['bindpw'])) {
81 $this->bindpw
= $param['bindpw'];
83 if(isset($param['protocol'])) {
84 $this->protocol
= $param['protocol'];
86 if(empty($param['name'])) {
87 $this->sname
= 'LDAP: ' . $param['host'];
90 $this->sname
= $param['name'];
95 $this->set_error('Invalid argument to constructor');
100 /* Open the LDAP server. New connection if $new is true */
101 function open($new = false) {
104 /* Connection is already open */
105 if($this->linkid
!= false && !$new) {
109 $this->linkid
= @ldap_connect
($this->server
, $this->port
);
111 if(function_exists('ldap_error')) {
112 return $this->set_error(ldap_error($this->linkid
));
114 return $this->set_error('ldap_connect failed');
118 if(!empty($this->protocol
)) {
119 if(!@ldap_set_option
($this->linkid
, LDAP_OPT_PROTOCOL_VERSION
, $this->protocol
)) {
120 if(function_exists('ldap_error')) {
121 return $this->set_error(ldap_error($this->linkid
));
123 return $this->set_error('ldap_set_option failed');
128 if(!empty($this->binddn
)) {
129 if(!@ldap_bind
($this->linkid
, $this->binddn
, $this->bindpw
)) {
130 if(function_exists('ldap_error')) {
131 return $this->set_error(ldap_error($this->linkid
));
133 return $this->set_error('authenticated ldap_bind failed');
137 if(!@ldap_bind
($this->linkid
)) {
138 if(function_exists('ldap_error')) {
139 return $this->set_error(ldap_error($this->linkid
));
141 return $this->set_error('anonymous ldap_bind failed');
152 /* Encode iso8859-1 string to the charset used by this LDAP server */
153 function charset_encode($str) {
154 if($this->charset
== 'utf-8') {
155 if(function_exists('utf8_encode')) {
156 return utf8_encode($str);
166 /* Decode from charset used by this LDAP server to iso8859-1 */
167 function charset_decode($str) {
168 if($this->charset
== 'utf-8') {
169 if(function_exists('utf8_decode')) {
170 return utf8_decode($str);
180 /* ========================== Public ======================== */
182 /* Search the LDAP server */
183 function search($expr) {
185 /* To be replaced by advanded search expression parsing */
186 if(is_array($expr)) return false;
188 /* Encode the expression */
189 $expr = $this->charset_encode($expr);
190 if(strstr($expr, '*') === false) {
193 $expression = "cn=$expr";
195 /* Make sure connection is there */
200 $sret = @ldap_search
($this->linkid
, $this->basedn
, $expression,
201 array('dn', 'o', 'ou', 'sn', 'givenname',
202 'cn', 'mail', 'telephonenumber'),
203 0, $this->maxrows
, $this->timeout
);
205 /* Should get error from server using the ldap_error() function,
206 * but it only exist in the PHP LDAP documentation. */
208 if(function_exists('ldap_error')) {
209 return $this->set_error(ldap_error($this->linkid
));
211 return $this->set_error('ldap_search failed');
215 if(@ldap_count_entries
($this->linkid
, $sret) <= 0) {
222 $res = @ldap_get_entries
($this->linkid
, $sret);
223 for($i = 0 ; $i < $res['count'] ; $i++
) {
226 /* Extract data common for all e-mail addresses
227 * of an object. Use only the first name */
228 $nickname = $this->charset_decode($row['dn']);
229 $fullname = $this->charset_decode($row['cn'][0]);
231 if(empty($row['telephonenumber'][0])) {
234 $phone = $this->charset_decode($row['telephonenumber'][0]);
237 if(!empty($row['ou'][0])) {
238 $label = $this->charset_decode($row['ou'][0]);
240 else if(!empty($row['o'][0])) {
241 $label = $this->charset_decode($row['o'][0]);
246 if(empty($row['givenname'][0])) {
249 $firstname = $this->charset_decode($row['givenname'][0]);
252 if(empty($row['sn'][0])) {
255 $surname = $this->charset_decode($row['sn'][0]);
258 /* Add one row to result for each e-mail address */
259 if(isset($row['mail']['count'])) {
260 for($j = 0 ; $j < $row['mail']['count'] ; $j++
) {
261 array_push($ret, array('nickname' => $nickname,
263 'firstname' => $firstname,
264 'lastname' => $surname,
265 'email' => $row['mail'][$j],
268 'backend' => $this->bnum
,
269 'source' => &$this->sname
));
271 // Limit number of hits
273 if(($returned_rows >= $this->maxrows
) &&
274 ($this->maxrows
> 0) ) {
275 ldap_free_result($sret);
281 } // isset($row['mail']['count'])
285 ldap_free_result($sret);
290 /* If you run a tiny LDAP server and you want the "List All" button
291 * to show EVERYONE, then uncomment this tiny block of code:
293 * function list_addr() {
294 * return $this->search('*');
297 * Careful with this -- it could get quite large for big sites. */