You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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