Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. foreach ((array)$_FILES['_file']['tmp_name'] as $i => $filepath) {
  26. // Process uploaded file if there is no error
  27. $err = $_FILES['_file']['error'][$i];
  28. if (!$err) {
  29. // check file content type first
  30. $ctype = rcube_mime::file_content_type($filepath, $_FILES['_file']['name'][$i], $_FILES['_file']['type'][$i]);
  31. list($mtype_primary, $mtype_secondary) = explode('/', $ctype);
  32. if (in_array($ctype, array('application/zip', 'application/x-zip'))) {
  33. $filepath = rcmail_zip_extract($filepath);
  34. if (empty($filepath)) {
  35. continue;
  36. }
  37. }
  38. else if (!in_array($mtype_primary, array('text', 'message'))) {
  39. continue;
  40. }
  41. foreach ((array) $filepath as $file) {
  42. // read the first few lines to detect header-like structure
  43. $fp = fopen($file, 'r');
  44. do {
  45. $line = fgets($fp);
  46. }
  47. while ($line !== false && trim($line) == '');
  48. if (!preg_match('/^From .+/', $line) && !preg_match('/^[a-z-_]+:\s+.+/i', $line)) {
  49. continue;
  50. }
  51. $message = $lastline = '';
  52. fseek($fp, 0);
  53. while (($line = fgets($fp)) !== false) {
  54. // importing mbox file, split by From - lines
  55. if ($lastline === '' && strncmp($line, 'From ', 5) === 0 && strlen($line) > 5) {
  56. if (!empty($message)) {
  57. // unquote ">From " lines in message body
  58. $message = preg_replace('/\n>([>]*)From /', "\n\\1From ", $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. $message .= $line;
  70. $lastline = rtrim($line);
  71. }
  72. if (!empty($message) && $RCMAIL->storage->save_message(null, rtrim($message))) {
  73. $imported++;
  74. }
  75. // remove temp files extracted from zip
  76. if (is_array($filepath)) {
  77. unlink($file);
  78. }
  79. }
  80. }
  81. if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
  82. $size = $RCMAIL->show_bytes(parse_bytes(ini_get('upload_max_filesize')));
  83. $msg = $RCMAIL->gettext(array('name' => 'filesizeerror', 'vars' => array('size' => $size)));
  84. }
  85. else if ($err) {
  86. $OUTPUT->show_message('fileuploaderror', 'error');
  87. }
  88. } // end foreach
  89. if ($imported) {
  90. $OUTPUT->show_message($RCMAIL->gettext(array('name' => 'importmessagesuccess', 'nr' => $imported, 'vars' => array('nr' => $imported))), 'confirmation');
  91. $OUTPUT->command('command', 'list');
  92. }
  93. else {
  94. $OUTPUT->show_message('importmessageerror', 'error');
  95. }
  96. }
  97. else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  98. // if filesize exceeds post_max_size then $_FILES array is empty,
  99. // show filesizeerror instead of fileuploaderror
  100. if ($maxsize = ini_get('post_max_size'))
  101. $msg = $RCMAIL->gettext(array('name' => 'filesizeerror', 'vars' => array('size' => $RCMAIL->show_bytes(parse_bytes($maxsize)))));
  102. else
  103. $msg = $RCMAIL->gettext('fileuploaderror');
  104. $OUTPUT->command('display_message', $msg, 'error');
  105. }
  106. // send html page with JS calls as response
  107. $OUTPUT->send('iframe');
  108. function rcmail_zip_extract($path)
  109. {
  110. if (!class_exists('ZipArchive', false)) {
  111. return;
  112. }
  113. $rcmail = rcmail::get_instance();
  114. $temp_dir = $rcmail->config->get('temp_dir');
  115. $zip = new ZipArchive;
  116. $files = array();
  117. if ($zip->open($path)) {
  118. for ($i = 0; $i < $zip->numFiles; $i++) {
  119. $entry = $zip->getNameIndex($i);
  120. $tmpfname = tempnam($temp_dir, 'zipimport');
  121. if (copy("zip://$path#$entry", $tmpfname)) {
  122. $ctype = rcube_mime::file_content_type($tmpfname, $entry);
  123. list($mtype_primary, $mtype_secondary) = explode('/', $ctype);
  124. if (in_array($mtype_primary, array('text', 'message'))) {
  125. $files[] = $tmpfname;
  126. }
  127. else {
  128. unlink($tmpfname);
  129. }
  130. }
  131. }
  132. $zip->close();
  133. }
  134. return $files;
  135. }