4 +-----------------------------------------------------------------------+
5 | program/steps/addressbook/export.inc |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2008-2011, The Roundcube Dev Team |
9 | Copyright (C) 2011, Kolab Systems AG |
10 | Licensed under the GNU GPL |
13 | Export the selected address book as vCard file |
15 +-----------------------------------------------------------------------+
16 | Author: Thomas Bruederli <roundcube@gmail.com> |
17 | Author: Aleksander Machniak <machniak@kolabsys.com> |
18 +-----------------------------------------------------------------------+
25 if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
27 $search = (array)$_SESSION['search'][$_REQUEST['_search']];
30 // Get records from all sources
31 foreach ($search as $s => $set) {
32 $source = $RCMAIL->get_address_book($s);
36 $source->set_pagesize(99999);
37 $source->set_search_set($set);
40 $result = $source->list_records();
42 while ($row = $result->next()) {
43 $row['sourceid'] = $s;
44 $key = $row['name'] . ':' . $row['sourceid'];
45 $records[$key] = $row;
51 ksort($records, SORT_LOCALE_STRING);
53 // create resultset object
54 $count = count($records);
55 $result = new rcube_result_set($count);
56 $result->records = array_values($records);
58 // selected directory/group
60 $CONTACTS = rcmail_contact_source(null, true);
62 // get contacts for this user
63 $CONTACTS->set_page(1);
64 $CONTACTS->set_pagesize(99999);
65 $result = $CONTACTS->list_records(null, 0, true);
68 // send downlaod headers
69 header('Content-Type: text/x-vcard; charset='.RCMAIL_CHARSET);
70 header('Content-Disposition: attachment; filename="rcube_contacts.vcf"');
72 while ($result && ($row = $result->next())) {
73 // we already have a vcard record
74 if ($row['vcard'] && $row['name']) {
75 $row['vcard'] = preg_replace('/\r?\n/', rcube_vcard::$eol, $row['vcard']);
76 echo rcube_vcard::rfc2425_fold($row['vcard']) . rcube_vcard::$eol;
78 // copy values into vcard object
80 $vcard = new rcube_vcard();
81 $vcard->extend_fieldmap($CONTACTS->vcard_map);
82 $vcard->load($row['vcard']);
85 foreach ($row as $key => $values) {
86 list($field, $section) = explode(':', $key);
87 foreach ((array)$values as $value) {
88 if (is_array($value) || strlen($value))
89 $vcard->set($field, $value, strtoupper($section));
93 echo $vcard->export(true) . rcube_vcard::$eol;