3 * abook_ldap_server.php
5 * Copyright (c) 1999-2005 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * Address book backend for LDAP server
10 * LDAP filtering code by Tim Bell
11 * <bhat at users.sourceforge.net> (#539534)
12 * ADS limit_scope code by Michael Brown
13 * <mcb30 at users.sourceforge.net> (#1035454)
16 * @package squirrelmail
17 * @subpackage addressbook
21 * Address book backend for LDAP server
23 * An array with the following elements must be passed to
24 * the class constructor (elements marked ? are optional):
26 * host => LDAP server hostname/IP-address
27 * base => LDAP server root (base dn). Empty string allowed.
28 * ? port => LDAP server TCP port number (default: 389)
29 * ? charset => LDAP server charset (default: utf-8)
30 * ? name => Name for LDAP server (default "LDAP: hostname")
31 * Used to tag the result data
32 * ? maxrows => Maximum # of rows in search result
33 * ? filter => Filter expression to limit ldap searches
34 * ? timeout => Timeout for LDAP operations (in seconds, default: 30)
35 * Might not work for all LDAP libraries or servers.
36 * ? binddn => LDAP Bind DN.
37 * ? bindpw => LDAP Bind Password.
38 * ? protocol => LDAP Bind protocol.
39 * ? limit_scope => Limits scope to base DN.
41 * NOTE. This class should not be used directly. Use the
42 * "AddressBook" class instead.
43 * @package squirrelmail
44 * @subpackage addressbook
46 class abook_ldap_server
extends addressbook_backend
{
48 * @var string backend type
50 var $btype = 'remote';
52 * @var string backend name
54 var $bname = 'ldap_server';
56 /* Parameters changed by class */
58 * @var string displayed name
60 var $sname = 'LDAP'; /* Service name */
62 * @var string LDAP server name or address or url
66 * @var integer LDAP server port
70 * @var string LDAP base DN
74 * @var string charset used for entries in LDAP server
76 var $charset = 'utf-8';
78 * @var object PHP LDAP link ID
82 * @var bool True if LDAP server is bound
86 * @var integer max rows in result
90 * @var string ldap filter
95 * @var integer timeout of LDAP operations (in seconds)
99 * @var string DN to bind to (non-anonymous bind)
100 * @since 1.5.0 and 1.4.3
104 * @var string password to bind with (non-anonymous bind)
105 * @since 1.5.0 and 1.4.3
109 * @var integer protocol used to connect to ldap server
110 * @since 1.5.0 and 1.4.3
114 * @var boolean limits scope to base dn
117 var $limit_scope = false;
120 * Constructor. Connects to database
121 * @param array connection options
123 function abook_ldap_server($param) {
124 if(!function_exists('ldap_connect')) {
125 $this->set_error(_("PHP install does not have LDAP support."));
128 if(is_array($param)) {
129 $this->server
= $param['host'];
130 $this->basedn
= $param['base'];
132 if(!empty($param['port']))
133 $this->port
= $param['port'];
135 if(!empty($param['charset']))
136 $this->charset
= strtolower($param['charset']);
138 if(isset($param['maxrows']))
139 $this->maxrows
= $param['maxrows'];
141 if(isset($param['timeout']))
142 $this->timeout
= $param['timeout'];
144 if(isset($param['binddn']))
145 $this->binddn
= $param['binddn'];
147 if(isset($param['bindpw']))
148 $this->bindpw
= $param['bindpw'];
150 if(isset($param['protocol']))
151 $this->protocol
= $param['protocol'];
153 if(isset($param['filter']))
154 $this->filter
= trim($param['filter']);
156 if(isset($param['limit_scope']))
157 $this->limit_scope
= $param['limit_scope'];
159 if(empty($param['name'])) {
160 $this->sname
= 'LDAP: ' . $param['host'];
162 $this->sname
= $param['name'];
166 * don't open LDAP server on addressbook_init(),
167 * open ldap connection only on search. Speeds up
168 * addressbook_init() call.
170 // $this->open(true);
172 $this->set_error('Invalid argument to constructor');
178 * Open the LDAP server.
179 * @param bool $new is it a new connection
182 function open($new = false) {
185 /* Connection is already open */
186 if($this->linkid
!= false && !$new) {
190 $this->linkid
= @ldap_connect
($this->server
, $this->port
);
192 if(function_exists('ldap_error') && is_resource($this->linkid
)) {
193 return $this->set_error(ldap_error($this->linkid
));
195 return $this->set_error('ldap_connect failed');
199 if(!empty($this->protocol
)) {
200 if(!@ldap_set_option
($this->linkid
, LDAP_OPT_PROTOCOL_VERSION
, $this->protocol
)) {
201 if(function_exists('ldap_error')) {
202 return $this->set_error(ldap_error($this->linkid
));
204 return $this->set_error('ldap_set_option failed');
209 if(!empty($this->limit_scope
) && $this->limit_scope
) {
210 if(empty($this->protocol
) ||
intval($this->protocol
) < 3) {
211 return $this->set_error('limit_scope requires protocol >= 3');
213 // See http://msdn.microsoft.com/library/en-us/ldap/ldap/ldap_server_domain_scope_oid.asp
214 $ctrl = array ( "oid" => "1.2.840.113556.1.4.1339", "iscritical" => TRUE );
215 if(!@ldap_set_option
($this->linkid
, LDAP_OPT_SERVER_CONTROLS
, array($ctrl))) {
216 if(function_exists('ldap_error')) {
217 return $this->set_error(ldap_error($this->linkid
));
219 return $this->set_error('limit domain scope failed');
224 if(!empty($this->binddn
)) {
225 if(!@ldap_bind
($this->linkid
, $this->binddn
, $this->bindpw
)) {
226 if(function_exists('ldap_error')) {
227 return $this->set_error(ldap_error($this->linkid
));
229 return $this->set_error('authenticated ldap_bind failed');
233 if(!@ldap_bind
($this->linkid
)) {
234 if(function_exists('ldap_error')) {
235 return $this->set_error(ldap_error($this->linkid
));
237 return $this->set_error('anonymous ldap_bind failed');
248 * Encode string to the charset used by this LDAP server
249 * @param string string that has to be encoded
250 * @return string encoded string
252 function charset_encode($str) {
253 global $default_charset;
254 if($this->charset
!= $default_charset) {
255 return charset_convert($default_charset,$str,$this->charset
,false);
262 * Decode from charset used by this LDAP server to charset used by translation
264 * Uses SquirrelMail charset_decode functions
265 * @param string string that has to be decoded
266 * @return string decoded string
268 function charset_decode($str) {
269 global $default_charset;
270 if ($this->charset
!= $default_charset) {
271 return charset_convert($this->charset
,$str,$default_charset,false);
278 * Sanitizes ldap search strings.
280 * @link http://www.faqs.org/rfcs/rfc2254.html
281 * @since 1.5.1 and 1.4.5
282 * @param string $string
283 * @return string sanitized string
285 function ldapspecialchars($string) {
286 $sanitized=array('\\' => '\5c',
292 return str_replace(array_keys($sanitized),array_values($sanitized),$string);
295 /* ========================== Public ======================== */
298 * Search the LDAP server
299 * @param string $expr search expression
300 * @return array search results
302 function search($expr) {
303 /* To be replaced by advanded search expression parsing */
304 if(is_array($expr)) return false;
306 /* Encode the expression */
307 $expr = $this->charset_encode($expr);
310 * allow use of one asterisk in search.
311 * Don't allow any ldap special chars if search is different
314 $expr = '*' . $this->ldapspecialchars($expr) . '*';
316 $expression = "(cn=$expr)";
318 if ($this->filter
!='')
319 $expression = '(&' . $this->filter
. $expression . ')';
321 /* Make sure connection is there */
326 $sret = @ldap_search
($this->linkid
, $this->basedn
, $expression,
327 array('dn', 'o', 'ou', 'sn', 'givenname', 'cn', 'mail'),
328 0, $this->maxrows
, $this->timeout
);
330 /* Should get error from server using the ldap_error() function,
331 * but it only exist in the PHP LDAP documentation. */
333 if(function_exists('ldap_error')) {
334 return $this->set_error(ldap_error($this->linkid
));
336 return $this->set_error('ldap_search failed');
340 if(@ldap_count_entries
($this->linkid
, $sret) <= 0) {
347 $res = @ldap_get_entries
($this->linkid
, $sret);
348 for($i = 0 ; $i < $res['count'] ; $i++
) {
351 /* Extract data common for all e-mail addresses
352 * of an object. Use only the first name */
353 $nickname = $this->charset_decode($row['dn']);
354 $fullname = $this->charset_decode($row['cn'][0]);
356 if(!empty($row['ou'][0])) {
357 $label = $this->charset_decode($row['ou'][0]);
359 else if(!empty($row['o'][0])) {
360 $label = $this->charset_decode($row['o'][0]);
365 if(empty($row['givenname'][0])) {
368 $firstname = $this->charset_decode($row['givenname'][0]);
371 if(empty($row['sn'][0])) {
374 $surname = $this->charset_decode($row['sn'][0]);
377 /* Add one row to result for each e-mail address */
378 if(isset($row['mail']['count'])) {
379 for($j = 0 ; $j < $row['mail']['count'] ; $j++
) {
380 array_push($ret, array('nickname' => $nickname,
382 'firstname' => $firstname,
383 'lastname' => $surname,
384 'email' => $row['mail'][$j],
386 'backend' => $this->bnum
,
387 'source' => &$this->sname
));
389 // Limit number of hits
391 if(($returned_rows >= $this->maxrows
) &&
392 ($this->maxrows
> 0) ) {
393 ldap_free_result($sret);
399 } // isset($row['mail']['count'])
403 ldap_free_result($sret);
409 * List all entries present in LDAP server
411 * If you run a tiny LDAP server and you want the "List All" button
412 * to show EVERYONE, disable first return call and enable the second one.
413 * Remember that maxrows setting might limit list of returned entries.
415 * Careful with this -- it could get quite large for big sites.
416 * @return array all entries in ldap server
418 function list_addr() {
420 // return $this->search('*');