From e5bf9530bbcc7f5b821af6108ffefb5ab2240e09 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Tue, 25 Feb 2014 21:11:12 -0600
Subject: [PATCH] upgrader: Fix upgrade from osTicket 1.6

---
 include/class.file.php                        | 13 ++----
 .../streams/core/15b30765-dd0022fb.task.php   | 43 ++++++++++++++++++-
 2 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/include/class.file.php b/include/class.file.php
index a5c0abef1..9a200d0fc 100644
--- a/include/class.file.php
+++ b/include/class.file.php
@@ -343,6 +343,7 @@ class AttachmentFile {
             .',size='.db_input($file['size'])
             .',name='.db_input($file['name'])
             .',`key`='.db_input($file['key'])
+            .',ft='.db_input($ft ?: 'T')
             .',signature='.db_input($file['signature']);
 
         if (!(db_query($sql) && ($id = db_insert_id())))
@@ -364,15 +365,9 @@ class AttachmentFile {
             return false;
         }
 
-        # XXX: ft does not exists during the upgrade when attachments are
-        #      migrated! Neither does `bk`
-        if ($ft) {
-            $sql = 'UPDATE '.FILE_TABLE.' SET bk='
-                .db_input($bk->getBkChar())
-                .', ft='.db_input($ft)
-                .' WHERE id='.db_input($f->getId());
-            db_query($sql);
-        }
+        $sql = 'UPDATE '.FILE_TABLE.' SET bk='.db_input($bk->getBkChar())
+            .' WHERE id='.db_input($f->getId());
+        db_query($sql);
 
         return $f->getId();
     }
diff --git a/include/upgrader/streams/core/15b30765-dd0022fb.task.php b/include/upgrader/streams/core/15b30765-dd0022fb.task.php
index a4f39fb78..4d7eac01f 100644
--- a/include/upgrader/streams/core/15b30765-dd0022fb.task.php
+++ b/include/upgrader/streams/core/15b30765-dd0022fb.task.php
@@ -108,7 +108,7 @@ class AttachmentMigrater extends MigrationTask {
         }
         # TODO: Add extension-based mime-type lookup
 
-        if (!($fileId = AttachmentFile::save($info, false))) {
+        if (!($fileId = $this->saveAttachment($info))) {
             return $this->skip($info['attachId'],
                 sprintf('%s: Unable to migrate attachment', $info['path']));
         }
@@ -227,6 +227,47 @@ class AttachmentMigrater extends MigrationTask {
     function getErrors() {
         return $this->errorList;
     }
+
+    // This is the AttachmentFile::save() method from osTicket 1.7.6. It's
+    // been ported here so that further changes to the %file table and the
+    // AttachmentFile::save() method do not affect upgrades from osTicket
+    // 1.6 to osTicket 1.8 and beyond.
+    function saveAttachment($file) {
+
+        if(!$file['hash'])
+            $file['hash']=MD5(md5_file($file['path']).time());
+        $file['data'] = file_get_contents($file['path']);
+        if(!$file['size'])
+            $file['size']=strlen($file['data']);
+
+        $sql='INSERT INTO '.FILE_TABLE.' SET created=NOW() '
+            .',type='.db_input($file['type'])
+            .',size='.db_input($file['size'])
+            .',name='.db_input($file['name'])
+            .',hash='.db_input($file['hash']);
+
+        if (!(db_query($sql) && ($id=db_insert_id())))
+            return false;
+
+        $f = new CompatAttachmentFile($id);
+        $bk = new AttachmentChunkedData($f);
+        if (!$bk->write($file['data']))
+            return false;
+
+        return $id;
+    }
+}
+
+class CompatAttachmentFile {
+    var $id;
+
+    function __construct($id) {
+        $this->id = $id;
+    }
+
+    function getId() {
+        return $this->id;
+    }
 }
 
 return 'AttachmentMigrater';
-- 
GitLab