alecpl
2009-05-02 50d515a7d690f6017d97017f1370705e1a214c27
commit | author | age
d48470 1 #!/usr/bin/env php
0712a3 2 <?php
d48470 3 if (php_sapi_name() != 'cli') {
T 4     die('Not on the "shell" (php-cli).');
5 }
0712a3 6
ab8b44 7 define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' );
6cc6e7 8 ini_set('memory_limit', -1);
0712a3 9
47124c 10 require_once INSTALL_PATH.'program/include/iniset.php';
0712a3 11
T 12 /**
13  * Parse commandline arguments into a hash array
14  */
687b22 15 function get_opt($aliases=array())
0712a3 16 {
T 17     $args = array();
18     for ($i=1; $i<count($_SERVER['argv']); $i++)
19     {
20         $arg = $_SERVER['argv'][$i];
21         if (substr($arg, 0, 2) == '--')
22         {
23             $sp = strpos($arg, '=');
24             $key = substr($arg, 2, $sp - 2);
25             $value = substr($arg, $sp+1);
26         }
27         else if ($arg{0} == '-')
28         {
29             $key = substr($arg, 1);
30             $value = $_SERVER['argv'][++$i];
31         }
32         else
33             continue;
34
35         $args[$key] = preg_replace(array('/^["\']/', '/["\']$/'), '', $value);
36         
37         if ($alias = $aliases[$key])
38             $args[$alias] = $args[$key];
39     }
40
41     return $args;
42 }
43
6d0998 44 function print_usage()
0712a3 45 {
687b22 46     print "Usage:  msgimport -h imap-host -u user-name -m mailbox -f message-file\n";
b48bd0 47     print "--host   IMAP host\n";
T 48     print "--user   IMAP user name\n";
687b22 49     print "--mbox   Target mailbox\n";
b48bd0 50     print "--file   Message file to upload\n";
6d0998 51 }
T 52
53
54 // get arguments
687b22 55 $args = get_opt(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file')) + array('host' => 'localhost', 'mbox' => 'INBOX');
6d0998 56
T 57 if ($_SERVER['argv'][1] == 'help')
58 {
59     print_usage();
60     exit;
61 }
62 else if (!($args['host'] && $args['file']))
63 {
64     print "Missing required parameters.\n";
65     print_usage();
0712a3 66     exit;
T 67 }
68 else if (!is_file($args['file']))
69 {
70     print "Cannot read message file\n";
6d0998 71     exit;
0712a3 72 }
T 73
6d0998 74 // prompt for username if not set
T 75 if (empty($args['user']))
76 {
77     //fwrite(STDOUT, "Please enter your name\n");
78     echo "IMAP user: ";
79     $args['user'] = trim(fgets(STDIN));
80 }
81
82 // prompt for password
9787ca 83 if (empty($args['pass']))
T 84 {
85     echo "Password: ";
86     $args['pass'] = trim(fgets(STDIN));
0712a3 87
9787ca 88     // clear password input
T 89     echo chr(8)."\rPassword: ".str_repeat("*", strlen($args['pass']))."\n";
90 }
b48bd0 91
0712a3 92 // parse $host URL
T 93 $a_host = parse_url($args['host']);
94 if ($a_host['host'])
95 {
96     $host = $a_host['host'];
97     $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
98     $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143);
99 }
100 else
101 {
102     $host = $args['host'];
103     $imap_port = 143;
104 }
105
106 // instantiate IMAP class
107 $IMAP = new rcube_imap(null);
108
109 // try to connect to IMAP server
110 if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl))
111 {
112     print "IMAP login successful.\n";
687b22 113     print "Uploading messages...\n";
0712a3 114     
687b22 115     $count = 0;
T 116     $message = $lastline = '';
117     
118     $fp = fopen($args['file'], 'r');
119     while (($line = fgets($fp)) !== false)
120     {
121         if (preg_match('/^From\s+/', $line) && $lastline == '')
122         {
123             if (!empty($message))
124             {
125                 if ($IMAP->save_message($args['mbox'], rtrim($message)))
126                     $count++;
127                 else
9787ca 128                     die("Failed to save message to {$args['mbox']}\n");
687b22 129                 $message = '';
T 130             }
131             continue;
132         }
133
134         $message .= $line;
135         $lastline = rtrim($line);
136     }
137
138     if (!empty($message) && $IMAP->save_message($args['mbox'], rtrim($message)))
139         $count++;
140
0712a3 141     // upload message from file
687b22 142     if ($count)
9787ca 143         print "$count messages successfully added to {$args['mbox']}.\n";
0712a3 144     else
687b22 145         print "Adding messages failed!\n";
0712a3 146 }
T 147 else
148 {
149     print "IMAP login failed.\n";
150 }
151
d48470 152 ?>