comparison program/steps/addressbook/export.inc @ 0:4681f974d28b

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:52:31 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4681f974d28b
1 <?php
2
3 /**
4 +-----------------------------------------------------------------------+
5 | program/steps/addressbook/export.inc |
6 | |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2008-2013, The Roundcube Dev Team |
9 | Copyright (C) 2011-2013, Kolab Systems AG |
10 | |
11 | Licensed under the GNU General Public License version 3 or |
12 | any later version with exceptions for skins & plugins. |
13 | See the README file for a full license statement. |
14 | |
15 | PURPOSE: |
16 | Export the selected address book as vCard file |
17 | |
18 +-----------------------------------------------------------------------+
19 | Author: Thomas Bruederli <roundcube@gmail.com> |
20 | Author: Aleksander Machniak <machniak@kolabsys.com> |
21 +-----------------------------------------------------------------------+
22 */
23
24 $RCMAIL->request_security_check(rcube_utils::INPUT_GET);
25
26 // Use search result
27 if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']])) {
28 $sort_col = $RCMAIL->config->get('addressbook_sort_col', 'name');
29 $search = (array)$_SESSION['search'][$_REQUEST['_search']];
30 $records = array();
31
32 // Get records from all sources
33 foreach ($search as $s => $set) {
34 $source = $RCMAIL->get_address_book($s);
35
36 // reset page
37 $source->set_page(1);
38 $source->set_pagesize(99999);
39 $source->set_search_set($set);
40
41 // get records
42 $result = $source->list_records();
43
44 while ($record = $result->next()) {
45 // because vcard_map is per-source we need to create vcard here
46 prepare_for_export($record, $source);
47
48 $record['sourceid'] = $s;
49 $key = rcube_addressbook::compose_contact_key($record, $sort_col);
50 $records[$key] = $record;
51 }
52
53 unset($result);
54 }
55
56 // sort the records
57 ksort($records, SORT_LOCALE_STRING);
58
59 // create resultset object
60 $count = count($records);
61 $result = new rcube_result_set($count);
62 $result->records = array_values($records);
63 }
64 // selected contacts
65 else if (!empty($_REQUEST['_cid'])) {
66 $sort_col = $RCMAIL->config->get('addressbook_sort_col', 'name');
67 $records = array();
68
69 // Selected contact IDs (with multi-source support)
70 $cids = rcmail_get_cids();
71
72 foreach ($cids as $s => $ids) {
73 $source = $RCMAIL->get_address_book($s);
74 $result = $source->search('ID', $ids, 1, true, true);
75
76 while ($record = $result->next()) {
77 // because vcard_map is per-source we need to create vcard here
78 prepare_for_export($record, $source);
79
80 $record['sourceid'] = $s;
81 $key = rcube_addressbook::compose_contact_key($record, $sort_col);
82 $records[$key] = $record;
83 }
84 }
85
86 ksort($records, SORT_LOCALE_STRING);
87
88 // create resultset object
89 $count = count($records);
90 $result = new rcube_result_set($count);
91 $result->records = array_values($records);
92 }
93 // selected directory/group
94 else {
95 $CONTACTS = rcmail_contact_source(null, true);
96
97 // get contacts for this user
98 $CONTACTS->set_page(1);
99 $CONTACTS->set_pagesize(99999);
100 $result = $CONTACTS->list_records(null, 0, true);
101 }
102
103 // Give plugins a possibility to implement other output formats or modify the result
104 $plugin = $RCMAIL->plugins->exec_hook('addressbook_export', array('result' => $result));
105 $result = $plugin['result'];
106
107 if ($plugin['abort']) {
108 exit;
109 }
110
111 // send downlaod headers
112 header('Content-Type: text/vcard; charset=' . RCUBE_CHARSET);
113 header('Content-Disposition: attachment; filename="contacts.vcf"');
114
115 while ($result && ($row = $result->next())) {
116 if ($CONTACTS) {
117 prepare_for_export($row, $CONTACTS);
118 }
119
120 // fix folding and end-of-line chars
121 $row['vcard'] = preg_replace('/\r|\n\s+/', '', $row['vcard']);
122 $row['vcard'] = preg_replace('/\n/', rcube_vcard::$eol, $row['vcard']);
123 echo rcube_vcard::rfc2425_fold($row['vcard']) . rcube_vcard::$eol;
124 }
125
126 exit;
127
128
129 /**
130 * Copy contact record properties into a vcard object
131 */
132 function prepare_for_export(&$record, $source = null)
133 {
134 $groups = $source && $source->groups && $source->export_groups ? $source->get_record_groups($record['ID']) : null;
135 $fieldmap = $source ? $source->vcard_map : null;
136
137 if (empty($record['vcard'])) {
138 $vcard = new rcube_vcard($record['vcard'], RCUBE_CHARSET, false, $fieldmap);
139 $vcard->reset();
140
141 foreach ($record as $key => $values) {
142 list($field, $section) = explode(':', $key);
143 // avoid unwanted casting of DateTime objects to an array
144 // (same as in rcube_contacts::convert_save_data())
145 if (is_object($values) && is_a($values, 'DateTime')) {
146 $values = array($values);
147 }
148
149 foreach ((array) $values as $value) {
150 if (is_array($value) || is_a($value, 'DateTime') || @strlen($value)) {
151 $vcard->set($field, $value, strtoupper($section));
152 }
153 }
154 }
155
156 // append group names
157 if ($groups) {
158 $vcard->set('groups', join(',', $groups), null);
159 }
160
161 $record['vcard'] = $vcard->export();
162 }
163 // patch categories to alread existing vcard block
164 else if ($record['vcard']) {
165 $vcard = new rcube_vcard($record['vcard'], RCUBE_CHARSET, false, $fieldmap);
166
167 // unset CATEGORIES entry, it might be not up-to-date (#1490277)
168 $vcard->set('groups', null);
169 $record['vcard'] = $vcard->export();
170
171 if (!empty($groups)) {
172 $vgroups = 'CATEGORIES:' . rcube_vcard::vcard_quote($groups, ',');
173 $record['vcard'] = str_replace('END:VCARD', $vgroups . rcube_vcard::$eol . 'END:VCARD', $record['vcard']);
174 }
175 }
176 }