You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

export.inc 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | program/steps/addressbook/export.inc |
  5. | |
  6. | This file is part of the Roundcube Webmail client |
  7. | Copyright (C) 2008-2013, The Roundcube Dev Team |
  8. | Copyright (C) 2011-2013, Kolab Systems AG |
  9. | |
  10. | Licensed under the GNU General Public License version 3 or |
  11. | any later version with exceptions for skins & plugins. |
  12. | See the README file for a full license statement. |
  13. | |
  14. | PURPOSE: |
  15. | Export the selected address book as vCard file |
  16. | |
  17. +-----------------------------------------------------------------------+
  18. | Author: Thomas Bruederli <roundcube@gmail.com> |
  19. | Author: Aleksander Machniak <machniak@kolabsys.com> |
  20. +-----------------------------------------------------------------------+
  21. */
  22. $RCMAIL->request_security_check(rcube_utils::INPUT_GET);
  23. // Use search result
  24. if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']])) {
  25. $sort_col = $RCMAIL->config->get('addressbook_sort_col', 'name');
  26. $search = (array)$_SESSION['search'][$_REQUEST['_search']];
  27. $records = array();
  28. // Get records from all sources
  29. foreach ($search as $s => $set) {
  30. $source = $RCMAIL->get_address_book($s);
  31. // reset page
  32. $source->set_page(1);
  33. $source->set_pagesize(99999);
  34. $source->set_search_set($set);
  35. // get records
  36. $result = $source->list_records();
  37. while ($record = $result->next()) {
  38. // because vcard_map is per-source we need to create vcard here
  39. prepare_for_export($record, $source);
  40. $record['sourceid'] = $s;
  41. $key = rcube_addressbook::compose_contact_key($record, $sort_col);
  42. $records[$key] = $record;
  43. }
  44. unset($result);
  45. }
  46. // sort the records
  47. ksort($records, SORT_LOCALE_STRING);
  48. // create resultset object
  49. $count = count($records);
  50. $result = new rcube_result_set($count);
  51. $result->records = array_values($records);
  52. }
  53. // selected contacts
  54. else if (!empty($_REQUEST['_cid'])) {
  55. $sort_col = $RCMAIL->config->get('addressbook_sort_col', 'name');
  56. $records = array();
  57. // Selected contact IDs (with multi-source support)
  58. $cids = rcmail_get_cids();
  59. foreach ($cids as $s => $ids) {
  60. $source = $RCMAIL->get_address_book($s);
  61. $result = $source->search('ID', $ids, 1, true, true);
  62. while ($record = $result->next()) {
  63. // because vcard_map is per-source we need to create vcard here
  64. prepare_for_export($record, $source);
  65. $record['sourceid'] = $s;
  66. $key = rcube_addressbook::compose_contact_key($record, $sort_col);
  67. $records[$key] = $record;
  68. }
  69. }
  70. ksort($records, SORT_LOCALE_STRING);
  71. // create resultset object
  72. $count = count($records);
  73. $result = new rcube_result_set($count);
  74. $result->records = array_values($records);
  75. }
  76. // selected directory/group
  77. else {
  78. $CONTACTS = rcmail_contact_source(null, true);
  79. // get contacts for this user
  80. $CONTACTS->set_page(1);
  81. $CONTACTS->set_pagesize(99999);
  82. $result = $CONTACTS->list_records(null, 0, true);
  83. }
  84. // Give plugins a possibility to implement other output formats or modify the result
  85. $plugin = $RCMAIL->plugins->exec_hook('addressbook_export', array('result' => $result));
  86. $result = $plugin['result'];
  87. if ($plugin['abort']) {
  88. exit;
  89. }
  90. // send downlaod headers
  91. header('Content-Type: text/x-vcard; charset='.RCUBE_CHARSET);
  92. header('Content-Disposition: attachment; filename="contacts.vcf"');
  93. while ($result && ($row = $result->next())) {
  94. if ($CONTACTS) {
  95. prepare_for_export($row, $CONTACTS);
  96. }
  97. // fix folding and end-of-line chars
  98. $row['vcard'] = preg_replace('/\r|\n\s+/', '', $row['vcard']);
  99. $row['vcard'] = preg_replace('/\n/', rcube_vcard::$eol, $row['vcard']);
  100. echo rcube_vcard::rfc2425_fold($row['vcard']) . rcube_vcard::$eol;
  101. }
  102. exit;
  103. /**
  104. * Copy contact record properties into a vcard object
  105. */
  106. function prepare_for_export(&$record, $source = null)
  107. {
  108. $groups = $source && $source->groups && $source->export_groups ? $source->get_record_groups($record['ID']) : null;
  109. $fieldmap = $source ? $source->vcard_map : null;
  110. if (empty($record['vcard'])) {
  111. $vcard = new rcube_vcard($record['vcard'], RCUBE_CHARSET, false, $fieldmap);
  112. $vcard->reset();
  113. foreach ($record as $key => $values) {
  114. list($field, $section) = explode(':', $key);
  115. // avoid unwanted casting of DateTime objects to an array
  116. // (same as in rcube_contacts::convert_save_data())
  117. if (is_object($values) && is_a($values, 'DateTime')) {
  118. $values = array($values);
  119. }
  120. foreach ((array) $values as $value) {
  121. if (is_array($value) || is_a($value, 'DateTime') || @strlen($value)) {
  122. $vcard->set($field, $value, strtoupper($section));
  123. }
  124. }
  125. }
  126. // append group names
  127. if ($groups) {
  128. $vcard->set('groups', join(',', $groups), null);
  129. }
  130. $record['vcard'] = $vcard->export();
  131. }
  132. // patch categories to alread existing vcard block
  133. else if ($record['vcard']) {
  134. $vcard = new rcube_vcard($record['vcard'], RCUBE_CHARSET, false, $fieldmap);
  135. // unset CATEGORIES entry, it might be not up-to-date (#1490277)
  136. $vcard->set('groups', null);
  137. $record['vcard'] = $vcard->export();
  138. if (!empty($groups)) {
  139. $vgroups = 'CATEGORIES:' . rcube_vcard::vcard_quote($groups, ',');
  140. $record['vcard'] = str_replace('END:VCARD', $vgroups . rcube_vcard::$eol . 'END:VCARD', $record['vcard']);
  141. }
  142. }
  143. }