0
|
1 <?php
|
|
2
|
|
3 /**
|
|
4 +-----------------------------------------------------------------------+
|
|
5 | program/steps/mail/import.inc |
|
|
6 | |
|
|
7 | This file is part of the Roundcube Webmail client |
|
|
8 | Copyright (C) 2005-2014, The Roundcube Dev Team |
|
|
9 | |
|
|
10 | Licensed under the GNU General Public License version 3 or |
|
|
11 | any later version with exceptions for skins & plugins. |
|
|
12 | See the README file for a full license statement. |
|
|
13 | |
|
|
14 | PURPOSE: |
|
|
15 | Save the uploaded file(s) as messages to the current IMAP folder |
|
|
16 | |
|
|
17 +-----------------------------------------------------------------------+
|
|
18 | Author: Thomas Bruederli <roundcube@gmail.com> |
|
|
19 | Author: Aleksander Machniak <alec@alec.pl> |
|
|
20 +-----------------------------------------------------------------------+
|
|
21 */
|
|
22
|
|
23 // clear all stored output properties (like scripts and env vars)
|
|
24 $OUTPUT->reset();
|
|
25
|
|
26 if (is_array($_FILES['_file'])) {
|
|
27 $imported = 0;
|
|
28 $folder = $RCMAIL->storage->get_folder();
|
|
29
|
|
30 foreach ((array)$_FILES['_file']['tmp_name'] as $i => $filepath) {
|
|
31 // Process uploaded file if there is no error
|
|
32 $err = $_FILES['_file']['error'][$i];
|
|
33
|
|
34 if (!$err) {
|
|
35 // check file content type first
|
|
36 $ctype = rcube_mime::file_content_type($filepath, $_FILES['_file']['name'][$i], $_FILES['_file']['type'][$i]);
|
|
37 list($mtype_primary, $mtype_secondary) = explode('/', $ctype);
|
|
38
|
|
39 if (in_array($ctype, array('application/zip', 'application/x-zip'))) {
|
|
40 $filepath = rcmail_zip_extract($filepath);
|
|
41 if (empty($filepath)) {
|
|
42 continue;
|
|
43 }
|
|
44 }
|
|
45 else if (!in_array($mtype_primary, array('text', 'message'))) {
|
|
46 continue;
|
|
47 }
|
|
48
|
|
49 foreach ((array) $filepath as $file) {
|
|
50 // read the first few lines to detect header-like structure
|
|
51 $fp = fopen($file, 'r');
|
|
52 do {
|
|
53 $line = fgets($fp);
|
|
54 }
|
|
55 while ($line !== false && trim($line) == '');
|
|
56
|
|
57 if (!preg_match('/^From .+/', $line) && !preg_match('/^[a-z-_]+:\s+.+/i', $line)) {
|
|
58 continue;
|
|
59 }
|
|
60
|
|
61 $message = $lastline = '';
|
|
62 fseek($fp, 0);
|
|
63
|
|
64 while (($line = fgets($fp)) !== false) {
|
|
65 // importing mbox file, split by From - lines
|
|
66 if ($lastline === '' && strncmp($line, 'From ', 5) === 0 && strlen($line) > 5) {
|
|
67 if (!empty($message)) {
|
|
68 $imported += (int) rcmail_save_message($folder, $message);
|
|
69 }
|
|
70
|
|
71 $message = $line;
|
|
72 $lastline = '';
|
|
73 continue;
|
|
74 }
|
|
75
|
|
76 $message .= $line;
|
|
77 $lastline = rtrim($line);
|
|
78 }
|
|
79
|
|
80 if (!empty($message)) {
|
|
81 $imported += (int) rcmail_save_message($folder, $message);
|
|
82 }
|
|
83
|
|
84 // remove temp files extracted from zip
|
|
85 if (is_array($filepath)) {
|
|
86 unlink($file);
|
|
87 }
|
|
88 }
|
|
89 }
|
|
90 else if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
|
|
91 $size = $RCMAIL->show_bytes(rcube_utils::max_upload_size());
|
|
92 $msg = $RCMAIL->gettext(array('name' => 'filesizeerror', 'vars' => array('size' => $size)));
|
|
93
|
|
94 $OUTPUT->command('display_message', $msg, 'error');
|
|
95 }
|
|
96 else if ($err) {
|
|
97 $OUTPUT->show_message('fileuploaderror', 'error');
|
|
98 }
|
|
99 }
|
|
100
|
|
101 if ($imported) {
|
|
102 $OUTPUT->show_message($RCMAIL->gettext(array('name' => 'importmessagesuccess', 'nr' => $imported, 'vars' => array('nr' => $imported))), 'confirmation');
|
|
103 $OUTPUT->command('command', 'list');
|
|
104 }
|
|
105 else {
|
|
106 $OUTPUT->show_message('importmessageerror', 'error');
|
|
107 }
|
|
108 }
|
|
109 else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
110 // if filesize exceeds post_max_size then $_FILES array is empty,
|
|
111 // show filesizeerror instead of fileuploaderror
|
|
112 if ($maxsize = ini_get('post_max_size'))
|
|
113 $msg = $RCMAIL->gettext(array('name' => 'filesizeerror', 'vars' => array('size' => $RCMAIL->show_bytes(parse_bytes($maxsize)))));
|
|
114 else
|
|
115 $msg = $RCMAIL->gettext('fileuploaderror');
|
|
116
|
|
117 $OUTPUT->command('display_message', $msg, 'error');
|
|
118 }
|
|
119
|
|
120 // send html page with JS calls as response
|
|
121 $OUTPUT->send('iframe');
|
|
122
|
|
123
|
|
124 function rcmail_zip_extract($path)
|
|
125 {
|
|
126 if (!class_exists('ZipArchive', false)) {
|
|
127 return;
|
|
128 }
|
|
129
|
|
130 $rcmail = rcmail::get_instance();
|
|
131 $temp_dir = $rcmail->config->get('temp_dir');
|
|
132 $zip = new ZipArchive;
|
|
133 $files = array();
|
|
134
|
|
135 if ($zip->open($path)) {
|
|
136 for ($i = 0; $i < $zip->numFiles; $i++) {
|
|
137 $entry = $zip->getNameIndex($i);
|
|
138 $tmpfname = tempnam($temp_dir, 'zipimport');
|
|
139
|
|
140 if (copy("zip://$path#$entry", $tmpfname)) {
|
|
141 $ctype = rcube_mime::file_content_type($tmpfname, $entry);
|
|
142 list($mtype_primary, $mtype_secondary) = explode('/', $ctype);
|
|
143
|
|
144 if (in_array($mtype_primary, array('text', 'message'))) {
|
|
145 $files[] = $tmpfname;
|
|
146 }
|
|
147 else {
|
|
148 unlink($tmpfname);
|
|
149 }
|
|
150 }
|
|
151 }
|
|
152
|
|
153 $zip->close();
|
|
154 }
|
|
155
|
|
156 return $files;
|
|
157 }
|
|
158
|
|
159 function rcmail_save_message($folder, &$message)
|
|
160 {
|
|
161 if (strncmp($message, 'From ', 5) === 0) {
|
|
162 // Extract the mbox from_line
|
|
163 $pos = strpos($message, "\n");
|
|
164 $from = substr($message, 0, $pos);
|
|
165 $message = substr($message, $pos + 1);
|
|
166
|
|
167 // Read the received date, support only known date formats
|
|
168
|
|
169 // RFC4155: "Sat Jan 3 01:05:34 1996"
|
|
170 $mboxdate_rx = '/^([a-z]{3} [a-z]{3} [0-9 ][0-9] [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4})/i';
|
|
171 // Roundcube/Zipdownload: "12-Dec-2016 10:56:33 +0100"
|
|
172 $imapdate_rx = '/^([0-9]{1,2}-[a-z]{3}-[0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9+-]{5})/i';
|
|
173
|
|
174 if (($pos = strpos($from, ' ', 6)) && ($dt_str = substr($from, $pos + 1))
|
|
175 && (preg_match($mboxdate_rx, $dt_str, $m) || preg_match($imapdate_rx, $dt_str, $m))
|
|
176 ) {
|
|
177 try {
|
|
178 $date = new DateTime($m[0], new DateTimeZone('UTC'));
|
|
179 }
|
|
180 catch (Exception $e) {
|
|
181 // ignore
|
|
182 }
|
|
183 }
|
|
184 }
|
|
185
|
|
186 // unquote ">From " lines in message body
|
|
187 $message = preg_replace('/\n>([>]*)From /', "\n\\1From ", $message);
|
|
188 $message = rtrim($message);
|
|
189 $rcmail = rcmail::get_instance();
|
|
190
|
|
191 if ($rcmail->storage->save_message($folder, $message, '', false, array(), $date)) {
|
|
192 return true;
|
|
193 }
|
|
194
|
|
195 rcube::raise_error("Failed to import message to $folder", true, false);
|
|
196 return false;
|
|
197 }
|