add mail attachments to deck & bug fixes

pull/16/head
Alex Puiu 2022-02-03 15:05:17 +02:00
rodzic 9b5fe32d5d
commit db0e9553d1
6 zmienionych plików z 83 dodań i 130 usunięć

1
.gitignore vendored 100644
Wyświetl plik

@ -0,0 +1 @@
config.php

11
config.example.php 100644
Wyświetl plik

@ -0,0 +1,11 @@
<?php
define("NC_SERVER", "localhost"); // server.domain
define("NC_USER", "deckbot");
define("NC_PASSWORD", "****");
define("MAIL_SERVER", "localhost"); // server.domain
define("MAIL_SERVER_FLAGS", "/novalidate-cert"); // flags needed to connect to server. Refer to https://www.php.net/manual/en/function.imap-open.php for a list of valid flags.
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
?>

Wyświetl plik

@ -1,10 +0,0 @@
<?php
define("NC_SERVER", ""); // server.domain
define("NC_USER", "deckbot");
define("NC_PASSWORD", "");
define("MAIL_SERVER", ""); // server.domain
define("MAIL_SERVER_FLAGS", "/no-validate-cert"); // flags needed to connect to server. Refer to https://www.php.net/manual/en/function.imap-open.php for a list of valid flags.
define("MAIL_USER", "incoming");
define("MAIL_PASSWORD", "");
define("DECODE_SPECIAL_CHARACTERS", true); //requires mbstring, if false special characters (like öäüß) won't be displayed correctly
?>

Wyświetl plik

@ -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);
?>

Wyświetl plik

@ -1,116 +1,87 @@
<?php
class DeckClass {
protected function apiCall($request, $endpoint, $data){
private function apiCall($request, $endpoint, $data = null, $attachment = false){
$curl = curl_init();
$headers = [
"OCS-APIRequest: true"
];
// set CURLOPTs commmon to all HTTP methods
$options = [
CURLOPT_USERPWD => 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);
}
}
}