From 271c5c976433c8318510ab16071dd3fa4cfe87c0 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Mon, 19 Nov 2012 07:43:43 -0500
Subject: [PATCH] Correctly handle multiple file uploads (#1488820)

---
 program/steps/mail/attachments.inc |    8 ++++++--
 program/js/app.js                  |   32 +++++++++++++++++++-------------
 2 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/program/js/app.js b/program/js/app.js
index 3d24ad9..374901a 100644
--- a/program/js/app.js
+++ b/program/js/app.js
@@ -3493,20 +3493,26 @@
     if (!form)
       return false;
 
-    // get file input field, count files on capable browser
-    var i, size = 0, field = $('input[type=file]', form).get(0),
-      files = field.files ? field.files.length : field.value ? 1 : 0;
+    // count files and size on capable browser
+    var size = 0, numfiles = 0;
+
+    $('input[type=file]', form).each(function(i, field) {
+      var files = field.files ? field.files.length : (field.value ? 1 : 0);
+
+      // check file size
+      if (field.files) {
+        for (var i=0; i < files; i++)
+          size += field.files[i].size;
+      }
+
+      numfiles += files;
+    });
 
     // create hidden iframe and post upload form
-    if (files) {
-      // check file size
-      if (field.files && this.env.max_filesize && this.env.filesizeerror) {
-        for (i=0; i<files; i++)
-          size += field.files[i].size;
-        if (size && size > this.env.max_filesize) {
-          this.display_message(this.env.filesizeerror, 'error');
-          return;
-        }
+    if (numfiles) {
+      if (this.env.max_filesize && this.env.filesizeerror && size > this.env.max_filesize) {
+        this.display_message(this.env.filesizeerror, 'error');
+        return;
       }
 
       var frame_name = this.async_upload_form(form, 'upload', function(e) {
@@ -3531,7 +3537,7 @@
       });
 
       // display upload indicator and cancel button
-      var content = '<span>' + this.get_label('uploading' + (files > 1 ? 'many' : '')) + '</span>',
+      var content = '<span>' + this.get_label('uploading' + (numfiles > 1 ? 'many' : '')) + '</span>',
         ts = frame_name.replace(/^rcmupload/, '');
 
       this.add2attachment_list(ts, { name:'', html:content, classname:'uploading', frame:frame_name, complete:false });
diff --git a/program/steps/mail/attachments.inc b/program/steps/mail/attachments.inc
index 21a9f5b..180fc0b 100644
--- a/program/steps/mail/attachments.inc
+++ b/program/steps/mail/attachments.inc
@@ -89,6 +89,8 @@
 $uploadid = get_input_value('_uploadid', RCUBE_INPUT_GET);
 
 if (is_array($_FILES['_attachments']['tmp_name'])) {
+  $multiple = count($_FILES['_attachments']['tmp_name']) > 1;
+
   foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath) {
     // Process uploaded attachment if there is no error
     $err = $_FILES['_attachments']['error'][$i];
@@ -149,8 +151,10 @@
         $msg = rcube_label('fileuploaderror');
       }
 
-      $OUTPUT->command('display_message', $msg, 'error');
-      $OUTPUT->command('remove_from_attachment_list', $uploadid);
+      if ($attachment['error'] || $err != UPLOAD_ERR_NO_FILE) {
+        $OUTPUT->command('display_message', $msg, 'error');
+        $OUTPUT->command('remove_from_attachment_list', $uploadid);
+      }
     }
   }
 }

--
Gitblit v1.9.1