RoundCube Webmail
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.

144 lines
4.3 KiB

  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. +-----------------------------------------------------------------------+
  5. | This file is part of the Roundcube Webmail client |
  6. | |
  7. | Copyright (C) The Roundcube Dev Team |
  8. | |
  9. | Licensed under the GNU General Public License version 3 or |
  10. | any later version with exceptions for skins & plugins. |
  11. | See the README file for a full license statement. |
  12. +-----------------------------------------------------------------------+
  13. | Author: Thomas Bruederli <thomas@roundcube.net> |
  14. +-----------------------------------------------------------------------+
  15. */
  16. define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/');
  17. ini_set('memory_limit', -1);
  18. require_once INSTALL_PATH . 'program/include/clisetup.php';
  19. function print_usage()
  20. {
  21. echo "Usage: msgexport.sh -h imap-host -u user-name -m mailbox name\n";
  22. echo "--host IMAP host\n";
  23. echo "--user IMAP user name\n";
  24. echo "--mbox Folder name, set to '*' for all\n";
  25. echo "--file Output file\n";
  26. }
  27. function vputs($str)
  28. {
  29. $out = !empty($GLOBALS['args']['file']) ? \STDOUT : \STDERR;
  30. fwrite($out, $str);
  31. }
  32. function progress_update($pos, $max)
  33. {
  34. $percent = round(100 * $pos / $max);
  35. vputs(sprintf("%3d%% [%-51s] %d/%d\033[K\r", $percent, @str_repeat('=', $percent / 2) . '>', $pos, $max));
  36. }
  37. function export_mailbox($mbox, $filename)
  38. {
  39. global $IMAP;
  40. $IMAP->set_folder($mbox);
  41. vputs("Getting message list of {$mbox}...");
  42. $index = $IMAP->index($mbox, null, 'ASC');
  43. $count = $index->count();
  44. $index = $index->get();
  45. vputs("{$count} messages\n");
  46. if ($filename) {
  47. if (!($out = fopen($filename, 'w'))) {
  48. vputs("Cannot write to output file\n");
  49. return;
  50. }
  51. vputs("Writing to {$filename}\n");
  52. } else {
  53. $out = \STDOUT;
  54. }
  55. for ($i = 0; $i < $count; $i++) {
  56. $headers = $IMAP->get_message_headers($index[$i]);
  57. $from = current(rcube_mime::decode_address_list($headers->from, 1, false));
  58. fwrite($out, sprintf("From %s %s UID %d\n", $from['mailto'], $headers->date, $headers->uid));
  59. $IMAP->get_raw_body($headers->uid, $out);
  60. fwrite($out, "\n\n\n");
  61. progress_update($i + 1, $count);
  62. }
  63. vputs("\ncomplete.\n");
  64. if ($filename) {
  65. fclose($out);
  66. }
  67. }
  68. // get arguments
  69. $opts = ['h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file'];
  70. $args = rcube_utils::get_opt($opts) + ['host' => 'localhost', 'mbox' => 'INBOX'];
  71. if (!isset($_SERVER['argv'][1]) || $_SERVER['argv'][1] == 'help') {
  72. print_usage();
  73. exit;
  74. } elseif (!$args['host']) {
  75. vputs("Missing required parameters.\n");
  76. print_usage();
  77. exit;
  78. }
  79. // prompt for username if not set
  80. if (empty($args['user'])) {
  81. vputs('IMAP user: ');
  82. $args['user'] = trim(fgets(\STDIN));
  83. }
  84. // prompt for password
  85. $args['pass'] = rcube_utils::prompt_silent('Password: ');
  86. // parse $host URL
  87. $a_host = parse_url($args['host']);
  88. if (!empty($a_host['host'])) {
  89. $host = $a_host['host'];
  90. $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], ['ssl', 'imaps', 'tls'])) ? true : false;
  91. $imap_port = $a_host['port'] ?? ($imap_ssl ? 993 : 143);
  92. } else {
  93. $host = $args['host'];
  94. $imap_port = 143;
  95. $imap_ssl = false;
  96. }
  97. // instantiate IMAP class
  98. $IMAP = new rcube_imap(null);
  99. // try to connect to IMAP server
  100. if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl)) {
  101. vputs("IMAP login successful.\n");
  102. $filename = null;
  103. $mailboxes = $args['mbox'] == '*' ? $IMAP->list_folders(null) : [$args['mbox']];
  104. foreach ($mailboxes as $mbox) {
  105. if (!empty($args['file'])) {
  106. $filename = preg_replace('/\.[a-z0-9]{3,4}$/i', '', $args['file']) . asciiwords($mbox) . '.mbox';
  107. } elseif ($args['mbox'] == '*') {
  108. $filename = asciiwords($mbox) . '.mbox';
  109. }
  110. if ($args['mbox'] == '*' && in_array(strtolower($mbox), ['junk', 'spam', 'trash'])) {
  111. continue;
  112. }
  113. export_mailbox($mbox, $filename);
  114. }
  115. } else {
  116. vputs("IMAP login failed.\n");
  117. }