4 define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' );
5 ini_set('memory_limit', -1);
7 require_once INSTALL_PATH.'program/include/iniset.php';
10 * Parse commandline arguments into a hash array
12 function get_opt($aliases=array())
15 for ($i=1; $i<count($_SERVER['argv']); $i++)
17 $arg = $_SERVER['argv'][$i];
18 if (substr($arg, 0, 2) == '--')
20 $sp = strpos($arg, '=');
21 $key = substr($arg, 2, $sp - 2);
22 $value = substr($arg, $sp+1);
24 else if ($arg{0} == '-')
26 $key = substr($arg, 1);
27 $value = $_SERVER['argv'][++$i];
32 $args[$key] = preg_replace(array('/^["\']/', '/["\']$/'), '', $value);
34 if ($alias = $aliases[$key])
35 $args[$alias] = $args[$key];
41 function print_usage()
43 print "Usage: msgexport -h imap-host -u user-name -m mailbox name\n";
44 print "--host IMAP host\n";
45 print "--user IMAP user name\n";
46 print "--mbox Folder name, set to '*' for all\n";
47 print "--file Output file\n";
52 $out = $GLOBALS['args']['file'] ? STDOUT : STDERR;
56 function progress_update($pos, $max)
58 $percent = round(100 * $pos / $max);
59 vputs(sprintf("%3d%% [%-51s] %d/%d\033[K\r", $percent, @str_repeat('=', $percent / 2) . '>', $pos, $max));
62 function export_mailbox($mbox, $filename)
66 $IMAP->set_mailbox($mbox);
68 vputs("Getting message list of {$mbox}...");
69 vputs($IMAP->messagecount()." messages\n");
73 if (!($out = fopen($filename, 'w')))
75 vputs("Cannot write to output file\n");
78 vputs("Writing to $filename\n");
83 for ($count = $IMAP->messagecount(), $i=1; $i <= $count; $i++)
85 $headers = $IMAP->get_headers($i, null, false);
86 $from = current($IMAP->decode_address_list($headers->from, 1, false));
88 fwrite($out, sprintf("From %s %s UID %d\n", $from['mailto'], $headers->date, $headers->uid));
89 fwrite($out, iil_C_FetchPartHeader($IMAP->conn, $IMAP->mailbox, $i, null));
90 fwrite($out, iil_C_HandlePartBody($IMAP->conn, $IMAP->mailbox, $i, null, 1));
91 fwrite($out, "\n\n\n");
93 progress_update($i, $count);
95 vputs("\ncomplete.\n");
103 $args = get_opt(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file')) + array('host' => 'localhost', 'mbox' => 'INBOX');
105 if ($_SERVER['argv'][1] == 'help')
110 else if (!$args['host'])
112 vputs("Missing required parameters.\n");
117 // prompt for username if not set
118 if (empty($args['user']))
120 vputs("IMAP user: ");
121 $args['user'] = trim(fgets(STDIN));
124 // prompt for password
126 $args['pass'] = trim(fgets(STDIN));
130 $a_host = parse_url($args['host']);
133 $host = $a_host['host'];
134 $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
135 $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143);
139 $host = $args['host'];
143 // instantiate IMAP class
144 $IMAP = new rcube_imap(null);
146 // try to connect to IMAP server
147 if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl))
149 vputs("IMAP login successful.\n");
152 $mailboxes = $args['mbox'] == '*' ? $IMAP->list_mailboxes(null) : array($args['mbox']);
154 foreach ($mailboxes as $mbox)
157 $filename = preg_replace('/\.[a-z0-9]{3,4}$/i', '', $args['file']) . asciiwords($mbox) . '.mbox';
158 else if ($args['mbox'] == '*')
159 $filename = asciiwords($mbox) . '.mbox';
161 if ($args['mbox'] == '*' && in_array(strtolower($mbox), array('junk','spam','trash')))
164 export_mailbox($mbox, $filename);
169 vputs("IMAP login failed.\n");