4 * abook_ldap_server.php
6 * Copyright (c) 1999-2002 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.
30 class abook_ldap_server
extends addressbook_backend
{
31 var $btype = 'remote';
32 var $bname = 'ldap_server';
34 /* Parameters changed by class */
35 var $sname = 'LDAP'; /* Service name */
36 var $server = ''; /* LDAP server name */
37 var $port = 389; /* LDAP server port */
38 var $basedn = ''; /* LDAP base DN */
39 var $charset = 'utf-8'; /* LDAP server charset */
40 var $linkid = false; /* PHP LDAP link ID */
41 var $bound = false; /* True if LDAP server is bound */
42 var $maxrows = 250; /* Max rows in result */
43 var $timeout = 30; /* Timeout for LDAP operations (in seconds) */
45 /* Constructor. Connects to database */
46 function abook_ldap_server($param) {
47 if(!function_exists('ldap_connect')) {
48 $this->set_error('LDAP support missing from PHP');
51 if(is_array($param)) {
52 $this->server
= $param['host'];
53 $this->basedn
= $param['base'];
54 if(!empty($param['port'])) {
55 $this->port
= $param['port'];
57 if(!empty($param['charset'])) {
58 $this->charset
= strtolower($param['charset']);
60 if(isset($param['maxrows'])) {
61 $this->maxrows
= $param['maxrows'];
63 if(isset($param['timeout'])) {
64 $this->timeout
= $param['timeout'];
66 if(empty($param['name'])) {
67 $this->sname
= 'LDAP: ' . $param['host'];
70 $this->sname
= $param['name'];
75 $this->set_error('Invalid argument to constructor');
80 /* Open the LDAP server. New connection if $new is true */
81 function open($new = false) {
84 /* Connection is already open */
85 if($this->linkid
!= false && !$new) {
89 $this->linkid
= @ldap_connect
($this->server
, $this->port
);
91 if(function_exists('ldap_error')) {
92 return $this->set_error(ldap_error($this->linkid
));
94 return $this->set_error('ldap_connect failed');
98 if(!@ldap_bind
($this->linkid
)) {
99 if(function_exists('ldap_error')) {
100 return $this->set_error(ldap_error($this->linkid
));
102 return $this->set_error('ldap_bind failed');
112 /* Encode iso8859-1 string to the charset used by this LDAP server */
113 function charset_encode($str) {
114 if($this->charset
== 'utf-8') {
115 if(function_exists('utf8_encode')) {
116 return utf8_encode($str);
126 /* Decode from charset used by this LDAP server to iso8859-1 */
127 function charset_decode($str) {
128 if($this->charset
== 'utf-8') {
129 if(function_exists('utf8_decode')) {
130 return utf8_decode($str);
140 /* ========================== Public ======================== */
142 /* Search the LDAP server */
143 function search($expr) {
145 /* To be replaced by advanded search expression parsing */
146 if(is_array($expr)) return false;
148 /* Encode the expression */
149 $expr = $this->charset_encode($expr);
150 if(strstr($expr, '*') === false) {
153 $expression = "cn=$expr";
155 /* Make sure connection is there */
160 /* Do the search. Use improved ldap_search() if PHP version is
162 if(sqCheckPHPVersion(4, 0, 2)) {
163 $sret = @ldap_search
($this->linkid
, $this->basedn
, $expression,
164 array('dn', 'o', 'ou', 'sn', 'givenname',
165 'cn', 'mail', 'telephonenumber'),
166 0, $this->maxrows
, $this->timeout
);
168 $sret = @ldap_search
($this->linkid
, $this->basedn
, $expression,
169 array('dn', 'o', 'ou', 'sn', 'givenname',
170 'cn', 'mail', 'telephonenumber'));
173 /* Should get error from server using the ldap_error() function,
174 * but it only exist in the PHP LDAP documentation. */
176 if(function_exists('ldap_error')) {
177 return $this->set_error(ldap_error($this->linkid
));
179 return $this->set_error('ldap_search failed');
183 if(@ldap_count_entries
($this->linkid
, $sret) <= 0) {
190 $res = @ldap_get_entries
($this->linkid
, $sret);
191 for($i = 0 ; $i < $res['count'] ; $i++
) {
194 /* Extract data common for all e-mail addresses
195 * of an object. Use only the first name */
196 $nickname = $this->charset_decode($row['dn']);
197 $fullname = $this->charset_decode($row['cn'][0]);
199 if(empty($row['telephonenumber'][0])) {
202 $phone = $this->charset_decode($row['telephonenumber'][0]);
205 if(!empty($row['ou'][0])) {
206 $label = $this->charset_decode($row['ou'][0]);
208 else if(!empty($row['o'][0])) {
209 $label = $this->charset_decode($row['o'][0]);
214 if(empty($row['givenname'][0])) {
217 $firstname = $this->charset_decode($row['givenname'][0]);
220 if(empty($row['sn'][0])) {
223 $surname = $this->charset_decode($row['sn'][0]);
226 /* Add one row to result for each e-mail address */
227 if(isset($row['mail']['count'])) {
228 for($j = 0 ; $j < $row['mail']['count'] ; $j++
) {
229 array_push($ret, array('nickname' => $nickname,
231 'firstname' => $firstname,
232 'lastname' => $surname,
233 'email' => $row['mail'][$j],
236 'backend' => $this->bnum
,
237 'source' => &$this->sname
));
239 // Limit number of hits
241 if(($returned_rows >= $this->maxrows
) &&
242 ($this->maxrows
> 0) ) {
243 ldap_free_result($sret);
249 } // isset($row['mail']['count'])
253 ldap_free_result($sret);
258 /* If you run a tiny LDAP server and you want the "List All" button
259 * to show EVERYONE, then uncomment this tiny block of code:
261 * function list_addr() {
262 * return $this->search('*');
265 * Careful with this -- it could get quite large for big sites. */