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