From eb3b9ba7ae78cf91429293387e4f89aac54ddfd8 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Tue, 12 Aug 2014 14:06:27 -0500
Subject: [PATCH] draft: Fix image uploading, pasting and dropping

---
 include/ajax.draft.php  |  4 ++--
 include/class.draft.php |  8 +++++---
 include/class.forms.php |  2 +-
 js/redactor-osticket.js | 20 ++++++++++++++++----
 4 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/include/ajax.draft.php b/include/ajax.draft.php
index c50817dac..ab68700d4 100644
--- a/include/ajax.draft.php
+++ b/include/ajax.draft.php
@@ -17,7 +17,7 @@ class DraftAjaxAPI extends AjaxController {
                 }
             }
         }
-        if (!isset($vars['body']) || !$vars['body'])
+        if (!isset($vars['body']))
             return JsonDataEncoder::encode(array(
                 'error' => __("Draft body not found in request"),
                 'code' => 422,
@@ -228,7 +228,7 @@ class DraftAjaxAPI extends AjaxController {
         );
 
         if (isset($_POST['name']))
-            $vars['body'] = $_POST[$_POST['name']];
+            $vars['body'] = urldecode($_POST[$_POST['name']]);
 
         return self::_createDraft($vars);
     }
diff --git a/include/class.draft.php b/include/class.draft.php
index d8bbbcd30..d71b89e57 100644
--- a/include/class.draft.php
+++ b/include/class.draft.php
@@ -48,9 +48,11 @@ class Draft extends VerySimpleModel {
             $attrs[] = sprintf('data-draft-id="%s"', $draft->getId());
             $draft_body = $draft->getBody();
         }
-        $attrs[] = sprintf('data-draft-original="%s"', Format::htmlchars($original));
+        $attrs[] = sprintf('data-draft-original="%s"',
+            Format::htmlchars(Format::viewableImages($original)));
 
-        return array($draft_body, implode(' ', $attrs));
+        return array(Format::htmlchars(Format::viewableImages($draft_body)),
+            implode(' ', $attrs));
     }
 
     function getAttachmentIds($body=false) {
@@ -105,7 +107,7 @@ class Draft extends VerySimpleModel {
 
     function isValid() {
         // Required fields
-        return $this->namespace && isset($this->body) && isset($this->staff_id);
+        return $this->namespace && isset($this->staff_id);
     }
 
     function save($refetch=false) {
diff --git a/include/class.forms.php b/include/class.forms.php
index fc1519a66..73afeaa0e 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -2032,7 +2032,7 @@ class ThreadEntryWidget extends Widget {
             class="<?php if ($cfg->isHtmlThreadEnabled()) echo 'richtext';
                 ?> draft draft-delete" <?php echo $attrs; ?>
             cols="21" rows="8" style="width:80%;"><?php echo
-            Format::htmlchars($draft ?: $this->value); ?></textarea>
+            $draft ?: Format::htmlchars($this->value); ?></textarea>
     <?php
         $config = $this->field->getConfiguration();
         if (!$config['attachments'])
diff --git a/js/redactor-osticket.js b/js/redactor-osticket.js
index 9bd0e57d7..33b27ef38 100644
--- a/js/redactor-osticket.js
+++ b/js/redactor-osticket.js
@@ -25,13 +25,18 @@ RedactorPlugins.draft = {
         this.opts.autosaveInterval = 10;
         this.opts.autosaveCallback = this.afterUpdateDraft;
         this.opts.autosaveErrorCallback = this.autosaveFailed;
+        this.opts.imageUploadErrorCallback = this.displayError;
         if (this.opts.draftId) {
             this.opts.autosave = 'ajax.php/draft/'+this.opts.draftId;
             this.opts.clipboardUploadUrl =
             this.opts.imageUpload =
                 'ajax.php/draft/'+this.opts.draftId+'/attach';
         }
-        this.opts.imageUploadErrorCallback = this.displayError;
+        else {
+            // Autosave quickly to and setup image upload in the
+            // afterUpdateDraft callback
+            this.opts.autosaveInterval = 1;
+        }
 
         this.$draft_saved = $('<span>')
             .addClass("pull-right draft-saved")
@@ -65,11 +70,18 @@ RedactorPlugins.draft = {
         if (data.draft_id) {
             this.opts.draftId = data.draft_id;
             this.opts.autosave = 'ajax.php/draft/' + data.draft_id;
+            this.opts.clipboardUploadUrl =
+            this.opts.imageUpload =
+                'ajax.php/draft/'+this.opts.draftId+'/attach';
+            if (this.opts.autosaveInterval < 10) {
+                clearInterval(this.autosaveInterval);
+                this.opts.autosaveInterval = 10;
+                this.autosave();
+            }
         }
-
         // Only show the [Draft Saved] notice if there is content in the
         // field that has been touched
-        if (this.opts.draftOriginal && this.opts.draftOriginal == this.get()) {
+        if (!this.opts.draftOriginal || this.opts.draftOriginal == this.get()) {
             // No change yet — dont't show the button
             return;
         }
@@ -108,7 +120,7 @@ RedactorPlugins.draft = {
             type: 'delete',
             async: false,
             success: function() {
-                self.draft_id = undefined;
+                self.draft_id = this.opts.draftId = undefined;
                 self.hideDraftSaved();
                 self.set(self.opts.draftOriginal || '', false, false);
                 self.opts.autosave = self.opts.autoCreateUrl;
-- 
GitLab