added mail2deck files and update README.md

pull/7/head
a11exandru 2019-11-14 16:37:34 +02:00
rodzic 1865b649e3
commit c7f09d2d4a
3 zmienionych plików z 241 dodań i 0 usunięć

122
DeckClass.php 100644
Wyświetl plik

@ -0,0 +1,122 @@
<?php
define("NC_BOT", "");
define("NC_URL", "");
define("NC_PASSWORD", "");
class DeckClass {
protected function apiCall($request, $endpoint, $data){
set_time_limit(0);
$curl = curl_init();
$headers = [
"OCS-APIRequest: true"
];
if ($request !== '') {// adding attachments doesn't support Content-Type: application/json.
array_push($headers, "Content-Type: application/json");
$options = [
CURLOPT_USERPWD => 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);
}
}
}
?>

Wyświetl plik

@ -22,3 +22,21 @@ The email address is composed like:
A particulary example:
<code>any.str.ing.personalBoard@ncserver.domain</code>.
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", "<nextcloudServer>");
define("NC_BOT", "<deckBotUsername>");
define("NC_PASSWORD", "<deckBotPassword>");
```
- Customize the mail server from file **_newCard.php_**:
```
$inbox = imap_open("{<mailServer>}INBOX", "<username>", "<password>")
or die("can't connect:" . imap_last_error());
```

101
newCard.php 100644
Wyświetl plik

@ -0,0 +1,101 @@
<?php
ini_set('display_errors', '0');
require_once('DeckClass.php');
$inbox = imap_open("{compendium.newro.co/novalidate-cert}INBOX", "incoming", "iFd27heCwe8dL5")
or die("can't connect:" . imap_last_error());
$emails = imap_search($inbox, 'UNSEEN');
rsort($emails);
$structure = imap_fetchstructure($inbox, $emails[0]);
$attachments = 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') {
$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);
?>