| | |
| | | |
| | | foreach ($this->prop['hosts'] as $host) |
| | | { |
| | | $host = idn_to_ascii(rcube_parse_host($host)); |
| | | $this->_debug("C: Connect [$host".($this->prop['port'] ? ':'.$this->prop['port'] : '')."]"); |
| | | $host = idn_to_ascii(rcube_parse_host($host)); |
| | | $hostname = $host.($this->prop['port'] ? ':'.$this->prop['port'] : ''); |
| | | |
| | | $this->_debug("C: Connect [$hostname]"); |
| | | |
| | | if ($lc = @ldap_connect($host, $this->prop['port'])) |
| | | { |
| | | if ($this->prop['use_tls']===true) |
| | | if ($this->prop['use_tls'] === true) |
| | | if (!ldap_start_tls($lc)) |
| | | continue; |
| | | |
| | |
| | | ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, $this->prop['ldap_version']); |
| | | $this->prop['host'] = $host; |
| | | $this->conn = $lc; |
| | | |
| | | if (isset($this->prop['referrals'])) |
| | | ldap_set_option($lc, LDAP_OPT_REFERRALS, $this->prop['referrals']); |
| | | break; |
| | | } |
| | | $this->_debug("S: NOT OK"); |
| | | } |
| | | |
| | | if (is_resource($this->conn)) |
| | | { |
| | | $this->ready = true; |
| | | |
| | | $bind_pass = $this->prop['bind_pass']; |
| | | $bind_user = $this->prop['bind_user']; |
| | | $bind_dn = $this->prop['bind_dn']; |
| | | |
| | | $this->base_dn = $this->prop['base_dn']; |
| | | $this->groups_base_dn = ($this->prop['groups']['base_dn']) ? |
| | | $this->prop['groups']['base_dn'] : $this->base_dn; |
| | | |
| | | // User specific access, generate the proper values to use. |
| | | if ($this->prop['user_specific']) { |
| | | // No password set, use the session password |
| | | if (empty($bind_pass)) { |
| | | $bind_pass = $RCMAIL->decrypt($_SESSION['password']); |
| | | } |
| | | |
| | | // Get the pieces needed for variable replacement. |
| | | if ($fu = $RCMAIL->user->get_username()) |
| | | list($u, $d) = explode('@', $fu); |
| | | else |
| | | $d = $this->mail_domain; |
| | | |
| | | $dc = 'dc='.strtr($d, array('.' => ',dc=')); // hierarchal domain string |
| | | |
| | | $replaces = array('%dc' => $dc, '%d' => $d, '%fu' => $fu, '%u' => $u); |
| | | |
| | | if ($this->prop['search_base_dn'] && $this->prop['search_filter']) { |
| | | // Search for the dn to use to authenticate |
| | | $this->prop['search_base_dn'] = strtr($this->prop['search_base_dn'], $replaces); |
| | | $this->prop['search_filter'] = strtr($this->prop['search_filter'], $replaces); |
| | | |
| | | $this->_debug("S: searching with base {$this->prop['search_base_dn']} for {$this->prop['search_filter']}"); |
| | | |
| | | $res = @ldap_search($this->conn, $this->prop['search_base_dn'], $this->prop['search_filter'], array('uid')); |
| | | if ($res && ($entry = ldap_first_entry($this->conn, $res))) { |
| | | $bind_dn = ldap_get_dn($this->conn, $entry); |
| | | |
| | | $this->_debug("S: search returned dn: $bind_dn"); |
| | | |
| | | if ($bind_dn) { |
| | | $dn = ldap_explode_dn($bind_dn, 1); |
| | | $replaces = array('%dn' => $dn[0]) + $replaces; |
| | | } |
| | | } |
| | | } |
| | | // Replace the bind_dn and base_dn variables. |
| | | $bind_dn = strtr($bind_dn, $replaces); |
| | | $this->base_dn = strtr($this->base_dn, $replaces); |
| | | $this->groups_base_dn = strtr($this->groups_base_dn, $replaces); |
| | | |
| | | if (empty($bind_user)) { |
| | | $bind_user = $u; |
| | | } |
| | | } |
| | | |
| | | if (!empty($bind_pass)) { |
| | | if (!empty($bind_dn)) { |
| | | $this->ready = $this->bind($bind_dn, $bind_pass); |
| | | } |
| | | else if (!empty($this->prop['auth_cid'])) { |
| | | $this->ready = $this->sasl_bind($this->prop['auth_cid'], $bind_pass, $bind_user); |
| | | } |
| | | else { |
| | | $this->ready = $this->sasl_bind($bind_user, $bind_pass); |
| | | } |
| | | } |
| | | } |
| | | else |
| | | raise_error(array('code' => 100, 'type' => 'ldap', |
| | | 'file' => __FILE__, 'line' => __LINE__, |
| | | 'message' => "Could not connect to any LDAP server, last tried $host:{$this->prop[port]}"), true); |
| | | |
| | | // See if the directory is writeable. |
| | | if ($this->prop['writable']) { |
| | | $this->readonly = false; |
| | | } // end if |
| | | } |
| | | |
| | | if (!is_resource($this->conn)) { |
| | | raise_error(array('code' => 100, 'type' => 'ldap', |
| | | 'file' => __FILE__, 'line' => __LINE__, |
| | | 'message' => "Could not connect to any LDAP server, last tried $hostname"), true); |
| | | |
| | | return false; |
| | | } |
| | | |
| | | $bind_pass = $this->prop['bind_pass']; |
| | | $bind_user = $this->prop['bind_user']; |
| | | $bind_dn = $this->prop['bind_dn']; |
| | | |
| | | $this->base_dn = $this->prop['base_dn']; |
| | | $this->groups_base_dn = ($this->prop['groups']['base_dn']) ? |
| | | $this->prop['groups']['base_dn'] : $this->base_dn; |
| | | |
| | | // User specific access, generate the proper values to use. |
| | | if ($this->prop['user_specific']) { |
| | | // No password set, use the session password |
| | | if (empty($bind_pass)) { |
| | | $bind_pass = $RCMAIL->decrypt($_SESSION['password']); |
| | | } |
| | | |
| | | // Get the pieces needed for variable replacement. |
| | | if ($fu = $RCMAIL->user->get_username()) |
| | | list($u, $d) = explode('@', $fu); |
| | | else |
| | | $d = $this->mail_domain; |
| | | |
| | | $dc = 'dc='.strtr($d, array('.' => ',dc=')); // hierarchal domain string |
| | | |
| | | $replaces = array('%dn' => '', '%dc' => $dc, '%d' => $d, '%fu' => $fu, '%u' => $u); |
| | | |
| | | if ($this->prop['search_base_dn'] && $this->prop['search_filter']) { |
| | | // Search for the dn to use to authenticate |
| | | $this->prop['search_base_dn'] = strtr($this->prop['search_base_dn'], $replaces); |
| | | $this->prop['search_filter'] = strtr($this->prop['search_filter'], $replaces); |
| | | |
| | | $this->_debug("S: searching with base {$this->prop['search_base_dn']} for {$this->prop['search_filter']}"); |
| | | |
| | | $res = @ldap_search($this->conn, $this->prop['search_base_dn'], $this->prop['search_filter'], array('uid')); |
| | | if ($res) { |
| | | if (($entry = ldap_first_entry($this->conn, $res)) |
| | | && ($bind_dn = ldap_get_dn($this->conn, $entry)) |
| | | ) { |
| | | $this->_debug("S: search returned dn: $bind_dn"); |
| | | $dn = ldap_explode_dn($bind_dn, 1); |
| | | $replaces['%dn'] = $dn[0]; |
| | | } |
| | | } |
| | | else { |
| | | $this->_debug("S: ".ldap_error($this->conn)); |
| | | } |
| | | |
| | | // DN not found |
| | | if (empty($replaces['%dn'])) { |
| | | if (!empty($this->prop['search_dn_default'])) |
| | | $replaces['%dn'] = $this->prop['search_dn_default']; |
| | | else { |
| | | raise_error(array( |
| | | 'code' => 100, 'type' => 'ldap', |
| | | 'file' => __FILE__, 'line' => __LINE__, |
| | | 'message' => "DN not found using LDAP search."), true); |
| | | return false; |
| | | } |
| | | } |
| | | } |
| | | |
| | | // Replace the bind_dn and base_dn variables. |
| | | $bind_dn = strtr($bind_dn, $replaces); |
| | | $this->base_dn = strtr($this->base_dn, $replaces); |
| | | $this->groups_base_dn = strtr($this->groups_base_dn, $replaces); |
| | | |
| | | if (empty($bind_user)) { |
| | | $bind_user = $u; |
| | | } |
| | | } |
| | | |
| | | if (empty($bind_pass)) { |
| | | $this->ready = true; |
| | | } |
| | | else { |
| | | if (!empty($bind_dn)) { |
| | | $this->ready = $this->bind($bind_dn, $bind_pass); |
| | | } |
| | | else if (!empty($this->prop['auth_cid'])) { |
| | | $this->ready = $this->sasl_bind($this->prop['auth_cid'], $bind_pass, $bind_user); |
| | | } |
| | | else { |
| | | $this->ready = $this->sasl_bind($bind_user, $bind_pass); |
| | | } |
| | | } |
| | | |
| | | return $this->ready; |
| | | } |
| | | |
| | | |
| | |
| | | * |
| | | * @param mixed $fields The field name of array of field names to search in |
| | | * @param mixed $value Search value (or array of values when $fields is array) |
| | | * @param boolean $strict True for strict, False for partial (fuzzy) matching |
| | | * @param int $mode Matching mode: |
| | | * 0 - partial (*abc*), |
| | | * 1 - strict (=), |
| | | * 2 - prefix (abc*) |
| | | * @param boolean $select True if results are requested, False if count only |
| | | * @param boolean $nocount (Not used) |
| | | * @param array $required List of fields that cannot be empty |
| | | * |
| | | * @return array Indexed list of contact records and 'count' value |
| | | */ |
| | | function search($fields, $value, $strict=false, $select=true, $nocount=false, $required=array()) |
| | | function search($fields, $value, $mode=0, $select=true, $nocount=false, $required=array()) |
| | | { |
| | | $mode = intval($mode); |
| | | |
| | | // special treatment for ID-based search |
| | | if ($fields == 'ID' || $fields == $this->primary_key) |
| | | { |
| | |
| | | return $result; |
| | | } |
| | | |
| | | // use VLV pseudo-search for autocompletion |
| | | if ($this->prop['vlv_search'] && $this->conn && join(',', (array)$fields) == 'email,name') |
| | | { |
| | | // add general filter to query |
| | | if (!empty($this->prop['filter']) && empty($this->filter)) |
| | | $this->set_search_set($this->prop['filter']); |
| | | |
| | | // set VLV controls with encoded search string |
| | | $this->_vlv_set_controls($this->prop, $this->list_page, $this->page_size, $value); |
| | | |
| | | $function = $this->_scope2func($this->prop['scope']); |
| | | $this->ldap_result = @$function($this->conn, $this->base_dn, $this->filter ? $this->filter : '(objectclass=*)', |
| | | array_values($this->fieldmap), 0, (int)$this->prop['sizelimit'], (int)$this->prop['timelimit']); |
| | | |
| | | // get all entries of this page and post-filter those that really match the query |
| | | $search = mb_strtolower($value); |
| | | $this->result = new rcube_result_set(0); |
| | | $entries = ldap_get_entries($this->conn, $this->ldap_result); |
| | | |
| | | for ($i = 0; $i < $entries['count']; $i++) { |
| | | $rec = $this->_ldap2result($entries[$i]); |
| | | foreach (array('email', 'name') as $f) { |
| | | $val = mb_strtolower($rec[$f]); |
| | | switch ($mode) { |
| | | case 1: |
| | | $got = ($val == $search); |
| | | break; |
| | | case 2: |
| | | $got = ($search == substr($val, 0, strlen($search))); |
| | | break; |
| | | default: |
| | | $got = (strpos($val, $search) !== false); |
| | | break; |
| | | } |
| | | |
| | | if ($got) { |
| | | $this->result->add($rec); |
| | | $this->result->count++; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | return $this->result; |
| | | } |
| | | |
| | | // use AND operator for advanced searches |
| | | $filter = is_array($value) ? '(&' : '(|'; |
| | | $wc = !$strict && $this->prop['fuzzy_search'] ? '*' : ''; |
| | | // set wildcards |
| | | $wp = $ws = ''; |
| | | if (!empty($this->prop['fuzzy_search']) && $mode != 1) { |
| | | $ws = '*'; |
| | | if (!$mode) { |
| | | $wp = '*'; |
| | | } |
| | | } |
| | | |
| | | if ($fields == '*') |
| | | { |
| | |
| | | if (is_array($this->prop['search_fields'])) |
| | | { |
| | | foreach ($this->prop['search_fields'] as $field) { |
| | | $filter .= "($field=$wc" . $this->_quote_string($value) . "$wc)"; |
| | | $filter .= "($field=$wp" . $this->_quote_string($value) . "$ws)"; |
| | | } |
| | | } |
| | | } |
| | |
| | | foreach ((array)$fields as $idx => $field) { |
| | | $val = is_array($value) ? $value[$idx] : $value; |
| | | if ($f = $this->_map_field($field)) { |
| | | $filter .= "($f=$wc" . $this->_quote_string($val) . "$wc)"; |
| | | $filter .= "($f=$wp" . $this->_quote_string($val) . "$ws)"; |
| | | } |
| | | } |
| | | } |
| | |
| | | /** |
| | | * Set server controls for Virtual List View (paginated listing) |
| | | */ |
| | | private function _vlv_set_controls($prop, $list_page, $page_size) |
| | | private function _vlv_set_controls($prop, $list_page, $page_size, $search = null) |
| | | { |
| | | $sort_ctrl = array('oid' => "1.2.840.113556.1.4.473", 'value' => $this->_sort_ber_encode((array)$prop['sort'])); |
| | | $vlv_ctrl = array('oid' => "2.16.840.1.113730.3.4.9", 'value' => $this->_vlv_ber_encode(($offset = ($list_page-1) * $page_size + 1), $page_size), 'iscritical' => true); |
| | | $vlv_ctrl = array('oid' => "2.16.840.1.113730.3.4.9", 'value' => $this->_vlv_ber_encode(($offset = ($list_page-1) * $page_size + 1), $page_size, $search), 'iscritical' => true); |
| | | |
| | | $sort = (array)$prop['sort']; |
| | | $this->_debug("C: set controls sort=" . join(' ', unpack('H'.(strlen($sort_ctrl['value'])*2), $sort_ctrl['value'])) . " ($sort[0]);" |
| | |
| | | |
| | | |
| | | /** |
| | | * Activate/deactivate debug mode |
| | | * |
| | | * @param boolean $dbg True if LDAP commands should be logged |
| | | * @access public |
| | | */ |
| | | function set_debug($dbg = true) |
| | | { |
| | | $this->debug = $dbg; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Quotes attribute value string |
| | | * |
| | | * @param string $str Attribute value |
| | |
| | | |
| | | $groups = array(); |
| | | if ($search) { |
| | | $search = strtolower($search); |
| | | $search = mb_strtolower($search); |
| | | foreach ($group_cache as $group) { |
| | | if (strstr(strtolower($group['name']), $search)) |
| | | if (strpos(mb_strtolower($group['name']), $search) !== false) |
| | | $groups[] = $group; |
| | | } |
| | | } |
| | |
| | | $groups[$group_id]['email'][] = $ldap_data[$i][$email_attr][$j]; |
| | | } |
| | | |
| | | $group_sortnames[] = strtolower($ldap_data[$i][$sort_attr][0]); |
| | | $group_sortnames[] = mb_strtolower($ldap_data[$i][$sort_attr][0]); |
| | | } |
| | | |
| | | // recursive call can exit here |
| | |
| | | * @param integer Records per page |
| | | * @return string BER encoded option value |
| | | */ |
| | | private function _vlv_ber_encode($offset, $rpp) |
| | | private function _vlv_ber_encode($offset, $rpp, $search = '') |
| | | { |
| | | # this string is ber-encoded, php will prefix this value with: |
| | | # 04 (octet string) and 10 (length of 16 bytes) |
| | |
| | | # a0 = type context-specific/constructed with a length of 06 (6) bytes following |
| | | # 02 = type integer with 2 bytes following (offset): 01 01 (ie 1) |
| | | # 02 = type integer with 2 bytes following (contentCount): 01 00 |
| | | |
| | | # whith a search string present: |
| | | # 81 = type context-specific/constructed with a length of 04 (4) bytes following (the length will change here) |
| | | # 81 indicates a user string is present where as a a0 indicates just a offset search |
| | | # 81 = type context-specific/constructed with a length of 06 (6) bytes following |
| | | |
| | | # the following info was taken from the ISO/IEC 8825-1:2003 x.690 standard re: the |
| | | # encoding of integer values (note: these values are in |
| | | # two-complement form so since offset will never be negative bit 8 of the |
| | |
| | | # of the second (to the left of first octet) octet: |
| | | # a) shall not all be ones; and |
| | | # b) shall not all be zero |
| | | |
| | | if ($search) |
| | | { |
| | | $search = preg_replace('/[^-[:alpha:] ,.()0-9]+/', '', $search); |
| | | $ber_val = self::_string2hex($search); |
| | | $str = self::_ber_addseq($ber_val, '81'); |
| | | } |
| | | else |
| | | { |
| | | # construct the string from right to left |
| | | $str = "020100"; # contentCount |
| | | |
| | | # construct the string from right to left |
| | | $str = "020100"; # contentCount |
| | | $ber_val = self::_ber_encode_int($offset); // returns encoded integer value in hex format |
| | | |
| | | $ber_val = self::_ber_encode_int($offset); // returns encoded integer value in hex format |
| | | // calculate octet length of $ber_val |
| | | $str = self::_ber_addseq($ber_val, '02') . $str; |
| | | |
| | | // calculate octet length of $ber_val |
| | | $str = self::_ber_addseq($ber_val, '02') . $str; |
| | | |
| | | // now compute length over $str |
| | | $str = self::_ber_addseq($str, 'a0'); |
| | | |
| | | // now compute length over $str |
| | | $str = self::_ber_addseq($str, 'a0'); |
| | | } |
| | | |
| | | // now tack on records per page |
| | | $str = "020100" . self::_ber_addseq(self::_ber_encode_int($rpp-1), '02') . $str; |
| | | |