commit | author | age
|
6cc6e7
|
1 |
#!/usr/bin/php -qC |
T |
2 |
<?php |
|
3 |
|
47124c
|
4 |
define('INSTALL_PATH', preg_replace('/bin\/$/', '', getcwd()) . '/'); |
6cc6e7
|
5 |
ini_set('memory_limit', -1); |
T |
6 |
|
47124c
|
7 |
require_once INSTALL_PATH.'program/include/iniset.php'; |
6cc6e7
|
8 |
|
T |
9 |
/** |
|
10 |
* Parse commandline arguments into a hash array |
|
11 |
*/ |
|
12 |
function get_opt($aliases=array()) |
|
13 |
{ |
|
14 |
$args = array(); |
|
15 |
for ($i=1; $i<count($_SERVER['argv']); $i++) |
|
16 |
{ |
|
17 |
$arg = $_SERVER['argv'][$i]; |
|
18 |
if (substr($arg, 0, 2) == '--') |
|
19 |
{ |
|
20 |
$sp = strpos($arg, '='); |
|
21 |
$key = substr($arg, 2, $sp - 2); |
|
22 |
$value = substr($arg, $sp+1); |
|
23 |
} |
|
24 |
else if ($arg{0} == '-') |
|
25 |
{ |
|
26 |
$key = substr($arg, 1); |
|
27 |
$value = $_SERVER['argv'][++$i]; |
|
28 |
} |
|
29 |
else |
|
30 |
continue; |
|
31 |
|
|
32 |
$args[$key] = preg_replace(array('/^["\']/', '/["\']$/'), '', $value); |
|
33 |
|
|
34 |
if ($alias = $aliases[$key]) |
|
35 |
$args[$alias] = $args[$key]; |
|
36 |
} |
|
37 |
|
|
38 |
return $args; |
|
39 |
} |
|
40 |
|
|
41 |
function print_usage() |
|
42 |
{ |
|
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 Mailbox/folder name\n"; |
|
47 |
print "--file Mailbox/folder name\n"; |
|
48 |
} |
|
49 |
|
|
50 |
function vputs($str) |
|
51 |
{ |
|
52 |
$out = $GLOBALS['args']['file'] ? STDOUT : STDERR; |
|
53 |
fwrite($out, $str); |
|
54 |
} |
|
55 |
|
|
56 |
function progress_update($pos, $max) |
|
57 |
{ |
|
58 |
$percent = round(100 * $pos / $max); |
|
59 |
vputs(sprintf("%3d%% [%-51s] %d/%d\033[K\r", $percent, @str_repeat('=', $percent / 2) . '>', $pos, $max)); |
|
60 |
} |
|
61 |
|
|
62 |
|
|
63 |
// get arguments |
|
64 |
$args = get_opt(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file')) + array('host' => 'localhost', 'mbox' => 'INBOX'); |
|
65 |
|
|
66 |
if ($_SERVER['argv'][1] == 'help') |
|
67 |
{ |
|
68 |
print_usage(); |
|
69 |
exit; |
|
70 |
} |
|
71 |
else if (!$args['host']) |
|
72 |
{ |
|
73 |
vputs("Missing required parameters.\n"); |
|
74 |
print_usage(); |
|
75 |
exit; |
|
76 |
} |
|
77 |
|
|
78 |
// prompt for username if not set |
|
79 |
if (empty($args['user'])) |
|
80 |
{ |
|
81 |
vputs("IMAP user: "); |
|
82 |
$args['user'] = trim(fgets(STDIN)); |
|
83 |
} |
|
84 |
|
|
85 |
// prompt for password |
|
86 |
vputs("Password: "); |
|
87 |
$args['pass'] = trim(fgets(STDIN)); |
|
88 |
|
|
89 |
|
|
90 |
// parse $host URL |
|
91 |
$a_host = parse_url($args['host']); |
|
92 |
if ($a_host['host']) |
|
93 |
{ |
|
94 |
$host = $a_host['host']; |
|
95 |
$imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE; |
|
96 |
$imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143); |
|
97 |
} |
|
98 |
else |
|
99 |
{ |
|
100 |
$host = $args['host']; |
|
101 |
$imap_port = 143; |
|
102 |
} |
|
103 |
|
|
104 |
// instantiate IMAP class |
|
105 |
$IMAP = new rcube_imap(null); |
|
106 |
|
|
107 |
// try to connect to IMAP server |
|
108 |
if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl)) |
|
109 |
{ |
|
110 |
vputs("IMAP login successful.\n"); |
|
111 |
|
|
112 |
$IMAP->set_mailbox($args['mbox']); |
|
113 |
|
|
114 |
vputs("Getting message list of {$args['mbox']}..."); |
|
115 |
vputs($IMAP->messagecount()." messages\n"); |
|
116 |
|
|
117 |
if ($args['file']) |
|
118 |
{ |
|
119 |
if (!($out = fopen($args['file'], 'w'))) |
|
120 |
{ |
|
121 |
vputs("Cannot write to output file\n"); |
|
122 |
exit; |
|
123 |
} |
|
124 |
} |
|
125 |
else |
|
126 |
$out = STDOUT; |
|
127 |
|
|
128 |
for ($count = $IMAP->messagecount(), $i=1; $i <= $count; $i++) |
|
129 |
{ |
|
130 |
$headers = $IMAP->get_headers($i, null, false); |
|
131 |
$from = current($IMAP->decode_address_list($headers->from, 1, false)); |
|
132 |
|
|
133 |
fwrite($out, sprintf("From %s %s UID %d\n", $from['mailto'], $headers->date, $headers->uid)); |
|
134 |
fwrite($out, iil_C_FetchPartHeader($IMAP->conn, $IMAP->mailbox, $i, null)); |
|
135 |
fwrite($out, iil_C_HandlePartBody($IMAP->conn, $IMAP->mailbox, $i, null, 1)); |
|
136 |
fwrite($out, "\n\n\n"); |
|
137 |
|
|
138 |
progress_update($i, $count); |
|
139 |
} |
|
140 |
vputs("\ncomplete.\n"); |
|
141 |
} |
|
142 |
else |
|
143 |
{ |
|
144 |
vputs("IMAP login failed.\n"); |
|
145 |
} |
|
146 |
|
|
147 |
?> |