4 * abook_ldap_server.php
6 * Copyright (c) 1999-2003 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.
24 * NOTE. This class should not be used directly. Use the
25 * "AddressBook" class instead.
28 * @package squirrelmail
32 * Undocumented class - fixme
33 * @package squirrelmail
35 class abook_ldap_server
extends addressbook_backend
{
36 var $btype = 'remote';
37 var $bname = 'ldap_server';
39 /* Parameters changed by class */
40 var $sname = 'LDAP'; /* Service name */
41 var $server = ''; /* LDAP server name */
42 var $port = 389; /* LDAP server port */
43 var $basedn = ''; /* LDAP base DN */
44 var $charset = 'utf-8'; /* LDAP server charset */
45 var $linkid = false; /* PHP LDAP link ID */
46 var $bound = false; /* True if LDAP server is bound */
47 var $maxrows = 250; /* Max rows in result */
48 var $timeout = 30; /* Timeout for LDAP operations (in seconds) */
50 /* Constructor. Connects to database */
51 function abook_ldap_server($param) {
52 if(!function_exists('ldap_connect')) {
53 $this->set_error('LDAP support missing from PHP');
56 if(is_array($param)) {
57 $this->server
= $param['host'];
58 $this->basedn
= $param['base'];
59 if(!empty($param['port'])) {
60 $this->port
= $param['port'];
62 if(!empty($param['charset'])) {
63 $this->charset
= strtolower($param['charset']);
65 if(isset($param['maxrows'])) {
66 $this->maxrows
= $param['maxrows'];
68 if(isset($param['timeout'])) {
69 $this->timeout
= $param['timeout'];
71 if(empty($param['name'])) {
72 $this->sname
= 'LDAP: ' . $param['host'];
75 $this->sname
= $param['name'];
80 $this->set_error('Invalid argument to constructor');
85 /* Open the LDAP server. New connection if $new is true */
86 function open($new = false) {
89 /* Connection is already open */
90 if($this->linkid
!= false && !$new) {
94 $this->linkid
= @ldap_connect
($this->server
, $this->port
);
96 if(function_exists('ldap_error')) {
97 return $this->set_error(ldap_error($this->linkid
));
99 return $this->set_error('ldap_connect failed');
103 if(!@ldap_bind
($this->linkid
)) {
104 if(function_exists('ldap_error')) {
105 return $this->set_error(ldap_error($this->linkid
));
107 return $this->set_error('ldap_bind failed');
117 /* Encode iso8859-1 string to the charset used by this LDAP server */
118 function charset_encode($str) {
119 if($this->charset
== 'utf-8') {
120 if(function_exists('utf8_encode')) {
121 return utf8_encode($str);
131 /* Decode from charset used by this LDAP server to iso8859-1 */
132 function charset_decode($str) {
133 if($this->charset
== 'utf-8') {
134 if(function_exists('utf8_decode')) {
135 return utf8_decode($str);
145 /* ========================== Public ======================== */
147 /* Search the LDAP server */
148 function search($expr) {
150 /* To be replaced by advanded search expression parsing */
151 if(is_array($expr)) return false;
153 /* Encode the expression */
154 $expr = $this->charset_encode($expr);
155 if(strstr($expr, '*') === false) {
158 $expression = "cn=$expr";
160 /* Make sure connection is there */
165 $sret = @ldap_search
($this->linkid
, $this->basedn
, $expression,
166 array('dn', 'o', 'ou', 'sn', 'givenname',
167 'cn', 'mail', 'telephonenumber'),
168 0, $this->maxrows
, $this->timeout
);
170 /* Should get error from server using the ldap_error() function,
171 * but it only exist in the PHP LDAP documentation. */
173 if(function_exists('ldap_error')) {
174 return $this->set_error(ldap_error($this->linkid
));
176 return $this->set_error('ldap_search failed');
180 if(@ldap_count_entries
($this->linkid
, $sret) <= 0) {
187 $res = @ldap_get_entries
($this->linkid
, $sret);
188 for($i = 0 ; $i < $res['count'] ; $i++
) {
191 /* Extract data common for all e-mail addresses
192 * of an object. Use only the first name */
193 $nickname = $this->charset_decode($row['dn']);
194 $fullname = $this->charset_decode($row['cn'][0]);
196 if(empty($row['telephonenumber'][0])) {
199 $phone = $this->charset_decode($row['telephonenumber'][0]);
202 if(!empty($row['ou'][0])) {
203 $label = $this->charset_decode($row['ou'][0]);
205 else if(!empty($row['o'][0])) {
206 $label = $this->charset_decode($row['o'][0]);
211 if(empty($row['givenname'][0])) {
214 $firstname = $this->charset_decode($row['givenname'][0]);
217 if(empty($row['sn'][0])) {
220 $surname = $this->charset_decode($row['sn'][0]);
223 /* Add one row to result for each e-mail address */
224 if(isset($row['mail']['count'])) {
225 for($j = 0 ; $j < $row['mail']['count'] ; $j++
) {
226 array_push($ret, array('nickname' => $nickname,
228 'firstname' => $firstname,
229 'lastname' => $surname,
230 'email' => $row['mail'][$j],
233 'backend' => $this->bnum
,
234 'source' => &$this->sname
));
236 // Limit number of hits
238 if(($returned_rows >= $this->maxrows
) &&
239 ($this->maxrows
> 0) ) {
240 ldap_free_result($sret);
246 } // isset($row['mail']['count'])
250 ldap_free_result($sret);
255 /* If you run a tiny LDAP server and you want the "List All" button
256 * to show EVERYONE, then uncomment this tiny block of code:
258 * function list_addr() {
259 * return $this->search('*');
262 * Careful with this -- it could get quite large for big sites. */