source: subversion/trunk/roundcubemail/program/steps/addressbook/export.inc @ 5787

Last change on this file since 5787 was 5787, checked in by thomasb, 16 months ago

Changed license to GNU GPLv3+ with exceptions for skins and plugins

File size: 3.5 KB
Line 
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 |                                                                       |
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 $Id$
24
25*/
26
27// Use search result
28if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']]))
29{
30    $search  = (array)$_SESSION['search'][$_REQUEST['_search']];
31    $records = array();
32
33    // Get records from all sources
34    foreach ($search as $s => $set) {
35        $source = $RCMAIL->get_address_book($s);
36
37        // reset page
38        $source->set_page(1);
39        $source->set_pagesize(99999);
40        $source->set_search_set($set);
41
42        // get records
43        $result = $source->list_records();
44
45        while ($row = $result->next()) {
46            $row['sourceid'] = $s;
47            $key = $row['name'] . ':' . $row['sourceid'];
48            $records[$key] = $row;
49        }
50        unset($result);
51    }
52
53    // sort the records
54    ksort($records, SORT_LOCALE_STRING);
55
56    // create resultset object
57    $count  = count($records);
58    $result = new rcube_result_set($count);
59    $result->records = array_values($records);
60}
61// selected directory/group
62else {
63    $CONTACTS = rcmail_contact_source(null, true);
64
65    // get contacts for this user
66    $CONTACTS->set_page(1);
67    $CONTACTS->set_pagesize(99999);
68    $result = $CONTACTS->list_records(null, 0, true);
69}
70
71// send downlaod headers
72header('Content-Type: text/x-vcard; charset='.RCMAIL_CHARSET);
73header('Content-Disposition: attachment; filename="rcube_contacts.vcf"');
74
75while ($result && ($row = $result->next())) {
76    // we already have a vcard record
77    if ($row['vcard'] && $row['name']) {
78        $row['vcard'] = preg_replace('/\r?\n/', rcube_vcard::$eol, $row['vcard']);
79        echo rcube_vcard::rfc2425_fold($row['vcard']) . rcube_vcard::$eol;
80    }
81    // copy values into vcard object
82    else {
83        $vcard = new rcube_vcard();
84        $vcard->extend_fieldmap($CONTACTS->vcard_map);
85        $vcard->load($row['vcard']);
86        $vcard->reset();
87
88        foreach ($row as $key => $values) {
89            list($field, $section) = explode(':', $key);
90            foreach ((array)$values as $value) {
91                if (is_array($value) || strlen($value))
92                    $vcard->set($field, $value, strtoupper($section));
93            }
94        }
95
96        echo $vcard->export(true) . rcube_vcard::$eol;
97    }
98}
99
100exit;
Note: See TracBrowser for help on using the repository browser.