commit | author | age
|
d1d2c4
|
1 |
<?php |
S |
2 |
/* |
|
3 |
+-----------------------------------------------------------------------+ |
47124c
|
4 |
| program/include/rcube_ldap.php | |
d1d2c4
|
5 |
| | |
S |
6 |
| This file is part of the RoundCube Webmail client | |
cbbef3
|
7 |
| Copyright (C) 2006-2009, RoundCube Dev. - Switzerland | |
d1d2c4
|
8 |
| Licensed under the GNU GPL | |
S |
9 |
| | |
|
10 |
| PURPOSE: | |
f11541
|
11 |
| Interface to an LDAP address directory | |
d1d2c4
|
12 |
| | |
S |
13 |
+-----------------------------------------------------------------------+ |
f11541
|
14 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
d1d2c4
|
15 |
+-----------------------------------------------------------------------+ |
S |
16 |
|
|
17 |
$Id$ |
|
18 |
|
|
19 |
*/ |
|
20 |
|
6d969b
|
21 |
|
T |
22 |
/** |
|
23 |
* Model class to access an LDAP address directory |
|
24 |
* |
|
25 |
* @package Addressbook |
|
26 |
*/ |
cc97ea
|
27 |
class rcube_ldap extends rcube_addressbook |
f11541
|
28 |
{ |
d1d2c4
|
29 |
var $conn; |
f11541
|
30 |
var $prop = array(); |
T |
31 |
var $fieldmap = array(); |
|
32 |
|
|
33 |
var $filter = ''; |
|
34 |
var $result = null; |
|
35 |
var $ldap_result = null; |
|
36 |
var $sort_col = ''; |
010274
|
37 |
var $mail_domain = ''; |
A |
38 |
var $debug = false; |
f11541
|
39 |
|
T |
40 |
/** public properties */ |
|
41 |
var $primary_key = 'ID'; |
|
42 |
var $readonly = true; |
|
43 |
var $list_page = 1; |
|
44 |
var $page_size = 10; |
|
45 |
var $ready = false; |
|
46 |
|
|
47 |
|
|
48 |
/** |
|
49 |
* Object constructor |
|
50 |
* |
010274
|
51 |
* @param array LDAP connection properties |
A |
52 |
* @param boolean Enables debug mode |
|
53 |
* @param string Current user mail domain name |
f11541
|
54 |
* @param integer User-ID |
T |
55 |
*/ |
010274
|
56 |
function __construct($p, $debug=false, $mail_domain=NULL) |
f11541
|
57 |
{ |
T |
58 |
$this->prop = $p; |
6855ce
|
59 |
|
f11541
|
60 |
foreach ($p as $prop => $value) |
T |
61 |
if (preg_match('/^(.+)_field$/', $prop, $matches)) |
4368a0
|
62 |
$this->fieldmap[$matches[1]] = $this->_attr_name(strtolower($value)); |
A |
63 |
|
380662
|
64 |
// make sure 'required_fields' is an array |
A |
65 |
if (!is_array($this->prop['required_fields'])) |
|
66 |
$this->prop['required_fields'] = (array) $this->prop['required_fields']; |
|
67 |
|
4368a0
|
68 |
foreach ($this->prop['required_fields'] as $key => $val) |
A |
69 |
$this->prop['required_fields'][$key] = $this->_attr_name(strtolower($val)); |
4f9c83
|
70 |
|
0131ec
|
71 |
$this->sort_col = $p['sort']; |
010274
|
72 |
$this->debug = $debug; |
A |
73 |
$this->mail_domain = $mail_domain; |
4f9c83
|
74 |
|
f11541
|
75 |
$this->connect(); |
d1d2c4
|
76 |
} |
S |
77 |
|
f11541
|
78 |
|
T |
79 |
/** |
|
80 |
* Establish a connection to the LDAP server |
|
81 |
*/ |
|
82 |
function connect() |
|
83 |
{ |
b9f9f1
|
84 |
global $RCMAIL; |
T |
85 |
|
f11541
|
86 |
if (!function_exists('ldap_connect')) |
10eedb
|
87 |
raise_error(array('code' => 100, 'type' => 'ldap', |
A |
88 |
'file' => __FILE__, 'line' => __LINE__, |
|
89 |
'message' => "No ldap support in this installation of PHP"), true); |
f11541
|
90 |
|
T |
91 |
if (is_resource($this->conn)) |
|
92 |
return true; |
|
93 |
|
|
94 |
if (!is_array($this->prop['hosts'])) |
|
95 |
$this->prop['hosts'] = array($this->prop['hosts']); |
|
96 |
|
03b271
|
97 |
if (empty($this->prop['ldap_version'])) |
T |
98 |
$this->prop['ldap_version'] = 3; |
|
99 |
|
f11541
|
100 |
foreach ($this->prop['hosts'] as $host) |
T |
101 |
{ |
bb8721
|
102 |
$host = rcube_parse_host($host); |
010274
|
103 |
$this->_debug("C: Connect [$host".($this->prop['port'] ? ':'.$this->prop['port'] : '')."]"); |
A |
104 |
|
f11541
|
105 |
if ($lc = @ldap_connect($host, $this->prop['port'])) |
T |
106 |
{ |
b4b31d
|
107 |
if ($this->prop['use_tls']===true) |
T |
108 |
if (!ldap_start_tls($lc)) |
|
109 |
continue; |
cd6749
|
110 |
|
010274
|
111 |
$this->_debug("S: OK"); |
A |
112 |
|
03b271
|
113 |
ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, $this->prop['ldap_version']); |
f11541
|
114 |
$this->prop['host'] = $host; |
T |
115 |
$this->conn = $lc; |
|
116 |
break; |
|
117 |
} |
010274
|
118 |
$this->_debug("S: NOT OK"); |
f11541
|
119 |
} |
T |
120 |
|
|
121 |
if (is_resource($this->conn)) |
e3caaf
|
122 |
{ |
f11541
|
123 |
$this->ready = true; |
4f9c83
|
124 |
|
b9f9f1
|
125 |
// User specific access, generate the proper values to use. |
0131ec
|
126 |
if ($this->prop['user_specific']) { |
b9f9f1
|
127 |
// No password set, use the session password |
4f9c83
|
128 |
if (empty($this->prop['bind_pass'])) { |
2471d3
|
129 |
$this->prop['bind_pass'] = $RCMAIL->decrypt($_SESSION['password']); |
b9f9f1
|
130 |
} |
4f9c83
|
131 |
|
S |
132 |
// Get the pieces needed for variable replacement. |
b9f9f1
|
133 |
$fu = $RCMAIL->user->get_username(); |
T |
134 |
list($u, $d) = explode('@', $fu); |
|
135 |
|
|
136 |
// Replace the bind_dn and base_dn variables. |
|
137 |
$replaces = array('%fu' => $fu, '%u' => $u, '%d' => $d); |
|
138 |
$this->prop['bind_dn'] = strtr($this->prop['bind_dn'], $replaces); |
|
139 |
$this->prop['base_dn'] = strtr($this->prop['base_dn'], $replaces); |
|
140 |
} |
|
141 |
|
|
142 |
if (!empty($this->prop['bind_dn']) && !empty($this->prop['bind_pass'])) |
e3caaf
|
143 |
$this->ready = $this->bind($this->prop['bind_dn'], $this->prop['bind_pass']); |
T |
144 |
} |
f11541
|
145 |
else |
10eedb
|
146 |
raise_error(array('code' => 100, 'type' => 'ldap', |
A |
147 |
'file' => __FILE__, 'line' => __LINE__, |
|
148 |
'message' => "Could not connect to any LDAP server, last tried $host:{$this->prop[port]}"), true); |
4f9c83
|
149 |
|
S |
150 |
// See if the directory is writeable. |
|
151 |
if ($this->prop['writable']) { |
|
152 |
$this->readonly = false; |
|
153 |
} // end if |
|
154 |
|
f11541
|
155 |
} |
T |
156 |
|
|
157 |
|
|
158 |
/** |
e3caaf
|
159 |
* Bind connection with DN and password |
6d969b
|
160 |
* |
T |
161 |
* @param string Bind DN |
|
162 |
* @param string Bind password |
|
163 |
* @return boolean True on success, False on error |
f11541
|
164 |
*/ |
e3caaf
|
165 |
function bind($dn, $pass) |
f11541
|
166 |
{ |
736307
|
167 |
if (!$this->conn) { |
e3caaf
|
168 |
return false; |
736307
|
169 |
} |
e3caaf
|
170 |
|
010274
|
171 |
$this->_debug("C: Bind [dn: $dn] [pass: $pass]"); |
A |
172 |
|
b4b31d
|
173 |
if (@ldap_bind($this->conn, $dn, $pass)) { |
010274
|
174 |
$this->_debug("S: OK"); |
e3caaf
|
175 |
return true; |
736307
|
176 |
} |
010274
|
177 |
|
A |
178 |
$this->_debug("S: ".ldap_error($this->conn)); |
736307
|
179 |
|
T |
180 |
raise_error(array( |
10eedb
|
181 |
'code' => ldap_errno($this->conn), 'type' => 'ldap', |
A |
182 |
'file' => __FILE__, 'line' => __LINE__, |
e3caaf
|
183 |
'message' => "Bind failed for dn=$dn: ".ldap_error($this->conn)), |
736307
|
184 |
true); |
T |
185 |
|
e3caaf
|
186 |
return false; |
T |
187 |
} |
f11541
|
188 |
|
T |
189 |
|
|
190 |
/** |
|
191 |
* Close connection to LDAP server |
|
192 |
*/ |
|
193 |
function close() |
|
194 |
{ |
|
195 |
if ($this->conn) |
6b603d
|
196 |
{ |
010274
|
197 |
$this->_debug("C: Close"); |
0131ec
|
198 |
ldap_unbind($this->conn); |
6b603d
|
199 |
$this->conn = null; |
T |
200 |
} |
f11541
|
201 |
} |
T |
202 |
|
|
203 |
|
|
204 |
/** |
|
205 |
* Set internal list page |
|
206 |
* |
|
207 |
* @param number Page number to list |
|
208 |
* @access public |
|
209 |
*/ |
|
210 |
function set_page($page) |
|
211 |
{ |
|
212 |
$this->list_page = (int)$page; |
|
213 |
} |
|
214 |
|
|
215 |
|
|
216 |
/** |
|
217 |
* Set internal page size |
|
218 |
* |
|
219 |
* @param number Number of messages to display on one page |
|
220 |
* @access public |
|
221 |
*/ |
|
222 |
function set_pagesize($size) |
|
223 |
{ |
|
224 |
$this->page_size = (int)$size; |
|
225 |
} |
|
226 |
|
|
227 |
|
|
228 |
/** |
|
229 |
* Save a search string for future listings |
|
230 |
* |
6d969b
|
231 |
* @param string Filter string |
f11541
|
232 |
*/ |
T |
233 |
function set_search_set($filter) |
|
234 |
{ |
|
235 |
$this->filter = $filter; |
|
236 |
} |
|
237 |
|
|
238 |
|
|
239 |
/** |
|
240 |
* Getter for saved search properties |
|
241 |
* |
|
242 |
* @return mixed Search properties used by this class |
|
243 |
*/ |
|
244 |
function get_search_set() |
|
245 |
{ |
|
246 |
return $this->filter; |
|
247 |
} |
|
248 |
|
|
249 |
|
|
250 |
/** |
|
251 |
* Reset all saved results and search parameters |
|
252 |
*/ |
|
253 |
function reset() |
|
254 |
{ |
|
255 |
$this->result = null; |
|
256 |
$this->ldap_result = null; |
|
257 |
$this->filter = ''; |
|
258 |
} |
|
259 |
|
|
260 |
|
|
261 |
/** |
|
262 |
* List the current set of contact records |
|
263 |
* |
|
264 |
* @param array List of cols to show |
4f9c83
|
265 |
* @param int Only return this number of records |
f11541
|
266 |
* @return array Indexed list of contact records, each a hash array |
T |
267 |
*/ |
|
268 |
function list_records($cols=null, $subset=0) |
|
269 |
{ |
6b603d
|
270 |
// add general filter to query |
3c884a
|
271 |
if (!empty($this->prop['filter']) && empty($this->filter)) |
6b603d
|
272 |
{ |
T |
273 |
$filter = $this->prop['filter']; |
|
274 |
$this->set_search_set($filter); |
|
275 |
} |
08ff05
|
276 |
|
f11541
|
277 |
// exec LDAP search if no result resource is stored |
T |
278 |
if ($this->conn && !$this->ldap_result) |
|
279 |
$this->_exec_search(); |
|
280 |
|
|
281 |
// count contacts for this user |
|
282 |
$this->result = $this->count(); |
0131ec
|
283 |
|
f11541
|
284 |
// we have a search result resource |
T |
285 |
if ($this->ldap_result && $this->result->count > 0) |
|
286 |
{ |
0131ec
|
287 |
if ($this->sort_col && $this->prop['scope'] !== 'base') |
A |
288 |
ldap_sort($this->conn, $this->ldap_result, $this->sort_col); |
4f9c83
|
289 |
|
S |
290 |
$start_row = $subset < 0 ? $this->result->first + $this->page_size + $subset : $this->result->first; |
|
291 |
$last_row = $this->result->first + $this->page_size; |
|
292 |
$last_row = $subset != 0 ? $start_row + abs($subset) : $last_row; |
|
293 |
|
f11541
|
294 |
$entries = ldap_get_entries($this->conn, $this->ldap_result); |
4f9c83
|
295 |
for ($i = $start_row; $i < min($entries['count'], $last_row); $i++) |
f11541
|
296 |
$this->result->add($this->_ldap2result($entries[$i])); |
T |
297 |
} |
|
298 |
|
|
299 |
return $this->result; |
|
300 |
} |
|
301 |
|
|
302 |
|
|
303 |
/** |
|
304 |
* Search contacts |
|
305 |
* |
|
306 |
* @param array List of fields to search in |
|
307 |
* @param string Search value |
eb27aa
|
308 |
* @param boolean True for strict, False for partial (fuzzy) matching |
f11541
|
309 |
* @param boolean True if results are requested, False if count only |
25fdec
|
310 |
* @param boolean (Not used) |
A |
311 |
* @param array List of fields that cannot be empty |
6d969b
|
312 |
* @return array Indexed list of contact records and 'count' value |
f11541
|
313 |
*/ |
25fdec
|
314 |
function search($fields, $value, $strict=false, $select=true, $nocount=false, $required=array()) |
f11541
|
315 |
{ |
T |
316 |
// special treatment for ID-based search |
|
317 |
if ($fields == 'ID' || $fields == $this->primary_key) |
|
318 |
{ |
|
319 |
$ids = explode(',', $value); |
|
320 |
$result = new rcube_result_set(); |
|
321 |
foreach ($ids as $id) |
|
322 |
if ($rec = $this->get_record($id, true)) |
|
323 |
{ |
|
324 |
$result->add($rec); |
|
325 |
$result->count++; |
|
326 |
} |
|
327 |
|
|
328 |
return $result; |
|
329 |
} |
|
330 |
|
|
331 |
$filter = '(|'; |
3fc00e
|
332 |
$wc = !$strict && $this->prop['fuzzy_search'] ? '*' : ''; |
f11541
|
333 |
if (is_array($this->prop['search_fields'])) |
T |
334 |
{ |
|
335 |
foreach ($this->prop['search_fields'] as $k => $field) |
|
336 |
$filter .= "($field=$wc" . rcube_ldap::quote_string($value) . "$wc)"; |
|
337 |
} |
|
338 |
else |
|
339 |
{ |
|
340 |
foreach ((array)$fields as $field) |
|
341 |
if ($f = $this->_map_field($field)) |
|
342 |
$filter .= "($f=$wc" . rcube_ldap::quote_string($value) . "$wc)"; |
|
343 |
} |
|
344 |
$filter .= ')'; |
25fdec
|
345 |
|
A |
346 |
// add required (non empty) fields filter |
|
347 |
$req_filter = ''; |
|
348 |
foreach ((array)$required as $field) |
|
349 |
if ($f = $this->_map_field($field)) |
|
350 |
$req_filter .= "($f=*)"; |
|
351 |
|
|
352 |
if (!empty($req_filter)) |
|
353 |
$filter = '(&' . $req_filter . $filter . ')'; |
|
354 |
|
72c722
|
355 |
// avoid double-wildcard if $value is empty |
T |
356 |
$filter = preg_replace('/\*+/', '*', $filter); |
25fdec
|
357 |
|
e3caaf
|
358 |
// add general filter to query |
T |
359 |
if (!empty($this->prop['filter'])) |
72c722
|
360 |
$filter = '(&(' . preg_replace('/^\(|\)$/', '', $this->prop['filter']) . ')' . $filter . ')'; |
f11541
|
361 |
|
T |
362 |
// set filter string and execute search |
|
363 |
$this->set_search_set($filter); |
|
364 |
$this->_exec_search(); |
|
365 |
|
|
366 |
if ($select) |
|
367 |
$this->list_records(); |
|
368 |
else |
|
369 |
$this->result = $this->count(); |
|
370 |
|
|
371 |
return $this->result; |
|
372 |
} |
|
373 |
|
|
374 |
|
|
375 |
/** |
|
376 |
* Count number of available contacts in database |
|
377 |
* |
6d969b
|
378 |
* @return object rcube_result_set Resultset with values for 'count' and 'first' |
f11541
|
379 |
*/ |
2b8207
|
380 |
function count() |
f11541
|
381 |
{ |
T |
382 |
$count = 0; |
4f9c83
|
383 |
if ($this->conn && $this->ldap_result) { |
f11541
|
384 |
$count = ldap_count_entries($this->conn, $this->ldap_result); |
4f9c83
|
385 |
} // end if |
S |
386 |
elseif ($this->conn) { |
|
387 |
// We have a connection but no result set, attempt to get one. |
|
388 |
if (empty($this->filter)) { |
|
389 |
// The filter is not set, set it. |
|
390 |
$this->filter = $this->prop['filter']; |
|
391 |
} // end if |
|
392 |
$this->_exec_search(); |
|
393 |
if ($this->ldap_result) { |
|
394 |
$count = ldap_count_entries($this->conn, $this->ldap_result); |
|
395 |
} // end if |
|
396 |
} // end else |
f11541
|
397 |
|
T |
398 |
return new rcube_result_set($count, ($this->list_page-1) * $this->page_size); |
|
399 |
} |
|
400 |
|
|
401 |
|
|
402 |
/** |
|
403 |
* Return the last result set |
|
404 |
* |
6d969b
|
405 |
* @return object rcube_result_set Current resultset or NULL if nothing selected yet |
f11541
|
406 |
*/ |
T |
407 |
function get_result() |
|
408 |
{ |
|
409 |
return $this->result; |
|
410 |
} |
|
411 |
|
|
412 |
|
|
413 |
/** |
|
414 |
* Get a specific contact record |
|
415 |
* |
6d969b
|
416 |
* @param mixed Record identifier |
T |
417 |
* @param boolean Return as associative array |
|
418 |
* @return mixed Hash array or rcube_result_set with all record fields |
f11541
|
419 |
*/ |
T |
420 |
function get_record($dn, $assoc=false) |
|
421 |
{ |
|
422 |
$res = null; |
|
423 |
if ($this->conn && $dn) |
|
424 |
{ |
100440
|
425 |
$dn = base64_decode($dn); |
A |
426 |
|
|
427 |
$this->_debug("C: Read [dn: $dn] [(objectclass=*)]"); |
010274
|
428 |
|
100440
|
429 |
if ($this->ldap_result = @ldap_read($this->conn, $dn, '(objectclass=*)', array_values($this->fieldmap))) |
4aaecb
|
430 |
$entry = ldap_first_entry($this->conn, $this->ldap_result); |
010274
|
431 |
else |
A |
432 |
$this->_debug("S: ".ldap_error($this->conn)); |
0131ec
|
433 |
|
f11541
|
434 |
if ($entry && ($rec = ldap_get_attributes($this->conn, $entry))) |
T |
435 |
{ |
010274
|
436 |
$this->_debug("S: OK"); |
A |
437 |
|
0131ec
|
438 |
$rec = array_change_key_case($rec, CASE_LOWER); |
A |
439 |
|
4f9c83
|
440 |
// Add in the dn for the entry. |
100440
|
441 |
$rec['dn'] = $dn; |
f11541
|
442 |
$res = $this->_ldap2result($rec); |
T |
443 |
$this->result = new rcube_result_set(1); |
|
444 |
$this->result->add($res); |
|
445 |
} |
|
446 |
} |
|
447 |
|
|
448 |
return $assoc ? $res : $this->result; |
|
449 |
} |
|
450 |
|
|
451 |
|
|
452 |
/** |
|
453 |
* Create a new contact record |
|
454 |
* |
6d969b
|
455 |
* @param array Hash array with save data |
4f9c83
|
456 |
* @return encoded record ID on success, False on error |
f11541
|
457 |
*/ |
T |
458 |
function insert($save_cols) |
|
459 |
{ |
4f9c83
|
460 |
// Map out the column names to their LDAP ones to build the new entry. |
S |
461 |
$newentry = array(); |
0131ec
|
462 |
$newentry['objectClass'] = $this->prop['LDAP_Object_Classes']; |
4f9c83
|
463 |
foreach ($save_cols as $col => $val) { |
S |
464 |
$fld = $this->_map_field($col); |
b4fa59
|
465 |
if ($fld && $val) { |
4f9c83
|
466 |
// The field does exist, add it to the entry. |
S |
467 |
$newentry[$fld] = $val; |
|
468 |
} // end if |
|
469 |
} // end foreach |
|
470 |
|
|
471 |
// Verify that the required fields are set. |
|
472 |
// We know that the email address is required as a default of rcube, so |
|
473 |
// we will default its value into any unfilled required fields. |
0131ec
|
474 |
foreach ($this->prop['required_fields'] as $fld) { |
4f9c83
|
475 |
if (!isset($newentry[$fld])) { |
0131ec
|
476 |
$newentry[$fld] = $newentry[$this->_map_field('email')]; |
4f9c83
|
477 |
} // end if |
S |
478 |
} // end foreach |
|
479 |
|
|
480 |
// Build the new entries DN. |
100440
|
481 |
$dn = $this->prop['LDAP_rdn'].'='.rcube_ldap::quote_string($newentry[$this->prop['LDAP_rdn']], true) |
A |
482 |
.','.$this->prop['base_dn']; |
010274
|
483 |
|
A |
484 |
$this->_debug("C: Add [dn: $dn]: ".print_r($newentry, true)); |
|
485 |
|
0131ec
|
486 |
$res = ldap_add($this->conn, $dn, $newentry); |
4f9c83
|
487 |
if ($res === FALSE) { |
010274
|
488 |
$this->_debug("S: ".ldap_error($this->conn)); |
4f9c83
|
489 |
return false; |
S |
490 |
} // end if |
010274
|
491 |
|
A |
492 |
$this->_debug("S: OK"); |
4f9c83
|
493 |
|
S |
494 |
return base64_encode($dn); |
f11541
|
495 |
} |
T |
496 |
|
|
497 |
|
|
498 |
/** |
|
499 |
* Update a specific contact record |
|
500 |
* |
|
501 |
* @param mixed Record identifier |
6d969b
|
502 |
* @param array Hash array with save data |
T |
503 |
* @return boolean True on success, False on error |
f11541
|
504 |
*/ |
T |
505 |
function update($id, $save_cols) |
|
506 |
{ |
4f9c83
|
507 |
$record = $this->get_record($id, true); |
S |
508 |
$result = $this->get_result(); |
|
509 |
$record = $result->first(); |
|
510 |
|
|
511 |
$newdata = array(); |
|
512 |
$replacedata = array(); |
|
513 |
$deletedata = array(); |
|
514 |
foreach ($save_cols as $col => $val) { |
|
515 |
$fld = $this->_map_field($col); |
b4fa59
|
516 |
if ($fld) { |
4f9c83
|
517 |
// The field does exist compare it to the ldap record. |
S |
518 |
if ($record[$col] != $val) { |
|
519 |
// Changed, but find out how. |
|
520 |
if (!isset($record[$col])) { |
|
521 |
// Field was not set prior, need to add it. |
|
522 |
$newdata[$fld] = $val; |
|
523 |
} // end if |
0131ec
|
524 |
elseif ($val == '') { |
4f9c83
|
525 |
// Field supplied is empty, verify that it is not required. |
0131ec
|
526 |
if (!in_array($fld, $this->prop['required_fields'])) { |
4f9c83
|
527 |
// It is not, safe to clear. |
S |
528 |
$deletedata[$fld] = $record[$col]; |
|
529 |
} // end if |
|
530 |
} // end elseif |
|
531 |
else { |
|
532 |
// The data was modified, save it out. |
|
533 |
$replacedata[$fld] = $val; |
|
534 |
} // end else |
|
535 |
} // end if |
|
536 |
} // end if |
|
537 |
} // end foreach |
|
538 |
|
|
539 |
$dn = base64_decode($id); |
e83f03
|
540 |
|
A |
541 |
// Update the entry as required. |
4f9c83
|
542 |
if (!empty($deletedata)) { |
S |
543 |
// Delete the fields. |
010274
|
544 |
$this->_debug("C: Delete [dn: $dn]: ".print_r($deletedata, true)); |
A |
545 |
if (!ldap_mod_del($this->conn, $dn, $deletedata)) { |
|
546 |
$this->_debug("S: ".ldap_error($this->conn)); |
4f9c83
|
547 |
return false; |
010274
|
548 |
} |
A |
549 |
$this->_debug("S: OK"); |
4f9c83
|
550 |
} // end if |
S |
551 |
|
|
552 |
if (!empty($replacedata)) { |
e83f03
|
553 |
// Handle RDN change |
A |
554 |
if ($replacedata[$this->prop['LDAP_rdn']]) { |
100440
|
555 |
$newdn = $this->prop['LDAP_rdn'].'=' |
A |
556 |
.rcube_ldap::quote_string($replacedata[$this->prop['LDAP_rdn']], true) |
|
557 |
.','.$this->prop['base_dn']; |
e83f03
|
558 |
if ($dn != $newdn) { |
100440
|
559 |
$newrdn = $this->prop['LDAP_rdn'].'=' |
A |
560 |
.rcube_ldap::quote_string($replacedata[$this->prop['LDAP_rdn']], true); |
e83f03
|
561 |
unset($replacedata[$this->prop['LDAP_rdn']]); |
A |
562 |
} |
|
563 |
} |
4f9c83
|
564 |
// Replace the fields. |
e83f03
|
565 |
if (!empty($replacedata)) { |
010274
|
566 |
$this->_debug("C: Replace [dn: $dn]: ".print_r($replacedata, true)); |
A |
567 |
if (!ldap_mod_replace($this->conn, $dn, $replacedata)) { |
|
568 |
$this->_debug("S: ".ldap_error($this->conn)); |
e83f03
|
569 |
return false; |
010274
|
570 |
} |
A |
571 |
$this->_debug("S: OK"); |
4f9c83
|
572 |
} // end if |
S |
573 |
} // end if |
|
574 |
|
|
575 |
if (!empty($newdata)) { |
|
576 |
// Add the fields. |
010274
|
577 |
$this->_debug("C: Add [dn: $dn]: ".print_r($newdata, true)); |
A |
578 |
if (!ldap_mod_add($this->conn, $dn, $newdata)) { |
|
579 |
$this->_debug("S: ".ldap_error($this->conn)); |
4f9c83
|
580 |
return false; |
010274
|
581 |
} |
A |
582 |
$this->_debug("S: OK"); |
4f9c83
|
583 |
} // end if |
S |
584 |
|
e83f03
|
585 |
// Handle RDN change |
A |
586 |
if (!empty($newrdn)) { |
010274
|
587 |
$this->_debug("C: Rename [dn: $dn] [dn: $newrdn]"); |
A |
588 |
if (@ldap_rename($this->conn, $dn, $newrdn, NULL, TRUE)) { |
|
589 |
$this->_debug("S: ".ldap_error($this->conn)); |
e83f03
|
590 |
return base64_encode($newdn); |
010274
|
591 |
} |
A |
592 |
$this->_debug("S: OK"); |
e83f03
|
593 |
} |
A |
594 |
|
4f9c83
|
595 |
return true; |
f11541
|
596 |
} |
T |
597 |
|
|
598 |
|
|
599 |
/** |
|
600 |
* Mark one or more contact records as deleted |
|
601 |
* |
|
602 |
* @param array Record identifiers |
6d969b
|
603 |
* @return boolean True on success, False on error |
f11541
|
604 |
*/ |
T |
605 |
function delete($ids) |
|
606 |
{ |
4f9c83
|
607 |
if (!is_array($ids)) { |
S |
608 |
// Not an array, break apart the encoded DNs. |
0131ec
|
609 |
$dns = explode(',', $ids); |
4f9c83
|
610 |
} // end if |
S |
611 |
|
|
612 |
foreach ($dns as $id) { |
|
613 |
$dn = base64_decode($id); |
010274
|
614 |
$this->_debug("C: Delete [dn: $dn]"); |
4f9c83
|
615 |
// Delete the record. |
0131ec
|
616 |
$res = ldap_delete($this->conn, $dn); |
4f9c83
|
617 |
if ($res === FALSE) { |
010274
|
618 |
$this->_debug("S: ".ldap_error($this->conn)); |
4f9c83
|
619 |
return false; |
S |
620 |
} // end if |
010274
|
621 |
$this->_debug("S: OK"); |
4f9c83
|
622 |
} // end foreach |
S |
623 |
|
b24f2c
|
624 |
return count($dns); |
f11541
|
625 |
} |
T |
626 |
|
|
627 |
|
|
628 |
/** |
|
629 |
* Execute the LDAP search based on the stored credentials |
|
630 |
* |
6d969b
|
631 |
* @access private |
f11541
|
632 |
*/ |
4368a0
|
633 |
private function _exec_search() |
f11541
|
634 |
{ |
08ff05
|
635 |
if ($this->ready) |
f11541
|
636 |
{ |
08ff05
|
637 |
$filter = $this->filter ? $this->filter : '(objectclass=*)'; |
f11541
|
638 |
$function = $this->prop['scope'] == 'sub' ? 'ldap_search' : ($this->prop['scope'] == 'base' ? 'ldap_read' : 'ldap_list'); |
4aaecb
|
639 |
|
010274
|
640 |
$this->_debug("C: Search [".$filter."]"); |
A |
641 |
|
93c018
|
642 |
if ($this->ldap_result = @$function($this->conn, $this->prop['base_dn'], $filter, |
A |
643 |
array_values($this->fieldmap), 0, (int) $this->prop['sizelimit'], (int) $this->prop['timelimit']) |
|
644 |
) { |
010274
|
645 |
$this->_debug("S: ".ldap_count_entries($this->conn, $this->ldap_result)." record(s)"); |
4aaecb
|
646 |
return true; |
010274
|
647 |
} else |
A |
648 |
$this->_debug("S: ".ldap_error($this->conn)); |
f11541
|
649 |
} |
4aaecb
|
650 |
|
A |
651 |
return false; |
f11541
|
652 |
} |
T |
653 |
|
|
654 |
|
|
655 |
/** |
6d969b
|
656 |
* @access private |
f11541
|
657 |
*/ |
4368a0
|
658 |
private function _ldap2result($rec) |
f11541
|
659 |
{ |
ea18c5
|
660 |
global $RCMAIL; |
A |
661 |
|
f11541
|
662 |
$out = array(); |
T |
663 |
|
|
664 |
if ($rec['dn']) |
|
665 |
$out[$this->primary_key] = base64_encode($rec['dn']); |
|
666 |
|
|
667 |
foreach ($this->fieldmap as $rf => $lf) |
|
668 |
{ |
ea18c5
|
669 |
if ($rec[$lf]['count']) { |
4fc7a7
|
670 |
if ($rf == 'email' && $this->mail_domain && !strpos($rec[$lf][0], '@')) |
010274
|
671 |
$out[$rf] = sprintf('%s@%s', $rec[$lf][0], $this->mail_domain); |
ea18c5
|
672 |
else |
A |
673 |
$out[$rf] = $rec[$lf][0]; |
|
674 |
} |
f11541
|
675 |
} |
T |
676 |
|
|
677 |
return $out; |
|
678 |
} |
|
679 |
|
|
680 |
|
|
681 |
/** |
6d969b
|
682 |
* @access private |
f11541
|
683 |
*/ |
4368a0
|
684 |
private function _map_field($field) |
f11541
|
685 |
{ |
T |
686 |
return $this->fieldmap[$field]; |
|
687 |
} |
|
688 |
|
|
689 |
|
|
690 |
/** |
4368a0
|
691 |
* @access private |
A |
692 |
*/ |
|
693 |
private function _attr_name($name) |
|
694 |
{ |
|
695 |
// list of known attribute aliases |
|
696 |
$aliases = array( |
|
697 |
'gn' => 'givenname', |
5ffceb
|
698 |
'rfc822mailbox' => 'email', |
4368a0
|
699 |
'userid' => 'uid', |
A |
700 |
'emailaddress' => 'email', |
|
701 |
'pkcs9email' => 'email', |
|
702 |
); |
|
703 |
return isset($aliases[$name]) ? $aliases[$name] : $name; |
|
704 |
} |
|
705 |
|
|
706 |
|
|
707 |
/** |
010274
|
708 |
* @access private |
A |
709 |
*/ |
|
710 |
private function _debug($str) |
|
711 |
{ |
|
712 |
if ($this->debug) |
|
713 |
write_log('ldap', $str); |
|
714 |
} |
|
715 |
|
|
716 |
|
|
717 |
/** |
f11541
|
718 |
* @static |
T |
719 |
*/ |
100440
|
720 |
function quote_string($str, $dn=false) |
f11541
|
721 |
{ |
100440
|
722 |
if ($dn) |
A |
723 |
$replace = array(','=>'\2c', '='=>'\3d', '+'=>'\2b', '<'=>'\3c', |
|
724 |
'>'=>'\3e', ';'=>'\3b', '\\'=>'\5c', '"'=>'\22', '#'=>'\23'); |
|
725 |
else |
|
726 |
$replace = array('*'=>'\2a', '('=>'\28', ')'=>'\29', '\\'=>'\5c', |
|
727 |
'/'=>'\2f'); |
|
728 |
|
|
729 |
return strtr($str, $replace); |
f11541
|
730 |
} |
T |
731 |
|
|
732 |
} |
|
733 |
|
0131ec
|
734 |
?> |