nextcloud-mail2deck/lib/CreateCardClass.php

86 wiersze
3.5 KiB
PHP
Czysty Zwykły widok Historia

<?php
namespace Mail2Deck;
class CreateCardClass{
function extractBoardName($overview){
$board = null;
if(isset($overview->{'X-Original-To'}) && strstr($overview->{'X-Original-To'}, '+')) {
$board = strstr(substr($overview->{'X-Original-To'}, strpos($overview->{'X-Original-To'}, '+') + 1), '@', true);
} else if(strstr($overview->to[0]->mailbox, '+')) {
$board = substr($overview->to[0]->mailbox, strpos($overview->to[0]->mailbox, '+') + 1);
}
if(strstr($board, '+')) $board = str_replace('+', ' ', $board);
return $board;
}
function createMailData($overview, $data) {
$data->title = DECODE_SPECIAL_CHARACTERS ? mb_decode_mimeheader($overview->subject) : $overview->subject;
$data->type = "plain";
$data->order = -time();
return $data;
}
function fetchMailBody($inbox, $emailId) {
$body = $inbox->fetchMessageBody($emailId, 1.2);
if (!strlen($body) > 0) {
$body = $inbox->fetchMessageBody($emailId, 1);
}
return $body;
}
function mailDescription ($body, $base64encode, $attachments) {
$description = DECODE_SPECIAL_CHARACTERS ? quoted_printable_decode($body) : $body;
if ($base64encode) {
$description = base64_decode($description);
}
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_ADMIN_USER.'/Deck/'.$attachment;
if ($hasCid) {
$description = preg_replace('/!\[' . preg_quote($attachment, '/') . '\]\(cid:[^)]+\)/',"[$attachment]($filePath)", $description);
}else{
$description .= "\n\n[$attachment]($filePath)";
}
}
}
return $description;
}
function cleanedSubject($subject, $description, $newcard) {
if (strpos($subject, "RE: ") !== false || strpos($subject, "Fwd") !== false || strpos($subject, "FW:") !== false || strpos($subject, "Re:") !== false) {
$cleanedSubject = preg_replace('/^(Re: |RE: |Fwd: |FW: )\s*/i', '', $subject);
}else{
$cleanedSubject = $newcard->findOriginalSubject(strip_tags($description));
}
return $cleanedSubject;
}
function createCard($newcard, $data, $mailSender, $cleanedSubject, $inbox){
$existingCardId = $newcard->findCardBySubject($cleanedSubject);
$mailSender->origin .= "{$mailSender->userId}@{$mailSender->host}";
if (!$existingCardId) {
$response = $newcard->addCard($data, $mailSender->origin, $mailSender->host);
error_log("New card created with response: " . json_encode($response));
if (MAIL_NOTIFICATION) {
if (!$response) {
$inbox->reply($mailSender->origin,$mailSender->userId);
}
$inbox->reply($mailSender->origin,$mailSender->userId, $response);
}
}else{
$cleanedDescription = $newcard->extractForwardedContent($data->description);
$response = $newcard->addCommentToCard($existingCardId, $cleanedDescription);
error_log("Comment added with response: " . json_encode($response));
}
}
}