Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

export.inc 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // reset page and page size (#6103)
  62. $source->set_page(1);
  63. $source->set_pagesize(count($ids));
  64. $result = $source->search('ID', $ids, 1, true, true);
  65. while ($record = $result->next()) {
  66. // because vcard_map is per-source we need to create vcard here
  67. prepare_for_export($record, $source);
  68. $record['sourceid'] = $s;
  69. $key = rcube_addressbook::compose_contact_key($record, $sort_col);
  70. $records[$key] = $record;
  71. }
  72. }
  73. ksort($records, SORT_LOCALE_STRING);
  74. // create resultset object
  75. $count = count($records);
  76. $result = new rcube_result_set($count);
  77. $result->records = array_values($records);
  78. }
  79. // selected directory/group
  80. else {
  81. $CONTACTS = rcmail_contact_source(null, true);
  82. // get contacts for this user
  83. $CONTACTS->set_page(1);
  84. $CONTACTS->set_pagesize(99999);
  85. $result = $CONTACTS->list_records(null, 0, true);
  86. }
  87. // Give plugins a possibility to implement other output formats or modify the result
  88. $plugin = $RCMAIL->plugins->exec_hook('addressbook_export', array('result' => $result));
  89. $result = $plugin['result'];
  90. if ($plugin['abort']) {
  91. exit;
  92. }
  93. // send downlaod headers
  94. header('Content-Type: text/vcard; charset=' . RCUBE_CHARSET);
  95. header('Content-Disposition: attachment; filename="contacts.vcf"');
  96. while ($result && ($row = $result->next())) {
  97. if ($CONTACTS) {
  98. prepare_for_export($row, $CONTACTS);
  99. }
  100. // fix folding and end-of-line chars
  101. $row['vcard'] = preg_replace('/\r|\n\s+/', '', $row['vcard']);
  102. $row['vcard'] = preg_replace('/\n/', rcube_vcard::$eol, $row['vcard']);
  103. echo rcube_vcard::rfc2425_fold($row['vcard']) . rcube_vcard::$eol;
  104. }
  105. exit;
  106. /**
  107. * Copy contact record properties into a vcard object
  108. */
  109. function prepare_for_export(&$record, $source = null)
  110. {
  111. $groups = $source && $source->groups && $source->export_groups ? $source->get_record_groups($record['ID']) : null;
  112. $fieldmap = $source ? $source->vcard_map : null;
  113. if (empty($record['vcard'])) {
  114. $vcard = new rcube_vcard($record['vcard'], RCUBE_CHARSET, false, $fieldmap);
  115. $vcard->reset();
  116. foreach ($record as $key => $values) {
  117. list($field, $section) = explode(':', $key);
  118. // avoid unwanted casting of DateTime objects to an array
  119. // (same as in rcube_contacts::convert_save_data())
  120. if (is_object($values) && is_a($values, 'DateTime')) {
  121. $values = array($values);
  122. }
  123. foreach ((array) $values as $value) {
  124. if (is_array($value) || is_a($value, 'DateTime') || @strlen($value)) {
  125. $vcard->set($field, $value, strtoupper($section));
  126. }
  127. }
  128. }
  129. // append group names
  130. if ($groups) {
  131. $vcard->set('groups', join(',', $groups), null);
  132. }
  133. $record['vcard'] = $vcard->export();
  134. }
  135. // patch categories to alread existing vcard block
  136. else if ($record['vcard']) {
  137. $vcard = new rcube_vcard($record['vcard'], RCUBE_CHARSET, false, $fieldmap);
  138. // unset CATEGORIES entry, it might be not up-to-date (#1490277)
  139. $vcard->set('groups', null);
  140. $record['vcard'] = $vcard->export();
  141. if (!empty($groups)) {
  142. $vgroups = 'CATEGORIES:' . rcube_vcard::vcard_quote($groups, ',');
  143. $record['vcard'] = str_replace('END:VCARD', $vgroups . rcube_vcard::$eol . 'END:VCARD', $record['vcard']);
  144. }
  145. }
  146. }