Aleksander Machniak
2012-09-07 00f34700632f7ff78df0c532d2605397820d0c79
commit | author | age
d48470 1 #!/usr/bin/env php
6cc6e7 2 <?php
T 3
ab8b44 4 define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' );
6cc6e7 5 ini_set('memory_limit', -1);
T 6
bd911b 7 require_once INSTALL_PATH.'program/include/clisetup.php';
6cc6e7 8
T 9 function print_usage()
10 {
11     print "Usage:  msgexport -h imap-host -u user-name -m mailbox name\n";
12     print "--host   IMAP host\n";
13     print "--user   IMAP user name\n";
d14a57 14     print "--mbox   Folder name, set to '*' for all\n";
T 15     print "--file   Output file\n";
6cc6e7 16 }
T 17
18 function vputs($str)
19 {
20     $out = $GLOBALS['args']['file'] ? STDOUT : STDERR;
21     fwrite($out, $str);
22 }
23
24 function progress_update($pos, $max)
25 {
26     $percent = round(100 * $pos / $max);
27     vputs(sprintf("%3d%% [%-51s] %d/%d\033[K\r", $percent, @str_repeat('=', $percent / 2) . '>', $pos, $max));
d14a57 28 }
T 29
30 function export_mailbox($mbox, $filename)
31 {
32     global $IMAP;
a90ad2 33
c321a9 34     $IMAP->set_folder($mbox);
T 35
36     $index = $IMAP->index($mbox, null, 'ASC');
0c2596 37     $count = $index->count();
c321a9 38     $index = $index->get();
a90ad2 39
d14a57 40     vputs("Getting message list of {$mbox}...");
c321a9 41     vputs("$count messages\n");
a90ad2 42
d14a57 43     if ($filename)
T 44     {
45         if (!($out = fopen($filename, 'w')))
46         {
47             vputs("Cannot write to output file\n");
48             return;
49         }
50         vputs("Writing to $filename\n");
51     }
52     else
53         $out = STDOUT;
a90ad2 54
c321a9 55     for ($i = 0; $i < $count; $i++)
d14a57 56     {
c321a9 57         $headers = $IMAP->get_message_headers($index[$i]);
1c4f23 58         $from = current(rcube_mime::decode_address_list($headers->from, 1, false));
A 59
d14a57 60         fwrite($out, sprintf("From %s %s UID %d\n", $from['mailto'], $headers->date, $headers->uid));
c321a9 61         fwrite($out, $IMAP->print_raw_body($headers->uid));
d14a57 62         fwrite($out, "\n\n\n");
1c4f23 63
c321a9 64         progress_update($i+1, $count);
d14a57 65     }
T 66     vputs("\ncomplete.\n");
1c4f23 67
d14a57 68     if ($filename)
T 69         fclose($out);
6cc6e7 70 }
T 71
72
73 // get arguments
74 $args = get_opt(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file')) + array('host' => 'localhost', 'mbox' => 'INBOX');
75
76 if ($_SERVER['argv'][1] == 'help')
77 {
78     print_usage();
79     exit;
80 }
81 else if (!$args['host'])
82 {
83     vputs("Missing required parameters.\n");
84     print_usage();
85     exit;
86 }
87
88 // prompt for username if not set
89 if (empty($args['user']))
90 {
91     vputs("IMAP user: ");
92     $args['user'] = trim(fgets(STDIN));
93 }
94
95 // prompt for password
bd911b 96 $args['pass'] = prompt_silent("Password: ");
6cc6e7 97
T 98
99 // parse $host URL
100 $a_host = parse_url($args['host']);
101 if ($a_host['host'])
102 {
103     $host = $a_host['host'];
104     $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
105     $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143);
106 }
107 else
108 {
109     $host = $args['host'];
110     $imap_port = 143;
111 }
112
113 // instantiate IMAP class
114 $IMAP = new rcube_imap(null);
115
116 // try to connect to IMAP server
117 if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl))
118 {
119     vputs("IMAP login successful.\n");
1c4f23 120
d14a57 121     $filename = null;
c321a9 122     $mailboxes = $args['mbox'] == '*' ? $IMAP->list_folders(null) : array($args['mbox']);
d14a57 123
T 124     foreach ($mailboxes as $mbox)
6cc6e7 125     {
d14a57 126         if ($args['file'])
T 127             $filename = preg_replace('/\.[a-z0-9]{3,4}$/i', '', $args['file']) . asciiwords($mbox) . '.mbox';
128         else if ($args['mbox'] == '*')
129             $filename = asciiwords($mbox) . '.mbox';
1c4f23 130
d14a57 131         if ($args['mbox'] == '*' && in_array(strtolower($mbox), array('junk','spam','trash')))
T 132             continue;
133
134         export_mailbox($mbox, $filename);
6cc6e7 135     }
T 136 }
137 else
138 {
139     vputs("IMAP login failed.\n");
140 }
141
d48470 142 ?>