alecpl
2008-05-02 c5cc386da4d2a8a3fb11254127fb36c11bdc326a
commit | author | age
0712a3 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);
0712a3 6
47124c 7 require_once INSTALL_PATH.'program/include/iniset.php';
0712a3 8
T 9 /**
10  * Parse commandline arguments into a hash array
11  */
12 function get_args($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
6d0998 42 function print_usage()
0712a3 43 {
6d0998 44     print "Usage:  msgimport -h imap-host -u user-name -f message-file\n";
b48bd0 45     print "--host   IMAP host\n";
T 46     print "--user   IMAP user name\n";
47     print "--file   Message file to upload\n";
6d0998 48 }
T 49
50
51 // get arguments
52 $args = get_args(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'f' => 'file')) + array('host' => 'localhost');
53
54 if ($_SERVER['argv'][1] == 'help')
55 {
56     print_usage();
57     exit;
58 }
59 else if (!($args['host'] && $args['file']))
60 {
61     print "Missing required parameters.\n";
62     print_usage();
0712a3 63     exit;
T 64 }
65 else if (!is_file($args['file']))
66 {
67     print "Cannot read message file\n";
6d0998 68     exit;
0712a3 69 }
T 70
6d0998 71 // prompt for username if not set
T 72 if (empty($args['user']))
73 {
74     //fwrite(STDOUT, "Please enter your name\n");
75     echo "IMAP user: ";
76     $args['user'] = trim(fgets(STDIN));
77 }
78
79 // prompt for password
80 echo "Password: ";
81 $args['pass'] = trim(fgets(STDIN));
0712a3 82
b48bd0 83 // clear password input
T 84 echo chr(8)."\rPassword: ".str_repeat("*", strlen($args['pass']))."\n";
85
0712a3 86 // parse $host URL
T 87 $a_host = parse_url($args['host']);
88 if ($a_host['host'])
89 {
90     $host = $a_host['host'];
91     $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
92     $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143);
93 }
94 else
95 {
96     $host = $args['host'];
97     $imap_port = 143;
98 }
99
100 // instantiate IMAP class
101 $IMAP = new rcube_imap(null);
102
103 // try to connect to IMAP server
104 if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl))
105 {
106     print "IMAP login successful.\n";
107     print "Uploading message...\n";
108     
109     // upload message from file
110     if  ($IMAP->save_message('INBOX', file_get_contents($args['file'])))
111         print "Message successfully added to INBOX.\n";
112     else
113         print "Adding message failed!\n";
114 }
115 else
116 {
117     print "IMAP login failed.\n";
118 }
119
120 ?>