From db0e9553d1ac412a9761fae2e358b7e8ef491ef7 Mon Sep 17 00:00:00 2001 From: Alex Puiu Date: Thu, 3 Feb 2022 15:05:17 +0200 Subject: [PATCH] add mail attachments to deck & bug fixes --- .gitignore | 1 + attachments/attachment.txt | 0 config.example.php | 11 ++++ config.php | 10 --- index.php | 60 ++++++----------- lib/DeckClass.php | 131 +++++++++++++++---------------------- 6 files changed, 83 insertions(+), 130 deletions(-) create mode 100644 .gitignore delete mode 100644 attachments/attachment.txt create mode 100644 config.example.php delete mode 100644 config.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e9b47a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.php \ No newline at end of file diff --git a/attachments/attachment.txt b/attachments/attachment.txt deleted file mode 100644 index e69de29..0000000 diff --git a/config.example.php b/config.example.php new file mode 100644 index 0000000..66c2bd6 --- /dev/null +++ b/config.example.php @@ -0,0 +1,11 @@ + diff --git a/config.php b/config.php deleted file mode 100644 index 9c230ae..0000000 --- a/config.php +++ /dev/null @@ -1,10 +0,0 @@ - diff --git a/index.php b/index.php index 9d10a3b..4753a6a 100644 --- a/index.php +++ b/index.php @@ -3,24 +3,18 @@ error_reporting(E_ERROR | E_PARSE); require_once("config.php"); require_once('lib/DeckClass.php'); -$inbox = imap_open("{" . MAIL_SERVER . "/imap" . MAIL_SERVER_FLAGS . "}INBOX", MAIL_USER, MAIL_PASSWORD) +$inbox = imap_open("{" . MAIL_SERVER . ":" . MAIL_SERVER_PORT . MAIL_SERVER_FLAGS . "}INBOX", MAIL_USER, MAIL_PASSWORD) or die("can't connect:" . imap_last_error()); $emails = imap_search($inbox, 'UNSEEN'); if ($emails) - for ($j = 0; $j <= count($emails) && $j <= 4; $j++) { + for ($j = 0; $j < count($emails) && $j < 5; $j++) { $structure = imap_fetchstructure($inbox, $emails[$j]); $attachments = array(); + $attNames = array(); if (isset($structure->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') { @@ -50,51 +44,37 @@ if ($emails) } } } - for ($i = 1; $i < count($attachments); $i++) { + 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("./attachments/" . $filename, "w+"); + $fp = fopen(getcwd() . '/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; + array_push($attNames, $attachments[$i]['filename']); } } $overview = imap_headerinfo($inbox, $emails[$j]); - $toAddress = strrev($overview->toaddress); - if(preg_match('/@([^+]+)/', $toAddress, $m)) { - global $boardName; - $boardName = strrev($m[1]); - } - if ($hasAttachment) { - $message = imap_fetchbody($inbox, $emails[$j], 1.1); + + $data = new stdClass(); + $data->title = DECODE_SPECIAL_CHARACTERS ? mb_decode_mimeheader($overview->subject) : $overview->subject; + $data->type = "plain"; + if(count($attachments)) { + $data->attachments = $attNames; + $description = DECODE_SPECIAL_CHARACTERS ? quoted_printable_decode(imap_fetchbody($inbox, $emails[$j], 1.1)) : imap_fetchbody($inbox, $emails[$j], 1.1); } else { - $message = imap_fetchbody($inbox, $emails[$j], 1); + $description = DECODE_SPECIAL_CHARACTERS ? quoted_printable_decode(imap_fetchbody($inbox, $emails[$j], 1)) : imap_fetchbody($inbox, $emails[$j], 1); } - $mailData = new stdClass(); - $mailData->mailSubject = DECODE_SPECIAL_CHARACTERS ? mb_decode_mimeheader($overview->subject) : $overview->subject; - $mailData->mailMessage = DECODE_SPECIAL_CHARACTERS ? quoted_printable_decode($message) : $message; - $mailData->from = $overview->from[0]->mailbox . '@' . $overview->from[0]->host; + $data->description = $description; $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); - } } - -imap_close($inbox); + + imap_close($inbox); ?> diff --git a/lib/DeckClass.php b/lib/DeckClass.php index 45d6cc5..b01710d 100644 --- a/lib/DeckClass.php +++ b/lib/DeckClass.php @@ -1,116 +1,87 @@ NC_USER . ":" . NC_PASSWORD, + if($data && !$attachment) { + $endpoint .= '?' . http_build_query($data); + } + curl_setopt_array($curl, array( CURLOPT_URL => $endpoint, CURLOPT_RETURNTRANSFER => true, - CURLOPT_SSLVERSION => "all", - ]; - - // set HTTP request specific headers and options/data - if ($request == '') {// an empty request value is used for attachments - // add data without JSON encoding or JSON Content-Type header - $options[CURLOPT_POST] = true; - $options[CURLOPT_POSTFIELDS] = $data; - } elseif ($request == "POST") { - array_push($headers, "Content-Type: application/json"); - $options[CURLOPT_POST] = true; - $options[CURLOPT_POSTFIELDS] = json_encode($data); - } elseif ($request == "GET") { - array_push($headers, "Content-Type: application/json"); - } - - // add headers to options - $options[CURLOPT_HTTPHEADER] = $headers; - curl_setopt_array($curl, $options); + CURLOPT_ENCODING => '', + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => $request, + CURLOPT_POSTFIELDS => (array) $data, + CURLOPT_HTTPHEADER => array( + 'Authorization: Basic ' . base64_encode(NC_USER . ':' . NC_PASSWORD), + 'OCS-APIRequest: true', + ), + )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); - - if ($err) { - echo "cURL Error #:" . $err; - } + if($err) echo "cURL Error #:" . $err; 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)) { + public function getParameters($params) {// get the board and the stack + if(preg_match('/b-"([^"]+)"/', $params, $m) || preg_match("/b-'([^']+)'/", $params, $m)) { $boardFromMail = $m[1]; - $mailData->mailSubject = str_replace($m[0], '', $mailData->mailSubject); + $params = str_replace($m[0], '', $params); } - if(preg_match('/s-"([^"]+)"/', $mailData->mailSubject, $m) || preg_match("/s-'([^']+)'/", $mailData->mailSubject, $m)) { + if(preg_match('/s-"([^"]+)"/', $params, $m) || preg_match("/s-'([^']+)'/", $params, $m)) { $stackFromMail = $m[1]; - $mailData->mailSubject = str_replace($m[0], '', $mailData->mailSubject); + $params = str_replace($m[0], '', $params); } - global $boardName; - $boards = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards", ''); + $boards = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards"); foreach($boards as $board) { - if($board->title == $boardFromMail || $board->title == $boardName) { + if(strtolower($board->title) == strtolower($boardFromMail)) { $boardId = $board->id; - } else { - echo "Board not found\n"; } } - $stacks = $this->apiCall("GET", NC_SERVER . "/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; - } + if($boardId) { + $stacks = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/$boardId/stacks"); + foreach($stacks as $stack) + (strtolower($stack->title) == strtolower($stackFromMail)) ? $stackId = $stack->id : $stackId = $stacks[0]->id; } + + $boardStack = new stdClass(); + $boardStack->board = $boardId; + $boardStack->stack = $stackId; + $boardStack->newTitle = $params; + + return $boardStack; } public function addCard($data) { - global $mailData; - global $stackId; + $params = $this->getParameters($data->title); + $data->title = $params->newTitle; + $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($data->attachments) $this->addAttachments($card, $data->attachments); - $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_SERVER . "/index.php/apps/deck/api/v1.0/boards/1/stacks/1/cards", $data); - global $cardId; - $cardId = $response->id; + return $card; } - public function addAttachment($data) { - global $mailData; - global $cardId; - $fullPath = getcwd() . "/attachments/"; //get full path to attachments dirctory - - for ($i = 1; $i < count($mailData->fileAttached); $i++) { + private function addAttachments($card, $attachments) { + $fullPath = getcwd() . "/attachments/"; //get full path to attachments directory + for ($i = 0; $i < count($attachments); $i++) { + $file = $fullPath . $attachments[$i]; $data = array( - 'file' => new CURLFile($fullPath . $mailData->fileAttached[$i]) - ); - $this->apiCall("", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/1/stacks/1/cards/$cardId/attachments?type=deck_file", $data); + 'file' => new CURLFile($file) + ); + $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); + unlink($file); } } }