From c7f09d2d4a4e7f5e6800ec70fc5ac44b21c6581c Mon Sep 17 00:00:00 2001 From: a11exandru Date: Thu, 14 Nov 2019 16:37:34 +0200 Subject: [PATCH] added mail2deck files and update README.md --- DeckClass.php | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 18 ++++++++ newCard.php | 101 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 241 insertions(+) create mode 100644 DeckClass.php create mode 100644 newCard.php diff --git a/DeckClass.php b/DeckClass.php new file mode 100644 index 0000000..35fb36a --- /dev/null +++ b/DeckClass.php @@ -0,0 +1,122 @@ + NC_BOT . ":" . NC_PASSWORD, + CURLOPT_URL => $endpoint, + CURLOPT_CUSTOMREQUEST => $request, + CURLOPT_POST => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POSTFIELDS => json_encode($data), + CURLOPT_HTTPHEADER => $headers, + ]; + } else { + $options = [ + CURLOPT_USERPWD => NC_BOT . ":" . NC_PASSWORD, + CURLOPT_URL => $endpoint, + CURLOPT_POST => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POSTFIELDS => $data, + CURLOPT_HTTPHEADER => $headers, + ]; + } + curl_setopt_array($curl, $options); + + $response = curl_exec($curl); + $err = curl_error($curl); + + curl_close($curl); + + if ($err) { + echo "cURL Error #:" . $err; + } else { + if ($request == 'POST') { + print_r($response); + } + } + + return json_decode($response); + } + + public function getParameters() {// get the board and the stack + global $mailData; + global $boardId; + + if(preg_match('/b-"([^"]+)"/', $mailData->mailSubject, $m) || preg_match("/b-'([^']+)'/", $mailData->mailSubject, $m)) { + $boardFromMail = $m[1]; + $mailData->mailSubject = str_replace($m[0], '', $mailData->mailSubject); + } + if(preg_match('/s-"([^"]+)"/', $mailData->mailSubject, $m) || preg_match("/s-'([^']+)'/", $mailData->mailSubject, $m)) { + $stackFromMail = $m[1]; + $mailData->mailSubject = str_replace($m[0], '', $mailData->mailSubject); + } + + global $boardName; + $boards = $this->apiCall("GET", NC_URL . "/index.php/apps/deck/api/v1.0/boards", ''); + foreach($boards as $board) { + if($board->title == $boardFromMail || $board->title == $boardName) { + $boardId = $board->id; + } else { + echo "Board not found\n"; + } + } + + $stacks = $this->apiCall("GET", NC_URL . "/index.php/apps/deck/api/v1.0/boards/$boardId/stacks", ''); + foreach($stacks as $stack) { + if($stack->title == $stackFromMail) { + global $stackId; + $stackId = $stack->id; + } else if (!is_numeric($stackId)) { + global $stackId; + $stackId = $stacks[0]->id; + } + } + } + + public function addCard($data) { + global $mailData; + global $stackId; + + $data = new stdClass(); + $data->stackId = $stackId; + $data->title = $mailData->mailSubject; + $data->description = +"$mailData->mailMessage +*** +### $mailData->from +"; + $data->type = "plain"; + $data->order = "-" . time(); // put the card to the top + + //create card + $response = $this->apiCall("POST", NC_URL . "/index.php/apps/deck/api/v1.0/boards/1/stacks/1/cards", $data); + global $cardId; + $cardId = $response->id; + } + + public function addAttachment($data) { + global $mailData; + global $cardId; + $fullPath = 'D:/projects/Deck API'; + + for ($i = 1; $i < count($mailData->fileAttached); $i++) { + $data = array( + 'file' => new CURLFile("$fullPath/attachments/" . $mailData->fileAttached[$i]) + ); + $this->apiCall("", NC_URL . "/index.php/apps/deck/api/v1.0/boards/1/stacks/1/cards/$cardId/attachments?type=deck_file", $data); + } + } +} +?> \ No newline at end of file diff --git a/README.md b/README.md index 1956c40..5a10dc3 100644 --- a/README.md +++ b/README.md @@ -22,3 +22,21 @@ The email address is composed like: A particulary example: any.str.ing.personalBoard@ncserver.domain. The card will be added in *personalBoard*, even if you use more dots in the email address. + +## B. For NextCloud admins to setup +### PHP configuration +From **_php.ini_** file, enable imap extension. +### Create a new user +Configure a new user from your User Management on your NC server, which will have to function as a bot. +### Edit files +- Go in file **_DeckClass.php_** and customize as you need the following: +``` +define("NC_URL", ""); +define("NC_BOT", ""); +define("NC_PASSWORD", ""); +``` +- Customize the mail server from file **_newCard.php_**: +``` +$inbox = imap_open("{}INBOX", "", "") + or die("can't connect:" . imap_last_error()); +``` diff --git a/newCard.php b/newCard.php new file mode 100644 index 0000000..d6d49a9 --- /dev/null +++ b/newCard.php @@ -0,0 +1,101 @@ +parts) && count($structure->parts)) { + for ($i = 0; $i < count($structure->parts); $i++) { + $attachments[$i] = array( + 'is_attachment' => false, + 'filename' => '', + 'name' => '', + 'attachment' => '' + ); + + if ($structure->parts[$i]->ifdparameters) { + foreach ($structure->parts[$i]->dparameters as $object) { + if (strtolower($object->attribute) == 'filename') { + $attachments[$i]['is_attachment'] = true; + $attachments[$i]['filename'] = $object->value; + } + } + } + + if ($structure->parts[$i]->ifparameters) { + foreach ($structure->parts[$i]->parameters as $object) { + if (strtolower($object->attribute) == 'name') { + $attachments[$i]['is_attachment'] = true; + $attachments[$i]['name'] = $object->value; + } + } + } + + if ($attachments[$i]['is_attachment']) { + $attachments[$i]['attachment'] = imap_fetchbody($inbox, $emails[0], $i+1); + if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64 + $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); + } + elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE + $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); + } + } + } +} +for ($i = 1; $i < count($attachments); $i++) { + if ($attachments[$i]['is_attachment'] == 1) { + $filename = $attachments[$i]['name']; + if (empty($filename)) $filename = $attachments[$i]['filename']; + + $fp = fopen("./attachments/" . $filename, "w+"); + fwrite($fp, $attachments[$i]['attachment']); + fclose($fp); + } +} + +$hasAttachment = false; +for ($i = 0; $i < count($attachments); $i++) { + if ($attachments[$i]['is_attachment'] != '') { + $hasAttachment = true; + } +} + +if ($emails) { + $overview = imap_headerinfo($inbox, $emails[0]); + $toAddress = strrev($overview->toaddress); + if(preg_match('/@([^.]+)/', $toAddress, $m)) { + global $boardName; + $boardName = strrev($m[1]); + } + if ($hasAttachment) { + $message = imap_fetchbody($inbox, $emails[0], 1.1); + } else { + $message = imap_fetchbody($inbox, $emails[0], 1); + } + $mailData = new stdClass(); + $mailData->mailSubject = $overview->subject; + $mailData->mailMessage = $message; + $mailData->from = $overview->from[0]->mailbox . '@' . $overview->from[0]->host; + + $newcard = new DeckClass(); + $newcard->getParameters(); + $newcard->addCard($data); + + if ($hasAttachment) { + for ($i = 1; $i <= count($attachments); $i++) { + $mailData->fileAttached[$i] = $attachments[$i]['name']; + } + $newcard->addAttachment($data); + } +} else { + echo "no mail arrived"; +} + +imap_close($inbox); +?> \ No newline at end of file