From 0473319004bea20f789d919b40bc2125f0b06924 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 20 Aug 2014 20:55:26 -0500
Subject: [PATCH] upload: Fix several validation processing errors

---
 include/ajax.forms.php  | 8 ++++++--
 include/class.forms.php | 8 ++++----
 js/filedrop.field.js    | 6 ++++--
 3 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/include/ajax.forms.php b/include/ajax.forms.php
index cbd0ae46e..031ae8785 100644
--- a/include/ajax.forms.php
+++ b/include/ajax.forms.php
@@ -91,13 +91,17 @@ class DynamicFormsAjaxAPI extends AjaxController {
         if (!$impl instanceof FileUploadField)
             Http::response(400, 'Upload to a non file-field');
 
-        return $impl->upload();
+        return JsonDataEncoder::encode(
+            array('id'=>$impl->upload())
+        );
     }
 
     function attach() {
         $field = new FileUploadField();
         $field->loadSystemDefaultConfig();
-        return $field->upload();
+        return JsonDataEncoder::encode(
+            array('id'=>$field->upload())
+        );
     }
 }
 ?>
diff --git a/include/class.forms.php b/include/class.forms.php
index af724db32..0e91683cf 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -1279,7 +1279,7 @@ class FileUploadField extends FormField {
 
         $files = AttachmentFile::format($_FILES['upload'],
             // For numeric fields assume configuration exists
-            is_numeric($this->get('id')));
+            !is_numeric($this->get('id')));
         if (count($files) != 1)
             Http::response(400, 'Send one file at a time');
         $file = array_shift($files);
@@ -1289,9 +1289,9 @@ class FileUploadField extends FormField {
         //       Return HTTP/413, 415, 417 or similar
 
         if (!($id = AttachmentFile::upload($file)))
-            Http::response(500, 'Unable to store file');
+            Http::response(500, 'Unable to store file: '. $file['error']);
 
-        Http::response(200, $id);
+        return $id;
     }
 
     function getFiles() {
@@ -1762,7 +1762,7 @@ class FileUploadWidget extends Widget {
           allowedfileextensions: '<?php echo $config['extensions'];
           ?>'.split(/,\s*/),
           maxfiles: <?php echo $config['max'] ?: 20; ?>,
-          maxfilesize: <?php echo $config['filesize'] ?: 1048576 / 1048576; ?>,
+          maxfilesize: <?php echo ($config['size'] ?: 1048576) / 1048576; ?>,
           name: '<?php echo $name; ?>[]',
           files: <?php echo JsonDataEncoder::encode($files); ?>
         });});
diff --git a/js/filedrop.field.js b/js/filedrop.field.js
index 989e05bdf..21419cfe4 100644
--- a/js/filedrop.field.js
+++ b/js/filedrop.field.js
@@ -55,11 +55,13 @@
       this.uploads.push(node);
       this.progressUpdated(i, file, 0);
     },
-    uploadFinished: function(i, file, response, time, xhr) {
+    uploadFinished: function(i, file, json, time, xhr) {
       var that = this;
       this.uploads.some(function(e) {
         if (e.data('file') == file) {
-          e.find('[name="'+that.options.name+'"]').val(response);
+          if (!json || !json.id)
+            return e.remove();
+          e.find('[name="'+that.options.name+'"]').val(json.id);
           e.find('.progress-bar')
             .width('100%')
             .attr({'aria-valuenow': 100})
-- 
GitLab