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
11 * @package squirrelmail
12 * @subpackage addressbook
16 * Address book backend for LDAP server
18 * An array with the following elements must be passed to
19 * the class constructor (elements marked ? are optional):
21 * host => LDAP server hostname/IP-address
22 * base => LDAP server root (base dn). Empty string allowed.
23 * ? port => LDAP server TCP port number (default: 389)
24 * ? charset => LDAP server charset (default: utf-8)
25 * ? name => Name for LDAP server (default "LDAP: hostname")
26 * Used to tag the result data
27 * ? maxrows => Maximum # of rows in search result
28 * ? timeout => Timeout for LDAP operations (in seconds, default: 30)
29 * Might not work for all LDAP libraries or servers.
30 * ? binddn => LDAP Bind DN.
31 * ? bindpw => LDAP Bind Password.
32 * ? protocol => LDAP Bind protocol.
34 * NOTE. This class should not be used directly. Use the
35 * "AddressBook" class instead.
36 * @package squirrelmail
37 * @subpackage addressbook
39 class abook_ldap_server
extends addressbook_backend
{
41 * @var string backend type
43 var $btype = 'remote';
45 * @var string backend name
47 var $bname = 'ldap_server';
49 /* Parameters changed by class */
51 * @var string displayed name
53 var $sname = 'LDAP'; /* Service name */
55 * @var string LDAP server name or address or url
59 * @var integer LDAP server port
63 * @var string LDAP base DN
67 * @var string charset used for entries in LDAP server
69 var $charset = 'utf-8';
71 * @var object PHP LDAP link ID
75 * @var bool True if LDAP server is bound
79 * @var integer max rows in result
83 * @var integer timeout of LDAP operations (in seconds)
87 * @var string DN to bind to (non-anonymous bind)
88 * @since 1.5.0 and 1.4.3
92 * @var string password to bind with (non-anonymous bind)
93 * @since 1.5.0 and 1.4.3
97 * @var integer protocol used to connect to ldap server
98 * @since 1.5.0 and 1.4.3
103 * Constructor. Connects to database
104 * @param array connection options
106 function abook_ldap_server($param) {
107 if(!function_exists('ldap_connect')) {
108 $this->set_error('LDAP support missing from PHP');
111 if(is_array($param)) {
112 $this->server
= $param['host'];
113 $this->basedn
= $param['base'];
114 if(!empty($param['port'])) {
115 $this->port
= $param['port'];
117 if(!empty($param['charset'])) {
118 $this->charset
= strtolower($param['charset']);
120 if(isset($param['maxrows'])) {
121 $this->maxrows
= $param['maxrows'];
123 if(isset($param['timeout'])) {
124 $this->timeout
= $param['timeout'];
126 if(isset($param['binddn'])) {
127 $this->binddn
= $param['binddn'];
129 if(isset($param['bindpw'])) {
130 $this->bindpw
= $param['bindpw'];
132 if(isset($param['protocol'])) {
133 $this->protocol
= $param['protocol'];
135 if(empty($param['name'])) {
136 $this->sname
= 'LDAP: ' . $param['host'];
139 $this->sname
= $param['name'];
144 $this->set_error('Invalid argument to constructor');
150 * Open the LDAP server.
151 * @param bool $new is it a new connection
154 function open($new = false) {
157 /* Connection is already open */
158 if($this->linkid
!= false && !$new) {
162 $this->linkid
= @ldap_connect
($this->server
, $this->port
);
164 if(function_exists('ldap_error')) {
165 return $this->set_error(ldap_error($this->linkid
));
167 return $this->set_error('ldap_connect failed');
171 if(!empty($this->protocol
)) {
172 if(!@ldap_set_option
($this->linkid
, LDAP_OPT_PROTOCOL_VERSION
, $this->protocol
)) {
173 if(function_exists('ldap_error')) {
174 return $this->set_error(ldap_error($this->linkid
));
176 return $this->set_error('ldap_set_option failed');
181 if(!empty($this->binddn
)) {
182 if(!@ldap_bind
($this->linkid
, $this->binddn
, $this->bindpw
)) {
183 if(function_exists('ldap_error')) {
184 return $this->set_error(ldap_error($this->linkid
));
186 return $this->set_error('authenticated ldap_bind failed');
190 if(!@ldap_bind
($this->linkid
)) {
191 if(function_exists('ldap_error')) {
192 return $this->set_error(ldap_error($this->linkid
));
194 return $this->set_error('anonymous ldap_bind failed');
205 * Encode string to the charset used by this LDAP server
206 * @param string string that has to be encoded
207 * @return string encoded string
209 function charset_encode($str) {
210 global $default_charset;
211 if($this->charset
!= $default_charset) {
212 return charset_convert($default_charset,$str,$this->charset
,false);
219 * Decode from charset used by this LDAP server to charset used by translation
221 * Uses squirrelmail charset_decode functions
222 * @param string string that has to be decoded
223 * @return string decoded string
225 function charset_decode($str) {
226 global $default_charset;
227 if ($this->charset
!= $default_charset) {
228 return charset_convert($this->charset
,$str,$default_charset,false);
235 /* ========================== Public ======================== */
238 * Search the LDAP server
239 * @param string $expr search expression
240 * @return array search results
242 function search($expr) {
244 /* To be replaced by advanded search expression parsing */
245 if(is_array($expr)) return false;
247 /* Encode the expression */
248 $expr = $this->charset_encode($expr);
249 if(strstr($expr, '*') === false) {
252 $expression = "cn=$expr";
254 /* Make sure connection is there */
259 $sret = @ldap_search
($this->linkid
, $this->basedn
, $expression,
260 array('dn', 'o', 'ou', 'sn', 'givenname', 'cn', 'mail'),
261 0, $this->maxrows
, $this->timeout
);
263 /* Should get error from server using the ldap_error() function,
264 * but it only exist in the PHP LDAP documentation. */
266 if(function_exists('ldap_error')) {
267 return $this->set_error(ldap_error($this->linkid
));
269 return $this->set_error('ldap_search failed');
273 if(@ldap_count_entries
($this->linkid
, $sret) <= 0) {
280 $res = @ldap_get_entries
($this->linkid
, $sret);
281 for($i = 0 ; $i < $res['count'] ; $i++
) {
284 /* Extract data common for all e-mail addresses
285 * of an object. Use only the first name */
286 $nickname = $this->charset_decode($row['dn']);
287 $fullname = $this->charset_decode($row['cn'][0]);
289 if(!empty($row['ou'][0])) {
290 $label = $this->charset_decode($row['ou'][0]);
292 else if(!empty($row['o'][0])) {
293 $label = $this->charset_decode($row['o'][0]);
298 if(empty($row['givenname'][0])) {
301 $firstname = $this->charset_decode($row['givenname'][0]);
304 if(empty($row['sn'][0])) {
307 $surname = $this->charset_decode($row['sn'][0]);
310 /* Add one row to result for each e-mail address */
311 if(isset($row['mail']['count'])) {
312 for($j = 0 ; $j < $row['mail']['count'] ; $j++
) {
313 array_push($ret, array('nickname' => $nickname,
315 'firstname' => $firstname,
316 'lastname' => $surname,
317 'email' => $row['mail'][$j],
319 'backend' => $this->bnum
,
320 'source' => &$this->sname
));
322 // Limit number of hits
324 if(($returned_rows >= $this->maxrows
) &&
325 ($this->maxrows
> 0) ) {
326 ldap_free_result($sret);
332 } // isset($row['mail']['count'])
336 ldap_free_result($sret);
342 * List all entries present in LDAP server
344 * If you run a tiny LDAP server and you want the "List All" button
345 * to show EVERYONE, disable first return call and enable the second one.
346 * Remember that maxrows setting might limit list of returned entries.
348 * Careful with this -- it could get quite large for big sites.
349 * @return array all entries in ldap server
351 function list_addr() {
353 // return $this->search('*');