Full implemantation

This commit is contained in:
2022-11-28 22:56:21 -03:00
parent 30ef4c6a35
commit c53eb4c7a6
55 changed files with 1505 additions and 1011 deletions

View File

@ -1,11 +1,12 @@
<?php
namespace ProVM\Common\Controller;
use ProVM\Common\Exception\Request\MissingArgument;
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;
use ProVM\Emails\Model\Attachment;
class Attachments
{
@ -13,47 +14,40 @@ class Attachments
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()
$attachments = array_map(function(Attachment $attachment) {
return $attachment->toArray();
},$service->getAll());
$output = [
'total' => count($attachments),
'attachments' => $attachments
];
}
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
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Jobs $jobsService): ResponseInterface
{
$body = $request->getBody()->getContents();
$json = json_decode($body);
if (!is_array($json)) {
$json = [$json];
$body = $request->getBody();
$json = \Safe\json_decode($body->getContents());
if (!isset($json->messages)) {
throw new MissingArgument('messages', 'array', 'message UIDs');
}
$output = ['input' => $json, 'attachments' => []];
foreach ($json as $attachment) {
$output['attachments'] []= $this->fileToArray($service->removeEncryption($attachment));
$output = [
'messages' => $json->messages,
'total' => count($json->messages),
'saved' => [
'attachments' => [],
'total' => 0
]
];
foreach ($json->messages as $message_id) {
if (!$jobsService->isPending($message_id)) {
continue;
}
if ($service->grab($message_id)) {
$job = $jobsService->find($message_id);
$jobsService->execute($job->getId());
$output['saved']['attachments'] []= $job->toArray();
$output['saved']['total'] ++;
}
}
return $this->withJson($response, $output);
}

View File

@ -0,0 +1,18 @@
<?php
namespace ProVM\Common\Controller;
use ProVM\Common\Implement\Controller\Json;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class Base
{
use Json;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
return $this->withJson($response, [
'version' => '1.0.0'
]);
}
}

View File

@ -1,141 +0,0 @@
<?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);
}
}

View File

@ -1,28 +0,0 @@
<?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()
]);
}
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace ProVM\Common\Controller;
use ProVM\Emails\Model\Job;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use ProVM\Common\Exception\Request\MissingArgument;
use ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service\Jobs as Service;
class Jobs
{
use Json;
public function schedule(ServerRequestInterface $request, ResponseInterface $response, Service $jobsService): ResponseInterface
{
$body = $request->getBody();
$json = \Safe\json_decode($body->getContents());
if (!isset($json->messages)) {
throw new MissingArgument('messages', 'array', 'messages ids');
}
$output = [
'messages' => $json->messages,
'total' => count($json->messages),
'scheduled' => 0
];
foreach ($json->messages as $message_id) {
if ($jobsService->schedule($message_id)) {
$output['scheduled'] ++;
}
}
return $this->withJson($response, $output);
}
public function pending(ServerRequestInterface $request, ResponseInterface $response, Service $jobsService): ResponseInterface
{
$pending = array_map(function(Job $job) {
return $job->toArray();
}, $jobsService->getPending());
$output = [
'total' => count($pending),
'pending' => $pending
];
return $this->withJson($response, $output);
}
}

View File

@ -1,83 +1,100 @@
<?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;
use Ddeboer\Imap\MailboxInterface;
use ProVM\Common\Exception\Request\MissingArgument;
use ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service\Mailboxes as Service;
use ProVM\Emails\Model\Mailbox;
class Mailboxes
{
use Json;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, \ProVM\Common\Service\Mailboxes $service, Mailbox $repository): ResponseInterface
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): 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 = array_values(array_map(function(MailboxInterface $mailbox) use ($service) {
$arr = ['name' => $mailbox->getName(), 'registered' => false];
if ($service->isRegistered($mailbox->getName())) {
$mb = $service->getLocalMailbox($mailbox->getName());
$arr['id'] = $mb->getId();
$arr['registered'] = true;
}
$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 $arr;
}, $service->getAll()));
$output = [
'total' => count($mailboxes),
'mailboxes' => $mailboxes
];
return $this->withJson($response, $output);
}
public function unregister(ServerRequestInterface $request, ResponseInterface $response, Mailbox $repository): ResponseInterface
public function registered(ServerRequestInterface $request, ResponseInterface $response, Service $service): 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) {
$mailboxes = array_map(function(Mailbox $mailbox) {
return $mailbox->toArray();
}, $data);
return $this->withJson($response, compact('mailboxes'));
}, $service->getRegistered());
$output = [
'total' => count($mailboxes),
'mailboxes' => $mailboxes
];
return $this->withJson($response, $output);
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Mailbox $repository, int $mailbox_id): ResponseInterface
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service, int $mailbox_id): ResponseInterface
{
$mailbox = $repository->fetchById($mailbox_id);
$mailbox = $service->getRepository()->fetchById($mailbox_id);
return $this->withJson($response, ['mailbox' => $mailbox->toArray()]);
}
public function register(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Messages $messagesService): ResponseInterface
{
$body = $request->getBody();
$json = \Safe\json_decode($body->getContents());
if (!isset($json->mailboxes)) {
throw new MissingArgument('mailboxes', 'array', 'mailboxes names');
}
$output = [
'mailboxes' => $json->mailboxes,
'total' => count($json->mailboxes),
'registered' => [
'total' => 0,
'mailboxes' => []
]
];
foreach ($json->mailboxes as $mailbox_name) {
$arr = [
'id' => '',
'name' => $mailbox_name,
'registered' => false
];
if ($service->register($mailbox_name)) {
$mailbox = $service->getLocalMailbox($mailbox_name);
$arr['id'] = $mailbox->getId();
$arr['registered'] = true;
$output['registered']['total'] ++;
$output['registered']['mailboxes'] []= $arr;
$messagesService->grab($mailbox_name);
}
}
return $this->withJson($response, $output, 201);
}
public function unregister(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$body = $request->getBody();
$json = \Safe\json_decode($body->getContents());
if (!isset($json->mailboxes)) {
throw new MissingArgument('mailboxes', 'array', 'mailboxes names');
}
$output = [
'mailboxes' => $json->mailboxes,
'total' => count($json->mailboxes),
'unregistered' => 0
];
foreach ($json->mailboxes as $mailbox_name) {
if ($service->unregister($mailbox_name)) {
$output['unregistered'] ++;
}
}
return $this->withJson($response, $output);
}
}

View File

@ -1,117 +1,73 @@
<?php
namespace ProVM\Common\Controller;
use Ddeboer\Imap\MessageInterface;
use ProVM\Common\Factory\Model;
use ProVM\Emails\Model\State\Mailbox;
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use ProVM\Common\Exception\Database\BlankResult;
use ProVM\Common\Exception\Request\MissingArgument;
use ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service\Mailboxes as Service;
use ProVM\Emails\Repository\Message;
use Safe\DateTimeImmutable;
use ProVM\Common\Service\Messages as Service;
use ProVM\Emails\Model\Message;
class Messages
{
use Json;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Message $repository): ResponseInterface
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service, int $mailbox_id): 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) {
$mailbox = $service->getMailboxes()->get($mailbox_id);
$messages = array_map(function(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')
}, $service->getAll($mailbox->getName()));
$output = [
'mailbox' => $mailbox->toArray(),
'total' => count($messages),
'messages' => $messages
];
$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 $this->withJson($response, $output);
}
public function valid(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Attachments $attachments, int $mailbox_id): ResponseInterface
{
$mailbox = $service->getMailboxes()->get($mailbox_id);
$messages = array_values(array_filter(array_map(function(Message $message) use ($service, $attachments) {
return $message->toArray();
}, $service->getValid($mailbox->getName())), function($message) {
return $message !== null;
}));
$output = [
'mailbox' => $mailbox->toArray(),
'total' => count($messages),
'messages' => $messages
];
return $this->withJson($response, $output);
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service, int $message_id): ResponseInterface
{
$message = $service->getRepository()->fetchById($message_id);
return $this->withJson($response, ['message' => $message->toArray()]);
}
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Attachments $attachmentsService): ResponseInterface
{
$body = $request->getBody();
$json = \Safe\json_decode($body->getContents());
if (!isset($json->mailboxes)) {
throw new MissingArgument('mailboxes', 'array', 'mailboxes names');
}
return false;
$output = [
'mailboxes' => $json->mailboxes,
'messages' => [],
'message_count' => 0
];
foreach ($json->mailboxes as $mailbox_name) {
$messages = $service->grab($mailbox_name);
foreach ($messages as $message) {
if ($message->hasValidAttachments()) {
$attachmentsService->create($message);
}
}
$output['messages'] = array_merge($output['messages'], $messages);
$output['message_count'] += count($messages);
}
return $this->withJson($response, $output);
}
}