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);
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace ProVM\Common\Exception\Attachment;
use Ddeboer\Imap\MessageInterface;
use Exception;
class NotFound extends Exception
{
public function __construct(MessageInterface $message, string $filename, ?Throwable $previous = null)
{
$message = "Attachment {$filename} not found in {$message->getId()}";
$code = 120;
parent::__construct($message, $code, $previous);
}
}

View File

@ -1,14 +1,15 @@
<?php
namespace ProVM\Common\Exception;
use Ddeboer\Imap\MailboxInterface;
use Exception;
class EmptyMailbox extends Exception
{
public function __construct(?Throwable $previous = null)
public function __construct(MailboxInterface $mailbox, ?Throwable $previous = null)
{
$message = "No mails found";
$code = 102;
$message = "No mails found in {$mailbox->getName()}";
$code = 101;
parent::__construct($message, $code, $previous);
}
}

View File

@ -5,9 +5,9 @@ use Exception;
class Invalid extends Exception
{
public function __construct(?Throwable $previous = null)
public function __construct(string $mailbox_name, ?Throwable $previous = null)
{
$message = "Mailbox not found";
$message = "Mailbox {$mailbox_name} not found";
$code = 100;
parent::__construct($message, $code, $previous);
}

View File

@ -0,0 +1,15 @@
<?php
namespace ProVM\Common\Exception\Mailbox;
use Exception;
use ProVM\Emails\Model\Mailbox;
class Stateless extends Exception
{
public function __construct(Mailbox $mailbox, ?Throwable $previous = null)
{
$message = "Mailbox {$mailbox->getName()} has not loaded any emails.";
$code = 102;
parent::__construct($message, $code, $previous);
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace ProVM\Common\Exception\Message;
use Ddeboer\Imap\MessageInterface;
use Exception;
class NoAttachments extends Exception
{
public function __construct(MessageInterface $message, ?Throwable $previous = null)
{
$message = "{$message->getSubject()} has no attachments";
$code = 110;
parent::__construct($message, $code, $previous);
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace ProVM\Common\Exception\Request;
use Exception;
class MissingArgument extends Exception
{
public function __construct(string $argument_name, string $argument_type, string $argument_description, ?Throwable $previous = null)
{
$message = "Missing argument {$argument_name} [{$argument_type}] | {$argument_description}";
$code = 10;
parent::__construct($message, $code, $previous);
}
}

View File

@ -2,6 +2,7 @@
namespace ProVM\Common\Implement;
use PDO;
use PDOException;
use ProVM\Common\Exception\Database\BlankResult;
use Psr\Log\LoggerInterface;
use ProVM\Common\Define\Model as ModelInterface;
@ -64,10 +65,10 @@ abstract class Repository
$old_values = $this->valuesForUpdate($old);
$columns = [];
$values = [];
foreach ($this->fieldsForUpdate() as $column => $method) {
if (isset($model_values[$column]) and $old_values[$column] !== $model_values[$column]) {
$columns []= "`{$column}`";
$values []= $model_values[$column];
foreach ($this->fieldsForUpdate() as $i => $column) {
if (isset($model_values[$i]) and $old_values[$i] !== $model_values[$i]) {
$columns []= "`{$column}` = ?";
$values []= $model_values[$i];
}
}
if (count($columns) === 0) {
@ -101,8 +102,9 @@ abstract class Repository
} catch (BlankResult $e) {
$this->insert($model);
$model->setId($this->getConnection()->lastInsertId());
} catch (\Error | \Exception $e) {
} catch(PDOException $e) {
$this->getLogger()->error($e);
throw $e;
}
}
abstract public function load(array $row): ModelInterface;

View File

@ -63,6 +63,7 @@ class Auth
}
}
}
$this->getLogger()->debug(sha1($this->getAPIKey()));
$response = $this->getResponseFactory()->createResponse(401);
$response->getBody()->write(\Safe\json_encode(['error' => 401, 'message' => 'Incorrect token']));
return $response

View File

@ -0,0 +1,58 @@
<?php
namespace ProVM\Common\Middleware;
use ProVM\Common\Exception\Database\BlankResult;
use ProVM\Common\Exception\Mailbox\Stateless;
use ProVM\Common\Exception\Request\MissingArgument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Safe\Exceptions\JsonException;
class CustomExceptions
{
public function __construct(ResponseFactoryInterface $factory, LoggerInterface $logger)
{
$this->setResponseFactory($factory)
->setLogger($logger);
}
protected ResponseFactoryInterface $factory;
protected LoggerInterface $logger;
public function getResponseFactory(): ResponseFactoryInterface
{
return $this->factory;
}
public function getLogger(): LoggerInterface
{
return $this->logger;
}
public function setResponseFactory(ResponseFactoryInterface $factory): CustomExceptions
{
$this->factory = $factory;
return $this;
}
public function setLogger(LoggerInterface $logger): CustomExceptions
{
$this->logger = $logger;
return $this;
}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
return $handler->handle($request);
} catch (BlankResult $e) {
$this->getLogger()->error($e);
return $this->getResponseFactory()->createResponse(204);
} catch (JsonException $e) {
$this->getLogger()->error($e);
return $this->getResponseFactory()->createResponse(415, 'Only JSON Media Type is supported for this request');
} catch (MissingArgument $e) {
$this->getLogger()->error($e);
return $this->getResponseFactory()->createResponse(400, $e->getMessage());
}
}
}

View File

@ -3,37 +3,65 @@ namespace ProVM\Common\Service;
use Ddeboer\Imap\Message\AttachmentInterface;
use Ddeboer\Imap\MessageInterface;
use ProVM\Emails\Model\Attachment;
use ProVM\Common\Exception\Message\NoAttachments;
use ProVM\Emails\Model\Message;
use ProVM\Emails\Repository\Attachment;
use Psr\Log\LoggerInterface;
use Safe\Exceptions\FilesystemException;
class Attachments
class Attachments extends Base
{
public function __construct(Decrypt $decrypt, \ProVM\Emails\Repository\Attachment $repository, string $attachments_folder)
public function __construct(Messages $messages, Attachment $repository, Remote\Attachments $remoteService,
Decrypt $decrypt, string $attachments_folder, LoggerInterface $logger)
{
$this->setDecrypt($decrypt);
$this->setAttachmentsFolder($attachments_folder);
$this->setMessages($messages)
->setRepository($repository)
->setRemoteService($remoteService)
->setDecrypt($decrypt)
->setFolder($attachments_folder)
->setLogger($logger);
}
protected string $attachments_folder;
protected Messages $messages;
protected Attachment $repository;
protected Remote\Attachments $remoteService;
protected Decrypt $decrypt;
protected \ProVM\Emails\Repository\Attachment $repository;
protected string $folder;
public function getAttachmentsFolder(): string
public function getMessages(): Messages
{
return $this->attachments_folder;
return $this->messages;
}
public function getRepository(): Attachment
{
return $this->repository;
}
public function getRemoteService(): Remote\Attachments
{
return $this->remoteService;
}
public function getDecrypt(): Decrypt
{
return $this->decrypt;
}
public function getRepository(): \ProVM\Emails\Repository\Attachment
public function getFolder(): string
{
return $this->repository;
return $this->folder;
}
public function setAttachmentsFolder(string $folder): Attachments
public function setMessages(Messages $messages): Attachments
{
$this->attachments_folder = $folder;
$this->messages = $messages;
return $this;
}
public function setRepository(Attachment $repository): Attachments
{
$this->repository = $repository;
return $this;
}
public function setRemoteService(Remote\Attachments $service): Attachments
{
$this->remoteService = $service;
return $this;
}
public function setDecrypt(Decrypt $decrypt): Attachments
@ -41,119 +69,164 @@ class Attachments
$this->decrypt = $decrypt;
return $this;
}
public function setRepository(\ProVM\Emails\Repository\Attachment $repository): Attachments
public function setFolder(string $folder): Attachments
{
$this->repository = $repository;
$this->folder = $folder;
return $this;
}
public function validateAttachment(AttachmentInterface $remote_attachment): bool
public function getLocalAttachment(Message $message, string $relative_filename): \ProVM\Emails\Model\Attachment
{
return str_contains($remote_attachment->getFilename(), '.pdf');
return $this->getRepository()->fetchByMessageAndFilename($message->getId(), $relative_filename);
}
public function getRemoteAttachment(Message $message, string $relative_filename): AttachmentInterface
{
$remote_message = $this->getMessages()->getRemoteMessage($message->getUID());
return $this->getRemoteService()->get($remote_message, $relative_filename);
}
public function buildAttachment(Message $message, AttachmentInterface $remote_attachment): Attachment
public function getAll(): array
{
return $this->getRepository()->fetchAll();
}
public function create(int $message_id): array
{
$message = $this->getMessages()->getRepository()->fetchById($message_id);
$remote_message = $this->getMessages()->getRemoteMessage($message->getUID());
if (!$remote_message->hasAttachments()) {
throw new NoAttachments($remote_message);
}
$attachments = [];
foreach ($remote_message->getAttachments() as $attachment) {
if (!$this->getMessages()->validateAttachment($attachment)) {
continue;
}
if ($this->save($message, $attachment, false)) {
$attachments []= $attachment->getFilename();
}
}
return $attachments;
}
public function grab(int $message_id): array
{
$message = $this->getMessages()->getRepository()->fetchById($message_id);
$remote_message = $this->getMessages()->getRemoteMessage($message->getUID());
if (!$remote_message->hasAttachments()) {
throw new NoAttachments($remote_message);
}
$attachments = [];
foreach ($remote_message->getAttachments() as $attachment) {
if (!$this->getMessages()->validateAttachment($attachment)) {
continue;
}
if ($this->save($message, $attachment)) {
$attachments []= $attachment->getFilename();
}
}
return $attachments;
}
public function save(Message $message, AttachmentInterface $remote_attachment, bool $upload = true): bool
{
$data = [
'message_id' => $message->getId(),
'filename' => $this->buildFilename($message, $remote_attachment)
'filename' => $remote_attachment->getFilename()
];
return $this->getRepository()->create($data);
}
try {
$attachment = $this->getRepository()->create($data);
$this->getRepository()->save($attachment);
if ($upload and $this->upload($attachment, $remote_attachment)) {
$attachment->itIsDownloaded();
$message->doesHaveDownloadedAttachments();
$this->getMessages()->getRepository()->save($message);
public function exists(Attachment $attachment): bool
{
$filename = $this->buildFilename($attachment);
return file_exists($filename);
}
if ($this->isFileEncrypted($attachment->getMessage(), $attachment->getFilename())) {
$attachment->itIsEncrypted();
public function buildFilename(Message $message, AttachmentInterface $attachment): string
{
$filename = implode(' - ', [
$message->getSubject(),
$message->getDateTime()->format('Y-m-d'),
$attachment->getFilename()
]);
return implode(DIRECTORY_SEPARATOR, [
$this->getAttachmentsFolder(),
$filename
]);
}
public function checkEncrypted(): array
{
$output = [];
foreach ($this->fetchAttachments() as $attachment) {
$output[$attachment->getFilename()] = $this->getDecrypt()->isEncrypted($attachment->getRealPath());
}
return $output;
}
public function fetchAttachments(): array
{
$files = new \FilesystemIterator($this->getAttachmentsFolder());
$attachments = [];
foreach ($files as $file) {
if ($file->isDir()) {
continue;
if ($this->isFileDecrypted($attachment->getMessage(), $attachment->getFilename())) {
$attachment->itIsDecrypted();
} else {
if ($this->decrypt($attachment->getMessage(), $attachment->getFilename())) {
$attachment->itIsDecrypted();
}
}
}
}
$attachments []= $file;
}
return $attachments;
}
public function fetchDecryptedAttachments(): array
{
$folder = implode(DIRECTORY_SEPARATOR, [$this->getAttachmentsFolder(), 'decrypted']);
$files = new \FilesystemIterator($folder);
$attachments = [];
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$attachments []= $file;
}
return $attachments;
}
public function fetchFullAttachments(): array
{
$attachments = $this->fetchAttachments();
$output = [];
foreach ($attachments as $attachment) {
$att = [
'original_attachment' => $attachment,
'encrypted' => $this->getDecrypt()->isEncrypted($attachment->getRealPath())
];
if ($att['encrypted']) {
$att['decrypted_attachment'] = $this->getDecryptedAttachment($attachment);
}
$output []= $att;
}
return $output;
}
public function getAttachment(string $filename): \SplFileInfo
{
if (!str_contains($filename, $this->getAttachmentsFolder())) {
$filename = implode(DIRECTORY_SEPARATOR, [$this->getAttachmentsFolder(), $filename]);
}
return new \SplFileInfo($filename);
}
public function getDecryptedAttachment(\SplFileInfo $info): \SplFileInfo|bool
{
$new_file = implode(DIRECTORY_SEPARATOR, [$info->getPath(), 'decrypted', $info->getFilename()]);
if (!file_exists($new_file)) {
$this->getRepository()->save($attachment);
return true;
} catch (PDOException $e) {
$this->getLogger()->error($e);
return false;
}
return new \SplFileInfo($new_file);
}
public function removeEncryption(string $filename): \SplFileInfo
public function upload(\ProVM\Emails\Model\Attachment $attachment, AttachmentInterface $remote_attachment): bool
{
$attachment = $this->getAttachment($filename);
$new_file = implode(DIRECTORY_SEPARATOR, [$attachment->getPath(), 'decrypted', $attachment->getFilename()]);
if ($this->getDecrypt()->runCommand($attachment->getRealPath(), $new_file)) {
return new \SplFileInfo($new_file);
$destination = implode(DIRECTORY_SEPARATOR, [
$this->getFolder(),
$attachment->getFullFilename()
]);
try {
\Safe\file_put_contents($destination, $remote_attachment->getDecodedContent());
return true;
} catch (FilesystemException $e) {
$this->getLogger()->error($e);
return false;
}
return $attachment;
}
public function isFileEncrypted(Message $message, string $relative_filename): bool
{
$attachment = $this->getLocalAttachment($message, $relative_filename);
$filename = implode(DIRECTORY_SEPARATOR, [
$this->getFolder(),
$attachment->getFullFilename()
]);
try {
return $this->getDecrypt()->isEncrypted($filename);
} catch (\InvalidArgumentException $e) {
return false;
}
}
public function isFileDecrypted(Message $message, string $relative_filename): bool
{
$attachment = $this->getLocalAttachment($message, $relative_filename);
$filename = implode(DIRECTORY_SEPARATOR, [
$this->getFolder(),
'decrypted',
$attachment->getFullFilename()
]);
try {
return !$this->getDecrypt()->isEncrypted($filename);
} catch (\InvalidArgumentException $e) {
return false;
}
}
public function decrypt(Message $message, string $relative_filename): bool
{
$attachment = $this->getLocalAttachment($message, $relative_filename);
$source = implode(DIRECTORY_SEPARATOR, [
$this->getFolder(),
$attachment->getFullFilename()
]);
$destination = implode(DIRECTORY_SEPARATOR, [
$this->getFolder(),
'decrypted',
$attachment->getFullFilename()
]);
return $this->getDecrypt()->runCommand($source, $destination);
}
public function isDownloaded(Message $message, MessageInterface $remote_message): bool
{
if (!$message->hasValidAttachments()) {
return false;
}
foreach ($remote_message->getAttachments() as $attachment) {
if (!str_contains($attachment->getFilename(), '.pdf')) {
continue;
}
$attachment = $this->getLocalAttachment($message, $attachment->getFilename());
if (!$attachment->isDownloaded()) {
return false;
}
}
return true;
}
}

View File

@ -1,158 +0,0 @@
<?php
namespace ProVM\Common\Service;
use Iterator;
use Generator;
use ProVM\Common\Exception\EmptyMailbox;
use Ddeboer\Imap\MailboxInterface;
use Ddeboer\Imap\MessageInterface;
use ProVM\Emails\Repository\Message;
class Emails extends Base
{
public function __construct(Mailboxes $mailboxService, Message $messageRepository, string $attachments_folder)
{
$this->setMailboxes($mailboxService);
$this->setMessageRepository($messageRepository);
$this->setAttachmentsFolder($attachments_folder);
}
protected Message $messageRepository;
protected Mailboxes $mailboxService;
protected string $attachments_folder;
public function getMailboxes(): Mailboxes
{
return $this->mailboxService;
}
public function getMessageRepository(): Message
{
return $this->messageRepository;
}
public function getAttachmentsFolder(): string
{
return $this->attachments_folder;
}
public function setMailboxes(Mailboxes $mailboxService): Emails
{
$this->mailboxService = $mailboxService;
return $this;
}
public function setMessageRepository(Message $messageRepository): Emails
{
$this->messageRepository = $messageRepository;
return $this;
}
public function setAttachmentsFolder(string $folder): Emails
{
$this->attachments_folder = $folder;
return $this;
}
//----------------------------------------------------------------
// Messages
public function getMessages(MailboxInterface $mailbox, int $start = 0, ?int $amount = null): array
{
$output = [];
$cnt = 0;
$messages = $this->getMessageRepository()->fetchByMailboxAndPosition($mailbox->getName(), $start, $amount ?? 1);
if ($messages) {
foreach ($messages as $message) {
$output []= $message;
$cnt ++;
}
}
if ($amount === null or $cnt < $amount) {
$messages = $this->getMailboxes()->getMessages($mailbox, $cnt + $start, $amount ?? $mailbox->count());
foreach ($messages as $m) {
$message = $this->saveMessage($mailbox, $m, $cnt + $start);
$cnt ++;
if ($message === null) {
continue;
}
$output []= $message;
}
}
return $output;
}
public function saveMessage(MailboxInterface $mailbox, MessageInterface $message, int $position): ?\ProVM\Emails\Model\Message
{
$data = [
'mailbox_id' => $mailbox->getId(),
'position' => $position,
'uid' => $message->getNumber(),
'subject' => $message->getSubject(),
'from' => $message->getFrom()->getFullAddress(),
'date_time' => $message->getDate()->format('Y-m-d H:i:s'),
];
return $this->getMessageRepository()->create($data);
}
public function getValidMessages(MailboxInterface $mailbox, int $start = 0, ?int $amount = null): array
{
$messages = $this->getMessages($mailbox, $start, $amount);
$output = array_filter($messages, function(\ProVM\Emails\Model\Message $message) {
return ($message->hasAttachments() and $message->hasValidAttachments());
});
if ($amount === null or count($output) >= $amount) {
return $output;
}
$cnt = $start + $amount;
while (count($output) < $amount) {
\Safe\ini_set('max_execution_time', ((int) \Safe\ini_get('max_execution_time')) + $amount * 5);
$messages = $this->getMailboxes()->getMessages($mailbox, $start + $amount, $amount);
foreach ($messages as $m) {
$message = $this->saveMessage($mailbox, $m, $cnt + $start);
$cnt ++;
if ($message === null) {
continue;
}
if ($message->hasAttachments() and $message->hasValidAttachments() and count($output) < $amount) {
$output []= $message;
}
}
}
return $output;
}
// Attachments
public function saveAttachments(MessageInterface $message, ?string $extension = null): array
{
if (!$message->hasAttachments()) {
return [];
}
$attachments = [];
foreach ($message->getAttachments() as $attachment) {
$this->getLogger()->debug($attachment->getFilename());
if ($extension !== null) {
$extension = trim($extension, '.');
if (!str_contains($attachment->getFilename(), ".{$extension}")) {
continue;
}
}
$filename = implode(DIRECTORY_SEPARATOR, [
$this->getAttachmentsFolder(),
"{$message->getSubject()} - {$message->getDate()->format('Y-m-d')} - {$attachment->getFilename()}"
]);
$this->getLogger()->debug($filename);
\Safe\file_put_contents($filename, $attachment->getDecodedContent());
$attachments []= $filename;
}
return $attachments;
}
public function getAttachments(?string $mailbox = null, int $start = 0, ?int $amount = null, ?string $extension = null): array
{
if ($mailbox === null) {
$mailbox = '/';
}
$mb = $this->getMailboxes()->get($mailbox);
$messages = $this->getMessages($mb, $start, $amount);
$attachments = [];
foreach ($messages as $message) {
$attachments = array_merge($attachments, $this->saveAttachments($message, $extension));
}
return $attachments;
}
}

View File

@ -1,73 +0,0 @@
<?php
namespace ProVM\Common\Service;
use Psr\Log\LoggerInterface;
use PDO;
class Install
{
public function __construct(LoggerInterface $logger, PDO $pdo)
{
$this->setLogger($logger);
$this->setConnection($pdo);
}
protected LoggerInterface $logger;
protected PDO $connection;
public function getLogger(): LoggerInterface
{
return $this->logger;
}
public function getConnection(): PDO
{
return $this->connection;
}
public function setLogger(LoggerInterface $logger): Install
{
$this->logger = $logger;
return $this;
}
public function setConnection(PDO $pdo): Install
{
$this->connection = $pdo;
return $this;
}
public function run(): void
{
$tables = [
'messages' => [
'`mailbox_id` int UNSIGNED NOT NULL',
'`position` int UNSIGNED NOT NULL',
'`uid` int UNSIGNED NOT NULL PRIMARY KEY',
'`subject` varchar(255) NOT NULL',
'`from` varchar(100) NOT NULL',
'`date_time` datetime NOT NULL',
'`has_attachments` int(1) DEFAULT 0',
'`valid_attachments` int(1) DEFAULT 0',
'`downloaded_attachments` int(1) DEFAULT 0'
],
'attachments' => [
'`message_uid` int UNSIGNED NOT NULL',
'`filename` varchar(255) NOT NULL PRIMARY KEY',
'`encrypted` int(1) DEFAULT 0',
'`decrypted` int(1) DEFAULT 0',
'CONSTRAINT `message_uid_fk` FOREIGN KEY (`message_uid`) REFERENCES `messages` (`uid`)'
],
'mailboxes' => [
'`'
]
];
foreach ($tables as $table => $definitions) {
$this->getConnection()->query($this->buildCreateTable($table, $definitions));
}
}
protected function buildCreateTable(string $table_name, array $columns): string
{
$query = ["CREATE TABLE IF NOT EXISTS `{$table_name}` ("];
$query []= implode(',' . PHP_EOL, $columns);
$query []= ')';
return implode(PHP_EOL, $query);
}
}

View File

@ -0,0 +1,71 @@
<?php
namespace ProVM\Common\Service;
use PDOException;
use ProVM\Common\Exception\Database\BlankResult;
use Safe\DateTimeImmutable;
use ProVM\Emails\Repository\Job;
class Jobs extends Base
{
public function __construct(Job $repository)
{
$this->setRepository($repository);
}
protected Job $repository;
public function getRepository(): Job
{
return $this->repository;
}
public function setRepository(Job $repository): Jobs
{
$this->repository = $repository;
return $this;
}
public function schedule(int $message_id): bool
{
$data = [
'message_id' => $message_id,
'date_time' => (new DateTimeImmutable())->format('Y-m-d H:i:s')
];
try {
$job = $this->getRepository()->create($data);
$this->getRepository()->save($job);
return true;
} catch (PDOException $e) {
return false;
}
}
public function getPending(): array
{
return $this->getRepository()->fetchAllPending();
}
public function isPending(int $message_id): bool
{
try {
$this->getRepository()->fetchPendingByMessage($message_id);
return true;
} catch (BlankResult $e) {
return false;
}
}
public function find(int $message_id): \ProVM\Emails\Model\Job
{
return $this->getRepository()->fetchPendingByMessage($message_id);
}
public function execute(int $job_id): bool
{
try {
$job = $this->getRepository()->fetchById($job_id);
$job->wasExecuted();
$this->getRepository()->save($job);
return true;
} catch (PDOException $e) {
return false;
}
}
}

View File

@ -1,151 +1,143 @@
<?php
namespace ProVM\Common\Service;
use Ddeboer\Imap\Message\AttachmentInterface;
use Iterator;
use Generator;
use Ddeboer\Imap\ConnectionInterface;
use Ddeboer\Imap\MailboxInterface;
use Ddeboer\Imap\MessageInterface;
use ProVM\Common\Exception\EmptyMailbox;
use ProVM\Common\Exception\Mailbox\Invalid;
use PDOException;
use Psr\Log\LoggerInterface;
use ProVM\Common\Exception\Database\BlankResult;
use ProVM\Emails\Repository\Mailbox;
use ProVM\Common\Service\Remote;
use ProVM\Emails\Repository\State;
class Mailboxes extends Base
{
public function __construct(ConnectionInterface $connection, Attachments $attachments, string $username)
public function __construct(Mailbox $repository, Remote\Mailboxes $remoteService, State\Mailbox $states, LoggerInterface $logger)
{
$this->setConnection($connection)
->setAttachments($attachments)
->setUsername($username);
$this->setRepository($repository)
->setRemoteService($remoteService)
->setStatesRepository($states)
->setLogger($logger);
}
protected ConnectionInterface $connection;
protected Attachments $attachments;
protected string $username;
protected Mailbox $repository;
protected Remote\Mailboxes $remoteService;
protected State\Mailbox $statesRepository;
public function getConnection(): ConnectionInterface
public function getRepository(): Mailbox
{
return $this->connection;
return $this->repository;
}
public function getAttachments(): Attachments
public function getRemoteService(): Remote\Mailboxes
{
return $this->attachments;
return $this->remoteService;
}
public function getUsername(): string
public function getStatesRepository(): State\Mailbox
{
return $this->username;
return $this->statesRepository;
}
public function setConnection(ConnectionInterface $connection): Mailboxes
public function setRepository(Mailbox $repository): Mailboxes
{
$this->connection = $connection;
$this->repository = $repository;
return $this;
}
public function setAttachments(Attachments $attachments): Mailboxes
public function setRemoteService(Remote\Mailboxes $service): Mailboxes
{
$this->attachments = $attachments;
$this->remoteService = $service;
return $this;
}
public function setUsername(string $username): Mailboxes
public function setStatesRepository(State\Mailbox $repository): Mailboxes
{
$this->username = $username;
$this->statesRepository = $repository;
return $this;
}
protected array $mailboxes;
public function getLocalMailbox(string $mailbox_name): \ProVM\Emails\Model\Mailbox
{
return $this->getRepository()->fetchByName($mailbox_name);
}
public function getRemoteMailbox(string $mailbox_name): MailboxInterface
{
return $this->getRemoteService()->get($mailbox_name);
}
public function getAll(): array
{
if (!isset($this->mailboxes)) {
$this->mailboxes = $this->getConnection()->getMailboxes();
}
return $this->mailboxes;
return $this->getRemoteService()->getAll();
}
public function get(string $mailbox): MailboxInterface
public function getRegistered(): array
{
if (!$this->getConnection()->hasMailbox($mailbox)) {
throw new Invalid();
}
return $this->getConnection()->getMailbox($mailbox);
return $this->getRepository()->fetchAll();
}
public function get(int $mailbox_id): \ProVM\Emails\Model\Mailbox
{
return $this->getRepository()->fetchById($mailbox_id);
}
// Messages
protected function advanceIterator(Iterator $iterator, int $up_to): Iterator
public function isRegistered(string $mailbox_name): bool
{
for ($i = 0; $i < $up_to; $i ++) {
$iterator->next();
try {
$mailbox = $this->getRepository()->fetchByName($mailbox_name);
return true;
} catch (BlankResult $e) {
return false;
}
return $iterator;
}
public function getMessages(MailboxInterface $mailbox, int $start = 0, ?int $amount = null): Generator
public function register(string $mailbox_name): bool
{
if ($mailbox->count() === 0) {
$this->getLogger()->notice('No mails found.');
throw new EmptyMailbox();
}
$it = $mailbox->getIterator();
if ($amount === null) {
$amount = $mailbox->count() - $start;
}
$it = $this->advanceIterator($it, $start);
for ($i = $start; $i < min($start + $amount, $mailbox->count()); $i ++) {
yield $it->key() => $it->current();
$it->next();
$remote_mailbox = $this->getRemoteMailbox($mailbox_name);
$name = $remote_mailbox->getName();
$validity = $remote_mailbox->getStatus()->uidvalidity;
try {
$mailbox = $this->getRepository()->create(compact('name', 'validity'));
$this->getRepository()->save($mailbox);
return true;
} catch (PDOException $e) {
return false;
}
}
public function countValid(MailboxInterface $mailbox): int
public function updateState(\ProVM\Emails\Model\Mailbox $mailbox, array $messages, \DateTimeInterface $dateTime): bool
{
$cnt = 0;
foreach ($mailbox->getIterator() as $message) {
if ($this->hasAttachments($message) and $this->validAttachments($message)) {
$cnt ++;
}
$data = [
'mailbox_id' => $mailbox->getId(),
'date_time' => $dateTime->format('Y-m-d H:i:s'),
'count' => count($messages),
'uids' => serialize($messages)
];
try {
$state = $this->getStatesRepository()->create($data);
$this->getStatesRepository()->save($state);
return true;
} catch (PDOException $e) {
return false;
}
return $cnt;
}
/**
* @param MailboxInterface $mailbox
* @param int $uid
* @return MessageInterface
*/
public function getMessage(MailboxInterface $mailbox, int $uid): MessageInterface
public function unregister(string $mailbox_name): bool
{
return $mailbox->getMessage($uid);
try {
$mailbox = $this->getRepository()->fetchByName($mailbox_name);
} catch (BlankResult $e) {
// It's already unregistered
return true;
}
try {
$this->getRepository()->delete($mailbox);
return true;
} catch (PDOException $e) {
return false;
}
}
public function validate(string $mailbox_name): bool
{
$mailbox = $this->getLocalMailbox($mailbox_name);
protected function validateAddress(array $to): bool
{
foreach ($to as $address) {
if (strtolower($address->getAddress()) === strtolower($this->getUsername())) {
return true;
}
}
return false;
}
public function hasAttachments(MessageInterface $message): bool
{
return ($message->hasAttachments() and $this->validateAddress($message->getTo()));
}
protected function validateAttachment(AttachmentInterface $attachment): bool
{
return str_contains($attachment->getFilename(), '.pdf');
}
public function validAttachments(MessageInterface $message): bool
{
foreach ($message->getAttachments() as $attachment) {
if ($this->validateAttachment($attachment)) {
return true;
}
}
return false;
}
public function downloadAttachments(MessageInterface $message)
{
foreach ($message->getAttachments() as $attachment) {
if ($this->validateAttachment($attachment)) {
$attachment->getContent();
}
if (!$this->getRemoteService()->validate($mailbox_name, $mailbox->getValidity())) {
$remote_mailbox = $this->getRemoteMailbox($mailbox_name);
$mailbox->setValidity($remote_mailbox->getStatus()->uidvalidity);
$this->getRepository()->save($mailbox);
return false;
}
return true;
}
}

View File

@ -1,37 +1,177 @@
<?php
namespace ProVM\Common\Service;
use ProVM\Common\Factory\Model;
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
use Ddeboer\Imap\MailboxInterface;
use PDOException;
use ProVM\Common\Exception\Mailbox\Stateless;
use Psr\Log\LoggerInterface;
use Ddeboer\Imap\MessageInterface;
use Ddeboer\Imap\Message\AttachmentInterface;
use ProVM\Emails\Model\Mailbox;
use ProVM\Emails\Repository\Message;
use Safe\DateTimeImmutable;
class Messages
class Messages extends Base
{
public function __construct(Model $factory, LoggerInterface $logger)
public function __construct(Mailboxes $mailboxes, Message $repository, Remote\Messages $remoteService, LoggerInterface $logger)
{
$this->setFactory($factory)
$this->setMailboxes($mailboxes)
->setRepository($repository)
->setRemoteService($remoteService)
->setLogger($logger);
}
protected Model $factory;
protected LoggerInterface $logger;
protected Mailboxes $mailboxes;
protected Message $repository;
protected Remote\Messages $remoteService;
public function getFactory(): Model
public function getMailboxes(): Mailboxes
{
return $this->factory;
return $this->mailboxes;
}
public function getLogger(): LoggerInterface
public function getRepository(): Message
{
return $this->logger;
return $this->repository;
}
public function getRemoteService(): Remote\Messages
{
return $this->remoteService;
}
public function setFactory(Model $factory): Messages
public function setMailboxes(Mailboxes $mailboxes): Messages
{
$this->factory = $factory;
$this->mailboxes = $mailboxes;
return $this;
}
public function setLogger(LoggerInterface $logger): Messages
public function setRepository(Message $repository): Messages
{
$this->logger = $logger;
$this->repository = $repository;
return $this;
}
public function setRemoteService(Remote\Messages $service): Messages
{
$this->remoteService = $service;
return $this;
}
public function getLocalMessage(string $message_uid): \ProVM\Emails\Model\Message
{
return $this->getRepository()->fetchByUID($message_uid);
}
public function getRemoteMessage(string $message_uid): MessageInterface
{
$message = $this->getLocalMessage($message_uid);
$remote_mailbox = $this->getMailboxes()->getRemoteMailbox($message->getMailbox()->getName());
return $this->getRemoteService()->get($remote_mailbox, $message->getSubject(), $message->getFrom(), $message->getDateTime());
}
public function getAll(string $mailbox_name): array
{
$mailbox = $this->getMailboxes()->getLocalMailbox($mailbox_name);
return $this->getRepository()->fetchByMailbox($mailbox->getId());
}
public function getValid(string $mailbox_name): array
{
$mailbox = $this->getMailboxes()->getLocalMailbox($mailbox_name);
return $this->getRepository()->fetchValidByMailbox($mailbox->getId());
}
public function grab(string $mailbox_name): array
{
$mailbox = $this->getMailboxes()->getLocalMailbox($mailbox_name);
if (!$this->getMailboxes()->validate($mailbox_name)) {
$this->restoreUIDs($mailbox_name);
}
try {
$start = $mailbox->lastPosition() + 1;
} catch (Stateless $e) {
$start = 0;
}
$remote_mailbox = $this->getMailboxes()->getRemoteMailbox($mailbox_name);
$total = $remote_mailbox->count();
$total_amount = $total - $start;
$amount = min(100, $total_amount);
$messages = [];
for ($i = 0; $i < $total_amount; $i += $amount) {
if ($amount + $i > $total_amount) {
$amount = $total_amount - $i;
}
$remote_messages = $this->getRemoteService()->getAll($remote_mailbox, $start + $i, $amount);
foreach ($remote_messages as $p => $m) {
if ($this->save($mailbox, $m, $p)) {
$messages []= $m->getId();
}
}
}
$this->getMailboxes()->updateState($mailbox, $messages, new DateTimeImmutable());
return $messages;
}
public function restoreUIDs(string $mailbox_name): void
{
$mailbox = $this->getMailboxes()->getLocalMailbox($mailbox_name);
$remote_mailbox = $this->getMailboxes()->getRemoteMailbox($mailbox_name);
$total_amount = $mailbox->lastPosition();
$amount = min(100, $total_amount);
for ($i = 0; $i < $total_amount; $i += $amount) {
if ($amount + $i > $total_amount) {
$amount = $total_amount - $i;
}
$remote_messages = $this->getRemoteService()->getAll($remote_mailbox, $i, $amount);
foreach ($remote_messages as $m) {
$this->update($mailbox, $m, $i);
}
}
}
public function save(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()->getAddress(),
'date_time' => $remote_message->getDate()->format('Y-m-d H:i:s')
];
try {
$message = $this->getRepository()->create($data);
if ($message->getId() === 0) {
if ($remote_message->hasAttachments()) {
$message->doesHaveAttachments();
}
if ($this->validAttachments($remote_message)) {
$message->doesHaveValidAttachments();
}
}
$this->getRepository()->save($message);
return true;
} catch (PDOException $e) {
$this->getLogger()->error($e);
return false;
}
}
public function update(Mailbox $mailbox, MessageInterface $remote_message, int $position): bool
{
try {
$message = $this->getRepository()->fetchByMailboxSubjectFromAndDate($mailbox->getId(), $remote_message->getSubject(), $remote_message->getFrom()->getAddress(), $remote_message->getDate());
$message->setUID($remote_message->getId());
$message->setPosition($position);
$this->getRepository()->save($message);
return true;
} catch (PDOException $e) {
return false;
}
}
public function validateAttachment(AttachmentInterface $attachment): bool
{
return str_contains($attachment->getFilename(), '.pdf');
}
public function validAttachments(MessageInterface $remote_message): bool
{
foreach ($remote_message->getAttachments() as $attachment) {
if ($this->validateAttachment($attachment)) {
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace ProVM\Common\Service\Remote;
use Psr\Log\LoggerInterface;
use Ddeboer\Imap\ConnectionInterface;
use Ddeboer\Imap\MessageInterface;
use Ddeboer\Imap\Message\AttachmentInterface;
use ProVM\Common\Exception\Attachment\NotFound;
class Attachments extends Base
{
public function __construct(ConnectionInterface $connection, LoggerInterface $logger)
{
$this->setConnection($connection)
->setLogger($logger);
}
public function getAttachments(MessageInterface $message): array
{
return $message->getAttachments();
}
public function get(MessageInterface $message, string $filename): AttachmentInterface
{
foreach ($message->getAttachments() as $attachment) {
if ($attachment->getFilename() === $filename) {
return $attachment;
}
}
throw new NotFound($message, $filename);
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace ProVM\Common\Service\Remote;
use Ddeboer\Imap\ConnectionInterface;
use ProVM\Common\Service\Base as BaseService;
abstract class Base extends BaseService
{
protected ConnectionInterface $connection;
public function getConnection(): ConnectionInterface
{
return $this->connection;
}
public function setConnection(ConnectionInterface $connection): Base
{
$this->connection = $connection;
return $this;
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace ProVM\Common\Service\Remote;
use Psr\Log\LoggerInterface;
use Ddeboer\Imap\ConnectionInterface;
use Ddeboer\Imap\MailboxInterface;
use ProVM\Common\Exception\Mailbox\Invalid;
/**
* Grab mailboxes from Email Provider
*/
class Mailboxes extends Base
{
public function __construct(ConnectionInterface $connection, LoggerInterface $logger)
{
$this->setConnection($connection)
->setLogger($logger);
}
protected array $mailboxes;
public function getAll(): array
{
if (!isset($this->mailboxes)) {
$this->mailboxes = $this->getConnection()->getMailboxes();
}
return $this->mailboxes;
}
/**
* @throws Invalid
*/
public function get(string $mailbox_name): MailboxInterface
{
if (!$this->getConnection()->hasMailbox($mailbox_name)) {
throw new Invalid($mailbox_name);
}
return $this->getConnection()->getMailbox($mailbox_name);
}
public function validate(string $mailbox_name, int $uidvalidity): bool
{
$mailbox = $this->get($mailbox_name);
return ($mailbox->getStatus()->uidvalidity === $uidvalidity);
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace ProVM\Common\Service\Remote;
use DateTimeInterface;
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
use Psr\Log\LoggerInterface;
use Ddeboer\Imap\ConnectionInterface;
use Ddeboer\Imap\MailboxInterface;
use Ddeboer\Imap\MessageInterface;
use Ddeboer\Imap\SearchExpression;
use Ddeboer\Imap\Search\Date\On;
use Ddeboer\Imap\Search\Email\From;
use Ddeboer\Imap\Search\Text\Subject;
use ProVM\Common\Exception\EmptyMailbox;
class Messages extends Base
{
public function __construct(ConnectionInterface $connection, LoggerInterface $logger)
{
$this->setConnection($connection)
->setLogger($logger);
}
public function getAll(MailboxInterface $mailbox, int $start = 0, ?int $amount = null): array
{
if ($mailbox->count() === 0) {
throw new EmptyMailbox($mailbox);
}
if ($amount === null) {
$amount = $mailbox->count() - $start;
}
$it = $mailbox->getIterator();
for ($i = 0; $i < $start; $i ++) {
$it->next();
}
$messages = [];
for ($i = $start; $i < $start + $amount; $i ++) {
if (!$it->valid()) {
break;
}
$messages[$i] = $it->current();
$it->next();
}
return $messages;
}
public function get(MailboxInterface $mailbox, string $subject, string $from, DateTimeInterface $dateTime): MessageInterface
{
if ($mailbox->count() === 0) {
$this->getLogger()->notice("Mailbox {$mailbox->getName()} is empty");
throw new EmptyMailbox($mailbox);
}
$query = new SearchExpression();
$query->addCondition(new Subject($subject));
$query->addCondition(new From($from));
$query->addCondition(new On($dateTime));
$result = $mailbox->getMessages($query);
if (count($result) === 0) {
throw new MessageDoesNotExistException("{$mailbox->getName()}: {$subject} - {$from} [{$dateTime->format('Y-m-d H:i:s')}]");
}
return $result->current();
}
}