From 24c7f7b8b668ec82d2fff642af7a756118cdc551 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 20 Aug 2014 21:49:31 -0500
Subject: [PATCH] forms: Add download links for canned attachments

---
 include/ajax.forms.php       |  1 -
 include/class.attachment.php |  2 ++
 include/class.file.php       |  8 ++++++--
 include/class.forms.php      | 18 +++---------------
 js/filedrop.field.js         |  9 ++++++++-
 5 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/include/ajax.forms.php b/include/ajax.forms.php
index 031ae8785..da6bb6560 100644
--- a/include/ajax.forms.php
+++ b/include/ajax.forms.php
@@ -98,7 +98,6 @@ class DynamicFormsAjaxAPI extends AjaxController {
 
     function attach() {
         $field = new FileUploadField();
-        $field->loadSystemDefaultConfig();
         return JsonDataEncoder::encode(
             array('id'=>$field->upload())
         );
diff --git a/include/class.attachment.php b/include/class.attachment.php
index 937d09edd..2e3b8c55e 100644
--- a/include/class.attachment.php
+++ b/include/class.attachment.php
@@ -167,6 +167,8 @@ class GenericAttachments {
                 .' AND a.object_id='.db_input($this->getId());
             if(($res=db_query($sql)) && db_num_rows($res)) {
                 while($rec=db_fetch_array($res)) {
+                    $rec['download'] = AttachmentFile::getDownloadForIdAndKey(
+                        $rec['id'], $rec['key']);
                     $this->attachments[] = $rec;
                 }
             }
diff --git a/include/class.file.php b/include/class.file.php
index d9266d633..702936c7d 100644
--- a/include/class.file.php
+++ b/include/class.file.php
@@ -107,13 +107,17 @@ class AttachmentFile {
         return $this->ht['created'];
     }
 
+    static function getDownloadForIdAndKey($id, $key) {
+        return strtolower($key . md5($id.session_id().strtolower($key)));
+    }
+
+
     /**
      * Retrieve a signature that can be sent to scp/file.php?h= in order to
      * download this file
      */
     function getDownloadHash() {
-        return strtolower($this->getKey()
-            . md5($this->getId().session_id().strtolower($this->getKey())));
+        return self::getDownloadForIdAndKey($this->getId(), $this->getKey());
     }
 
     function open() {
diff --git a/include/class.forms.php b/include/class.forms.php
index 0e91683cf..2390912d0 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -1235,11 +1235,12 @@ class FileUploadField extends FormField {
         if ($next < $limit * 2)
             $sizes[$limit] = Format::file_size($limit);
 
+        global $cfg;
         return array(
             'size' => new ChoiceField(array(
                 'label'=>'Maximum File Size',
                 'hint'=>'Maximum size of a single file uploaded to this field',
-                'default'=>'262144',
+                'default'=>$cfg->getMaxFileSize(),
                 'choices'=>$sizes
             )),
             'extensions' => new TextareaField(array(
@@ -1247,7 +1248,7 @@ class FileUploadField extends FormField {
                 'hint'=>'Enter allowed file extensions separated by a comma.
                 e.g .doc, .pdf. To accept all files enter wildcard
                 <b><i>.*</i></b> — i.e dotStar (NOT Recommended).',
-                'default'=>'.doc, .pdf, .jpg, .jpeg, .gif, .png, .xls, .docx, .xlsx, .txt',
+                'default'=>$cfg->getAllowedFileTypes(),
                 'configuration'=>array('html'=>false, 'rows'=>2),
             )),
             'max' => new TextboxField(array(
@@ -1261,19 +1262,6 @@ class FileUploadField extends FormField {
         );
     }
 
-    function loadSystemDefaultConfig() {
-        global $cfg;
-        return array(
-            'max' => $cfg->getStaffMaxFileUploads(),
-            'size' => $cfg->getMaxFileSize(),
-            'extensions' => $cfg->getAllowedFileTypes(),
-        );
-    }
-
-    function getConfiguration() {
-        return parent::getConfiguration() ?: $this->loadSystemDefaultConfig();
-    }
-
     function upload() {
         $config = $this->getConfiguration();
 
diff --git a/js/filedrop.field.js b/js/filedrop.field.js
index 987e4ba42..1dd179d6a 100644
--- a/js/filedrop.field.js
+++ b/js/filedrop.field.js
@@ -95,7 +95,7 @@
 
       var filenode = $('<div class="file"></div>')
           .append($('<div class="filetype"></div>').addClass())
-          .append($('<div class="filename"></div>').text(file.name)
+          .append($('<div class="filename"></div>')
             .append($('<span class="filesize"></span>').text(
               this.fileSize(parseInt(file.size))
             )).append($('<div class="upload-rate pull-right"></div>'))
@@ -113,6 +113,13 @@
       }
       if (file.id)
         filenode.data('fileId', file.id);
+      if (file.download)
+        filenode.find('.filename').prepend(
+          $('<a class="no-pjax" target="_blank"></a>').text(file.name)
+            .attr('href', 'file.php?h='+escape(file.download))
+        );
+      else
+        filenode.find('.filename').prepend(document.createTextNode(file.name));
       this.$element.parent().find('.files').append(filenode);
       this.uploads.push(filenode);
       return filenode;
-- 
GitLab