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