commit | author | age
|
0dbac3
|
1 |
<?php |
T |
2 |
|
a95874
|
3 |
/** |
0dbac3
|
4 |
+-----------------------------------------------------------------------+ |
T |
5 |
| program/steps/addressbook/export.inc | |
|
6 |
| | |
e019f2
|
7 |
| This file is part of the Roundcube Webmail client | |
79367a
|
8 |
| Copyright (C) 2008-2013, The Roundcube Dev Team | |
f5d2ee
|
9 |
| Copyright (C) 2011-2013, Kolab Systems AG | |
7fe381
|
10 |
| | |
T |
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. | |
0dbac3
|
14 |
| | |
T |
15 |
| PURPOSE: | |
|
16 |
| Export the selected address book as vCard file | |
|
17 |
| | |
|
18 |
+-----------------------------------------------------------------------+ |
|
19 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
ecf295
|
20 |
| Author: Aleksander Machniak <machniak@kolabsys.com> | |
0dbac3
|
21 |
+-----------------------------------------------------------------------+ |
T |
22 |
*/ |
79367a
|
23 |
|
bd0551
|
24 |
$RCMAIL->request_security_check(rcube_utils::INPUT_GET); |
AM |
25 |
|
ecf295
|
26 |
// Use search result |
f5d2ee
|
27 |
if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']])) { |
0203f1
|
28 |
$sort_col = $RCMAIL->config->get('addressbook_sort_col', 'name'); |
ecf295
|
29 |
$search = (array)$_SESSION['search'][$_REQUEST['_search']]; |
A |
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 |
|
8e8f3b
|
44 |
while ($record = $result->next()) { |
AM |
45 |
// because vcard_map is per-source we need to create vcard here |
79367a
|
46 |
prepare_for_export($record, $source); |
8e8f3b
|
47 |
|
AM |
48 |
$record['sourceid'] = $s; |
13dc9f
|
49 |
$key = rcube_addressbook::compose_contact_key($record, $sort_col); |
8e8f3b
|
50 |
$records[$key] = $record; |
ecf295
|
51 |
} |
8e8f3b
|
52 |
|
ecf295
|
53 |
unset($result); |
A |
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 |
} |
9a6c38
|
64 |
// selected contacts |
TB |
65 |
else if (!empty($_REQUEST['_cid'])) { |
|
66 |
$sort_col = $RCMAIL->config->get('addressbook_sort_col', 'name'); |
8e8f3b
|
67 |
$records = array(); |
9a6c38
|
68 |
|
8e8f3b
|
69 |
// Selected contact IDs (with multi-source support) |
AM |
70 |
$cids = rcmail_get_cids(); |
9a6c38
|
71 |
|
8e8f3b
|
72 |
foreach ($cids as $s => $ids) { |
AM |
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 |
79367a
|
78 |
prepare_for_export($record, $source); |
8e8f3b
|
79 |
|
AM |
80 |
$record['sourceid'] = $s; |
13dc9f
|
81 |
$key = rcube_addressbook::compose_contact_key($record, $sort_col); |
8e8f3b
|
82 |
$records[$key] = $record; |
AM |
83 |
} |
9a6c38
|
84 |
} |
TB |
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 |
} |
ecf295
|
93 |
// selected directory/group |
A |
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 |
} |
0dbac3
|
102 |
|
T |
103 |
// send downlaod headers |
6b2b2e
|
104 |
header('Content-Type: text/x-vcard; charset='.RCUBE_CHARSET); |
8e8f3b
|
105 |
header('Content-Disposition: attachment; filename="contacts.vcf"'); |
0dbac3
|
106 |
|
T |
107 |
while ($result && ($row = $result->next())) { |
1e09be
|
108 |
if ($CONTACTS) { |
AM |
109 |
prepare_for_export($row, $CONTACTS); |
|
110 |
} |
cbf891
|
111 |
|
79367a
|
112 |
// fix folding and end-of-line chars |
TB |
113 |
$row['vcard'] = preg_replace('/\r|\n\s+/', '', $row['vcard']); |
|
114 |
$row['vcard'] = preg_replace('/\n/', rcube_vcard::$eol, $row['vcard']); |
|
115 |
echo rcube_vcard::rfc2425_fold($row['vcard']) . rcube_vcard::$eol; |
0dbac3
|
116 |
} |
T |
117 |
|
|
118 |
exit; |
f5d2ee
|
119 |
|
AM |
120 |
|
|
121 |
/** |
|
122 |
* Copy contact record properties into a vcard object |
|
123 |
*/ |
|
124 |
function prepare_for_export(&$record, $source = null) |
|
125 |
{ |
bd8252
|
126 |
$groups = $source && $source->groups && $source->export_groups ? $source->get_record_groups($record['ID']) : null; |
AM |
127 |
$fieldmap = $source ? $source->vcard_map : null; |
f5d2ee
|
128 |
|
AM |
129 |
if (empty($record['vcard'])) { |
bd8252
|
130 |
$vcard = new rcube_vcard($record['vcard'], RCUBE_CHARSET, false, $fieldmap); |
f5d2ee
|
131 |
$vcard->reset(); |
AM |
132 |
|
|
133 |
foreach ($record as $key => $values) { |
|
134 |
list($field, $section) = explode(':', $key); |
e25b0d
|
135 |
// avoid unwanted casting of DateTime objects to an array |
7d8592
|
136 |
// (same as in rcube_contacts::convert_save_data()) |
TB |
137 |
if (is_object($values) && is_a($values, 'DateTime')) { |
e25b0d
|
138 |
$values = array($values); |
7d8592
|
139 |
} |
e25b0d
|
140 |
|
AM |
141 |
foreach ((array) $values as $value) { |
7d8592
|
142 |
if (is_array($value) || is_a($value, 'DateTime') || @strlen($value)) { |
f5d2ee
|
143 |
$vcard->set($field, $value, strtoupper($section)); |
AM |
144 |
} |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
// append group names |
|
149 |
if ($groups) { |
|
150 |
$vcard->set('groups', join(',', $groups), null); |
|
151 |
} |
|
152 |
|
bd8252
|
153 |
$record['vcard'] = $vcard->export(); |
f5d2ee
|
154 |
} |
AM |
155 |
// patch categories to alread existing vcard block |
bd8252
|
156 |
else if ($record['vcard']) { |
AM |
157 |
$vcard = new rcube_vcard($record['vcard'], RCUBE_CHARSET, false, $fieldmap); |
|
158 |
|
|
159 |
// unset CATEGORIES entry, it might be not up-to-date (#1490277) |
|
160 |
$vcard->set('groups', null); |
|
161 |
$record['vcard'] = $vcard->export(); |
|
162 |
|
|
163 |
if (!empty($groups)) { |
|
164 |
$vgroups = 'CATEGORIES:' . rcube_vcard::vcard_quote($groups, ','); |
|
165 |
$record['vcard'] = str_replace('END:VCARD', $vgroups . rcube_vcard::$eol . 'END:VCARD', $record['vcard']); |
|
166 |
} |
f5d2ee
|
167 |
} |
AM |
168 |
} |