Dennis1993
2013-09-02 7dff4437c06369da69841e3a4fee2b931bac0ccc
commit | author | age
4f53ab 1 <?php
TB 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/import.inc                                         |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2005-2013, 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  +-----------------------------------------------------------------------+
20 */
21
22 // clear all stored output properties (like scripts and env vars)
23 $OUTPUT->reset();
24
25 if (is_array($_FILES['_file'])) {
26     $imported = 0;
27
28     foreach ((array)$_FILES['_file']['tmp_name'] as $i => $filepath) {
29         // Process uploaded file if there is no error
30         $err = $_FILES['_file']['error'][$i];
31
32         if (!$err) {
33             // check file content type first
34             list($mtype_primary,) = explode('/', rc_mime_content_type($filepath, $_FILES['_file']['name'][$i], $_FILES['_file']['type'][$i]));
35             if (!in_array($mtype_primary, array('text','message'))) {
36                 $OUTPUT->show_message('importmessageerror', 'error');
37                 continue;
38             }
39
40             // read the first few lines to detect header-like structure
41             $fp = fopen($filepath, 'r');
42             do { $line = fgets($fp); }
43             while ($line !== false && trim($line) == '');
44
45             if (!preg_match('/^From\s+-/', $line) && !preg_match('/^[a-z-_]+:\s+.+/i', $line)) {
46                 $OUTPUT->show_message('importmessageerror', 'error');
47                 continue;
48             }
49
50             $message = $lastline = '';
51             fseek($fp, 0);
52             while (($line = fgets($fp)) !== false) {
53                 // importing mbox file, split by From - lines
54                 if (preg_match('/^From\s+-/', $line) && $lastline == '') {
55                     if (!empty($message)) {
56                         if ($RCMAIL->storage->save_message(null, rtrim($message))) {
57                             $imported++;
58                         }
59                         else {
60                             rcube::raise_error("Failed to import message to " . $RCMAIL->storage->get_folder(), false, true);
61                         }
62                         $message = '';
63                     }
64                     continue;
65                 }
66
67                 $message .= $line;
68                 $lastline = rtrim($line);
69             }
70
71             if (!empty($message) && $RCMAIL->storage->save_message(null, rtrim($message))) {
72                 $imported++;
73             }
74         }
75
76         if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
77             $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
78         }
79         else if ($err) {
80             $OUTPUT->show_message('fileuploaderror', 'error');
81         }
82     }  // end foreach
83
84     if ($imported) {
85         $OUTPUT->show_message(rcube_label(array('name' => 'importmessagesuccess', 'nr' => $imported, 'vars' => array('nr' => $imported))), 'confirmation');
86         $OUTPUT->command('command', 'list');
87     }
88     else {
89         $OUTPUT->show_message('importmessageerror', 'error');
90     }
91 }
92 else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
93     // if filesize exceeds post_max_size then $_FILES array is empty,
94     // show filesizeerror instead of fileuploaderror
95     if ($maxsize = ini_get('post_max_size'))
96         $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes($maxsize)))));
97     else
98         $msg = rcube_label('fileuploaderror');
99
100     $OUTPUT->command('display_message', $msg, 'error');
101 }
102
103 // send html page with JS calls as response
104 $OUTPUT->send('iframe');
105