Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented MS-TNEF file handling in e-mail import feature #410

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions include/class.mailfetch.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class.mailfetch.php
require_once(INCLUDE_DIR.'class.dept.php');
require_once(INCLUDE_DIR.'class.email.php');
require_once(INCLUDE_DIR.'class.filter.php');
require_once(INCLUDE_DIR.'tnef_decoder.php');


class MailFetcher {

Expand All @@ -28,7 +30,7 @@ class MailFetcher {
var $srvstr;

var $charset = 'UTF-8';
var $encodings =array('UTF-8','WINDOWS-1251', 'ISO-8859-5', 'ISO-8859-1','KOI8-R');
var $encodings = array('UTF-8', 'ISO-8859-1', 'WINDOWS-1251', 'ISO-8859-5', 'KOI8-R');

function MailFetcher($email, $charset='UTF-8') {

Expand Down Expand Up @@ -200,7 +202,7 @@ function mime_encode($text, $charset=null, $enc='utf-8') { //Thank in part to af
if($charset)
return iconv($charset, $enc.'//IGNORE', $text);
elseif(function_exists('mb_detect_encoding'))
return iconv(mb_detect_encoding($text, $this->encodings), $enc, $text);
return iconv(mb_detect_encoding($text, $this->encodings, true), $enc, $text);
}

return utf8_encode($text);
Expand Down Expand Up @@ -310,11 +312,19 @@ function getAttachments($part, $index=0) {
if($part && !$part->parts) {
//Check if the part is an attachment.
$filename = '';
if($part->ifdisposition && in_array(strtolower($part->disposition), array('attachment', 'inline')))
$filename = $part->dparameters[0]->value;
elseif($part->ifparameters && $part->type == 5) //inline image without disposition.
$filename = $part->parameters[0]->value;

if($part->ifdisposition && in_array(strtolower($part->disposition), array('attachment', 'inline'))) {
foreach ($part->dparameters as $dparameter) {
if (strncmp(strtolower($dparameter->attribute),'filename',strlen('filename')) == 0)
$filename.=$this->mime_encode($this->mime_decode(urldecode($dparameter->value)));
}
}elseif($part->ifparameters && $part->type == 5) { //inline image without disposition.
foreach ($part->parameters as $parameter) {
if (strncmp(strtolower($parameter->attribute),'filename',strlen('filename')) == 0)
$filename.=$this->mime_encode($this->mime_decode(urldecode($parameter->value)));
}
}elseif(strtolower($this->getMimeType($part)) == "application/ms-tnef")
$filename = "winmail.dat";

if($filename) {
return array(
array(
Expand Down Expand Up @@ -441,7 +451,25 @@ function createTicket($mid) {
//We're just checking the type of file - not size or number of attachments...
// Restrictions are mainly due to PHP file uploads limitations
foreach($attachments as $a ) {
if($ost->isFileTypeAllowed($a['name'], $a['mime'])) {
if(strtolower($a['mime']) == "application/ms-tnef"){ //unpack winmail.dat attachments
$tnef = new tnef_decoder;
$tnef_arr = $tnef->decompress($this->decode($a['encoding'], imap_fetchbody($this->mbox, $mid, $a['index'])));
foreach ($tnef_arr as $tnef_file) {
$file = array(
'name' => $this->mime_encode($tnef_file['name']),
'type' => trim(strtolower($tnef_file['type']))."/".trim(strtolower($tnef_file['subtype'])),
'data' => $tnef_file['stream']
);
if($ost->isFileTypeAllowed($file['name'], $file['type'])) {
$ost->logDebug($file['name'],$file['type']);
$ticket->saveAttachment($file, $msgid, 'M');
}else{
$error = sprintf(_('Attachment %s [%s] rejected because of file type'), $file['name'], $file['type']);
$ticket->postNote(_('Email Attachment Rejected'), $error, 'SYSTEM', false);
$ost->logDebug(_('Email Attachment Rejected (Ticket #').$ticket->getExtId().')', $error);
}
}
} elseif($ost->isFileTypeAllowed($a['name'], $a['mime'])) {
$file = array(
'name' => $a['name'],
'type' => $a['mime'],
Expand Down
Loading