From 08c0ac712e36f65edd875e8764598802daf6da7a Mon Sep 17 00:00:00 2001
From: aydreeihn <adriane@enhancesoft.com>
Date: Fri, 28 Sep 2018 14:10:07 -0500
Subject: [PATCH] Attachment Names Issue

This commit ensures that we will always get the correct attachment name regardless of if the file content is the same. Additionally, it ensures that the file_ids for attachments are compared in the correct order (elseifs)
---
 file.php                 | 8 +++++---
 include/class.file.php   | 4 ++--
 include/class.forms.php  | 4 +---
 include/class.thread.php | 6 +++---
 4 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/file.php b/file.php
index 994b77a0c..4a2ce7962 100644
--- a/file.php
+++ b/file.php
@@ -28,10 +28,11 @@ if (!$_GET['key']
 
 // Get the object type the file is attached to
 $type = '';
+$attachment = null;
 if ($_GET['id']
-        && ($a=$file->attachments->findFirst(array(
+        && ($attachment=$file->attachments->findFirst(array(
                     'id' => $_GET['id']))))
-    $type = $a->type;
+    $type = $attachment->type;
 
 // Enforce security settings if enabled.
 if ($cfg->isAuthRequiredForFiles()
@@ -62,7 +63,8 @@ if ($file->verifySignature($_GET['signature'], $_GET['expires'])) {
             return $file->display($s);
 
         // Download the file..
-        $file->download(@$_GET['disposition'] ?: false, $_GET['expires']);
+        $filename = $attachment ? $attachment->name : $file->getName();
+        $file->download(@$_GET['disposition'] ?: false, $_GET['expires'], $filename);
     }
     catch (Exception $ex) {
         Http::response(500, 'Unable to find that file: '.$ex->getMessage());
diff --git a/include/class.file.php b/include/class.file.php
index d88084086..4d67b93ae 100644
--- a/include/class.file.php
+++ b/include/class.file.php
@@ -248,7 +248,7 @@ class AttachmentFile extends VerySimpleModel {
         return hash_hmac('sha1', implode("\n", $pieces), SECRET_SALT);
     }
 
-    function download($disposition=false, $expires=false) {
+    function download($disposition=false, $expires=false, $name=false) {
         $disposition = $disposition ?: 'inline';
         $bk = $this->open();
         if ($bk->sendRedirectUrl($disposition))
@@ -258,7 +258,7 @@ class AttachmentFile extends VerySimpleModel {
         $type = $this->getType() ?: 'application/octet-stream';
         if (isset($_REQUEST['overridetype']))
             $type = $_REQUEST['overridetype'];
-        Http::download($this->getName(), $type, null, 'inline');
+        Http::download($name ?: $this->getName(), $type, null, 'inline');
         header('Content-Length: '.$this->getSize());
         $this->sendData(false);
         exit();
diff --git a/include/class.forms.php b/include/class.forms.php
index 86c6cd9d1..612e24119 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -3427,9 +3427,7 @@ class FileUploadField extends FormField {
     }
 
     function parse($value) {
-        // Values in the database should be integer file-ids
-        return array_map(function($e) { return (int) $e; },
-            $value ?: array());
+        return $value;
     }
 
     function to_php($value) {
diff --git a/include/class.thread.php b/include/class.thread.php
index 59d864060..bb65c26f6 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -1085,12 +1085,12 @@ implements TemplateVariable {
         foreach ($files as $id => $info) {
             $F = array('inline' => is_array($info) && @$info['inline']);
 
-            if (is_numeric($id) && $id != 0)
+            if (is_array($info) && isset($info['id']))
+                $fileId = $info['id'];
+            elseif (is_numeric($id) && $id != 0)
                 $fileId = $id;
             elseif ($info instanceof AttachmentFile)
                 $fileId = $info->getId();
-            elseif (is_array($info) && isset($info['id']))
-                $fileId = $info['id'];
             elseif ($AF = AttachmentFile::create($info))
                 $fileId = $AF->getId();
             elseif ($add_error) {
-- 
GitLab