alecpl
2008-09-30 ef925ffe08cf221f1415fb8405ad4fb229172139
commit | author | age
d14a57 1 #!/usr/bin/php
6cc6e7 2 <?php
T 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";
d14a57 46     print "--mbox   Folder name, set to '*' for all\n";
T 47     print "--file   Output file\n";
6cc6e7 48 }
T 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));
d14a57 60 }
T 61
62 function export_mailbox($mbox, $filename)
63 {
64     global $IMAP;
65     
66     $IMAP->set_mailbox($mbox);
67     
68     vputs("Getting message list of {$mbox}...");
69     vputs($IMAP->messagecount()." messages\n");
70     
71     if ($filename)
72     {
73         if (!($out = fopen($filename, 'w')))
74         {
75             vputs("Cannot write to output file\n");
76             return;
77         }
78         vputs("Writing to $filename\n");
79     }
80     else
81         $out = STDOUT;
82     
83     for ($count = $IMAP->messagecount(), $i=1; $i <= $count; $i++)
84     {
85         $headers = $IMAP->get_headers($i, null, false);
86         $from = current($IMAP->decode_address_list($headers->from, 1, false));
87         
88         fwrite($out, sprintf("From %s %s UID %d\n", $from['mailto'], $headers->date, $headers->uid));
89         fwrite($out, iil_C_FetchPartHeader($IMAP->conn, $IMAP->mailbox, $i, null));
90         fwrite($out, iil_C_HandlePartBody($IMAP->conn, $IMAP->mailbox, $i, null, 1));
91         fwrite($out, "\n\n\n");
92         
93         progress_update($i, $count);
94     }
95     vputs("\ncomplete.\n");
96     
97     if ($filename)
98         fclose($out);
6cc6e7 99 }
T 100
101
102 // get arguments
103 $args = get_opt(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file')) + array('host' => 'localhost', 'mbox' => 'INBOX');
104
105 if ($_SERVER['argv'][1] == 'help')
106 {
107     print_usage();
108     exit;
109 }
110 else if (!$args['host'])
111 {
112     vputs("Missing required parameters.\n");
113     print_usage();
114     exit;
115 }
116
117 // prompt for username if not set
118 if (empty($args['user']))
119 {
120     vputs("IMAP user: ");
121     $args['user'] = trim(fgets(STDIN));
122 }
123
124 // prompt for password
125 vputs("Password: ");
126 $args['pass'] = trim(fgets(STDIN));
127
128
129 // parse $host URL
130 $a_host = parse_url($args['host']);
131 if ($a_host['host'])
132 {
133     $host = $a_host['host'];
134     $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
135     $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143);
136 }
137 else
138 {
139     $host = $args['host'];
140     $imap_port = 143;
141 }
142
143 // instantiate IMAP class
144 $IMAP = new rcube_imap(null);
145
146 // try to connect to IMAP server
147 if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl))
148 {
149     vputs("IMAP login successful.\n");
150     
d14a57 151     $filename = null;
T 152     $mailboxes = $args['mbox'] == '*' ? $IMAP->list_mailboxes(null) : array($args['mbox']);
153
154     foreach ($mailboxes as $mbox)
6cc6e7 155     {
d14a57 156         if ($args['file'])
T 157             $filename = preg_replace('/\.[a-z0-9]{3,4}$/i', '', $args['file']) . asciiwords($mbox) . '.mbox';
158         else if ($args['mbox'] == '*')
159             $filename = asciiwords($mbox) . '.mbox';
160             
161         if ($args['mbox'] == '*' && in_array(strtolower($mbox), array('junk','spam','trash')))
162             continue;
163
164         export_mailbox($mbox, $filename);
6cc6e7 165     }
T 166 }
167 else
168 {
169     vputs("IMAP login failed.\n");
170 }
171
172 ?>