API
This commit is contained in:
60
api/common/Controller/Attachments.php
Normal file
60
api/common/Controller/Attachments.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use function Safe\json_decode;
|
||||
use ProVM\Common\Implement\Controller\Json;
|
||||
use ProVM\Common\Service\Attachments as Service;
|
||||
|
||||
class Attachments
|
||||
{
|
||||
use Json;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||
{
|
||||
$attachments = array_map(function($attachment) {
|
||||
$attachment['original_attachment'] = $attachment['original_attachment']->getRealPath();
|
||||
if (isset($attachment['decrypted_attachment']) and $attachment['decrypted_attachment'] !== false) {
|
||||
$attachment['decrypted_attachment'] = $attachment['decrypted_attachment']->getRealPath();
|
||||
}
|
||||
return $attachment;
|
||||
}, $service->fetchFullAttachments());
|
||||
return $this->withJson($response, compact('attachments'));
|
||||
}
|
||||
protected function fileToArray(\SplFileInfo $info): array
|
||||
{
|
||||
return [
|
||||
'filename' => $info->getFilename(),
|
||||
'path' => $info->getPath(),
|
||||
'full_name' => $info->getRealPath(),
|
||||
'extension' => $info->getExtension()
|
||||
];
|
||||
}
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody()->getContents();
|
||||
$json = json_decode($body);
|
||||
if (!is_array($json)) {
|
||||
$json = [$json];
|
||||
}
|
||||
$output = ['input' => $json, 'attachments' => []];
|
||||
foreach ($json as $attachment) {
|
||||
$output['attachments'] []= $this->fileToArray($service->getAttachment($json->attachment));
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function decrypt(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody()->getContents();
|
||||
$json = json_decode($body);
|
||||
if (!is_array($json)) {
|
||||
$json = [$json];
|
||||
}
|
||||
$output = ['input' => $json, 'attachments' => []];
|
||||
foreach ($json as $attachment) {
|
||||
$output['attachments'] []= $this->fileToArray($service->removeEncryption($attachment));
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
141
api/common/Controller/Emails.php
Normal file
141
api/common/Controller/Emails.php
Normal file
@ -0,0 +1,141 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Controller;
|
||||
|
||||
use Ddeboer\Imap\MailboxInterface;
|
||||
use ProVM\Common\Implement\Controller\Json;
|
||||
use ProVM\Emails\Repository\Mailbox;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use ProVM\Common\Service\Emails as Service;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Safe\Exceptions\FilesystemException;
|
||||
|
||||
class Emails
|
||||
{
|
||||
use Json;
|
||||
|
||||
public function __construct(LoggerInterface $logger)
|
||||
{
|
||||
$this->setLogger($logger);
|
||||
}
|
||||
|
||||
protected LoggerInterface $logger;
|
||||
public function getLogger(): LoggerInterface
|
||||
{
|
||||
return $this->logger;
|
||||
}
|
||||
public function setLogger(LoggerInterface $logger): Emails
|
||||
{
|
||||
$this->logger = $logger;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function mailboxes(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||
{
|
||||
$mailboxes = array_map(function(MailboxInterface $mailbox) {
|
||||
return ['name' => $mailbox->getName(), 'message_count' => $mailbox->count()];
|
||||
}, array_values($service->getMailboxes()->getAll()));
|
||||
return $this->withJson($response, compact('mailboxes'));
|
||||
}
|
||||
public function messages(ServerRequestInterface $request, ResponseInterface $response, Service $service, Mailbox $repository): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody()->getContents();
|
||||
$json = \Safe\json_decode($body);
|
||||
$mailbox = $repository->fetchById($json->mailbox);
|
||||
$remote_mailbox = $service->getMailboxes()->get($mailbox->getName());
|
||||
$messages = $service->getMessages($remote_mailbox, $json->start ?? 0, $json->amount ?? null);
|
||||
$mails = [];
|
||||
foreach ($messages as $message) {
|
||||
$mails []= [
|
||||
'position' => $message->getPosition(),
|
||||
'uid' => $message->getUID(),
|
||||
'subject' => $message->getSubject(),
|
||||
'date' => $message->getDateTime()->format('Y-m-d H:i:s'),
|
||||
'from' => $message->getFrom(),
|
||||
'has_attachments' => $message->hasAttachments(),
|
||||
'valid_attachments' => $message->hasValidAttachments(),
|
||||
'downloaded_attachments' => $message->hasDownloadedAttachments()
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, [
|
||||
'input' => $json,
|
||||
'total' => $mailbox->count(),
|
||||
'messages' => $mails
|
||||
]);
|
||||
}
|
||||
public function withAttachments(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody()->getContents();
|
||||
$json = \Safe\json_decode($body);
|
||||
$mailbox = $service->getMailboxes()->get($json->mailbox);
|
||||
$messages = $service->getValidMessages($mailbox, $json->start ?? 0, $json->amount ?? null);
|
||||
$mails = [];
|
||||
foreach ($messages as $message) {
|
||||
$mails []= [
|
||||
'position' => $message->getPosition(),
|
||||
'uid' => $message->getUID(),
|
||||
'subject' => $message->getSubject(),
|
||||
'date' => $message->getDateTime()->format('Y-m-d H:i:s'),
|
||||
'from' => $message->getFrom(),
|
||||
'has_attachments' => $message->hasAttachments(),
|
||||
'valid_attachments' => $message->hasValidAttachments(),
|
||||
'downloaded_attachments' => $message->hasDownloadedAttachments()
|
||||
];
|
||||
}
|
||||
return $this->withJson($response, [
|
||||
'input' => $json,
|
||||
'total' => $service->getMailboxes()->countValid($mailbox),
|
||||
'messages' => $mails
|
||||
]);
|
||||
}
|
||||
public function attachments(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody()->getContents();
|
||||
$json = \Safe\json_decode($body);
|
||||
$mailbox = $service->getMailboxes()->get($json->mailbox);
|
||||
$messages = $service->getMessages($mailbox, $json->start ?? 0, $json->amount ?? null);
|
||||
$cnt = 0;
|
||||
$attachments = [];
|
||||
foreach ($messages as $message) {
|
||||
$attachments = array_merge($attachments, $service->saveAttachments($message, $json->extension ?? null));
|
||||
$cnt ++;
|
||||
}
|
||||
return $this->withJson($response, [
|
||||
'input' => $json,
|
||||
'messages_count' => $cnt,
|
||||
'attachments_count' => count($attachments),
|
||||
'attachments' => $attachments
|
||||
]);
|
||||
}
|
||||
public function attachment(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody()->getContents();
|
||||
$json = \Safe\json_decode($body);
|
||||
$mailbox = $service->getMailbox($json->mailbox);
|
||||
$cnt = 0;
|
||||
$attachments = [];
|
||||
$errors = [];
|
||||
foreach ($json->messages as $uid) {
|
||||
$message = $service->getMessage($mailbox, $uid);
|
||||
try {
|
||||
$attachments = array_merge($attachments, $service->saveAttachments($message, $json->extension ?? null));
|
||||
} catch (FilesystemException $e) {
|
||||
$errors []= [
|
||||
'message' => $uid,
|
||||
'error' => $e->getMessage()
|
||||
];
|
||||
}
|
||||
$cnt ++;
|
||||
}
|
||||
$output = [
|
||||
'input' => $json,
|
||||
'messages_count' => $cnt,
|
||||
'attachments_count' => count($attachments),
|
||||
'attachments' => $attachments
|
||||
];
|
||||
if (count($errors) > 0) {
|
||||
$output['errors'] = $errors;
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
28
api/common/Controller/Install.php
Normal file
28
api/common/Controller/Install.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Controller;
|
||||
|
||||
use ProVM\Common\Implement\Controller\Json;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Exception;
|
||||
use ProVM\Common\Service\Install as Service;
|
||||
|
||||
class Install
|
||||
{
|
||||
use Json;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||
{
|
||||
try {
|
||||
$service->run();
|
||||
return $this->withJson($response, [
|
||||
'message' => 'Install finished'
|
||||
]);
|
||||
} catch (Exception $e) {
|
||||
return $this->withJson($response, [
|
||||
'message' => 'Install with error',
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
83
api/common/Controller/Mailboxes.php
Normal file
83
api/common/Controller/Mailboxes.php
Normal file
@ -0,0 +1,83 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Controller;
|
||||
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use ProVM\Common\Implement\Controller\Json;
|
||||
use ProVM\Emails\Repository\Mailbox;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class Mailboxes
|
||||
{
|
||||
use Json;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, \ProVM\Common\Service\Mailboxes $service, Mailbox $repository): ResponseInterface
|
||||
{
|
||||
$source_mailboxes = $service->getAll();
|
||||
$mailboxes = [];
|
||||
foreach ($source_mailboxes as $mailbox) {
|
||||
$m = [
|
||||
'name' => $mailbox->getName(),
|
||||
'registered' => false
|
||||
];
|
||||
try {
|
||||
$s = $repository->fetchByName($mailbox->getName());
|
||||
$m['registered'] = true;
|
||||
$m['id'] = $s->getId();
|
||||
} catch (BlankResult $e) {
|
||||
}
|
||||
$mailboxes []= $m;
|
||||
}
|
||||
return $this->withJson($response, compact('mailboxes'));
|
||||
}
|
||||
public function register(ServerRequestInterface $request, ResponseInterface $response, \ProVM\Common\Service\Mailboxes $service, Mailbox $repository): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = \Safe\json_decode($body->getContents());
|
||||
$output = ['input' => $json, 'mailboxes' => []];
|
||||
foreach ($json->mailboxes as $mailbox) {
|
||||
try {
|
||||
$model = $repository->create([
|
||||
'name' => $mailbox
|
||||
]);
|
||||
$output['mailboxes'] []= ['name' => $mailbox, 'created' => true, 'id' => $model->getId()];
|
||||
} catch (\PDOException $e) {
|
||||
$output['mailboxes'] []= ['name' => $mailbox, 'created' => false];
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function unregister(ServerRequestInterface $request, ResponseInterface $response, Mailbox $repository): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = \Safe\json_decode($body->getContents());
|
||||
$output = ['input' => $json, 'mailboxes' => []];
|
||||
foreach ($json->mailboxes as $id) {
|
||||
try {
|
||||
$mailbox = $repository->fetchById($id);
|
||||
try {
|
||||
$repository->delete($mailbox);
|
||||
$output['mailboxes'] []= ['name' => $mailbox->getName(), 'id' => $id, 'removed' => true];
|
||||
} catch (\PDOException $e) {
|
||||
$output['mailboxes'] []= ['name' => $mailbox->getName(), 'id' => $id, 'removed' => false];
|
||||
}
|
||||
} catch (\PDOException | BlankResult $e) {
|
||||
$output['mailboxes'] []= ['id' => $id, 'removed' => false];
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function registered(ServerRequestInterface $request, ResponseInterface $response, Mailbox $repository): ResponseInterface
|
||||
{
|
||||
$data = $repository->fetchAll();
|
||||
$mailboxes = array_map(function(\ProVM\Emails\Model\Mailbox $mailbox) {
|
||||
return $mailbox->toArray();
|
||||
}, $data);
|
||||
return $this->withJson($response, compact('mailboxes'));
|
||||
}
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, Mailbox $repository, int $mailbox_id): ResponseInterface
|
||||
{
|
||||
$mailbox = $repository->fetchById($mailbox_id);
|
||||
return $this->withJson($response, ['mailbox' => $mailbox->toArray()]);
|
||||
}
|
||||
}
|
117
api/common/Controller/Messages.php
Normal file
117
api/common/Controller/Messages.php
Normal file
@ -0,0 +1,117 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Controller;
|
||||
|
||||
use Ddeboer\Imap\MessageInterface;
|
||||
use ProVM\Common\Factory\Model;
|
||||
use ProVM\Emails\Model\State\Mailbox;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use ProVM\Common\Implement\Controller\Json;
|
||||
use ProVM\Common\Service\Mailboxes as Service;
|
||||
use ProVM\Emails\Repository\Message;
|
||||
use Safe\DateTimeImmutable;
|
||||
|
||||
class Messages
|
||||
{
|
||||
use Json;
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Message $repository): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = \Safe\json_decode($body->getContents());
|
||||
$messages = [];
|
||||
foreach ($json->mailboxes as $mailbox_id) {
|
||||
$messages = array_merge($messages, $repository->fetchByMailbox($mailbox_id) ?? []);
|
||||
}
|
||||
$messages = array_map(function(\ProVM\Emails\Model\Message $message) {
|
||||
return $message->toArray();
|
||||
}, $messages);
|
||||
return $this->withJson($response, ['messages' => $messages, 'total' => count($messages)]);
|
||||
}
|
||||
public function valid(ServerRequestInterface $request, ResponseInterface $response, Message $repository): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = \Safe\json_decode($body->getContents());
|
||||
$messages = [];
|
||||
foreach ($json->mailboxes as $mailbox_id) {
|
||||
try {
|
||||
$messages = array_merge($messages, $repository->fetchByMailbox($mailbox_id) ?? []);
|
||||
} catch (BlankResult $e) {
|
||||
}
|
||||
}
|
||||
$messages = array_values(array_map(function(\ProVM\Emails\Model\Message $message) {
|
||||
return $message->toArray();
|
||||
}, array_filter($messages, function(\ProVM\Emails\Model\Message $message) {
|
||||
return $message->hasValidAttachments();
|
||||
})));
|
||||
return $this->withJson($response, ['messages' => $messages, 'total' => count($messages)]);
|
||||
}
|
||||
public function grab(ServerRequestInterface $request, ResponseInterface $response, Model $factory, Service $service): ResponseInterface
|
||||
{
|
||||
$body = $request->getBody();
|
||||
$json = \Safe\json_decode($body->getContents());
|
||||
$message_count = 0;
|
||||
foreach ($json->mailboxes as $mailbox_name) {
|
||||
$message_count += $this->grabFromMailbox($factory, $service, $mailbox_name);
|
||||
}
|
||||
return $this->withJson($response, compact('message_count'));
|
||||
}
|
||||
|
||||
protected function grabFromMailbox(Model $factory, Service $service, string $mailbox_name): int
|
||||
{
|
||||
$mailbox = $factory->find(\ProVM\Emails\Model\Mailbox::class)->fetchByName($mailbox_name);
|
||||
$stored = array_reduce($mailbox->getStates(), function($count, Mailbox $state) {
|
||||
return $count + $state->getCount();
|
||||
}) ?? 0;
|
||||
$remote_mailbox = $service->get($mailbox->getName());
|
||||
$total = $remote_mailbox->count();
|
||||
if ($stored >= $total) {
|
||||
return 0;
|
||||
}
|
||||
$added = 0;
|
||||
$uids = [];
|
||||
$amount = $total - $stored;
|
||||
$messages = $service->getMessages($remote_mailbox, $stored, $amount);
|
||||
foreach ($messages as $j => $m) {
|
||||
if ($this->addMessage($factory->find(\ProVM\Emails\Model\Message::class), $service, $mailbox, $m, $j + 1)) {
|
||||
$uids []= $m->getId();
|
||||
$added ++;
|
||||
}
|
||||
}
|
||||
if ($added > 0 ) {
|
||||
$data = [
|
||||
'mailbox_id' => $mailbox->getId(),
|
||||
'date_time' => (new DateTimeImmutable('now'))->format('Y-m-d H:i:s'),
|
||||
'count' => $added,
|
||||
'uids' => serialize($uids)
|
||||
];
|
||||
$state = $factory->find(\ProVM\Emails\Model\State\Mailbox::class)->create($data);
|
||||
$factory->find(\ProVM\Emails\Model\State\Mailbox::class)->save($state);
|
||||
}
|
||||
return $added;
|
||||
}
|
||||
protected function addMessage(Message $repository, Service $service, \ProVM\Emails\Model\Mailbox $mailbox, MessageInterface $remote_message, int $position): bool
|
||||
{
|
||||
$data = [
|
||||
'mailbox_id' => $mailbox->getId(),
|
||||
'position' => $position,
|
||||
'uid' => $remote_message->getId(),
|
||||
'subject' => $remote_message->getSubject() ?? '',
|
||||
'from' => $remote_message->getFrom()->getFullAddress(),
|
||||
'date_time' => $remote_message->getDate()->format('Y-m-d H:i:s')
|
||||
];
|
||||
$message = $repository->create($data);
|
||||
if ($message->getId() === 0) {
|
||||
if ($remote_message->hasAttachments()) {
|
||||
$message->doesHaveAttachments();
|
||||
}
|
||||
if ($service->validAttachments($remote_message)) {
|
||||
$message->doesHaveValidAttachments();
|
||||
}
|
||||
$repository->save($message);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user