3 if (php_sapi_name() != 'cli') {
4 die('Not on the "shell" (php-cli).');
7 define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' );
8 ini_set('memory_limit', -1);
10 require_once INSTALL_PATH.'program/include/iniset.php';
13 * Parse commandline arguments into a hash array
15 function get_opt($aliases=array())
18 for ($i=1; $i<count($_SERVER['argv']); $i++)
20 $arg = $_SERVER['argv'][$i];
21 if (substr($arg, 0, 2) == '--')
23 $sp = strpos($arg, '=');
24 $key = substr($arg, 2, $sp - 2);
25 $value = substr($arg, $sp+1);
27 else if ($arg{0} == '-')
29 $key = substr($arg, 1);
30 $value = $_SERVER['argv'][++$i];
35 $args[$key] = preg_replace(array('/^["\']/', '/["\']$/'), '', $value);
37 if ($alias = $aliases[$key])
38 $args[$alias] = $args[$key];
44 function print_usage()
46 print "Usage: msgexport -h imap-host -u user-name -m mailbox name\n";
47 print "--host IMAP host\n";
48 print "--user IMAP user name\n";
49 print "--mbox Folder name, set to '*' for all\n";
50 print "--file Output file\n";
55 $out = $GLOBALS['args']['file'] ? STDOUT : STDERR;
59 function progress_update($pos, $max)
61 $percent = round(100 * $pos / $max);
62 vputs(sprintf("%3d%% [%-51s] %d/%d\033[K\r", $percent, @str_repeat('=', $percent / 2) . '>', $pos, $max));
65 function export_mailbox($mbox, $filename)
69 $IMAP->set_mailbox($mbox);
71 vputs("Getting message list of {$mbox}...");
72 vputs($IMAP->messagecount()." messages\n");
76 if (!($out = fopen($filename, 'w')))
78 vputs("Cannot write to output file\n");
81 vputs("Writing to $filename\n");
86 for ($count = $IMAP->messagecount(), $i=1; $i <= $count; $i++)
88 $headers = $IMAP->get_headers($i, null, false);
89 $from = current($IMAP->decode_address_list($headers->from, 1, false));
91 fwrite($out, sprintf("From %s %s UID %d\n", $from['mailto'], $headers->date, $headers->uid));
92 fwrite($out, iil_C_FetchPartHeader($IMAP->conn, $IMAP->mailbox, $i, null));
93 fwrite($out, iil_C_HandlePartBody($IMAP->conn, $IMAP->mailbox, $i, null, 1));
94 fwrite($out, "\n\n\n");
96 progress_update($i, $count);
98 vputs("\ncomplete.\n");
106 $args = get_opt(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file')) + array('host' => 'localhost', 'mbox' => 'INBOX');
108 if ($_SERVER['argv'][1] == 'help')
113 else if (!$args['host'])
115 vputs("Missing required parameters.\n");
120 // prompt for username if not set
121 if (empty($args['user']))
123 vputs("IMAP user: ");
124 $args['user'] = trim(fgets(STDIN));
127 // prompt for password
129 $args['pass'] = trim(fgets(STDIN));
133 $a_host = parse_url($args['host']);
136 $host = $a_host['host'];
137 $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
138 $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143);
142 $host = $args['host'];
146 // instantiate IMAP class
147 $IMAP = new rcube_imap(null);
149 // try to connect to IMAP server
150 if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl))
152 vputs("IMAP login successful.\n");
155 $mailboxes = $args['mbox'] == '*' ? $IMAP->list_mailboxes(null) : array($args['mbox']);
157 foreach ($mailboxes as $mbox)
160 $filename = preg_replace('/\.[a-z0-9]{3,4}$/i', '', $args['file']) . asciiwords($mbox) . '.mbox';
161 else if ($args['mbox'] == '*')
162 $filename = asciiwords($mbox) . '.mbox';
164 if ($args['mbox'] == '*' && in_array(strtolower($mbox), array('junk','spam','trash')))
167 export_mailbox($mbox, $filename);
172 vputs("IMAP login failed.\n");