update function to add inline attachment

dev
Oana Ursuleasa 2025-02-19 14:57:43 +02:00
rodzic 146dd89fd2
commit 9e859850df
2 zmienionych plików z 71 dodań i 40 usunięć

Wyświetl plik

@ -4,42 +4,33 @@ namespace Mail2Deck;
class AttachmentClass { class AttachmentClass {
function Attachment($structure,$inbox, $emails){ function Attachment($structure,$inbox, $emails){
$attachments = array(); $attachments = array();
$attNames = array(); $attNames = array();
if (isset($structure->parts) && count($structure->parts)) { if (isset($structure->parts) && count($structure->parts)) {
for ($i = 0; $i < count($structure->parts); $i++) { $parentIndex = null;
$parts =$structure->parts[$i]; foreach ($structure->parts as $index => $part) {
if ($parts->ifdparameters || $parts->ifparameters) { $partIndex = $parentIndex ? strval($parentIndex) . '.' . ($index + 1) : ($index + 1);
$parameters = $parts->ifdparameters ? $parts->dparameters : $parts->parameters;
$attributeName = $parts->ifdparameters ? 'filename' : 'name';
foreach ($parameters as $object) { if (isset($part->parts) && count($part->parts) > 1) {
if (strtolower($object->attribute) == strtolower($attributeName)) { $result= $this->formattingAttachment($part->parts, $inbox, $emails, $partIndex); //subpart exists in the structure
$attachments[$i]['is_attachment'] = true; }else{
$attachments[$i][$attributeName] = $object->value; $result= $this->formattingAttachment([$part], $inbox, $emails, $partIndex);
}
if ($result) {
$attachments = array_merge($attachments, $result);
} }
} }
} }
if ($attachments[$i]['is_attachment']) { //Add formatted attachments
$attachments[$i]['attachment'] = $inbox->fetchMessageBody($emails, $i+1); for ($i = 0; $i < count($attachments); $i++) {
if ($parts->encoding == 3) { // 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
elseif ($parts->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
for ($i = 1; $i <= count($attachments); $i++) {
if(! file_exists(getcwd() . '/attachments')) { if(! file_exists(getcwd() . '/attachments')) {
mkdir(getcwd() . '/attachments'); mkdir(getcwd() . '/attachments');
} }
if ($attachments[$i]['is_attachment'] == 1) { if ($attachments[$i]['is_attachment'] == 1) {
$filename = $attachments[$i]['name']; $filename = $attachments[$i]['name'];
if (empty($filename)) $filename = $attachments[$i]['filename']; if (empty($filename)) $filename = $attachments[$i]['filename'];
$fp = fopen(getcwd() . '/attachments/' . $filename, "w+"); $fp = fopen(getcwd() . '/attachments/' . $filename, "w+");
@ -50,5 +41,39 @@ function Attachment($structure,$inbox, $emails){
} }
} }
return $attNames; return $attNames;
} }
function formattingAttachment($parts, $inbox, $emails, $index) {
$attachments = array();
foreach ($parts as $j => $part) {
if ($part->ifdparameters || $part->ifparameters) {
$parameters = $part->ifdparameters ? $part->dparameters : $part->parameters;
$attributeName = $part->ifdparameters ? 'filename' : 'name';
foreach ($parameters as $object) {
if (strtolower($object->attribute) == strtolower($attributeName)) {
$attachment['is_attachment'] = true;
$attachment[$attributeName] = $object->value;
if($part->disposition == "inline" || $part->disposition == "attachment"){
$partindex= $index;
}else{
$partindex= $index . "." . ($j + 1); //if the attachment is part of the subpart
}
$attachment['attachment']= $inbox->fetchMessageBody($emails,$partindex);
if ($part->encoding == 3) { // BASE64
$attachment['attachment'] = base64_decode( $attachment['attachment'],true);
} else if ($part->encoding == 4) { // QUOTED-PRINTABLE
$attachment['attachment'] = quoted_printable_decode( $attachment['attachment']);
}
$attachments[] = $attachment;
}
}
}
}
return $attachments;
}
} }

Wyświetl plik

@ -38,10 +38,16 @@ class CreateCardClass{
if ($description != strip_tags($description)) { if ($description != strip_tags($description)) {
$description = (new ConvertToMD($description))->execute(); $description = (new ConvertToMD($description))->execute();
} }
$hasCid = preg_match('/!\[.*?\]\(cid:[^)]+\)/', $description);
if (!empty($attachments)) { if (!empty($attachments)) {
foreach ($attachments as $attachment) { foreach ($attachments as $attachment) {
$filePath = NC_SERVER .'/remote.php/dav/files/'.NC_USER.'/Deck/'.$attachment; $filePath = NC_SERVER .'/remote.php/dav/files/'.NC_USER.'/Deck/'.$attachment;
if ($hasCid) {
$description = preg_replace('/!\[' . preg_quote($attachment, '/') . '\]\(cid:[^)]+\)/',"[$attachment]($filePath)", $description); $description = preg_replace('/!\[' . preg_quote($attachment, '/') . '\]\(cid:[^)]+\)/',"[$attachment]($filePath)", $description);
}else{
$description .= "\n\n[$attachment]($filePath)";
}
} }
} }