]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/export.inc
Fix symlink mess
[roundcube.git] / program / steps / addressbook / export.inc
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-2011, The Roundcube Dev Team                       |
9  | Copyright (C) 2011, Kolab Systems AG                                  |
10  | Licensed under the GNU GPL                                            |
11  |                                                                       |
12  | PURPOSE:                                                              |
13  |   Export the selected address book as vCard file                      |
14  |                                                                       |
15  +-----------------------------------------------------------------------+
16  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
17  | Author: Aleksander Machniak <machniak@kolabsys.com>                   |
18  +-----------------------------------------------------------------------+
19
20  $Id$
21
22 */
23
24 // Use search result
25 if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
26 {
27     $sort_col = $RCMAIL->config->get('addressbook_sort_col', 'name');
28     $search  = (array)$_SESSION['search'][$_REQUEST['_search']];
29     $records = array();
30
31     // Get records from all sources
32     foreach ($search as $s => $set) {
33         $source = $RCMAIL->get_address_book($s);
34
35         // reset page
36         $source->set_page(1);
37         $source->set_pagesize(99999);
38         $source->set_search_set($set);
39
40         // get records
41         $result = $source->list_records();
42
43         while ($row = $result->next()) {
44             $row['sourceid'] = $s;
45             $key = rcmail_contact_key($row, $sort_col);
46             $records[$key] = $row;
47         }
48         unset($result);
49     }
50
51     // sort the records
52     ksort($records, SORT_LOCALE_STRING);
53
54     // create resultset object
55     $count  = count($records);
56     $result = new rcube_result_set($count);
57     $result->records = array_values($records);
58 }
59 // selected directory/group
60 else {
61     $CONTACTS = rcmail_contact_source(null, true);
62
63     // get contacts for this user
64     $CONTACTS->set_page(1);
65     $CONTACTS->set_pagesize(99999);
66     $result = $CONTACTS->list_records(null, 0, true);
67 }
68
69 // send downlaod headers
70 header('Content-Type: text/x-vcard; charset='.RCMAIL_CHARSET);
71 header('Content-Disposition: attachment; filename="rcube_contacts.vcf"');
72
73 while ($result && ($row = $result->next())) {
74     // we already have a vcard record
75     if ($row['vcard'] && $row['name']) {
76         $row['vcard'] = preg_replace('/\r?\n/', rcube_vcard::$eol, $row['vcard']);
77         echo rcube_vcard::rfc2425_fold($row['vcard']) . rcube_vcard::$eol;
78     }
79     // copy values into vcard object
80     else {
81         $vcard = new rcube_vcard();
82         $vcard->extend_fieldmap($CONTACTS->vcard_map);
83         $vcard->load($row['vcard']);
84         $vcard->reset();
85
86         foreach ($row as $key => $values) {
87             list($field, $section) = explode(':', $key);
88             foreach ((array)$values as $value) {
89                 if (is_array($value) || strlen($value))
90                     $vcard->set($field, $value, strtoupper($section));
91             }
92         }
93
94         echo $vcard->export(true) . rcube_vcard::$eol;
95     }
96 }
97
98 exit;