From 75b81dd415ffa4d26a9a08d51b12c1f84a2ef2ab Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Thu, 9 Oct 2014 17:07:49 -0500
Subject: [PATCH] Add "View Email Headers" feature for thread entries

This allows agents to view the raw email headers parsed with the email when
the ticket or thread entry was created.
---
 include/ajax.tickets.php                      |  1 +
 include/class.thread.php                      | 16 +++++-
 include/class.thread_actions.php              | 55 +++++++++++++++++++
 .../templates/thread-email-headers.tmpl.php   | 15 +++++
 include/staff/ticket-view.inc.php             |  6 +-
 5 files changed, 89 insertions(+), 4 deletions(-)
 create mode 100644 include/class.thread_actions.php
 create mode 100644 include/staff/templates/thread-email-headers.tmpl.php

diff --git a/include/ajax.tickets.php b/include/ajax.tickets.php
index 85a0c1516..a23278cd9 100644
--- a/include/ajax.tickets.php
+++ b/include/ajax.tickets.php
@@ -19,6 +19,7 @@ if(!defined('INCLUDE_DIR')) die('403');
 include_once(INCLUDE_DIR.'class.ticket.php');
 require_once(INCLUDE_DIR.'class.ajax.php');
 require_once(INCLUDE_DIR.'class.note.php');
+include_once INCLUDE_DIR . 'class.thread_actions.php';
 
 class TicketsAjaxAPI extends AjaxController {
 
diff --git a/include/class.thread.php b/include/class.thread.php
index ff5b1e707..a8b1ab67d 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -245,6 +245,7 @@ class ThreadEntry {
 
     var $thread;
     var $attachments;
+    var $_actions;
 
     function ThreadEntry($id, $threadId=0, $type='') {
         $this->load($id, $threadId, $type);
@@ -799,7 +800,7 @@ class ThreadEntry {
     function lookupByEmailHeaders(&$mailinfo, &$seen=false) {
         // Search for messages using the References header, then the
         // in-reply-to header
-        $search = 'SELECT thread_entery_id, mid FROM '.THREAD_ENTRY_EMAIL_TABLE
+        $search = 'SELECT thread_entry_id, mid FROM '.THREAD_ENTRY_EMAIL_TABLE
                . ' WHERE mid=%s '
                . ' ORDER BY thread_entry_id DESC';
 
@@ -1673,10 +1674,21 @@ abstract class ThreadEntryAction {
      */
     abstract function getJsStub();
 
+    /**
+     * getAjaxUrl
+     *
+     * Generate a URL to be used as an AJAX callback. The URL can be used to
+     * trigger this thread entry action via the callback.
+     *
+     * Parameters:
+     * $dialog - (bool) used in conjunction with `$.dialog()` javascript
+     *      function which assumes the `ajax.php/` should be replace a leading
+     *      `#` in the url
+     */
     function getAjaxUrl($dialog=false) {
         return sprintf('%stickets/%d/thread/%d/%s',
             $dialog ? '#' : 'ajax.php/',
-            $this->thread->getTicketId(),
+            $this->thread->getThread()->getObjectId(),
             $this->thread->getId(),
             static::getId()
         );
diff --git a/include/class.thread_actions.php b/include/class.thread_actions.php
new file mode 100644
index 000000000..ce6ac8246
--- /dev/null
+++ b/include/class.thread_actions.php
@@ -0,0 +1,55 @@
+<?php
+/*********************************************************************
+    class.thread_actions.php
+
+    Actions for thread entries. This serves as a simple repository for
+    drop-down actions which can be triggered on the ticket-view page for an
+    object's thread.
+
+    Jared Hancock <jared@osticket.com>
+    Peter Rotich <peter@osticket.com>
+    Copyright (c)  2006-2014 osTicket
+    http://www.osticket.com
+
+    Released under the GNU General Public License WITHOUT ANY WARRANTY.
+    See LICENSE.TXT for details.
+
+    vim: expandtab sw=4 ts=4 sts=4:
+**********************************************************************/
+include_once(INCLUDE_DIR.'class.thread.php');
+
+class TEA_ShowEmailHeaders extends ThreadEntryAction {
+    static $id = 'view_headers';
+    static $name = /* trans */ 'View Email Headers';
+    static $icon = 'envelope';
+
+    function isEnabled() {
+        global $thisstaff;
+
+        return $thisstaff && $thisstaff->isAdmin();
+    }
+
+    function isVisible() {
+        return (bool) $this->thread->getEmailHeader();
+    }
+
+    function getJsStub() {
+        return sprintf("$.dialog('%s');",
+            $this->getAjaxUrl()
+        );
+    }
+
+    function trigger() {
+        switch ($_SERVER['REQUEST_METHOD']) {
+        case 'GET':
+            return $this->trigger__get();
+        }
+    }
+
+    private function trigger__get() {
+        $headers = $this->thread->getEmailHeader();
+
+        include STAFFINC_DIR . 'templates/thread-email-headers.tmpl.php';
+    }
+}
+ThreadEntry::registerAction(/* trans */ 'E-Mail', 'TEA_ShowEmailHeaders');
diff --git a/include/staff/templates/thread-email-headers.tmpl.php b/include/staff/templates/thread-email-headers.tmpl.php
new file mode 100644
index 000000000..6e2f45809
--- /dev/null
+++ b/include/staff/templates/thread-email-headers.tmpl.php
@@ -0,0 +1,15 @@
+<h3><?php echo __('Raw Email Headers'); ?></h3>
+<b><a class="close" href="#"><i class="icon-remove-circle"></i></a></b>
+<hr/>
+
+<pre style="max-height: 300px; overflow-y: scroll">
+<?php echo $headers; ?>
+</pre>
+
+<hr>
+<p class="full-width">
+    <span class="buttons pull-right">
+        <input type="button" name="cancel" class="close"
+            value="<?php echo __('Close'); ?>">
+    </span>
+</p>
diff --git a/include/staff/ticket-view.inc.php b/include/staff/ticket-view.inc.php
index 3542a6631..9518c6f72 100644
--- a/include/staff/ticket-view.inc.php
+++ b/include/staff/ticket-view.inc.php
@@ -1,4 +1,6 @@
 <?php
+include_once INCLUDE_DIR . 'class.thread_actions.php';
+
 //Note that ticket obj is initiated in tickets.php.
 if(!defined('OSTSCPINC') || !$thisstaff || !is_object($ticket) || !$ticket->getId()) die('Invalid path');
 
@@ -408,7 +410,7 @@ $tcount+= $ticket->getNumNotes();
 <?php           if ($tentry->hasActions()) {
                     $actions = $tentry->getActions(); ?>
                     <div class="pull-right">
-                    <span class="action-button" data-dropdown="#entry-action-more-<?php echo $entry['id']; ?>">
+                    <span class="action-button pull-right" data-dropdown="#entry-action-more-<?php echo $entry['id']; ?>">
                         <i class="icon-caret-down"></i>
                         <span ><i class="icon-cog"></i></span>
                     </span>
@@ -427,7 +429,7 @@ $tcount+= $ticket->getNumNotes();
                 </ul>
                     </div>
 <?php           } ?>
-                    <span class="pull-right" style="white-space:no-wrap;display:inline-block">
+                    <span style="vertical-align:middle">
                         <span style="vertical-align:middle;" class="textra"></span>
                         <span style="vertical-align:middle;"
                             class="tmeta faded title"><?php
-- 
GitLab