From e2c9ab4df80ec9077408e59531a6f8d1baae53e4 Mon Sep 17 00:00:00 2001 From: alecpl Date: Wed, 4 Jan 2012 13:32:14 +0000 Subject: - Fix strict email address searching if contact has more than one address --- program/include/rcube_contacts.php | 69 +++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 27 deletions(-) (limited to 'program/include') diff --git a/program/include/rcube_contacts.php b/program/include/rcube_contacts.php index fe600e008..ab3b181a5 100644 --- a/program/include/rcube_contacts.php +++ b/program/include/rcube_contacts.php @@ -62,6 +62,8 @@ class rcube_contacts extends rcube_addressbook 'gender', 'maidenname', 'spouse', 'email', 'phone', 'address', 'birthday', 'anniversary', 'website', 'im', 'notes', 'photo'); + const SEPARATOR = ','; + /** * Object constructor @@ -232,8 +234,10 @@ class rcube_contacts extends rcube_addressbook if ($read_vcard) $sql_arr = $this->convert_db_data($sql_arr); - else - $sql_arr['email'] = preg_split('/,\s*/', $sql_arr['email']); + else { + $sql_arr['email'] = explode(self::SEPARATOR, $sql_arr['email']); + $sql_arr['email'] = array_map('trim', $sql_arr['email']); + } // make sure we have a name to display if (empty($sql_arr['name'])) { @@ -287,11 +291,13 @@ class rcube_contacts extends rcube_addressbook $where = $and_where = array(); $mode = intval($mode); + $WS = ' '; + $AS = self::SEPARATOR; foreach ($fields as $idx => $col) { // direct ID search if ($col == 'ID' || $col == $this->primary_key) { - $ids = !is_array($value) ? explode(',', $value) : $value; + $ids = !is_array($value) ? explode(self::SEPARATOR, $value) : $value; $ids = $this->db->array2list($ids, 'integer'); $where[] = 'c.' . $this->primary_key.' IN ('.$ids.')'; continue; @@ -299,19 +305,19 @@ class rcube_contacts extends rcube_addressbook // fulltext search in all fields else if ($col == '*') { $words = array(); - foreach (explode(" ", self::normalize_string($value)) as $word) { + foreach (explode($WS, self::normalize_string($value)) as $word) { switch ($mode) { case 1: // strict - $words[] = '(' . $this->db->ilike('words', $word.' %') - . ' OR ' . $this->db->ilike('words', '% '.$word.' %') - . ' OR ' . $this->db->ilike('words', '% '.$word) . ')'; + $words[] = '(' . $this->db->ilike('words', $word . '%') + . ' OR ' . $this->db->ilike('words', '%' . $WS . $word . $WS . '%') + . ' OR ' . $this->db->ilike('words', '%' . $WS . $word) . ')'; break; case 2: // prefix - $words[] = '(' . $this->db->ilike('words', $word.'%') - . ' OR ' . $this->db->ilike('words', '% '.$word.'%') . ')'; + $words[] = '(' . $this->db->ilike('words', $word . '%') + . ' OR ' . $this->db->ilike('words', '%' . $WS . $word . '%') . ')'; break; default: // partial - $words[] = $this->db->ilike('words', '%'.$word.'%'); + $words[] = $this->db->ilike('words', '%' . $word . '%'); } } $where[] = '(' . join(' AND ', $words) . ')'; @@ -322,13 +328,17 @@ class rcube_contacts extends rcube_addressbook if (in_array($col, $this->table_cols)) { switch ($mode) { case 1: // strict - $where[] = $this->db->quoteIdentifier($col).' = '.$this->db->quote($val); + $where[] = '(' . $this->db->quoteIdentifier($col) . ' = ' . $this->db->quote($val) + . ' OR ' . $this->db->ilike($col, $val . $AS . '%') + . ' OR ' . $this->db->ilike($col, '%' . $AS . $val . $AS . '%') + . ' OR ' . $this->db->ilike($col, '%' . $AS . $val) . ')'; break; case 2: // prefix - $where[] = $this->db->ilike($col, $val.'%'); + $where[] = '(' . $this->db->ilike($col, $val . '%') + . ' OR ' . $this->db->ilike($col, $AS . $val . '%') . ')'; break; default: // partial - $where[] = $this->db->ilike($col, '%'.$val.'%'); + $where[] = $this->db->ilike($col, '%' . $val . '%'); } } // vCard field @@ -337,16 +347,16 @@ class rcube_contacts extends rcube_addressbook foreach (explode(" ", self::normalize_string($val)) as $word) { switch ($mode) { case 1: // strict - $words[] = '(' . $this->db->ilike('words', $word.' %') - . ' OR ' . $this->db->ilike('words', '% '.$word.' %') - . ' OR ' . $this->db->ilike('words', '% '.$word) . ')'; + $words[] = '(' . $this->db->ilike('words', $word . $WS . '%') + . ' OR ' . $this->db->ilike('words', '%' . $AS . $word . $WS .'%') + . ' OR ' . $this->db->ilike('words', '%' . $AS . $word) . ')'; break; case 2: // prefix - $words[] = '(' . $this->db->ilike('words', $word.'%') - . ' OR ' . $this->db->ilike('words', ' '.$word.'%') . ')'; + $words[] = '(' . $this->db->ilike('words', $word . '%') + . ' OR ' . $this->db->ilike('words', $AS . $word . '%') . ')'; break; default: // partial - $words[] = $this->db->ilike('words', '%'.$word.'%'); + $words[] = $this->db->ilike('words', '%' . $word . '%'); } } $where[] = '(' . join(' AND ', $words) . ')'; @@ -687,7 +697,8 @@ class rcube_contacts extends rcube_addressbook } else { $record += $sql_arr; - $record['email'] = preg_split('/,\s*/', $record['email']); + $record['email'] = explode(self::SEPARATOR, $record['email']); + $record['email'] = array_map('trim', $record['email']); } return $record; @@ -720,12 +731,16 @@ class rcube_contacts extends rcube_addressbook $key = $col; if (!isset($save_data[$key])) $key .= ':home'; - if (isset($save_data[$key])) - $out[$col] = is_array($save_data[$key]) ? join(',', $save_data[$key]) : $save_data[$key]; + if (isset($save_data[$key])) { + if (is_array($save_data[$key])) + $out[$col] = join(self::SEPARATOR, $save_data[$key]); + else + $out[$col] = $save_data[$key]; + } } // save all e-mails in database column - $out['email'] = join(", ", $vcard->email); + $out['email'] = join(self::SEPARATOR, $vcard->email); // join words for fulltext search $out['words'] = join(" ", array_unique(explode(" ", $words))); @@ -743,7 +758,7 @@ class rcube_contacts extends rcube_addressbook function delete($ids, $force=true) { if (!is_array($ids)) - $ids = explode(',', $ids); + $ids = explode(self::SEPARATOR, $ids); $ids = $this->db->array2list($ids, 'integer'); @@ -770,7 +785,7 @@ class rcube_contacts extends rcube_addressbook function undelete($ids) { if (!is_array($ids)) - $ids = explode(',', $ids); + $ids = explode(self::SEPARATOR, $ids); $ids = $this->db->array2list($ids, 'integer'); @@ -887,7 +902,7 @@ class rcube_contacts extends rcube_addressbook function add_to_group($group_id, $ids) { if (!is_array($ids)) - $ids = explode(',', $ids); + $ids = explode(self::SEPARATOR, $ids); $added = 0; $exists = array(); @@ -932,7 +947,7 @@ class rcube_contacts extends rcube_addressbook function remove_from_group($group_id, $ids) { if (!is_array($ids)) - $ids = explode(',', $ids); + $ids = explode(self::SEPARATOR, $ids); $ids = $this->db->array2list($ids, 'integer'); -- cgit v1.2.3