Thomas Bruederli
2014-01-16 2baeac116abef9d5bcb748c687577d16dce868a0
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
6b2b2e 34             list($mtype_primary,) = explode('/', rcube_mime::file_content_type($filepath, $_FILES['_file']['name'][$i], $_FILES['_file']['type'][$i]));
c97625 35
4f53ab 36             if (!in_array($mtype_primary, array('text','message'))) {
TB 37                 $OUTPUT->show_message('importmessageerror', 'error');
38                 continue;
39             }
40
41             // read the first few lines to detect header-like structure
42             $fp = fopen($filepath, 'r');
c97625 43             do {
AM 44                 $line = fgets($fp);
45             }
4f53ab 46             while ($line !== false && trim($line) == '');
TB 47
48             if (!preg_match('/^From\s+-/', $line) && !preg_match('/^[a-z-_]+:\s+.+/i', $line)) {
49                 $OUTPUT->show_message('importmessageerror', 'error');
50                 continue;
51             }
52
53             $message = $lastline = '';
54             fseek($fp, 0);
55             while (($line = fgets($fp)) !== false) {
56                 // importing mbox file, split by From - lines
57                 if (preg_match('/^From\s+-/', $line) && $lastline == '') {
58                     if (!empty($message)) {
59                         if ($RCMAIL->storage->save_message(null, rtrim($message))) {
60                             $imported++;
61                         }
62                         else {
63                             rcube::raise_error("Failed to import message to " . $RCMAIL->storage->get_folder(), false, true);
64                         }
65                         $message = '';
66                     }
67                     continue;
68                 }
69
70                 $message .= $line;
71                 $lastline = rtrim($line);
72             }
73
74             if (!empty($message) && $RCMAIL->storage->save_message(null, rtrim($message))) {
75                 $imported++;
76             }
77         }
78
79         if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
6b2b2e 80             $size = $RCMAIL->show_bytes(parse_bytes(ini_get('upload_max_filesize')));
AM 81             $msg  = $RCMAIL->gettext(array('name' => 'filesizeerror', 'vars' => array('size' => $size)));
4f53ab 82         }
TB 83         else if ($err) {
84             $OUTPUT->show_message('fileuploaderror', 'error');
85         }
86     }  // end foreach
87
88     if ($imported) {
6b2b2e 89         $OUTPUT->show_message($RCMAIL->gettext(array('name' => 'importmessagesuccess', 'nr' => $imported, 'vars' => array('nr' => $imported))), 'confirmation');
4f53ab 90         $OUTPUT->command('command', 'list');
TB 91     }
92     else {
93         $OUTPUT->show_message('importmessageerror', 'error');
94     }
95 }
96 else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
97     // if filesize exceeds post_max_size then $_FILES array is empty,
98     // show filesizeerror instead of fileuploaderror
99     if ($maxsize = ini_get('post_max_size'))
6b2b2e 100         $msg = $RCMAIL->gettext(array('name' => 'filesizeerror', 'vars' => array('size' => $RCMAIL->show_bytes(parse_bytes($maxsize)))));
4f53ab 101     else
6b2b2e 102         $msg = $RCMAIL->gettext('fileuploaderror');
4f53ab 103
TB 104     $OUTPUT->command('display_message', $msg, 'error');
105 }
106
107 // send html page with JS calls as response
108 $OUTPUT->send('iframe');