diff --git a/include/class.config.php b/include/class.config.php
index 6841e6a3cf31e55d98623b6f1a5b4d26ba7f20c5..78284e49daa52abb910a4dbb5d94b73522c74ed5 100644
--- a/include/class.config.php
+++ b/include/class.config.php
@@ -1092,6 +1092,7 @@ class OsticketConfig extends Config {
 
         $company = $ost->company;
         $company_form = $company->getForm();
+        $company_form->setSource($_POST);
         if (!$company_form->isValid())
             $errors += $company_form->errors();
 
diff --git a/include/class.thread.php b/include/class.thread.php
index ad9752d0a8572a847fb4db014d9caa2ea05e937d..f99c0f45f3e22e96f20331c1c9dc27e09d285712 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -1286,7 +1286,7 @@ class ThreadBody /* extends SplString */ {
         if (strlen($this->body) > 250000) {
             $max_packet = db_get_variable('max_allowed_packet', 'global');
             // Truncate just short of the max_allowed_packet
-            $this->body = substr($this->body, $max_packet - 2048) . ' ... '
+            $this->body = substr($this->body, 0, $max_packet - 2048) . ' ... '
                . _S('(truncated)');
         }
         $this->type = $type;
diff --git a/include/class.ticket.php b/include/class.ticket.php
index b10f78097376e255c844045e095da3dcf1727921..5fff02517bc54e36c27ec9d9b1b9fcf16ab7fda1 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -2411,16 +2411,6 @@ class Ticket {
                     $vars['email'], $filter->getName()));
         }
 
-        if ($vars['topicId'] && ($topic=Topic::lookup($vars['topicId']))) {
-            if ($topic_form = $topic->getForm()) {
-                $TF = $topic_form->getForm($vars);
-                $topic_form = $topic_form->instanciate();
-                $topic_form->setSource($vars);
-                if (!$TF->isValid($field_filter('topic')))
-                    $errors = array_merge($errors, $TF->errors());
-            }
-        }
-
         $id=0;
         $fields=array();
         $fields['message']  = array('type'=>'*',     'required'=>1, 'error'=>__('Message content is required'));
@@ -2446,9 +2436,6 @@ class Ticket {
         if(!Validator::process($fields, $vars, $errors) && !$errors['err'])
             $errors['err'] =__('Missing or invalid data - check the errors and try again');
 
-        if ($vars['topicId'] && !$topic)
-            $errors['topicId'] = 'Invalid help topic selected';
-
         //Make sure the due date is valid
         if($vars['duedate']) {
             if(!$vars['time'] || strpos($vars['time'],':')===false)
@@ -2491,6 +2478,21 @@ class Ticket {
             }
         }
 
+        if ($vars['topicId']) {
+            if ($topic=Topic::lookup($vars['topicId'])) {
+                if ($topic_form = $topic->getForm()) {
+                    $TF = $topic_form->getForm($vars);
+                    $topic_form = $topic_form->instanciate();
+                    $topic_form->setSource($vars);
+                    if (!$TF->isValid($field_filter('topic')))
+                        $errors = array_merge($errors, $TF->errors());
+                }
+            }
+            else  {
+                $errors['topicId'] = 'Invalid help topic selected';
+            }
+        }
+
         // Any error above is fatal.
         if ($errors)
             return 0;
diff --git a/include/class.user.php b/include/class.user.php
index 18209bae0aa493d8c4125dcc1b194c50d7a53acc..127a078d5067d1acf43ff136ec6d0f0d27e804b1 100644
--- a/include/class.user.php
+++ b/include/class.user.php
@@ -469,6 +469,8 @@ class User extends UserModel {
 
     function importFromPost($stuff, $extra=array()) {
         if (is_array($stuff) && !$stuff['error']) {
+            // Properly detect Macintosh style line endings
+            ini_set('auto_detect_line_endings', true);
             $stream = fopen($stuff['tmp_name'], 'r');
         }
         elseif ($stuff) {
diff --git a/include/staff/header.inc.php b/include/staff/header.inc.php
index bca7df85e9f50f0b288e707c1f5d9605a6852582..08fc55dcc6438ca437bac75f9a52814c097a1fb0 100644
--- a/include/staff/header.inc.php
+++ b/include/staff/header.inc.php
@@ -74,6 +74,7 @@ if (($lang = Internationalization::getCurrentLanguage())
     </div>
     <div id="pjax-container" class="<?php if ($_POST) echo 'no-pjax'; ?>">
 <?php } else {
+    header('X-PJAX-Version: ' . GIT_VERSION);
     if ($pjax = $ost->getExtraPjax()) { ?>
     <script type="text/javascript">
     <?php foreach (array_filter($pjax) as $s) echo $s.";"; ?>
@@ -84,7 +85,6 @@ if (($lang = Internationalization::getCurrentLanguage())
             echo $h;
     } ?>
     <title><?php echo ($ost && ($title=$ost->getPageTitle()))?$title:'osTicket :: '.__('Staff Control Panel'); ?></title><?php
-    header('X-PJAX-Version: ' . GIT_VERSION);
 } # endif X_PJAX ?>
     <ul id="nav">
 <?php include STAFFINC_DIR . "templates/navigation.tmpl.php"; ?>
diff --git a/include/staff/system.inc.php b/include/staff/system.inc.php
index 6ca862558ff275812c6210921498a034bca7d925..2a5b06658a229d20d1f74271a83b545733e991ed 100644
--- a/include/staff/system.inc.php
+++ b/include/staff/system.inc.php
@@ -55,7 +55,7 @@ $commit = GIT_VERSION != '$git' ? GIT_VERSION : (
     <tr><td><?php echo __('PHP Settings'); ?></td>
         <td><table><tbody>
         <tr><td><i class="icon icon-<?php
-                echo extension_loaded('mbstring')?'check':'warning-sign'; ?>"></i>
+                echo ini_get('cgi.fix_pathinfo') == 1 ?'check':'warning-sign'; ?>"></i>
             </td><td>
             <span class="ltr"><code>cgi.fix_pathinfo</code> =
                 <?php echo ini_get('cgi.fix_pathinfo'); ?>
diff --git a/include/staff/tickets.inc.php b/include/staff/tickets.inc.php
index 6441f13f82fb97cb20c3f698bec4ca6aa8c3e1e3..094d28ded43cf56a50280d5e0413f682f8f9613f 100644
--- a/include/staff/tickets.inc.php
+++ b/include/staff/tickets.inc.php
@@ -166,7 +166,7 @@ if ($_REQUEST['advsid'] && isset($_SESSION['adv_'.$_REQUEST['advsid']])) {
     $order = ' ';
 }
 
-$sortOptions=array('date'=>'effective_date','ID'=>'ticket.`number`',
+$sortOptions=array('date'=>'effective_date','ID'=>'ticket.`number`*1',
     'pri'=>'pri.priority_urgency','name'=>'user.name','subj'=>'cdata.subject',
     'status'=>'status.name','assignee'=>'assigned','staff'=>'staff',
     'dept'=>'dept.dept_name');
diff --git a/include/staff/tpl.inc.php b/include/staff/tpl.inc.php
index d8c26e6083d869ea76a025d2ded88a5b3b2d8bd6..8cef8832426bef794519671c365055111ccffeb9 100644
--- a/include/staff/tpl.inc.php
+++ b/include/staff/tpl.inc.php
@@ -78,7 +78,7 @@ $tpl=$msgtemplates[$selected];
 <form action="templates.php?id=<?php echo $id; ?>&amp;a=manage" method="post" id="save">
 <?php csrf_token(); ?>
 <?php foreach ($extras as $k=>$v) { ?>
-    <input type="hidden" name="<?php echo $k; ?>" value="<?php echo $v; ?>" />
+    <input type="hidden" name="<?php echo $k; ?>" value="<?php echo Format::htmlchars($v); ?>" />
 <?php } ?>
 <input type="hidden" name="id" value="<?php echo $id; ?>">
 <input type="hidden" name="a" value="manage">
diff --git a/setup/cli/modules/user.php b/setup/cli/modules/user.php
index aef84a2123e5ecea6b4d61841516b225d0fc2a60..2d6d146a9950a95fda1bd4ad07c4b7a836148368 100644
--- a/setup/cli/modules/user.php
+++ b/setup/cli/modules/user.php
@@ -31,6 +31,9 @@ class UserManager extends Module {
 
         switch ($args['action']) {
             case 'import':
+                // Properly detect Macintosh style line endings
+                ini_set('auto_detect_line_endings', true);
+
                 if (!$options['file'])
                     $this->fail('CSV file to import users from is required!');
                 elseif (!($this->stream = fopen($options['file'], 'rb')))