From 77f99de5bb337666e644fde3fe6b8c63ec1aea22 Mon Sep 17 00:00:00 2001 From: Alex Puiu Date: Fri, 4 Feb 2022 10:46:44 +0200 Subject: [PATCH] Assign user to newly created card if has NC account. --- config.example.php | 1 + index.php | 25 +++++++++++++------------ lib/DeckClass.php | 21 +++++++++++++++++++-- lib/MailClass.php | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 14 deletions(-) create mode 100644 lib/MailClass.php diff --git a/config.example.php b/config.example.php index 66c2bd6..665ffca 100644 --- a/config.example.php +++ b/config.example.php @@ -8,4 +8,5 @@ define("MAIL_SERVER_PORT", "143"); define("MAIL_USER", "incoming"); define("MAIL_PASSWORD", "****"); define("DECODE_SPECIAL_CHARACTERS", true); //requires mbstring, if false special characters (like öäüß) won't be displayed correctly +define("ASSIGN_SENDER", true); // if true, sender will be assigned to card if has NC account ?> diff --git a/index.php b/index.php index 4753a6a..7f36eb5 100644 --- a/index.php +++ b/index.php @@ -2,15 +2,14 @@ error_reporting(E_ERROR | E_PARSE); require_once("config.php"); require_once('lib/DeckClass.php'); +require_once('lib/MailClass.php'); -$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'); +$inbox = new MailClass(); +$emails = $inbox->getNewMessages(); if ($emails) for ($j = 0; $j < count($emails) && $j < 5; $j++) { - $structure = imap_fetchstructure($inbox, $emails[$j]); + $structure = $inbox->fetchMessageStructure($emails[$j]); $attachments = array(); $attNames = array(); if (isset($structure->parts) && count($structure->parts)) { @@ -34,7 +33,7 @@ if ($emails) } if ($attachments[$i]['is_attachment']) { - $attachments[$i]['attachment'] = imap_fetchbody($inbox, $emails[$j], $i+1); + $attachments[$i]['attachment'] = $inbox->fetchMessageBody($emails[$j], $i+1); if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64 $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); } @@ -59,22 +58,24 @@ if ($emails) } } - $overview = imap_headerinfo($inbox, $emails[$j]); + $overview = $inbox->headerInfo($emails[$j]); $data = new stdClass(); $data->title = DECODE_SPECIAL_CHARACTERS ? mb_decode_mimeheader($overview->subject) : $overview->subject; $data->type = "plain"; + $data->order = 999; 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); + $description = DECODE_SPECIAL_CHARACTERS ? quoted_printable_decode($inbox->fetchMessageBody($emails[$j], 1.1)) : $inbox->fetchMessageBody($emails[$j], 1.1); } else { - $description = DECODE_SPECIAL_CHARACTERS ? quoted_printable_decode(imap_fetchbody($inbox, $emails[$j], 1)) : imap_fetchbody($inbox, $emails[$j], 1); + $description = DECODE_SPECIAL_CHARACTERS ? quoted_printable_decode($inbox->fetchMessageBody($emails[$j], 1)) : $inbox->fetchMessageBody($emails[$j], 1); } $data->description = $description; + $mailSender = new stdClass(); + $mailSender->userId = $overview->from[0]->mailbox; + $mailSender = 'alex.puiu'; $newcard = new DeckClass(); - $newcard->addCard($data); + $newcard->addCard($data, $mailSender); } - - imap_close($inbox); ?> diff --git a/lib/DeckClass.php b/lib/DeckClass.php index b01710d..b0486ee 100644 --- a/lib/DeckClass.php +++ b/lib/DeckClass.php @@ -15,13 +15,14 @@ class DeckClass { 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', ), )); + if($request === 'POST') curl_setopt($curl, CURLOPT_POSTFIELDS, (array) $data); + $response = curl_exec($curl); $err = curl_error($curl); @@ -62,12 +63,15 @@ class DeckClass { return $boardStack; } - public function addCard($data) { + public function addCard($data, $user) { $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(ASSIGN_SENDER) { + $this->assignUser($card, $user); + } if($data->attachments) $this->addAttachments($card, $data->attachments); return $card; @@ -84,5 +88,18 @@ class DeckClass { unlink($file); } } + + public function assignUser($card, $mailUser) + { + $board = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$card->board}"); + $boardUsers = array_map(function ($user) { return $user->uid; }, $board->users); + + foreach($boardUsers as $user) { + if($user === $mailUser->userId) { + $this->apiCall("PUT", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$card->board}/stacks/{$card->stack}/cards/{$card->id}/assignUser", $mailUser); + break; + } + } + } } ?> diff --git a/lib/MailClass.php b/lib/MailClass.php new file mode 100644 index 0000000..438fc7f --- /dev/null +++ b/lib/MailClass.php @@ -0,0 +1,37 @@ +inbox = imap_open("{" . MAIL_SERVER . ":" . MAIL_SERVER_PORT . MAIL_SERVER_FLAGS . "}INBOX", MAIL_USER, MAIL_PASSWORD) + or die("can't connect:" . imap_last_error()); + } + + public function __destruct() + { + imap_close($this->inbox); + } + + public function getNewMessages() { + return imap_search($this->inbox, 'UNSEEN'); + } + + public function fetchMessageStructure($email) { + return imap_fetchstructure($this->inbox, $email); + } + + public function fetchMessageBody($email, $section) { + return imap_fetchbody($this->inbox, $email, $section); + } + + public function headerInfo($email) { + return imap_headerinfo($this->inbox, $email); + } + + public function reply($sender, $response) { + $to = $sender->mailbox . '@' . $sender->host; + mail($to, 'your card has been created', 'card/board link.....'); + } +} \ No newline at end of file