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
32 * @subpackage addressbook
36 * Undocumented class - fixme
37 * @package squirrelmail
39 class abook_ldap_server
extends addressbook_backend
{
40 var $btype = 'remote';
41 var $bname = 'ldap_server';
43 /* Parameters changed by class */
44 var $sname = 'LDAP'; /* Service name */
45 var $server = ''; /* LDAP server name */
46 var $port = 389; /* LDAP server port */
47 var $basedn = ''; /* LDAP base DN */
48 var $charset = 'utf-8'; /* LDAP server charset */
49 var $linkid = false; /* PHP LDAP link ID */
50 var $bound = false; /* True if LDAP server is bound */
51 var $maxrows = 250; /* Max rows in result */
52 var $timeout = 30; /* Timeout for LDAP operations (in seconds) */
53 var $binddn = ''; /* DN to bind to (non-anonymous bind) */
54 var $bindpw = ''; /* password to bind with (non-anonymous bind) */
55 var $protocol = ''; /* protocol used to connect to ldap server */
57 /* Constructor. Connects to database */
58 function abook_ldap_server($param) {
59 if(!function_exists('ldap_connect')) {
60 $this->set_error('LDAP support missing from PHP');
63 if(is_array($param)) {
64 $this->server
= $param['host'];
65 $this->basedn
= $param['base'];
66 if(!empty($param['port'])) {
67 $this->port
= $param['port'];
69 if(!empty($param['charset'])) {
70 $this->charset
= strtolower($param['charset']);
72 if(isset($param['maxrows'])) {
73 $this->maxrows
= $param['maxrows'];
75 if(isset($param['timeout'])) {
76 $this->timeout
= $param['timeout'];
78 if(isset($param['binddn'])) {
79 $this->binddn
= $param['binddn'];
81 if(isset($param['bindpw'])) {
82 $this->bindpw
= $param['bindpw'];
84 if(isset($param['protocol'])) {
85 $this->protocol
= $param['protocol'];
87 if(empty($param['name'])) {
88 $this->sname
= 'LDAP: ' . $param['host'];
91 $this->sname
= $param['name'];
96 $this->set_error('Invalid argument to constructor');
101 /* Open the LDAP server. New connection if $new is true */
102 function open($new = false) {
105 /* Connection is already open */
106 if($this->linkid
!= false && !$new) {
110 $this->linkid
= @ldap_connect
($this->server
, $this->port
);
112 if(function_exists('ldap_error')) {
113 return $this->set_error(ldap_error($this->linkid
));
115 return $this->set_error('ldap_connect failed');
119 if(!empty($this->protocol
)) {
120 if(!@ldap_set_option
($this->linkid
, LDAP_OPT_PROTOCOL_VERSION
, $this->protocol
)) {
121 if(function_exists('ldap_error')) {
122 return $this->set_error(ldap_error($this->linkid
));
124 return $this->set_error('ldap_set_option failed');
129 if(!empty($this->binddn
)) {
130 if(!@ldap_bind
($this->linkid
, $this->binddn
, $this->bindpw
)) {
131 if(function_exists('ldap_error')) {
132 return $this->set_error(ldap_error($this->linkid
));
134 return $this->set_error('authenticated ldap_bind failed');
138 if(!@ldap_bind
($this->linkid
)) {
139 if(function_exists('ldap_error')) {
140 return $this->set_error(ldap_error($this->linkid
));
142 return $this->set_error('anonymous ldap_bind failed');
153 /* Encode iso8859-1 string to the charset used by this LDAP server */
154 function charset_encode($str) {
155 if($this->charset
== 'utf-8') {
156 if(function_exists('utf8_encode')) {
157 return utf8_encode($str);
167 /* Decode from charset used by this LDAP server to iso8859-1 */
168 function charset_decode($str) {
169 if($this->charset
== 'utf-8') {
170 if(function_exists('utf8_decode')) {
171 return utf8_decode($str);
181 /* ========================== Public ======================== */
183 /* Search the LDAP server */
184 function search($expr) {
186 /* To be replaced by advanded search expression parsing */
187 if(is_array($expr)) return false;
189 /* Encode the expression */
190 $expr = $this->charset_encode($expr);
191 if(strstr($expr, '*') === false) {
194 $expression = "cn=$expr";
196 /* Make sure connection is there */
201 $sret = @ldap_search
($this->linkid
, $this->basedn
, $expression,
202 array('dn', 'o', 'ou', 'sn', 'givenname',
203 'cn', 'mail', 'telephonenumber'),
204 0, $this->maxrows
, $this->timeout
);
206 /* Should get error from server using the ldap_error() function,
207 * but it only exist in the PHP LDAP documentation. */
209 if(function_exists('ldap_error')) {
210 return $this->set_error(ldap_error($this->linkid
));
212 return $this->set_error('ldap_search failed');
216 if(@ldap_count_entries
($this->linkid
, $sret) <= 0) {
223 $res = @ldap_get_entries
($this->linkid
, $sret);
224 for($i = 0 ; $i < $res['count'] ; $i++
) {
227 /* Extract data common for all e-mail addresses
228 * of an object. Use only the first name */
229 $nickname = $this->charset_decode($row['dn']);
230 $fullname = $this->charset_decode($row['cn'][0]);
232 if(empty($row['telephonenumber'][0])) {
235 $phone = $this->charset_decode($row['telephonenumber'][0]);
238 if(!empty($row['ou'][0])) {
239 $label = $this->charset_decode($row['ou'][0]);
241 else if(!empty($row['o'][0])) {
242 $label = $this->charset_decode($row['o'][0]);
247 if(empty($row['givenname'][0])) {
250 $firstname = $this->charset_decode($row['givenname'][0]);
253 if(empty($row['sn'][0])) {
256 $surname = $this->charset_decode($row['sn'][0]);
259 /* Add one row to result for each e-mail address */
260 if(isset($row['mail']['count'])) {
261 for($j = 0 ; $j < $row['mail']['count'] ; $j++
) {
262 array_push($ret, array('nickname' => $nickname,
264 'firstname' => $firstname,
265 'lastname' => $surname,
266 'email' => $row['mail'][$j],
269 'backend' => $this->bnum
,
270 'source' => &$this->sname
));
272 // Limit number of hits
274 if(($returned_rows >= $this->maxrows
) &&
275 ($this->maxrows
> 0) ) {
276 ldap_free_result($sret);
282 } // isset($row['mail']['count'])
286 ldap_free_result($sret);
291 /* If you run a tiny LDAP server and you want the "List All" button
292 * to show EVERYONE, then uncomment this tiny block of code:
294 * function list_addr() {
295 * return $this->search('*');
298 * Careful with this -- it could get quite large for big sites. */