nextcloud-mail2deck/lib/DeckClass.php

237 wiersze
8.8 KiB
PHP
Czysty Zwykły widok Historia

<?php
namespace Mail2Deck;
2022-05-26 21:07:44 +00:00
class DeckClass {
private $responseCode;
private function apiCall($request, $endpoint, $data = null, $attachment = false, $userapi = false){
$curl = curl_init();
2022-05-26 21:07:44 +00:00
if($data && !$attachment) {
$endpoint .= '?' . http_build_query($data);
}
curl_setopt_array($curl, array(
CURLOPT_URL => $endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPGET => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $request,
CURLOPT_HTTPHEADER => array(
2025-01-27 14:17:56 +00:00
'Authorization: Basic ' . base64_encode(NC_ADMIN_USER . ':' . NC_ADMIN_PASSWORD),
'OCS-APIRequest: true',
),
));
if ($request === 'POST') {
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, array_merge(
array(
'Accept: application/json',
'OCS-APIRequest: true',
'Content-Type: application/json',
2025-01-27 14:17:56 +00:00
'Authorization: Basic ' . base64_encode(NC_ADMIN_USER . ':' . NC_ADMIN_PASSWORD),
)
));
}
$response = curl_exec($curl);
$err = curl_error($curl);
if($userapi) return simplexml_load_string($response);
$this->responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
2022-05-26 21:07:44 +00:00
if($err) echo "cURL Error #:" . $err;
return json_decode($response);
}
2022-05-26 21:07:44 +00:00
public function getParameters($params, $boardFromMail = null) {// get the board and the stack
if(!$boardFromMail) // if board is not set within the email address, look for board into email subject
if(preg_match('/b-"([^"]+)"/', $params, $m) || preg_match("/b-'([^']+)'/", $params, $m)) {
$boardFromMail = $m[1];
$params = str_replace($m[0], '', $params);
}else{
$boardFromMail = NC_DEFAULT_BOARD;
}
2022-05-26 21:07:44 +00:00
if(preg_match('/s-"([^"]+)"/', $params, $m) || preg_match("/s-'([^']+)'/", $params, $m)) {
$stackFromMail = $m[1];
$params = str_replace($m[0], '', $params);
}
2022-05-26 21:07:44 +00:00
if(preg_match('/u-"([^"]+)"/', $params, $m) || preg_match("/u-'([^']+)'/", $params, $m)) {
$userFromMail = $m[1];
$params = str_replace($m[0], '', $params);
}
if(preg_match('/d-"([^"]+)"/', $params, $m) || preg_match("/d-'([^']+)'/", $params, $m)) {
$duedateFromMail = $m[1];
$params = str_replace($m[0], '', $params);
}
$boards = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards");
2022-04-13 10:31:54 +00:00
$boardId = $boardName = null;
2022-05-26 21:07:44 +00:00
foreach($boards as $board) {
if(strtolower($board->title) == strtolower($boardFromMail)) {
if(!$this->checkBotPermissions($board)) {
2022-02-08 11:34:16 +00:00
return false;
}
$boardId = $board->id;
$boardName = $board->title;
break;
}
}
2022-05-26 21:07:44 +00:00
if($boardId) {
$stacks = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/$boardId/stacks");
2025-01-27 11:41:42 +00:00
if (!empty($stacks)) {
usort($stacks, function ($a, $b) {
return $a->order <=> $b->order;
});
$stackId = $stacks[0]->id;
}
2022-02-08 11:34:16 +00:00
} else {
return false;
}
2022-05-19 08:47:30 +00:00
$boardStack = new \stdClass();
$boardStack->board = $boardId;
$boardStack->stack = $stackId;
$boardStack->newTitle = $params;
$boardStack->boardTitle = $boardName;
2022-05-26 21:01:58 +00:00
$boardStack->userId = strtolower($userFromMail);
$boardStack->dueDate = $duedateFromMail;
return $boardStack;
}
//Add a new card
2022-05-26 21:07:44 +00:00
public function addCard($data, $user, $board = null) {
$params = $this->getParameters($data->title, $board);
2022-05-26 21:07:44 +00:00
if($params) {
2022-02-08 11:34:16 +00:00
$data->title = $params->newTitle;
$data->duedate = $params->dueDate;
2022-02-08 11:34:16 +00:00
$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;
2022-05-26 21:07:44 +00:00
if($this->responseCode == 200) {
if(ASSIGN_SENDER || $user) $this->assignUser($card, $user);
2022-05-26 21:07:44 +00:00
if($data->attachments) $this->addAttachments($card, $data->attachments);
2022-02-08 11:34:16 +00:00
$card->boardTitle = $params->boardTitle;
}
else {
2022-02-08 11:34:16 +00:00
return false;
}
return $card;
}
return false;
}
//Add a new attachment
2022-05-26 21:07:44 +00:00
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(
2022-05-19 08:47:30 +00:00
'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);
}
}
//Assign a user to the card
public function assignUser($card, $mailUser)
{
2025-01-27 14:17:56 +00:00
$adminUser = urlencode(NC_ADMIN_USER);
$adminPassword = urlencode(NC_ADMIN_PASSWORD);
2025-01-27 14:17:56 +00:00
$url = "https://{$adminUser}:{$adminPassword}@" . NC_HOST;
$allUsers= $this->apiCall("GET",$url."/ocs/v1.php/cloud/users", null, false, true); //nextcloud user list
foreach ($allUsers->data->users->element as $userId) {
$userDetails = $this->apiCall("GET",$url."/ocs/v1.php/cloud/users/{$userId}",null, false, true);//search in the nextcloud user list
if (isset($userDetails->data->email[0]) && $userDetails->data->email[0] == $mailUser) {
$this->apiCall("PUT", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$card->board}/stacks/{$card->stack}/cards/{$card->id}/assignUser", ['userId' => (string)$userId]);
break;
}
}
}
2022-02-08 11:34:16 +00:00
2022-05-26 21:07:44 +00:00
private function checkBotPermissions($board) {
foreach($board->acl as $acl)
if($acl->participant->uid == NC_USER && $acl->permissionEdit)
2022-02-08 11:34:16 +00:00
return true;
return false;
}
//Identify the card by email subject
public function findCardBySubject($subject) {
$boards = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards");
$cleanSubject = preg_replace("/\s[b|s|u]-'.*?'/", '', $subject);
foreach ($boards as $board) {
$stacks = $this->apiCall("GET", NC_SERVER . "/index.php/apps/deck/api/v1.0/boards/{$board->id}/stacks");
foreach ($stacks as $stack) {
foreach ($stack->cards as $card) {
if (strtolower(trim($card->title)) == strtolower(trim($cleanSubject))) {
return [
'cardId' => $card->id,
'boardId' => $board->id
];
}
}
}
}
return null;
}
//Add new comment
public function addCommentToCard($result, $comment) {
$cardId = $result['cardId'];
$boardId = $result['boardId'];
$url = NC_SERVER . "/ocs/v2.php/apps/deck/api/v1.0/cards/{$cardId}/comments";
$data = [
'comment' => $comment,
'actorType' => 'users',
'message' => $comment,
'verb' => 'create',
];
$response = $this->apiCall("POST", $url, $data);
if ($response) {
echo "Comment added successfully to card ID: $cardId on board ID: $boardId.";
return true;
} else {
echo "Failed to add comment to card ID: $cardId.";
return false;
}
}
//Extract the initial message from all forwarded content
public function extractForwardedContent($body) {
$separator = "---------- Forwarded message ---------";
$position = strpos($body, $separator);
if ($position !== false) {
$body = substr($body, 0, $position);
}
$body = trim($body);
return $body;
}
//Extract the initial subject from the description
public function findOriginalSubject($message) {
preg_match_all('/\bSubject:\s*([^\n\r]*)/i', $message, $matches);
if (!empty($matches[1])) {
$subject = trim($matches[1][0]);
$subject = preg_replace('/\s+To:.*$/', '', $subject);
return $subject;
}
return null;
}
}