alecpl
2011-08-03 5f5cf89c84c7c92f489f02bae41468ab2a63e4db
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;
33     
34     $IMAP->set_mailbox($mbox);
35     
36     vputs("Getting message list of {$mbox}...");
37     vputs($IMAP->messagecount()." messages\n");
38     
39     if ($filename)
40     {
41         if (!($out = fopen($filename, 'w')))
42         {
43             vputs("Cannot write to output file\n");
44             return;
45         }
46         vputs("Writing to $filename\n");
47     }
48     else
49         $out = STDOUT;
50     
51     for ($count = $IMAP->messagecount(), $i=1; $i <= $count; $i++)
52     {
53         $headers = $IMAP->get_headers($i, null, false);
54         $from = current($IMAP->decode_address_list($headers->from, 1, false));
55         
56         fwrite($out, sprintf("From %s %s UID %d\n", $from['mailto'], $headers->date, $headers->uid));
bd911b 57         fwrite($out, $IMAP->conn->fetchPartHeader($mbox, $i));
T 58         fwrite($out, $IMAP->conn->handlePartBody($mbox, $i));
d14a57 59         fwrite($out, "\n\n\n");
T 60         
61         progress_update($i, $count);
62     }
63     vputs("\ncomplete.\n");
64     
65     if ($filename)
66         fclose($out);
6cc6e7 67 }
T 68
69
70 // get arguments
71 $args = get_opt(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file')) + array('host' => 'localhost', 'mbox' => 'INBOX');
72
73 if ($_SERVER['argv'][1] == 'help')
74 {
75     print_usage();
76     exit;
77 }
78 else if (!$args['host'])
79 {
80     vputs("Missing required parameters.\n");
81     print_usage();
82     exit;
83 }
84
85 // prompt for username if not set
86 if (empty($args['user']))
87 {
88     vputs("IMAP user: ");
89     $args['user'] = trim(fgets(STDIN));
90 }
91
92 // prompt for password
bd911b 93 $args['pass'] = prompt_silent("Password: ");
6cc6e7 94
T 95
96 // parse $host URL
97 $a_host = parse_url($args['host']);
98 if ($a_host['host'])
99 {
100     $host = $a_host['host'];
101     $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
102     $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143);
103 }
104 else
105 {
106     $host = $args['host'];
107     $imap_port = 143;
108 }
109
110 // instantiate IMAP class
111 $IMAP = new rcube_imap(null);
112
113 // try to connect to IMAP server
114 if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl))
115 {
116     vputs("IMAP login successful.\n");
117     
d14a57 118     $filename = null;
T 119     $mailboxes = $args['mbox'] == '*' ? $IMAP->list_mailboxes(null) : array($args['mbox']);
120
121     foreach ($mailboxes as $mbox)
6cc6e7 122     {
d14a57 123         if ($args['file'])
T 124             $filename = preg_replace('/\.[a-z0-9]{3,4}$/i', '', $args['file']) . asciiwords($mbox) . '.mbox';
125         else if ($args['mbox'] == '*')
126             $filename = asciiwords($mbox) . '.mbox';
127             
128         if ($args['mbox'] == '*' && in_array(strtolower($mbox), array('junk','spam','trash')))
129             continue;
130
131         export_mailbox($mbox, $filename);
6cc6e7 132     }
T 133 }
134 else
135 {
136     vputs("IMAP login failed.\n");
137 }
138
d48470 139 ?>