Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

upload.inc 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | program/steps/settings/upload.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. | Handles image uploads |
  15. | |
  16. +-----------------------------------------------------------------------+
  17. | Author: Aleksander Machniak <alec@alec.pl> |
  18. +-----------------------------------------------------------------------+
  19. */
  20. // Upload progress update
  21. if (!empty($_GET['_progress'])) {
  22. $RCMAIL->upload_progress();
  23. }
  24. $from = rcube_utils::get_input_value('_from', rcube_utils::INPUT_GET);
  25. $type = preg_replace('/(add|edit)-/', '', $from);
  26. // Plugins in Settings may use this file for some uploads (#5694)
  27. // Make sure it does not contain a dot, which is a special character
  28. // when using rcube_session::append() below
  29. $type = str_replace('.', '-', $type);
  30. if ($RCMAIL->action == 'upload-display') {
  31. $id = 'undefined';
  32. if (preg_match('/^rcmfile(\w+)$/', $_GET['_file'], $regs)) {
  33. $id = $regs[1];
  34. }
  35. $RCMAIL->display_uploaded_file($_SESSION[$type]['files'][$id]);
  36. exit;
  37. }
  38. // Supported image format types
  39. $IMAGE_TYPES = explode(',', 'jpeg,jpg,jp2,tiff,tif,bmp,eps,gif,png,png8,png24,png32,svg,ico');
  40. // clear all stored output properties (like scripts and env vars)
  41. $OUTPUT->reset();
  42. $max_size = $RCMAIL->config->get($type . '_image_size', 64) * 1024;
  43. $post_size = $RCMAIL->show_bytes(rcube_utils::max_upload_size());
  44. $uploadid = rcube_utils::get_input_value('_uploadid', rcube_utils::INPUT_GET);
  45. if (is_array($_FILES['_file']['tmp_name'])) {
  46. $multiple = count($_FILES['_file']['tmp_name']) > 1;
  47. foreach ($_FILES['_file']['tmp_name'] as $i => $filepath) {
  48. // Process uploaded attachment if there is no error
  49. $err = $_FILES['_file']['error'][$i];
  50. if (!$err) {
  51. if ($max_size < $_FILES['_file']['size'][$i]) {
  52. $err = 'size_error';
  53. }
  54. // check image file type
  55. else {
  56. $image = new rcube_image($filepath);
  57. $imageprop = $image->props();
  58. if (!in_array(strtolower($imageprop['type']), $IMAGE_TYPES)) {
  59. $err = 'type_error';
  60. }
  61. }
  62. }
  63. // save uploaded image in storage backend
  64. if (!$err) {
  65. $attachment = $RCMAIL->plugins->exec_hook('attachment_upload', array(
  66. 'path' => $filepath,
  67. 'size' => $_FILES['_file']['size'][$i],
  68. 'name' => $_FILES['_file']['name'][$i],
  69. 'mimetype' => 'image/' . $imageprop['type'],
  70. 'group' => $type,
  71. ));
  72. }
  73. if (!$err && $attachment['status'] && !$attachment['abort']) {
  74. $id = $attachment['id'];
  75. // store new file in session
  76. unset($attachment['status'], $attachment['abort']);
  77. $RCMAIL->session->append($type . '.files', $id, $attachment);
  78. $content = rcube::Q($attachment['name']);
  79. $OUTPUT->command('add2attachment_list', "rcmfile$id", array(
  80. 'html' => $content,
  81. 'name' => $attachment['name'],
  82. 'mimetype' => $attachment['mimetype'],
  83. 'classname' => rcube_utils::file2class($attachment['mimetype'], $attachment['name']),
  84. 'complete' => true
  85. ),
  86. $uploadid
  87. );
  88. }
  89. else {
  90. if ($err == 'type_error') {
  91. $msg = $RCMAIL->gettext('invalidimageformat');
  92. }
  93. else if ($err == 'size_error') {
  94. $msg = $RCMAIL->gettext(array('name' => 'filesizeerror', 'vars' => array('size' => $max_size)));
  95. }
  96. else if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) {
  97. $msg = $RCMAIL->gettext(array('name' => 'filesizeerror', 'vars' => array('size' => $post_size)));
  98. }
  99. else if ($attachment['error']) {
  100. $msg = $attachment['error'];
  101. }
  102. else {
  103. $msg = $RCMAIL->gettext('fileuploaderror');
  104. }
  105. $OUTPUT->command('display_message', $msg, 'error');
  106. }
  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(
  114. 'name' => 'filesizeerror',
  115. 'vars' => array('size' => $RCMAIL->show_bytes(parse_bytes($maxsize)))
  116. ));
  117. }
  118. else {
  119. $msg = $RCMAIL->gettext('fileuploaderror');
  120. }
  121. $OUTPUT->command('display_message', $msg, 'error');
  122. $OUTPUT->command('remove_from_attachment_list', $uploadid);
  123. }
  124. $OUTPUT->send('iframe');