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)); |
6345f3
|
61 |
$IMAP->get_raw_body($headers->uid, $out); |
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 |
929030
|
74 |
$opts = array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file'); |
AM |
75 |
$args = rcube_utils::get_opt($opts) + array('host' => 'localhost', 'mbox' => 'INBOX'); |
6cc6e7
|
76 |
|
T |
77 |
if ($_SERVER['argv'][1] == 'help') |
|
78 |
{ |
|
79 |
print_usage(); |
|
80 |
exit; |
|
81 |
} |
|
82 |
else if (!$args['host']) |
|
83 |
{ |
|
84 |
vputs("Missing required parameters.\n"); |
|
85 |
print_usage(); |
|
86 |
exit; |
|
87 |
} |
|
88 |
|
|
89 |
// prompt for username if not set |
|
90 |
if (empty($args['user'])) |
|
91 |
{ |
|
92 |
vputs("IMAP user: "); |
|
93 |
$args['user'] = trim(fgets(STDIN)); |
|
94 |
} |
|
95 |
|
|
96 |
// prompt for password |
929030
|
97 |
$args['pass'] = rcube_utils::prompt_silent("Password: "); |
6cc6e7
|
98 |
|
T |
99 |
|
|
100 |
// parse $host URL |
|
101 |
$a_host = parse_url($args['host']); |
|
102 |
if ($a_host['host']) |
|
103 |
{ |
|
104 |
$host = $a_host['host']; |
|
105 |
$imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE; |
|
106 |
$imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143); |
|
107 |
} |
|
108 |
else |
|
109 |
{ |
|
110 |
$host = $args['host']; |
|
111 |
$imap_port = 143; |
|
112 |
} |
|
113 |
|
|
114 |
// instantiate IMAP class |
|
115 |
$IMAP = new rcube_imap(null); |
|
116 |
|
|
117 |
// try to connect to IMAP server |
|
118 |
if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl)) |
|
119 |
{ |
|
120 |
vputs("IMAP login successful.\n"); |
1c4f23
|
121 |
|
d14a57
|
122 |
$filename = null; |
c321a9
|
123 |
$mailboxes = $args['mbox'] == '*' ? $IMAP->list_folders(null) : array($args['mbox']); |
d14a57
|
124 |
|
T |
125 |
foreach ($mailboxes as $mbox) |
6cc6e7
|
126 |
{ |
d14a57
|
127 |
if ($args['file']) |
T |
128 |
$filename = preg_replace('/\.[a-z0-9]{3,4}$/i', '', $args['file']) . asciiwords($mbox) . '.mbox'; |
|
129 |
else if ($args['mbox'] == '*') |
|
130 |
$filename = asciiwords($mbox) . '.mbox'; |
1c4f23
|
131 |
|
d14a57
|
132 |
if ($args['mbox'] == '*' && in_array(strtolower($mbox), array('junk','spam','trash'))) |
T |
133 |
continue; |
|
134 |
|
|
135 |
export_mailbox($mbox, $filename); |
6cc6e7
|
136 |
} |
T |
137 |
} |
|
138 |
else |
|
139 |
{ |
|
140 |
vputs("IMAP login failed.\n"); |
|
141 |
} |
|
142 |
|
d48470
|
143 |
?> |