| 1 | #!/usr/bin/php -qC |
|---|
| 2 | <?php |
|---|
| 3 | |
|---|
| 4 | $CWD = $INSTALL_PATH = preg_replace('/bin\/$/', '', getcwd() . '/'); |
|---|
| 5 | ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . $CWD.'program/'); |
|---|
| 6 | |
|---|
| 7 | require_once('include/rcube_shared.inc'); |
|---|
| 8 | require_once('include/rcube_imap.inc'); |
|---|
| 9 | require_once('include/main.inc'); |
|---|
| 10 | require_once('include/bugs.inc'); |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * Parse commandline arguments into a hash array |
|---|
| 14 | */ |
|---|
| 15 | function get_args($aliases=array()) |
|---|
| 16 | { |
|---|
| 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 | |
|---|
| 44 | |
|---|
| 45 | function print_usage() |
|---|
| 46 | { |
|---|
| 47 | print "Usage: msgimport -h imap-host -u user-name -f message-file\n"; |
|---|
| 48 | print "-host IMAP host\n"; |
|---|
| 49 | print "-user IMAP user name\n"; |
|---|
| 50 | print "-file Message file to upload\n"; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | // get arguments |
|---|
| 55 | $args = get_args(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'f' => 'file')) + array('host' => 'localhost'); |
|---|
| 56 | |
|---|
| 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(); |
|---|
| 66 | exit; |
|---|
| 67 | } |
|---|
| 68 | else if (!is_file($args['file'])) |
|---|
| 69 | { |
|---|
| 70 | print "Cannot read message file\n"; |
|---|
| 71 | exit; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | // prompt for username if not set |
|---|
| 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 |
|---|
| 83 | echo "Password: "; |
|---|
| 84 | $args['pass'] = trim(fgets(STDIN)); |
|---|
| 85 | |
|---|
| 86 | // parse $host URL |
|---|
| 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 | ?> |
|---|