Add phpdoc doc blocks to some files.
[squirrelmail.git] / functions / abook_ldap_server.php
1 <?php
2
3 /**
4 * abook_ldap_server.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Address book backend for LDAP server
10 *
11 * An array with the following elements must be passed to
12 * the class constructor (elements marked ? are optional):
13 *
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 *
24 * NOTE. This class should not be used directly. Use the
25 * "AddressBook" class instead.
26 *
27 * $Id$
28 * @package squirrelmail
29 */
30
31 /**
32 * Undocumented class - fixme
33 * @package squirrelmail
34 */
35 class abook_ldap_server extends addressbook_backend {
36 var $btype = 'remote';
37 var $bname = 'ldap_server';
38
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) */
49
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');
54 return;
55 }
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'];
61 }
62 if(!empty($param['charset'])) {
63 $this->charset = strtolower($param['charset']);
64 }
65 if(isset($param['maxrows'])) {
66 $this->maxrows = $param['maxrows'];
67 }
68 if(isset($param['timeout'])) {
69 $this->timeout = $param['timeout'];
70 }
71 if(empty($param['name'])) {
72 $this->sname = 'LDAP: ' . $param['host'];
73 }
74 else {
75 $this->sname = $param['name'];
76 }
77
78 $this->open(true);
79 } else {
80 $this->set_error('Invalid argument to constructor');
81 }
82 }
83
84
85 /* Open the LDAP server. New connection if $new is true */
86 function open($new = false) {
87 $this->error = '';
88
89 /* Connection is already open */
90 if($this->linkid != false && !$new) {
91 return true;
92 }
93
94 $this->linkid = @ldap_connect($this->server, $this->port);
95 if(!$this->linkid) {
96 if(function_exists('ldap_error')) {
97 return $this->set_error(ldap_error($this->linkid));
98 } else {
99 return $this->set_error('ldap_connect failed');
100 }
101 }
102
103 if(!@ldap_bind($this->linkid)) {
104 if(function_exists('ldap_error')) {
105 return $this->set_error(ldap_error($this->linkid));
106 } else {
107 return $this->set_error('ldap_bind failed');
108 }
109 }
110
111 $this->bound = true;
112
113 return true;
114 }
115
116
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);
122 } else {
123 return $str;
124 }
125 } else {
126 return $str;
127 }
128 }
129
130
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);
136 } else {
137 return $str;
138 }
139 } else {
140 return $str;
141 }
142 }
143
144
145 /* ========================== Public ======================== */
146
147 /* Search the LDAP server */
148 function search($expr) {
149
150 /* To be replaced by advanded search expression parsing */
151 if(is_array($expr)) return false;
152
153 /* Encode the expression */
154 $expr = $this->charset_encode($expr);
155 if(strstr($expr, '*') === false) {
156 $expr = "*$expr*";
157 }
158 $expression = "cn=$expr";
159
160 /* Make sure connection is there */
161 if(!$this->open()) {
162 return false;
163 }
164
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);
169
170 /* Should get error from server using the ldap_error() function,
171 * but it only exist in the PHP LDAP documentation. */
172 if(!$sret) {
173 if(function_exists('ldap_error')) {
174 return $this->set_error(ldap_error($this->linkid));
175 } else {
176 return $this->set_error('ldap_search failed');
177 }
178 }
179
180 if(@ldap_count_entries($this->linkid, $sret) <= 0) {
181 return array();
182 }
183
184 /* Get results */
185 $ret = array();
186 $returned_rows = 0;
187 $res = @ldap_get_entries($this->linkid, $sret);
188 for($i = 0 ; $i < $res['count'] ; $i++) {
189 $row = $res[$i];
190
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]);
195
196 if(empty($row['telephonenumber'][0])) {
197 $phone = '';
198 } else {
199 $phone = $this->charset_decode($row['telephonenumber'][0]);
200 }
201
202 if(!empty($row['ou'][0])) {
203 $label = $this->charset_decode($row['ou'][0]);
204 }
205 else if(!empty($row['o'][0])) {
206 $label = $this->charset_decode($row['o'][0]);
207 } else {
208 $label = '';
209 }
210
211 if(empty($row['givenname'][0])) {
212 $firstname = '';
213 } else {
214 $firstname = $this->charset_decode($row['givenname'][0]);
215 }
216
217 if(empty($row['sn'][0])) {
218 $surname = '';
219 } else {
220 $surname = $this->charset_decode($row['sn'][0]);
221 }
222
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,
227 'name' => $fullname,
228 'firstname' => $firstname,
229 'lastname' => $surname,
230 'email' => $row['mail'][$j],
231 'label' => $label,
232 'phone' => $phone,
233 'backend' => $this->bnum,
234 'source' => &$this->sname));
235
236 // Limit number of hits
237 $returned_rows++;
238 if(($returned_rows >= $this->maxrows) &&
239 ($this->maxrows > 0) ) {
240 ldap_free_result($sret);
241 return $ret;
242 }
243
244 } // for($j ...)
245
246 } // isset($row['mail']['count'])
247
248 }
249
250 ldap_free_result($sret);
251 return $ret;
252 } /* end search() */
253
254
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:
257 *
258 * function list_addr() {
259 * return $this->search('*');
260 * }
261 *
262 * Careful with this -- it could get quite large for big sites. */
263 }
264 ?>