From 754ff1ab04f6b1f4c5eef1cc2f0ffb9ba2de75ca Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Mon, 6 Oct 2014 01:12:06 +0000
Subject: [PATCH] Bug: attachments

Auto-detect inline attachments
Attached keepers to canned reply
---
 include/class.attachment.php | 43 ++++++++++++++++++++++--------------
 include/class.draft.php      |  4 +++-
 scp/canned.php               |  3 ++-
 3 files changed, 32 insertions(+), 18 deletions(-)

diff --git a/include/class.attachment.php b/include/class.attachment.php
index 2e3b8c55e..d562b31a0 100644
--- a/include/class.attachment.php
+++ b/include/class.attachment.php
@@ -120,26 +120,37 @@ class GenericAttachments {
         $i=array();
         if (!is_array($files)) $files=array($files);
         foreach ($files as $file) {
-            if (($fileId = is_numeric($file)
-                    ? $file : AttachmentFile::upload($file))
-                    && is_numeric($fileId)) {
-                $sql ='INSERT INTO '.ATTACHMENT_TABLE
-                    .' SET `type`='.db_input($this->getType())
-                    .',object_id='.db_input($this->getId())
-                    .',file_id='.db_input($fileId)
-                    .',inline='.db_input($inline ? 1 : 0);
-                // File may already be associated with the draft (in the
-                // event it was deleted and re-added)
-                if (db_query($sql, function($errno) { return $errno != 1062; })
-                        || db_errno() == 1062)
-                    $i[] = $fileId;
-            }
+            if (is_numeric($file))
+                $fileId = $file;
+            elseif (is_array($file) && isset($file['id']))
+                $fileId = $file['id'];
+            elseif (!($fileId = AttachmentFile::upload($file)))
+                continue;
+
+            $_inline = isset($file['inline']) ? $file['inline'] : $inline;
+
+            $sql ='INSERT INTO '.ATTACHMENT_TABLE
+                .' SET `type`='.db_input($this->getType())
+                .',object_id='.db_input($this->getId())
+                .',file_id='.db_input($fileId)
+                .',inline='.db_input($_inline ? 1 : 0);
+            // File may already be associated with the draft (in the
+            // event it was deleted and re-added)
+            if (db_query($sql, function($errno) { return $errno != 1062; })
+                    || db_errno() == 1062)
+                $i[] = $fileId;
         }
+
         return $i;
     }
 
-    function save($info, $inline=true) {
-        if (!($fileId = AttachmentFile::save($info)))
+    function save($file, $inline=true) {
+
+        if (is_numeric($file))
+            $fileId = $file;
+        elseif (is_array($file) && isset($file['id']))
+            $fileId = $file['id'];
+        elseif (!($fileId = AttachmentFile::save($file)))
             return false;
 
         $sql ='INSERT INTO '.ATTACHMENT_TABLE
diff --git a/include/class.draft.php b/include/class.draft.php
index cc6ad1fef..cdb5d43f4 100644
--- a/include/class.draft.php
+++ b/include/class.draft.php
@@ -33,7 +33,9 @@ class Draft {
         if (preg_match_all('/"cid:([\\w.-]{32})"/', $body, $matches)) {
             foreach ($matches[1] as $hash) {
                 if ($file_id = AttachmentFile::getIdByHash($hash))
-                    $attachments[] = $file_id;
+                    $attachments[] = array(
+                            'id' => $file_id,
+                            'inline' => true);
             }
         }
         return $attachments;
diff --git a/scp/canned.php b/scp/canned.php
index a600cee5a..5de2f87b5 100644
--- a/scp/canned.php
+++ b/scp/canned.php
@@ -52,9 +52,10 @@ if($_POST && $thisstaff->canManageCannedResponses()) {
                         $canned->attachments->delete($file['id']);
                     }
                 }
+
                 //Upload NEW attachments IF ANY - TODO: validate attachment types??
                 if ($keepers)
-                    $canned->attachments->upload($attachments);
+                    $canned->attachments->upload($keepers);
 
                 // Attach inline attachments from the editor
                 if (isset($_POST['draft_id'])
-- 
GitLab