Thomas Bruederli
2016-01-16 699af1e5206ed9114322adaa3c25c1c969640a53
commit | author | age
0dbac3 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
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
699af1 24 $RCMAIL->request_security_check(rcube_utils::INPUT_GET);
TB 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
8d047c 103 // Give plugins a possibility to implement other output formats or modify the result
AM 104 $plugin = $RCMAIL->plugins->exec_hook('addressbook_export', array('result' => $result));
105 $result = $plugin['result'];
106
107 if ($plugin['abort']) {
108     exit;
109 }
110
0dbac3 111 // send downlaod headers
6b2b2e 112 header('Content-Type: text/x-vcard; charset='.RCUBE_CHARSET);
8e8f3b 113 header('Content-Disposition: attachment; filename="contacts.vcf"');
0dbac3 114
T 115 while ($result && ($row = $result->next())) {
1e09be 116     if ($CONTACTS) {
AM 117         prepare_for_export($row, $CONTACTS);
118     }
cbf891 119
79367a 120     // fix folding and end-of-line chars
TB 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;
0dbac3 124 }
T 125
126 exit;
f5d2ee 127
AM 128
129 /**
130  * Copy contact record properties into a vcard object
131  */
132 function prepare_for_export(&$record, $source = null)
133 {
99dafc 134     $groups   = $source && $source->groups && $source->export_groups ? $source->get_record_groups($record['ID']) : null;
AM 135     $fieldmap = $source ? $source->vcard_map : null;
f5d2ee 136
AM 137     if (empty($record['vcard'])) {
99dafc 138         $vcard = new rcube_vcard($record['vcard'], RCUBE_CHARSET, false, $fieldmap);
f5d2ee 139         $vcard->reset();
AM 140
141         foreach ($record as $key => $values) {
142             list($field, $section) = explode(':', $key);
9ed3c4 143             // avoid unwanted casting of DateTime objects to an array
7d8592 144             // (same as in rcube_contacts::convert_save_data())
TB 145             if (is_object($values) && is_a($values, 'DateTime')) {
9ed3c4 146                 $values = array($values);
7d8592 147             }
9ed3c4 148
AM 149             foreach ((array) $values as $value) {
7d8592 150                 if (is_array($value) || is_a($value, 'DateTime') || @strlen($value)) {
f5d2ee 151                     $vcard->set($field, $value, strtoupper($section));
AM 152                 }
153             }
154         }
155
156         // append group names
157         if ($groups) {
158             $vcard->set('groups', join(',', $groups), null);
159         }
160
99dafc 161         $record['vcard'] = $vcard->export();
f5d2ee 162     }
AM 163     // patch categories to alread existing vcard block
99dafc 164     else if ($record['vcard']) {
AM 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         }
f5d2ee 175     }
AM 176 }