kopia lustrzana https://github.com/newroco/mail2deck
update function to add inline attachment
rodzic
7fa7a7f07e
commit
bee453e316
|
@ -36,7 +36,7 @@ for ($j = 0; $j < count($emails) && $j < 5; $j++) {
|
|||
|
||||
$mailSender = new stdClass();
|
||||
$data = $createCardClass->createMailData($overview, $mailSender);
|
||||
$body = $createCardClass->fetchMailBody($inbox, $emails[$j]);
|
||||
$body = $createCardClass->fetchMailBody($structure,$inbox, $emails[$j]);
|
||||
|
||||
$newcard = new DeckClass();
|
||||
$data->attachments = $attNames;
|
||||
|
|
|
@ -14,9 +14,9 @@ class AttachmentClass {
|
|||
$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
|
||||
$result= $this->processAttachment($part->parts, $inbox, $emails, $partIndex);//subpart exists in the structure
|
||||
}else{
|
||||
$result= $this->formattingAttachment([$part], $inbox, $emails, $partIndex);
|
||||
$result= $this->processAttachment([$part], $inbox, $emails, $partIndex);
|
||||
}
|
||||
if ($result) {
|
||||
$attachments = array_merge($attachments, $result);
|
||||
|
@ -43,8 +43,7 @@ class AttachmentClass {
|
|||
return $attNames;
|
||||
}
|
||||
|
||||
|
||||
function formattingAttachment($parts, $inbox, $emails, $index) {
|
||||
function processAttachment($parts, $inbox, $emails, $index) {
|
||||
$attachments = array();
|
||||
foreach ($parts as $j => $part) {
|
||||
if ($part->ifdparameters || $part->ifparameters) {
|
||||
|
@ -56,10 +55,10 @@ class AttachmentClass {
|
|||
$attachment['is_attachment'] = true;
|
||||
$attachment[$attributeName] = $object->value;
|
||||
|
||||
if($part->disposition == "inline" || $part->disposition == "attachment"){
|
||||
if( $part->disposition == "attachment"){
|
||||
$partindex= $index;
|
||||
}else{
|
||||
$partindex= $index . "." . ($j + 1); //if the attachment is part of the subpart
|
||||
$partindex= $index . "." . ($j + 1);
|
||||
}
|
||||
|
||||
$attachment['attachment']= $inbox->fetchMessageBody($emails,$partindex);
|
||||
|
|
|
@ -22,10 +22,37 @@ class CreateCardClass{
|
|||
return $data;
|
||||
}
|
||||
|
||||
function fetchMailBody($inbox, $emailId) {
|
||||
$body = $inbox->fetchMessageBody($emailId, 1.2);
|
||||
if (!strlen($body) > 0) {
|
||||
$body = $inbox->fetchMessageBody($emailId, 1);
|
||||
function fetchMailBody($structure,$inbox, $emailId) {
|
||||
return $this->getHtmlPart($inbox, $emailId, $structure, '');
|
||||
}
|
||||
function getHtmlPart($inbox, $emailId, $structure, $prefix) {
|
||||
if (!isset($structure->parts)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($structure->parts as $index => $part) {
|
||||
$partIndex = $prefix ? "$prefix." . ($index + 1) : ($index + 1);
|
||||
|
||||
if (isset($part->subtype) && strtoupper($part->subtype) == 'HTML') {
|
||||
$body = $inbox->fetchMessageBody($emailId, $partIndex);;
|
||||
return $this->decodeBody($body, $part->encoding);
|
||||
}
|
||||
|
||||
if (isset($part->subtype) && (strtoupper($part->subtype) == 'RELATED' || strtoupper($part->subtype) == 'ALTERNATIVE')) {
|
||||
$result = $this->getHtmlPart($inbox, $emailId, $part, $partIndex);
|
||||
if ($result) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function decodeBody($body, $encoding) {
|
||||
if ($encoding == 3) { // BASE64
|
||||
return base64_decode($body);
|
||||
} elseif ($encoding == 4) { // QUOTED-PRINTABLE
|
||||
return quoted_printable_decode($body);
|
||||
}
|
||||
return $body;
|
||||
}
|
||||
|
@ -38,19 +65,6 @@ 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_ADMIN_USER.'/Deck/'.$attachment;
|
||||
if ($hasCid) {
|
||||
$description = preg_replace('/!\[' . preg_quote($attachment, '/') . '\]\(cid:[^)]+\)/',"[$attachment]($filePath)", $description);
|
||||
}else{
|
||||
$description .= "\n\n[$attachment]($filePath)";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $description;
|
||||
}
|
||||
|
||||
|
@ -71,6 +85,13 @@ class CreateCardClass{
|
|||
$response = $newcard->addCard($data, $mailSender->origin, $mailSender->host);
|
||||
error_log("New card created with response: " . json_encode($response));
|
||||
|
||||
if($data->attachments){
|
||||
$imageUrls = $newcard->addAttachments($response, $data->attachments);
|
||||
if (!empty($imageUrls)) {
|
||||
$newcard->updateCardDescription($response, $imageUrls);
|
||||
}
|
||||
}
|
||||
|
||||
if (MAIL_NOTIFICATION) {
|
||||
if (!$response) {
|
||||
$inbox->reply($mailSender->origin,$mailSender->userId);
|
||||
|
|
|
@ -7,24 +7,14 @@ class DeckClass {
|
|||
|
||||
private function apiCall($request, $endpoint, $data = null, $attachment = false, $userapi = false){
|
||||
$curl = curl_init();
|
||||
if($data && !$attachment) {
|
||||
if ($request === "PUT") {
|
||||
if (is_array($data) || is_object($data)) {
|
||||
$jsonData = json_encode($data);
|
||||
} else {
|
||||
$jsonData = $data;
|
||||
}
|
||||
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen($jsonData)
|
||||
]);
|
||||
} else {
|
||||
$endpoint .= '?' . http_build_query($data);
|
||||
}
|
||||
// POST and PUT
|
||||
if ($data) {
|
||||
$data = (array) $data;
|
||||
$jsonData = $attachment ? $data : json_encode($data, JSON_PRETTY_PRINT);
|
||||
}
|
||||
curl_setopt_array($curl, array(
|
||||
|
||||
curl_setopt_array($curl, [
|
||||
CURLOPT_URL => $endpoint,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPGET => true,
|
||||
|
@ -34,35 +24,10 @@ class DeckClass {
|
|||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
||||
CURLOPT_CUSTOMREQUEST => $request,
|
||||
CURLOPT_HTTPHEADER => array(
|
||||
'Authorization: Basic ' . base64_encode(NC_ADMIN_USER . ':' . NC_ADMIN_PASSWORD),
|
||||
'OCS-APIRequest: true',
|
||||
),
|
||||
));
|
||||
|
||||
if ($request === 'POST') {
|
||||
if ($attachment){
|
||||
curl_setopt($curl, CURLOPT_POST, true);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge(
|
||||
array(
|
||||
'Accept: application/json',
|
||||
'Content-Type: multipart/form-data',
|
||||
'Authorization: Basic ' . base64_encode(NC_ADMIN_USER . ':' . NC_ADMIN_PASSWORD),
|
||||
)
|
||||
));
|
||||
curl_setopt($curl, CURLOPT_USERPWD, NC_ADMIN_USER . ":" . NC_ADMIN_PASSWORD);
|
||||
}else{
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge(
|
||||
array(
|
||||
'Accept: application/json',
|
||||
'OCS-APIRequest: true',
|
||||
'Content-Type: application/json',
|
||||
'Authorization: Basic ' . base64_encode(NC_ADMIN_USER . ':' . NC_ADMIN_PASSWORD),
|
||||
)
|
||||
));
|
||||
}
|
||||
CURLOPT_HTTPHEADER => $this->setRequestHeaders($curl, $attachment),
|
||||
]);
|
||||
if ($request === 'POST' || $request === 'PUT') {
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);
|
||||
}
|
||||
|
||||
$response = curl_exec($curl);
|
||||
|
@ -76,6 +41,21 @@ class DeckClass {
|
|||
return json_decode($response);
|
||||
}
|
||||
|
||||
private function setRequestHeaders($curl, $attachment) {
|
||||
$headers = [
|
||||
'Authorization: Basic ' . base64_encode(NC_ADMIN_USER . ':' . NC_ADMIN_PASSWORD),
|
||||
'Accept: application/json',
|
||||
'OCS-APIRequest: true',
|
||||
];
|
||||
|
||||
if ($attachment) {
|
||||
$headers[] = 'Content-Type: multipart/form-data';
|
||||
} else {
|
||||
$headers[] = 'Content-Type: application/json';
|
||||
}
|
||||
return $headers;
|
||||
}
|
||||
|
||||
public function getParameters($params, $boardFromMail = null, $mail_domain) {// get the board and the stack
|
||||
if(!$boardFromMail) // if board is not set within the email address, look for board into email subject
|
||||
if(preg_match('/b-"([^"]+)"/', $params, $m) || preg_match("/b-'([^']+)'/", $params, $m)) {
|
||||
|
@ -147,14 +127,13 @@ class DeckClass {
|
|||
if($params) {
|
||||
$data->title = $params->newTitle;
|
||||
$data->duedate = $params->dueDate;
|
||||
$card = $this->apiCall("POST", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$params->board}/stacks/{$params->stack}/cards", $data);
|
||||
$card = $this->apiCall("POST", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$params->board}/stacks/{$params->stack}/cards",$data);
|
||||
$card->board = $params->board;
|
||||
$card->stack = $params->stack;
|
||||
|
||||
if($this->responseCode == 200) {
|
||||
if(ASSIGN_SENDER || $user) $this->assignUser($card, $user);
|
||||
if($data->attachments) $this->addAttachments($card, $data->attachments);
|
||||
$card->boardTitle = $params->boardTitle;
|
||||
$card->description = $data->description;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
|
@ -165,22 +144,52 @@ class DeckClass {
|
|||
}
|
||||
|
||||
//Add a new attachment
|
||||
private function addAttachments($card, $attachments) {
|
||||
public function addAttachments($card, $attachments) {
|
||||
$fullPath = getcwd() . "/attachments/"; //get full path to attachments directory
|
||||
$imageUrls = [];
|
||||
for ($i = 0; $i < count($attachments); $i++) {
|
||||
$file = $fullPath . $attachments[$i];
|
||||
if (!file_exists($file)) {
|
||||
die("Eroare: Fișierul $file nu există!");
|
||||
}
|
||||
$data = array(
|
||||
'file' => new \CURLFile($file)
|
||||
);
|
||||
$response = $this->apiCall("POST", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$card->board}/stacks/{$card->stack}/cards/{$card->id}/attachments?type=file", $data, true);
|
||||
|
||||
if (isset($response->extendedData->fileid)) {
|
||||
$imageUrl = NC_SERVER . "/index.php/apps/files/?dir=/Deck&fileid=" . $response->extendedData->fileid;
|
||||
$imageUrls[] = [
|
||||
'fileid' => $response->extendedData->fileid,
|
||||
'filename' => $attachments[$i]
|
||||
];
|
||||
}
|
||||
|
||||
unlink($file);
|
||||
return $imageUrl;
|
||||
}
|
||||
return $imageUrls;
|
||||
}
|
||||
|
||||
public function updateCardDescription($card, $imageUrls) {
|
||||
$newDescription = $card->description . "\n\n";
|
||||
$matches = [];
|
||||
preg_match_all('/!\[.*?\]\(cid:[^)]+\)/', $newDescription, $matches);
|
||||
|
||||
foreach ($imageUrls as $img) {
|
||||
$fileid = $img['fileid'];
|
||||
$filename = $img['filename'];
|
||||
$url = NC_SERVER . "/f/{$fileid}";
|
||||
|
||||
$newDescription = preg_replace('/!\[.*?\]\(cid:[^)]+\)/', "[$filename]($url)", $newDescription);
|
||||
}
|
||||
|
||||
$data = [
|
||||
'title' => $card->title,
|
||||
'description' => $newDescription,
|
||||
'type' => $card->type,
|
||||
'order' =>$card->order,
|
||||
'duedate'=>$card->duedate,
|
||||
'owner'=>$card->owner
|
||||
];
|
||||
|
||||
$this->apiCall("PUT",NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$card->board}/stacks/{$card->stack}/cards/{$card->id}",$data);
|
||||
}
|
||||
|
||||
//Assign a user to the card
|
||||
|
@ -209,14 +218,24 @@ class DeckClass {
|
|||
return false;
|
||||
}
|
||||
|
||||
public function getAllBoards() {
|
||||
return $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards");
|
||||
}
|
||||
public function getStacksByBoard($boardId) {
|
||||
return $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$boardId}/stacks");
|
||||
}
|
||||
public function getCardsByStack($stack) {
|
||||
return $stack->cards ?? [];
|
||||
}
|
||||
//Identify the card by email subject
|
||||
public function findCardBySubject($subject) {
|
||||
$boards = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards");
|
||||
$boards = $this->getAllBoards();
|
||||
$cleanSubject = preg_replace("/\s[b|s|u]-'.*?'/", '', $subject);
|
||||
foreach ($boards as $board) {
|
||||
$stacks = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$board->id}/stacks");
|
||||
$stacks =$this->getStacksByBoard($board->id);
|
||||
foreach ($stacks as $stack) {
|
||||
foreach ($stack->cards as $card) {
|
||||
$cards = $this->getCardsByStack($stack);
|
||||
foreach ($cards as $card) {
|
||||
if (strtolower(trim($card->title)) == strtolower(trim($cleanSubject))) {
|
||||
return [
|
||||
'cardId' => $card->id,
|
||||
|
|
Ładowanie…
Reference in New Issue