diff --git a/include/class.crypto.php b/include/class.crypto.php
index 43954e64bb343c964ce704d9c580294d2176d058..92ab1e953f1b3786cac39e25e34acc7abfd59f9b 100644
--- a/include/class.crypto.php
+++ b/include/class.crypto.php
@@ -165,13 +165,17 @@ class Crypto {
     function random($len) {
 
         if(CRYPT_IS_WINDOWS) {
-            if (function_exists('mcrypt_create_iv')
-                    && version_compare(PHP_VERSION, '5.3', '>='))
-                return mcrypt_create_iv($len);
-
             if (function_exists('openssl_random_pseudo_bytes')
                     && version_compare(PHP_VERSION, '5.3.4', '>='))
                 return openssl_random_pseudo_bytes($len);
+
+            // Looks like mcrypt_create_iv with MCRYPT_DEV_RANDOM is still
+            // unreliable on 5.3.6:
+            // https://bugs.php.net/bug.php?id=52523
+            if (function_exists('mcrypt_create_iv')
+                    && version_compare(PHP_VERSION, '5.3.7', '>='))
+                return mcrypt_create_iv($len);
+
         } else {
 
             if (function_exists('openssl_random_pseudo_bytes'))
diff --git a/include/class.file.php b/include/class.file.php
index 6664b4f827a74632c027c7bdfd2f9365b1a728ee..5083c76f9fc7b9a53dc6c2275597f18b003dab0e 100644
--- a/include/class.file.php
+++ b/include/class.file.php
@@ -362,13 +362,10 @@ class AttachmentFile {
     /* static */ function deleteOrphans() {
 
         $sql = 'DELETE FROM '.FILE_TABLE.' WHERE id NOT IN ('
-                # DISTINCT implies sort and may not be necessary
-                .'SELECT DISTINCT(file_id) FROM ('
-                    .'SELECT file_id FROM '.TICKET_ATTACHMENT_TABLE
-                    .' UNION ALL '
-                    .'SELECT file_id FROM '.ATTACHMENT_TABLE
-                .') still_loved'
-            .') AND `ft` = "T"';
+                .'SELECT file_id FROM '.TICKET_ATTACHMENT_TABLE
+                .' UNION '
+                .'SELECT file_id FROM '.ATTACHMENT_TABLE
+            .") AND `ft` = 'T'";
 
         db_query($sql);
 
diff --git a/include/class.format.php b/include/class.format.php
index 7b80dc97bff94aba13eda8ccb6eb590b94afad2d..11f0b1040b7e5a8e36f3ba7b65b92cf326dfe21e 100644
--- a/include/class.format.php
+++ b/include/class.format.php
@@ -43,10 +43,10 @@ class Format {
 
         // Cleanup - incorrect, bogus, or ambiguous charsets
         if($charset && in_array(strtolower(trim($charset)),
-                array('default','x-user-defined','iso')))
+                array('default','x-user-defined','iso','us-ascii')))
             $charset = 'ISO-8859-1';
 
-        if (strcasecmp($charset, $encoding) === 0)
+        if ($charset && strcasecmp($charset, $encoding) === 0)
             return $text;
 
         $original = $text;
diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index 283c1aee4d7adb591178f30668989caa37bd730e..b9c9602fd9aa741bb9d955621ae5286f59523b36 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -305,12 +305,16 @@ class MailFetcher {
                     $text=$this->decode($text, $struct->encoding);
 
                 $charset=null;
-                if($encoding) { //Convert text to desired mime encoding...
-                    if($struct->ifparameters) {
-                        if(!strcasecmp($struct->parameters[0]->attribute,'CHARSET') && strcasecmp($struct->parameters[0]->value,'US-ASCII'))
-                            $charset=trim($struct->parameters[0]->value);
+                if ($encoding) { //Convert text to desired mime encoding...
+                    if ($struct->ifparameters && $struct->parameters) {
+                        foreach ($struct->parameters as $p) {
+                            if (!strcasecmp($p->attribute, 'charset')) {
+                                $charset = trim($p->value);
+                                break;
+                            }
+                        }
                     }
-                    $text=$this->mime_encode($text, $charset, $encoding);
+                    $text = $this->mime_encode($text, $charset, $encoding);
                 }
                 return $text;
             }
diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index 953435d5a1bfed11a4d506f317834ad68f0fad46..fa88c393d342136af381dc9f39a4a5b372fd90e5 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -132,16 +132,26 @@ class Mail_Parse {
 
 
     function getFromAddressList(){
-        return Mail_Parse::parseAddressList($this->struct->headers['from']);
+        if (!($header = $this->struct->headers['from']))
+            return null;
+
+        return Mail_Parse::parseAddressList($header);
     }
 
     function getToAddressList(){
-        //Delivered-to incase it was a BBC mail.
-       return Mail_Parse::parseAddressList($this->struct->headers['to']?$this->struct->headers['to']:$this->struct->headers['delivered-to']);
+        // Delivered-to incase it was a BBC mail.
+        if (!($header = $this->struct->headers['to']))
+            if (!($header = $this->struct->headers['delivered-to']))
+                return null;
+
+        return Mail_Parse::parseAddressList($header);
     }
 
     function getCcAddressList(){
-        return $this->struct->headers['cc']?Mail_Parse::parseAddressList($this->struct->headers['cc']):null;
+        if (!($header = $this->struct->headers['cc']))
+            return null;
+
+        return Mail_Parse::parseAddressList($header);
     }
 
     function getMessageId(){
@@ -153,7 +163,10 @@ class Mail_Parse {
     }
 
     function getReplyTo() {
-        return Mail_Parse::parseAddressList($this->struct->headers['reply-to']);
+        if (!($header = $this->struct->headers['reply-to']))
+            return null;
+
+        return Mail_Parse::parseAddressList($header);
     }
 
     function getBody(){
@@ -181,8 +194,9 @@ class Mail_Parse {
             if($ctype && strcasecmp($ctype,$ctypepart)==0) {
                 $content = $struct->body;
                 //Encode to desired encoding - ONLY if charset is known??
-                if(isset($struct->ctype_parameters['charset']) && strcasecmp($struct->ctype_parameters['charset'], $this->charset))
-                    $content = Format::encode($content, $struct->ctype_parameters['charset'], $this->charset);
+                if (isset($struct->ctype_parameters['charset']))
+                    $content = Format::encode($content,
+                        $struct->ctype_parameters['charset'], $this->charset);
 
                 return $content;
             }
@@ -379,7 +393,7 @@ class EmailDataParser {
         $data['in-reply-to'] = $parser->struct->headers['in-reply-to'];
         $data['references'] = $parser->struct->headers['references'];
 
-        if ($replyto = $parser->getReplyTo()) {
+        if (($replyto = $parser->getReplyTo()) && !PEAR::isError($replyto)) {
             $replyto = $replyto[0];
             $data['reply-to'] = $replyto->mailbox.'@'.$replyto->host;
             if ($replyto->personal)
diff --git a/include/class.ticket.php b/include/class.ticket.php
index 3a5a639496169d08da85c7f7a0e409a83d47a919..d6002ab73f7e0598fb9690a571d8be75716d6857 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -1683,6 +1683,7 @@ class Ticket {
         $sql='UPDATE '.TICKET_TABLE.' SET updated=NOW() '
             .' ,topic_id='.db_input($vars['topicId'])
             .' ,sla_id='.db_input($vars['slaId'])
+            .' ,source='.db_input($vars['source'])
             .' ,duedate='.($vars['duedate']?db_input(date('Y-m-d G:i',Misc::dbtime($vars['duedate'].' '.$vars['time']))):'NULL');
 
         if($vars['duedate']) { //We are setting new duedate...
@@ -2197,7 +2198,7 @@ class Ticket {
     function checkOverdue() {
 
         $sql='SELECT ticket_id FROM '.TICKET_TABLE.' T1 '
-            .' INNER JOIN '.SLA_TABLE.' T2 ON (T1.sla_id=T2.id AND T2.isactive=1) '
+            .' LEFT JOIN '.SLA_TABLE.' T2 ON (T1.sla_id=T2.id AND T2.isactive=1) '
             .' WHERE status=\'open\' AND isoverdue=0 '
             .' AND ((reopened is NULL AND duedate is NULL AND TIME_TO_SEC(TIMEDIFF(NOW(),T1.created))>=T2.grace_period*3600) '
             .' OR (reopened is NOT NULL AND duedate is NULL AND TIME_TO_SEC(TIMEDIFF(NOW(),reopened))>=T2.grace_period*3600) '