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,51 +4,76 @@ namespace Mail2Deck;
class AttachmentClass {
function Attachment($structure,$inbox, $emails){
$attachments = array();
$attNames = array();
if (isset($structure->parts) && count($structure->parts)) {
for ($i = 0; $i < count($structure->parts); $i++) {
$parts =$structure->parts[$i];
if ($parts->ifdparameters || $parts->ifparameters) {
$parameters = $parts->ifdparameters ? $parts->dparameters : $parts->parameters;
$attributeName = $parts->ifdparameters ? 'filename' : 'name';
function Attachment($structure,$inbox, $emails){
$attachments = array();
$attNames = array();
if (isset($structure->parts) && count($structure->parts)) {
$parentIndex = null;
foreach ($structure->parts as $index => $part) {
$partIndex = $parentIndex ? strval($parentIndex) . '.' . ($index + 1) : ($index + 1);
if (isset($part->parts) && count($part->parts) > 1) {
$result= $this->formattingAttachment($part->parts, $inbox, $emails, $partIndex); //subpart exists in the structure
}else{
$result= $this->formattingAttachment([$part], $inbox, $emails, $partIndex);
}
if ($result) {
$attachments = array_merge($attachments, $result);
}
}
}
//Add formatted attachments
for ($i = 0; $i < count($attachments); $i++) {
if(! file_exists(getcwd() . '/attachments')) {
mkdir(getcwd() . '/attachments');
}
if ($attachments[$i]['is_attachment'] == 1) {
$filename = $attachments[$i]['name'];
if (empty($filename)) $filename = $attachments[$i]['filename'];
$fp = fopen(getcwd() . '/attachments/' . $filename, "w+");
fwrite($fp, $attachments[$i]['attachment']);
fclose($fp);
array_push($attNames, $attachments[$i]['filename']);
}
}
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)) {
$attachments[$i]['is_attachment'] = true;
$attachments[$i][$attributeName] = $object->value;
$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;
}
}
}
if ($attachments[$i]['is_attachment']) {
$attachments[$i]['attachment'] = $inbox->fetchMessageBody($emails, $i+1);
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']);
}
}
}
return $attachments;
}
for ($i = 1; $i <= count($attachments); $i++) {
if(! file_exists(getcwd() . '/attachments')) {
mkdir(getcwd() . '/attachments');
}
if ($attachments[$i]['is_attachment'] == 1) {
$filename = $attachments[$i]['name'];
if (empty($filename)) $filename = $attachments[$i]['filename'];
$fp = fopen(getcwd() . '/attachments/' . $filename, "w+");
fwrite($fp, $attachments[$i]['attachment']);
fclose($fp);
array_push($attNames, $attachments[$i]['filename']);
}
}
return $attNames;
}
}

Wyświetl plik

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