Aleksander Machniak
2016-05-22 0344b168276f80189e2254c75a762aff5b517b6b
bin/msgexport.sh
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php
define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' );
define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' );
ini_set('memory_limit', -1);
require_once INSTALL_PATH.'program/include/clisetup.php';
@@ -30,12 +30,16 @@
function export_mailbox($mbox, $filename)
{
   global $IMAP;
   $IMAP->set_mailbox($mbox);
   $IMAP->set_folder($mbox);
    $index = $IMAP->index($mbox, null, 'ASC');
    $count = $index->count();
    $index = $index->get();
   vputs("Getting message list of {$mbox}...");
   vputs($IMAP->messagecount()." messages\n");
   vputs("$count messages\n");
   if ($filename)
   {
      if (!($out = fopen($filename, 'w')))
@@ -47,28 +51,28 @@
   }
   else
      $out = STDOUT;
   for ($count = $IMAP->messagecount(), $i=1; $i <= $count; $i++)
   for ($i = 0; $i < $count; $i++)
   {
      $headers = $IMAP->get_headers($i, null, false);
      $from = current($IMAP->decode_address_list($headers->from, 1, false));
      $headers = $IMAP->get_message_headers($index[$i]);
      $from = current(rcube_mime::decode_address_list($headers->from, 1, false));
      fwrite($out, sprintf("From %s %s UID %d\n", $from['mailto'], $headers->date, $headers->uid));
      fwrite($out, $IMAP->conn->fetchPartHeader($mbox, $i));
      fwrite($out, $IMAP->conn->handlePartBody($mbox, $i));
      $IMAP->get_raw_body($headers->uid, $out);
      fwrite($out, "\n\n\n");
      progress_update($i, $count);
      progress_update($i+1, $count);
   }
   vputs("\ncomplete.\n");
   if ($filename)
      fclose($out);
}
// get arguments
$args = get_opt(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file')) + array('host' => 'localhost', 'mbox' => 'INBOX');
$opts = array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file');
$args = rcube_utils::get_opt($opts) + array('host' => 'localhost', 'mbox' => 'INBOX');
if ($_SERVER['argv'][1] == 'help')
{
@@ -90,7 +94,7 @@
}
// prompt for password
$args['pass'] = prompt_silent("Password: ");
$args['pass'] = rcube_utils::prompt_silent("Password: ");
// parse $host URL
@@ -114,9 +118,9 @@
if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl))
{
   vputs("IMAP login successful.\n");
   $filename = null;
   $mailboxes = $args['mbox'] == '*' ? $IMAP->list_mailboxes(null) : array($args['mbox']);
   $mailboxes = $args['mbox'] == '*' ? $IMAP->list_folders(null) : array($args['mbox']);
   foreach ($mailboxes as $mbox)
   {
@@ -124,7 +128,7 @@
         $filename = preg_replace('/\.[a-z0-9]{3,4}$/i', '', $args['file']) . asciiwords($mbox) . '.mbox';
      else if ($args['mbox'] == '*')
         $filename = asciiwords($mbox) . '.mbox';
      if ($args['mbox'] == '*' && in_array(strtolower($mbox), array('junk','spam','trash')))
         continue;