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.

149 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. print "Usage: msgexport -h imap-host -u user-name -m mailbox name\n";
  22. print "--host IMAP host\n";
  23. print "--user IMAP user name\n";
  24. print "--mbox Folder name, set to '*' for all\n";
  25. print "--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. }
  53. else {
  54. $out = STDOUT;
  55. }
  56. for ($i = 0; $i < $count; $i++) {
  57. $headers = $IMAP->get_message_headers($index[$i]);
  58. $from = current(rcube_mime::decode_address_list($headers->from, 1, false));
  59. fwrite($out, sprintf("From %s %s UID %d\n", $from['mailto'], $headers->date, $headers->uid));
  60. $IMAP->get_raw_body($headers->uid, $out);
  61. fwrite($out, "\n\n\n");
  62. progress_update($i+1, $count);
  63. }
  64. vputs("\ncomplete.\n");
  65. if ($filename) {
  66. fclose($out);
  67. }
  68. }
  69. // get arguments
  70. $opts = ['h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file'];
  71. $args = rcube_utils::get_opt($opts) + ['host' => 'localhost', 'mbox' => 'INBOX'];
  72. if (!isset($_SERVER['argv'][1]) || $_SERVER['argv'][1] == 'help') {
  73. print_usage();
  74. exit;
  75. }
  76. else if (!$args['host']) {
  77. vputs("Missing required parameters.\n");
  78. print_usage();
  79. exit;
  80. }
  81. // prompt for username if not set
  82. if (empty($args['user'])) {
  83. vputs("IMAP user: ");
  84. $args['user'] = trim(fgets(STDIN));
  85. }
  86. // prompt for password
  87. $args['pass'] = rcube_utils::prompt_silent("Password: ");
  88. // parse $host URL
  89. $a_host = parse_url($args['host']);
  90. if (!empty($a_host['host'])) {
  91. $host = $a_host['host'];
  92. $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], ['ssl','imaps','tls'])) ? TRUE : FALSE;
  93. $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143);
  94. }
  95. else {
  96. $host = $args['host'];
  97. $imap_port = 143;
  98. $imap_ssl = false;
  99. }
  100. // instantiate IMAP class
  101. $IMAP = new rcube_imap(null);
  102. // try to connect to IMAP server
  103. if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl)) {
  104. vputs("IMAP login successful.\n");
  105. $filename = null;
  106. $mailboxes = $args['mbox'] == '*' ? $IMAP->list_folders(null) : [$args['mbox']];
  107. foreach ($mailboxes as $mbox) {
  108. if (!empty($args['file'])) {
  109. $filename = preg_replace('/\.[a-z0-9]{3,4}$/i', '', $args['file']) . asciiwords($mbox) . '.mbox';
  110. }
  111. else if ($args['mbox'] == '*') {
  112. $filename = asciiwords($mbox) . '.mbox';
  113. }
  114. if ($args['mbox'] == '*' && in_array(strtolower($mbox), ['junk','spam','trash'])) {
  115. continue;
  116. }
  117. export_mailbox($mbox, $filename);
  118. }
  119. }
  120. else {
  121. vputs("IMAP login failed.\n");
  122. }