Duh
[squirrelmail.git] / functions / abook_ldap_server.php
CommitLineData
5100704d 1<?php
4b4abf93 2
35586184 3/**
4 * abook_ldap_server.php
5 *
981681d5 6 * Address book backend for LDAP server
7 *
ae4d36f7 8 * LDAP filtering code by Tim Bell
9 * <bhat at users.sourceforge.net> (#539534)
f8a1ed5a 10 * ADS limit_scope code by Michael Brown
ae4d36f7 11 * <mcb30 at users.sourceforge.net> (#1035454)
593370a4 12 * StartTLS code by John Lane
13 * <starfry at users.sourceforge.net> (#1197703)
664fd7a0 14 * Code for remove, add, modify, lookup by David Härdeman
15 * <david at 2gen.com> (#1495763)
ae4d36f7 16 *
664fd7a0 17 * This backend uses LDAP person (RFC2256), organizationalPerson (RFC2256)
18 * and inetOrgPerson (RFC2798) objects and dn, description, sn, givenname,
19 * cn, mail attributes. Other attributes are ignored.
20 *
47ccfad4 21 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
4b4abf93 22 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
981681d5 23 * @version $Id$
24 * @package squirrelmail
25 * @subpackage addressbook
26 */
27
28/**
35586184 29 * Address book backend for LDAP server
30 *
31 * An array with the following elements must be passed to
3d1fa376 32 * the class constructor (elements marked ? are optional)
33 *
34 * Main settings:
981681d5 35 * <pre>
664fd7a0 36 * host => LDAP server hostname, IP-address or any other URI compatible
37 * with used LDAP library.
35586184 38 * base => LDAP server root (base dn). Empty string allowed.
39 * ? port => LDAP server TCP port number (default: 389)
40 * ? charset => LDAP server charset (default: utf-8)
41 * ? name => Name for LDAP server (default "LDAP: hostname")
42 * Used to tag the result data
43 * ? maxrows => Maximum # of rows in search result
44 * ? timeout => Timeout for LDAP operations (in seconds, default: 30)
45 * Might not work for all LDAP libraries or servers.
30e9932c 46 * ? binddn => LDAP Bind DN.
47 * ? bindpw => LDAP Bind Password.
48 * ? protocol => LDAP Bind protocol.
3d1fa376 49 * </pre>
50 * Advanced settings:
51 * <pre>
52 * ? filter => Filter expression to limit ldap searches
53 * ? limit_scope => Limits scope to base DN (Specific to Win2k3 ADS).
54 * ? listing => Controls listing of LDAP directory.
664fd7a0 55 * ? writeable => Controls write access to address book
593370a4 56 * ? search_tree => Controls subtree or one level search.
57 * ? starttls => Controls use of StartTLS on LDAP connections
981681d5 58 * </pre>
664fd7a0 59 * NOTE. This class should not be used directly. Use addressbook_init()
60 * function instead.
d6c32258 61 * @package squirrelmail
a9d318b0 62 * @subpackage addressbook
35586184 63 */
35586184 64class abook_ldap_server extends addressbook_backend {
981681d5 65 /**
66 * @var string backend type
67 */
06b4facd 68 var $btype = 'remote';
981681d5 69 /**
70 * @var string backend name
71 */
06b4facd 72 var $bname = 'ldap_server';
73
74 /* Parameters changed by class */
981681d5 75 /**
76 * @var string displayed name
77 */
06b4facd 78 var $sname = 'LDAP'; /* Service name */
981681d5 79 /**
80 * @var string LDAP server name or address or url
81 */
82 var $server = '';
83 /**
84 * @var integer LDAP server port
85 */
86 var $port = 389;
87 /**
88 * @var string LDAP base DN
89 */
90 var $basedn = '';
91 /**
92 * @var string charset used for entries in LDAP server
93 */
94 var $charset = 'utf-8';
95 /**
96 * @var object PHP LDAP link ID
97 */
98 var $linkid = false;
99 /**
100 * @var bool True if LDAP server is bound
101 */
102 var $bound = false;
103 /**
104 * @var integer max rows in result
105 */
106 var $maxrows = 250;
ae4d36f7 107 /**
108 * @var string ldap filter
109 * @since 1.5.1
110 */
111 var $filter = '';
981681d5 112 /**
113 * @var integer timeout of LDAP operations (in seconds)
114 */
115 var $timeout = 30;
116 /**
117 * @var string DN to bind to (non-anonymous bind)
118 * @since 1.5.0 and 1.4.3
119 */
120 var $binddn = '';
121 /**
122 * @var string password to bind with (non-anonymous bind)
123 * @since 1.5.0 and 1.4.3
124 */
125 var $bindpw = '';
126 /**
127 * @var integer protocol used to connect to ldap server
128 * @since 1.5.0 and 1.4.3
129 */
130 var $protocol = '';
ae4d36f7 131 /**
132 * @var boolean limits scope to base dn
133 * @since 1.5.1
134 */
135 var $limit_scope = false;
3d1fa376 136 /**
137 * @var boolean controls listing of directory
138 * @since 1.5.1
139 */
140 var $listing = false;
664fd7a0 141 /**
142 * @var boolean true if removing/adding/modifying entries is allowed
143 * @since 1.5.2
144 */
9ab998a6 145 var $writeable = false;
593370a4 146 /**
147 * @var boolean controls ldap search type.
148 * only first level entries are displayed if set to false
149 * @since 1.5.1
150 */
151 var $search_tree = true;
152 /**
153 * @var boolean controls use of StartTLS on ldap
154 * connections. Requires php 4.2+ and protocol >= 3
155 * @since 1.5.1
156 */
157 var $starttls = false;
981681d5 158
159 /**
160 * Constructor. Connects to database
161 * @param array connection options
162 */
06b4facd 163 function abook_ldap_server($param) {
164 if(!function_exists('ldap_connect')) {
ae4d36f7 165 $this->set_error(_("PHP install does not have LDAP support."));
06b4facd 166 return;
167 }
168 if(is_array($param)) {
169 $this->server = $param['host'];
664fd7a0 170 // remove whitespace from basedn
171 $this->basedn = preg_replace('/,\s*/',',',trim($param['base']));
ae4d36f7 172
173 if(!empty($param['port']))
06b4facd 174 $this->port = $param['port'];
ae4d36f7 175
176 if(!empty($param['charset']))
06b4facd 177 $this->charset = strtolower($param['charset']);
ae4d36f7 178
179 if(isset($param['maxrows']))
06b4facd 180 $this->maxrows = $param['maxrows'];
ae4d36f7 181
182 if(isset($param['timeout']))
06b4facd 183 $this->timeout = $param['timeout'];
ae4d36f7 184
185 if(isset($param['binddn']))
30e9932c 186 $this->binddn = $param['binddn'];
ae4d36f7 187
188 if(isset($param['bindpw']))
30e9932c 189 $this->bindpw = $param['bindpw'];
ae4d36f7 190
191 if(isset($param['protocol']))
593370a4 192 $this->protocol = (int) $param['protocol'];
ae4d36f7 193
194 if(isset($param['filter']))
195 $this->filter = trim($param['filter']);
196
197 if(isset($param['limit_scope']))
593370a4 198 $this->limit_scope = (bool) $param['limit_scope'];
ae4d36f7 199
3d1fa376 200 if(isset($param['listing']))
593370a4 201 $this->listing = (bool) $param['listing'];
202
664fd7a0 203 if(isset($param['writeable'])) {
204 $this->writeable = (bool) $param['writeable'];
205 // switch backend type to local, if it is writable
206 if($this->writeable) $this->btype = 'local';
207 }
208
593370a4 209 if(isset($param['search_tree']))
210 $this->search_tree = (bool) $param['search_tree'];
211
212 if(isset($param['starttls']))
213 $this->starttls = (bool) $param['starttls'];
3d1fa376 214
06b4facd 215 if(empty($param['name'])) {
216 $this->sname = 'LDAP: ' . $param['host'];
ae4d36f7 217 } else {
06b4facd 218 $this->sname = $param['name'];
219 }
62f7daa5 220
ae4d36f7 221 /*
222 * don't open LDAP server on addressbook_init(),
223 * open ldap connection only on search. Speeds up
224 * addressbook_init() call.
225 */
226 // $this->open(true);
06b4facd 227 } else {
228 $this->set_error('Invalid argument to constructor');
229 }
230 }
231
232
981681d5 233 /**
234 * Open the LDAP server.
235 * @param bool $new is it a new connection
236 * @return bool
237 */
06b4facd 238 function open($new = false) {
239 $this->error = '';
62f7daa5 240
06b4facd 241 /* Connection is already open */
242 if($this->linkid != false && !$new) {
243 return true;
244 }
62f7daa5 245
06b4facd 246 $this->linkid = @ldap_connect($this->server, $this->port);
593370a4 247 /**
248 * check if connection was successful
249 * It does not work with OpenLDAP 2.x libraries. Connect error will be
250 * displayed only on ldap command that tries to make connection
251 * (ldap_start_tls or ldap_bind).
252 */
06b4facd 253 if(!$this->linkid) {
593370a4 254 return $this->set_error($this->ldap_error('ldap_connect failed'));
06b4facd 255 }
62f7daa5 256
981681d5 257 if(!empty($this->protocol)) {
593370a4 258 // make sure that ldap_set_option() is available before using it
259 if(! function_exists('ldap_set_option') ||
260 !@ldap_set_option($this->linkid, LDAP_OPT_PROTOCOL_VERSION, $this->protocol)) {
261 return $this->set_error('unable to set ldap protocol number');
262 }
263 }
264
265 /**
266 * http://www.php.net/ldap-start-tls
267 * Check if v3 or newer protocol is used,
268 * check if ldap_start_tls function is available.
8f227330 269 * Silently ignore setting, if these requirements are not satisfied.
270 * Break with error message if somebody tries to start TLS on
271 * ldaps or socket connection.
593370a4 272 */
273 if($this->starttls &&
274 !empty($this->protocol) && $this->protocol >= 3 &&
275 function_exists('ldap_start_tls') ) {
8f227330 276 // make sure that $this->server is not ldaps:// or ldapi:// URL.
277 if (preg_match("/^ldap[si]:\/\/.+/i",$this->server)) {
278 return $this->set_error("you can't enable starttls on ldaps and ldapi connections.");
593370a4 279 }
593370a4 280
281 // try starting tls
282 if (! @ldap_start_tls($this->linkid)) {
283 // set error if call fails
284 return $this->set_error($this->ldap_error('ldap_start_tls failed'));
981681d5 285 }
286 }
30e9932c 287
ae4d36f7 288 if(!empty($this->limit_scope) && $this->limit_scope) {
289 if(empty($this->protocol) || intval($this->protocol) < 3) {
290 return $this->set_error('limit_scope requires protocol >= 3');
291 }
292 // See http://msdn.microsoft.com/library/en-us/ldap/ldap/ldap_server_domain_scope_oid.asp
293 $ctrl = array ( "oid" => "1.2.840.113556.1.4.1339", "iscritical" => TRUE );
593370a4 294 /*
295 * Option is set only during connection.
296 * It does not cause immediate errors with OpenLDAP 2.x libraries.
297 */
298 if(! function_exists('ldap_set_option') ||
299 !@ldap_set_option($this->linkid, LDAP_OPT_SERVER_CONTROLS, array($ctrl))) {
300 return $this->set_error($this->ldap_error('limit domain scope failed'));
ae4d36f7 301 }
302 }
303
593370a4 304 // authenticated bind
30e9932c 305 if(!empty($this->binddn)) {
306 if(!@ldap_bind($this->linkid, $this->binddn, $this->bindpw)) {
593370a4 307 return $this->set_error($this->ldap_error('authenticated ldap_bind failed'));
308 }
30e9932c 309 } else {
593370a4 310 // anonymous bind
981681d5 311 if(!@ldap_bind($this->linkid)) {
593370a4 312 return $this->set_error($this->ldap_error('anonymous ldap_bind failed'));
981681d5 313 }
06b4facd 314 }
30e9932c 315
06b4facd 316 $this->bound = true;
62f7daa5 317
06b4facd 318 return true;
319 }
320
981681d5 321 /**
322 * Encode string to the charset used by this LDAP server
323 * @param string string that has to be encoded
324 * @return string encoded string
325 */
06b4facd 326 function charset_encode($str) {
b64dd897 327 global $default_charset;
328 if($this->charset != $default_charset) {
329 return charset_convert($default_charset,$str,$this->charset,false);
06b4facd 330 } else {
331 return $str;
332 }
333 }
334
981681d5 335 /**
b64dd897 336 * Decode from charset used by this LDAP server to charset used by translation
981681d5 337 *
598294a7 338 * Uses SquirrelMail charset_decode functions
981681d5 339 * @param string string that has to be decoded
340 * @return string decoded string
341 */
06b4facd 342 function charset_decode($str) {
981681d5 343 global $default_charset;
344 if ($this->charset != $default_charset) {
b64dd897 345 return charset_convert($this->charset,$str,$default_charset,false);
06b4facd 346 } else {
347 return $str;
348 }
349 }
350
d58ed98f 351 /**
352 * Sanitizes ldap search strings.
353 * See rfc2254
354 * @link http://www.faqs.org/rfcs/rfc2254.html
ae4d36f7 355 * @since 1.5.1 and 1.4.5
d58ed98f 356 * @param string $string
357 * @return string sanitized string
358 */
359 function ldapspecialchars($string) {
360 $sanitized=array('\\' => '\5c',
361 '*' => '\2a',
362 '(' => '\28',
363 ')' => '\29',
364 "\x00" => '\00');
365
366 return str_replace(array_keys($sanitized),array_values($sanitized),$string);
367 }
62f7daa5 368
664fd7a0 369 /**
370 * Prepares user input for use in a ldap query.
371 *
372 * Function converts input string to character set used in LDAP server
373 * (charset_encode() method) and sanitizes it (ldapspecialchars()).
374 *
375 * @param string $string string to encode
376 * @return string ldap encoded string
377 * @since 1.5.2
378 */
379 function quotevalue($string) {
380 $sanitized = $this->charset_encode($string);
381 return $this->ldapspecialchars($sanitized);
382 }
383
981681d5 384 /**
3d1fa376 385 * Search LDAP server.
386 *
387 * Warning: You must make sure that ldap query is correctly formated and
388 * sanitize use of special ldap keywords.
389 * @param string $expression ldap query
664fd7a0 390 * @param boolean $singleentry (since 1.5.2) whether we are looking for a
391 * single entry. Boolean true forces LDAP_SCOPE_BASE search.
3d1fa376 392 * @return array search results (false on error)
393 * @since 1.5.1
981681d5 394 */
664fd7a0 395 function ldap_search($expression, $singleentry = false) {
06b4facd 396 /* Make sure connection is there */
397 if(!$this->open()) {
398 return false;
399 }
62f7daa5 400
664fd7a0 401 $attributes = array('dn', 'description', 'sn', 'givenname', 'cn', 'mail');
402
403 if ($singleentry) {
404 // ldap_read - search for one single entry
405 $sret = @ldap_read($this->linkid, $expression, "objectClass=*",
406 $attributes, 0, $this->maxrows, $this->timeout);
407 } elseif ($this->search_tree) {
593370a4 408 // ldap_search - search subtree
409 $sret = @ldap_search($this->linkid, $this->basedn, $expression,
664fd7a0 410 $attributes, 0, $this->maxrows, $this->timeout);
593370a4 411 } else {
412 // ldap_list - search one level
413 $sret = @ldap_list($this->linkid, $this->basedn, $expression,
664fd7a0 414 $attributes, 0, $this->maxrows, $this->timeout);
593370a4 415 }
62f7daa5 416
593370a4 417 /* Return error if search failed */
06b4facd 418 if(!$sret) {
664fd7a0 419 // Check for LDAP_NO_SUCH_OBJECT (0x20 or 32) error
420 if (ldap_errno($this->linkid)==32) {
421 return array();
422 } else {
423 return $this->set_error($this->ldap_error('ldap_search failed'));
424 }
06b4facd 425 }
62f7daa5 426
06b4facd 427 if(@ldap_count_entries($this->linkid, $sret) <= 0) {
428 return array();
429 }
62f7daa5 430
06b4facd 431 /* Get results */
432 $ret = array();
433 $returned_rows = 0;
434 $res = @ldap_get_entries($this->linkid, $sret);
435 for($i = 0 ; $i < $res['count'] ; $i++) {
436 $row = $res[$i];
62f7daa5 437
06b4facd 438 /* Extract data common for all e-mail addresses
b682f335 439 * of an object. Use only the first name */
06b4facd 440 $nickname = $this->charset_decode($row['dn']);
b682f335 441
442 /**
664fd7a0 443 * remove trailing basedn
444 * remove whitespaces between RDNs
445 * remove leading "cn="
446 * which gives nicknames which are shorter while still unique
447 */
448 $nickname = preg_replace('/,\s*/',',', trim($nickname));
449 $offset = strlen($nickname) - strlen($this->basedn);
450
451 if($offset > 0 && substr($nickname, $offset) == $this->basedn) {
452 $nickname = substr($nickname, 0, $offset);
453 if(substr($nickname, -1) == ",")
454 $nickname = substr($nickname, 0, -1);
06b4facd 455 }
664fd7a0 456 if(strncasecmp($nickname, "cn=", 3) == 0)
457 $nickname=substr($nickname, 3);
458
459 if(empty($row['description'][0])) {
06b4facd 460 $label = '';
664fd7a0 461 } else {
462 $label = $this->charset_decode($row['description'][0]);
06b4facd 463 }
62f7daa5 464
06b4facd 465 if(empty($row['givenname'][0])) {
466 $firstname = '';
467 } else {
468 $firstname = $this->charset_decode($row['givenname'][0]);
469 }
62f7daa5 470
06b4facd 471 if(empty($row['sn'][0])) {
472 $surname = '';
473 } else {
664fd7a0 474 // remove whitespace in order to handle sn set to empty string
475 $surname = trim($this->charset_decode($row['sn'][0]));
06b4facd 476 }
62f7daa5 477
35235328 478 $fullname = $this->fullname($firstname,$surname);
664fd7a0 479
06b4facd 480 /* Add one row to result for each e-mail address */
481 if(isset($row['mail']['count'])) {
482 for($j = 0 ; $j < $row['mail']['count'] ; $j++) {
483 array_push($ret, array('nickname' => $nickname,
484 'name' => $fullname,
485 'firstname' => $firstname,
486 'lastname' => $surname,
487 'email' => $row['mail'][$j],
488 'label' => $label,
06b4facd 489 'backend' => $this->bnum,
490 'source' => &$this->sname));
62f7daa5 491
06b4facd 492 // Limit number of hits
493 $returned_rows++;
62f7daa5 494 if(($returned_rows >= $this->maxrows) &&
06b4facd 495 ($this->maxrows > 0) ) {
496 ldap_free_result($sret);
497 return $ret;
498 }
499
500 } // for($j ...)
501
502 } // isset($row['mail']['count'])
62f7daa5 503
06b4facd 504 }
62f7daa5 505
06b4facd 506 ldap_free_result($sret);
507 return $ret;
3d1fa376 508 }
509
664fd7a0 510 /**
511 * Add an entry to LDAP server.
512 *
513 * Warning: You must make sure that the arguments are correctly formated and
514 * sanitize use of special ldap keywords.
515 * @param string $dn the dn of the entry to be added
516 * @param array $data the values of the entry to be added
517 * @return boolean result (false on error)
518 * @since 1.5.2
519 */
520 function ldap_add($dn, $data) {
521 /* Make sure connection is there */
522 if(!$this->open()) {
523 return false;
524 }
525
526 if(!@ldap_add($this->linkid, $dn, $data)) {
527 $this->set_error(_("Write to address book failed"));
528 return false;
529 }
530
531 return true;
532 }
533
534 /**
535 * Remove an entry from LDAP server.
536 *
537 * Warning: You must make sure that the argument is correctly formated and
538 * sanitize use of special ldap keywords.
539 * @param string $dn the dn of the entry to remove
540 * @return boolean result (false on error)
541 * @since 1.5.2
542 */
543 function ldap_remove($dn) {
544 /* Make sure connection is there */
545 if(!$this->open()) {
546 return false;
547 }
548
549 if(!@ldap_delete($this->linkid, $dn)) {
550 $this->set_error(_("Removing entry from address book failed"));
551 return false;
552 }
553
554 return true;
555 }
556
557 /**
558 * Rename an entry on LDAP server.
559 *
560 * Warning: You must make sure that the arguments are correctly formated and
561 * sanitize use of special ldap keywords.
562 * @param string $sourcedn the dn of the entry to be renamed
563 * @param string $targetdn the dn which $sourcedn should be renamed to
564 * @param string $parent the dn of the parent entry
565 * @return boolean result (false on error)
566 * @since 1.5.2
567 */
568 function ldap_rename($sourcedn, $targetdn, $parent) {
569 /* Make sure connection is there */
570 if(!$this->open()) {
571 return false;
572 }
573
574 /* Make sure that the protocol version supports rename */
575 if($this->protocol < 3) {
576 $this->set_error(_("LDAP rename is not supported by used protocol version"));
577 return false;
578 }
579 /**
580 * Function is available only in OpenLDAP 2.x.x or Netscape Directory
581 * SDK x.x, and was added in PHP 4.0.5
582 * @todo maybe we can use copy + delete instead of ldap_rename()
583 */
584 if(!function_exists('ldap_rename')) {
585 $this->set_error(_("LDAP rename is not supported by used LDAP library. You can't change nickname"));
586 return false;
587 }
588
589 /* OK, go for it */
590 if(!@ldap_rename($this->linkid, $sourcedn, $targetdn, $parent, true)) {
591 $this->set_error(_("LDAP rename failed"));
592 return false;
593 }
594
595 return true;
596 }
597
598 /**
599 * Modify the values of an entry on LDAP server.
600 *
601 * Warning: You must make sure that the arguments are correctly formated and
602 * sanitize use of special ldap keywords.
603 * @param string $dn the dn of the entry to be modified
604 * @param array $data the new values of the entry
605 * @param array $deleted_attribs attributes that should be deleted.
606 * @return bool result (false on error)
607 * @since 1.5.2
608 */
609 function ldap_modify($dn, $data, $deleted_attribs) {
610 /* Make sure connection is there */
611 if(!$this->open()) {
612 return false;
613 }
614
615 if(!@ldap_modify($this->linkid, $dn, $data)) {
616 $this->set_error(_("Write to address book failed"));
617 return false;
618 }
619
620 if (!@ldap_mod_del($this->linkid, $dn, $deleted_attribs)) {
621 $this->set_error(_("Unable to remove some field values"));
622 return false;
623 }
624
625 return true;
626 }
627
593370a4 628 /**
629 * Get error from LDAP resource if possible
630 *
631 * Should get error from server using the ldap_errno() and ldap_err2str() functions
632 * @param string $sError error message used when ldap error functions
633 * and connection resource are unavailable
634 * @return string error message
635 * @since 1.5.1
636 */
637 function ldap_error($sError) {
638 // it is possible that function_exists() tests are not needed
639 if(function_exists('ldap_err2str') &&
640 function_exists('ldap_errno') &&
641 is_resource($this->linkid)) {
642 return ldap_err2str(ldap_errno($this->linkid));
643 // return ldap_error($this->linkid);
644 } else {
645 return $sError;
646 }
647 }
648
3d1fa376 649 /* ========================== Public ======================== */
650
651 /**
652 * Search the LDAP server
653 * @param string $expr search expression
654 * @return array search results
655 */
656 function search($expr) {
657 /* To be replaced by advanded search expression parsing */
658 if(is_array($expr)) return false;
659
660 // don't allow wide search when listing is disabled.
327e2d96 661 if ($expr=='*' && ! $this->listing) {
662 return array();
663 } elseif ($expr=='*') {
664 // allow use of wildcard when listing is enabled.
665 $expression = '(cn=*)';
666 } else {
664fd7a0 667 /* Convert search from user's charset to the one used in ldap and sanitize */
668 $expr = $this->quotevalue($expr);
17a62a1b 669
670 /* Search for same string in cn, main and sn */
671 $expression = '(|(cn=*'.$expr.'*)(mail=*'.$expr.'*)(sn=*'.$expr.'*))';
3d1fa376 672
327e2d96 673 /* Undo sanitizing of * symbol */
674 $expression = str_replace('\2a','*',$expression);
327e2d96 675 }
3d1fa376 676
677 /* Add search filtering */
678 if ($this->filter!='')
679 $expression = '(&' . $this->filter . $expression . ')';
680
681 /* Use internal search function and return search results */
682 return $this->ldap_search($expression);
683 }
06b4facd 684
664fd7a0 685 /**
686 * Lookup an alias
687 * @param string $alias alias
688 * @return array search results
689 * @since 1.5.2
690 */
691 function lookup($alias) {
692 /* Generate the dn and try to retrieve that single entry */
693 $cn = $this->quotevalue($alias);
694 $dn = 'cn=' . $cn . ',' . $this->basedn;
695
696 /* Do the search */
697 $result = $this->ldap_search($dn, true);
698 if (!is_array($result) || count($result) < 1)
699 return array();
700
701 return $result[0];
702 }
06b4facd 703
981681d5 704 /**
705 * List all entries present in LDAP server
06b4facd 706 *
3d1fa376 707 * maxrows setting might limit list of returned entries.
981681d5 708 * Careful with this -- it could get quite large for big sites.
709 * @return array all entries in ldap server
710 */
711 function list_addr() {
3d1fa376 712 if (! $this->listing)
713 return array();
714
715 /* set wide search expression */
716 $expression = '(cn=*)';
717
718 /* add filtering */
719 if ($this->filter!='')
720 $expression = '(&' . $this->filter . $expression .')';
721
722 /* use internal search function and return search results */
723 return $this->ldap_search($expression);
981681d5 724 }
664fd7a0 725
726 /**
727 * Add address
728 * @param array $userdata new data
729 * @return boolean
730 * @since 1.5.2
731 */
732 function add($userdata) {
733 if(!$this->writeable) {
734 return $this->set_error(_("Address book is read-only"));
735 }
736
737 /* Convert search from user's charset to the one used in ldap and sanitize */
738 $cn = $this->quotevalue($userdata['nickname']);
739 $dn = 'cn=' . $cn . ',' . trim($this->basedn);
740
741 /* See if user exists already */
742 $user = $this->ldap_search($dn, true);
743 if (!is_array($user)) {
744 return false;
745 } elseif (count($user) > 0) {
746 return $this->set_error(sprintf(_("User \"%s\" already exists"), $userdata['nickname']));
747 }
748
749 /* init variable */
750 $data = array();
751
752 /* Prepare data */
753 $data['cn'] = $cn;
754 $data['mail'] = $this->quotevalue($userdata['email']);
755 $data["objectclass"][0] = "top";
756 $data["objectclass"][1] = "person";
757 $data["objectclass"][2] = "organizationalPerson";
758 $data["objectclass"][3] = "inetOrgPerson";
759 /* sn is required in person object */
760 if(!empty($userdata['lastname'])) {
761 $data['sn'] = $this->quotevalue($userdata['lastname']);
762 } else {
763 $data['sn'] = ' ';
764 }
765 /* optional fields */
766 if(!empty($userdata['firstname']))
767 $data['givenName'] = $this->quotevalue($userdata['firstname']);
768 if(!empty($userdata['label'])) {
769 $data['description'] = $this->quotevalue($userdata['label']);
770 }
771 return $this->ldap_add($dn, $data);
772 }
773
774 /**
775 * Delete address
776 * @param array $aliases array of entries that have to be removed.
777 * @return boolean
778 * @since 1.5.2
779 */
780 function remove($aliases) {
781 if(!$this->writeable) {
782 return $this->set_error(_("Address book is read-only"));
783 }
784
785 foreach ($aliases as $alias) {
786 /* Convert nickname from user's charset and derive cn/dn */
787 $cn = $this->quotevalue($alias);
788 $dn = 'cn=' . $cn . ',' . $this->basedn;
789
790 if (!$this->ldap_remove($dn))
791 return false;
792 }
793
794 return true;
795 }
796
797 /**
798 * Modify address
799 * @param string $alias modified alias
800 * @param array $userdata new data
801 * @return boolean
802 * @since 1.5.2
803 */
804 function modify($alias, $userdata) {
805 if(!$this->writeable) {
806 return $this->set_error(_("Address book is read-only"));
807 }
808
809 /* Convert search from user's charset to the one used in ldap and sanitize */
810 $sourcecn = $this->quotevalue($alias);
811 $sourcedn = 'cn=' . $sourcecn . ',' . trim($this->basedn);
812 $targetcn = $this->quotevalue($userdata['nickname']);
813 $targetdn = 'cn=' . $targetcn . ',' . trim($this->basedn);
814
815 /* Check that the dn to modify exists */
816 $sourceuser = $this->lookup($alias);
817 if (!is_array($sourceuser) || count($sourceuser) < 1)
818 return false;
819
820 /* Check if dn is going to change */
821 if ($alias != $userdata['nickname']) {
822
823 /* Check that the target dn doesn't exist */
824 $targetuser = $this->lookup($userdata['nickname']);
825 if (is_array($targetuser) && count($targetuser) > 0)
826 return $this->set_error(sprintf(_("User \"%s\" already exists"), $userdata['nickname']));
827
828 /* Rename from the source dn to target dn */
829 if (!$this->ldap_rename($sourcedn, 'cn=' . $targetcn, $this->basedn))
830 return $this->set_error(sprintf(_("Unable to rename user \"%s\" to \"%s\""), $alias, $userdata['nickname']));
831 }
832
833 // initial vars
834 $data = array();
835 $deleted_attribs = array();
836
837 /* Prepare data */
838 $data['cn'] = $this->quotevalue($targetcn);
839 $data['mail'] = $this->quotevalue($userdata['email']);
840 $data["objectclass"][0] = "top";
841 $data["objectclass"][1] = "person";
842 $data["objectclass"][2] = "organizationalPerson";
843 $data["objectclass"][3] = "inetOrgPerson";
844
845 if(!empty($userdata['firstname'])) {
846 $data['givenName'] = $this->quotevalue($userdata['firstname']);
847 } elseif (!empty($sourceuser['firstname'])) {
848 $deleted_attribs['givenName'] = $this->quotevalue($sourceuser['firstname']);
849 }
850
851 if(!empty($userdata['lastname'])) {
852 $data['sn'] = $this->quotevalue($userdata['lastname']);
853 } else {
854 // sn is required attribute in LDAP person object.
855 // SquirrelMail requires givenName or Surname
856 $data['sn'] = ' ';
857 }
858
859 if(!empty($userdata['label'])) {
860 $data['description'] = $this->quotevalue($userdata['label']);
861 } elseif (!empty($sourceuser['label'])) {
862 $deleted_attribs['description'] = $this->quotevalue($sourceuser['label']);
863 }
864
865 return $this->ldap_modify($targetdn, $data, $deleted_attribs);
866 }
06b4facd 867}