From 86e97e8d5090e02a764a2f6c5dadb0345e5d9a0c Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Sat, 23 May 2015 17:36:19 +0200 Subject: [PATCH] Create groups for multi-address contacts (#1487858) --- .../squirrelmail_usercopy.php | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/plugins/squirrelmail_usercopy/squirrelmail_usercopy.php b/plugins/squirrelmail_usercopy/squirrelmail_usercopy.php index 178e54bb698..9047efdd2fb 100644 --- a/plugins/squirrelmail_usercopy/squirrelmail_usercopy.php +++ b/plugins/squirrelmail_usercopy/squirrelmail_usercopy.php @@ -1,9 +1,9 @@ get_address_book(null, true); - if ($contacts && count($this->abook)) { + $contacts = $rcmail->get_address_book(null, true); + $addresses = array(); + $groups = array(); + + if ($contacts && !empty($this->abook)) { foreach ($this->abook as $rec) { - // #1487096 handle multi-address and/or too long items - $rec['email'] = array_shift(explode(';', $rec['email'])); - if (rcube_utils::check_email(rcube_utils::idn_to_ascii($rec['email']))) { - $rec['email'] = rcube_utils::idn_to_utf8($rec['email']); - $contacts->insert($rec, true); + // #1487096: handle multi-address and/or too long items + // #1487858: convert multi-address contacts into groups + $emails = preg_split('/[;,]/', $rec['email'], -1, PREG_SPLIT_NO_EMPTY); + $group_id = null; + + // create group for addresses + if (count($emails) > 1) { + if (!($group_id = $groups[$rec['name']])) { + if ($group = $contacts->create_group($rec['name'])) { + $group_id = $group['id']; + $groups[$rec['name']] = $group_id; + } + } + } + + // create contacts + foreach ($emails as $email) { + if (!($contact_id = $addresses[$email]) && rcube_utils::check_email(rcube_utils::idn_to_ascii($email))) { + $rec['email'] = rcube_utils::idn_to_utf8($email); + if ($contact_id = $contacts->insert($rec, true)) { + $addresses[$email] = $contact_id; + } + } + + if ($group_id && $contact_id) { + $contacts->add_to_group($group_id, array($contact_id)); + } } } }