Full implemantation
This commit is contained in:
10
NOTES.md
10
NOTES.md
@ -3,7 +3,7 @@
|
|||||||
* [x] Select which to *register* or unregister for watching.
|
* [x] Select which to *register* or unregister for watching.
|
||||||
* [x] List *registered* `mailboxes`.
|
* [x] List *registered* `mailboxes`.
|
||||||
* [x] List `messages` for selected `mailbox`.
|
* [x] List `messages` for selected `mailbox`.
|
||||||
* [ ] Schedule `attachments` downloads.
|
* [x] Schedule `attachments` downloads.
|
||||||
* [ ] Download `attachments` (*encrypted* & *decrypted*).
|
* [ ] Download `attachments` (*encrypted* & *decrypted*).
|
||||||
|
|
||||||
## CLI
|
## CLI
|
||||||
@ -12,12 +12,12 @@
|
|||||||
|
|
||||||
## API
|
## API
|
||||||
* [x] Grab all `mailboxes` from `Email Provider`, identifying those that are registered.
|
* [x] Grab all `mailboxes` from `Email Provider`, identifying those that are registered.
|
||||||
* [x] Register `mailboxes` into **[database]**.
|
* [x] Register `mailboxes` into **[database]** and grab latest `messages`.
|
||||||
* [x] Grab new `messages` from `Email Provider` for selected `mailboxes` and store them in the `database`.
|
* [x] Grab new `messages` from `Email Provider` for selected `mailboxes` and store them in the `database`.
|
||||||
* [x] Grab `messages` from **[database]** for selected `mailboxes`.
|
* [x] Grab `messages` from **[database]** for selected `mailboxes`.
|
||||||
* [ ] Grab `attachments` from `Email Provider` for selected `messages`.
|
* [x] Grab `attachments` from `Email Provider` for selected `messages`.
|
||||||
* [ ] Decrypt `attachments`.
|
* [x] Register `messages` for `attachment` job.
|
||||||
* [ ] Register `messages` for `attachment` job.
|
* [x] Decrypt `attachments`.
|
||||||
|
|
||||||
|
|
||||||
## Workflow
|
## Workflow
|
||||||
|
25
README.md
25
README.md
@ -1,23 +1,8 @@
|
|||||||
# Emails
|
# Emails
|
||||||
ProVM
|
ProVM
|
||||||
|
|
||||||
## API
|
## Description
|
||||||
|
Grab attachments from emails by inbox.
|
||||||
| Concept | Model | Repository | Table |
|
* Choose what mailboxes to watch.
|
||||||
|------------|--------------------|--------------------|--------------------|
|
* Select messages that you want to grab attachments from.
|
||||||
| Message | | | |
|
* Download (or view in browser) (decrypted) attachments from messages.
|
||||||
| Attachment | | | |
|
|
||||||
| Mailbox | | | |
|
|
||||||
|
|
||||||
## UI
|
|
||||||
+ [ ] Mailboxes
|
|
||||||
+ [ ] Register
|
|
||||||
+ [ ] Messages
|
|
||||||
+ [ ] Attachments
|
|
||||||
## CLI
|
|
||||||
+ [ ] Mailboxes
|
|
||||||
+ Scrub
|
|
||||||
+ [ ] Messages
|
|
||||||
+ Grab Attachments
|
|
||||||
+ [ ] Attachments
|
|
||||||
+ Decrypt
|
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Common\Controller;
|
namespace ProVM\Common\Controller;
|
||||||
|
|
||||||
|
use ProVM\Common\Exception\Request\MissingArgument;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use function Safe\json_decode;
|
|
||||||
use ProVM\Common\Implement\Controller\Json;
|
use ProVM\Common\Implement\Controller\Json;
|
||||||
use ProVM\Common\Service\Attachments as Service;
|
use ProVM\Common\Service\Attachments as Service;
|
||||||
|
use ProVM\Emails\Model\Attachment;
|
||||||
|
|
||||||
class Attachments
|
class Attachments
|
||||||
{
|
{
|
||||||
@ -13,47 +14,40 @@ class Attachments
|
|||||||
|
|
||||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||||
{
|
{
|
||||||
$attachments = array_map(function($attachment) {
|
$attachments = array_map(function(Attachment $attachment) {
|
||||||
$attachment['original_attachment'] = $attachment['original_attachment']->getRealPath();
|
return $attachment->toArray();
|
||||||
if (isset($attachment['decrypted_attachment']) and $attachment['decrypted_attachment'] !== false) {
|
},$service->getAll());
|
||||||
$attachment['decrypted_attachment'] = $attachment['decrypted_attachment']->getRealPath();
|
$output = [
|
||||||
}
|
'total' => count($attachments),
|
||||||
return $attachment;
|
'attachments' => $attachments
|
||||||
}, $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);
|
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();
|
$body = $request->getBody();
|
||||||
$json = json_decode($body);
|
$json = \Safe\json_decode($body->getContents());
|
||||||
if (!is_array($json)) {
|
if (!isset($json->messages)) {
|
||||||
$json = [$json];
|
throw new MissingArgument('messages', 'array', 'message UIDs');
|
||||||
}
|
}
|
||||||
$output = ['input' => $json, 'attachments' => []];
|
$output = [
|
||||||
foreach ($json as $attachment) {
|
'messages' => $json->messages,
|
||||||
$output['attachments'] []= $this->fileToArray($service->removeEncryption($attachment));
|
'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);
|
return $this->withJson($response, $output);
|
||||||
}
|
}
|
||||||
|
18
api/common/Controller/Base.php
Normal file
18
api/common/Controller/Base.php
Normal 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'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -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()
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
45
api/common/Controller/Jobs.php
Normal file
45
api/common/Controller/Jobs.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,83 +1,100 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Common\Controller;
|
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\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
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
|
class Mailboxes
|
||||||
{
|
{
|
||||||
use Json;
|
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 = array_values(array_map(function(MailboxInterface $mailbox) use ($service) {
|
||||||
$mailboxes = [];
|
$arr = ['name' => $mailbox->getName(), 'registered' => false];
|
||||||
foreach ($source_mailboxes as $mailbox) {
|
if ($service->isRegistered($mailbox->getName())) {
|
||||||
$m = [
|
$mb = $service->getLocalMailbox($mailbox->getName());
|
||||||
'name' => $mailbox->getName(),
|
$arr['id'] = $mb->getId();
|
||||||
'registered' => false
|
$arr['registered'] = true;
|
||||||
];
|
|
||||||
try {
|
|
||||||
$s = $repository->fetchByName($mailbox->getName());
|
|
||||||
$m['registered'] = true;
|
|
||||||
$m['id'] = $s->getId();
|
|
||||||
} catch (BlankResult $e) {
|
|
||||||
}
|
}
|
||||||
$mailboxes []= $m;
|
return $arr;
|
||||||
}
|
}, $service->getAll()));
|
||||||
return $this->withJson($response, compact('mailboxes'));
|
$output = [
|
||||||
}
|
'total' => count($mailboxes),
|
||||||
public function register(ServerRequestInterface $request, ResponseInterface $response, \ProVM\Common\Service\Mailboxes $service, Mailbox $repository): ResponseInterface
|
'mailboxes' => $mailboxes
|
||||||
{
|
];
|
||||||
$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);
|
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();
|
$mailboxes = array_map(function(Mailbox $mailbox) {
|
||||||
$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();
|
return $mailbox->toArray();
|
||||||
}, $data);
|
}, $service->getRegistered());
|
||||||
return $this->withJson($response, compact('mailboxes'));
|
$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()]);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,117 +1,73 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Common\Controller;
|
namespace ProVM\Common\Controller;
|
||||||
|
|
||||||
use Ddeboer\Imap\MessageInterface;
|
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
|
||||||
use ProVM\Common\Factory\Model;
|
|
||||||
use ProVM\Emails\Model\State\Mailbox;
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
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\Implement\Controller\Json;
|
||||||
use ProVM\Common\Service\Mailboxes as Service;
|
use ProVM\Common\Service\Messages as Service;
|
||||||
use ProVM\Emails\Repository\Message;
|
use ProVM\Emails\Model\Message;
|
||||||
use Safe\DateTimeImmutable;
|
|
||||||
|
|
||||||
class Messages
|
class Messages
|
||||||
{
|
{
|
||||||
use Json;
|
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();
|
$mailbox = $service->getMailboxes()->get($mailbox_id);
|
||||||
$json = \Safe\json_decode($body->getContents());
|
$messages = array_map(function(Message $message) {
|
||||||
$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();
|
return $message->toArray();
|
||||||
}, $messages);
|
}, $service->getAll($mailbox->getName()));
|
||||||
return $this->withJson($response, ['messages' => $messages, 'total' => count($messages)]);
|
$output = [
|
||||||
}
|
'mailbox' => $mailbox->toArray(),
|
||||||
public function valid(ServerRequestInterface $request, ResponseInterface $response, Message $repository): ResponseInterface
|
'total' => count($messages),
|
||||||
{
|
'messages' => $messages
|
||||||
$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);
|
return $this->withJson($response, $output);
|
||||||
if ($message->getId() === 0) {
|
}
|
||||||
if ($remote_message->hasAttachments()) {
|
public function valid(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Attachments $attachments, int $mailbox_id): ResponseInterface
|
||||||
$message->doesHaveAttachments();
|
{
|
||||||
}
|
$mailbox = $service->getMailboxes()->get($mailbox_id);
|
||||||
if ($service->validAttachments($remote_message)) {
|
$messages = array_values(array_filter(array_map(function(Message $message) use ($service, $attachments) {
|
||||||
$message->doesHaveValidAttachments();
|
return $message->toArray();
|
||||||
}
|
}, $service->getValid($mailbox->getName())), function($message) {
|
||||||
$repository->save($message);
|
return $message !== null;
|
||||||
return true;
|
}));
|
||||||
|
$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);
|
||||||
}
|
}
|
||||||
}
|
}
|
15
api/common/Exception/Attachment/NotFound.php
Normal file
15
api/common/Exception/Attachment/NotFound.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -1,14 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Common\Exception;
|
namespace ProVM\Common\Exception;
|
||||||
|
|
||||||
|
use Ddeboer\Imap\MailboxInterface;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
class EmptyMailbox extends Exception
|
class EmptyMailbox extends Exception
|
||||||
{
|
{
|
||||||
public function __construct(?Throwable $previous = null)
|
public function __construct(MailboxInterface $mailbox, ?Throwable $previous = null)
|
||||||
{
|
{
|
||||||
$message = "No mails found";
|
$message = "No mails found in {$mailbox->getName()}";
|
||||||
$code = 102;
|
$code = 101;
|
||||||
parent::__construct($message, $code, $previous);
|
parent::__construct($message, $code, $previous);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,9 +5,9 @@ use Exception;
|
|||||||
|
|
||||||
class Invalid extends 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;
|
$code = 100;
|
||||||
parent::__construct($message, $code, $previous);
|
parent::__construct($message, $code, $previous);
|
||||||
}
|
}
|
||||||
|
15
api/common/Exception/Mailbox/Stateless.php
Normal file
15
api/common/Exception/Mailbox/Stateless.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
15
api/common/Exception/Message/NoAttachments.php
Normal file
15
api/common/Exception/Message/NoAttachments.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
14
api/common/Exception/Request/MissingArgument.php
Normal file
14
api/common/Exception/Request/MissingArgument.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
namespace ProVM\Common\Implement;
|
namespace ProVM\Common\Implement;
|
||||||
|
|
||||||
use PDO;
|
use PDO;
|
||||||
|
use PDOException;
|
||||||
use ProVM\Common\Exception\Database\BlankResult;
|
use ProVM\Common\Exception\Database\BlankResult;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use ProVM\Common\Define\Model as ModelInterface;
|
use ProVM\Common\Define\Model as ModelInterface;
|
||||||
@ -64,10 +65,10 @@ abstract class Repository
|
|||||||
$old_values = $this->valuesForUpdate($old);
|
$old_values = $this->valuesForUpdate($old);
|
||||||
$columns = [];
|
$columns = [];
|
||||||
$values = [];
|
$values = [];
|
||||||
foreach ($this->fieldsForUpdate() as $column => $method) {
|
foreach ($this->fieldsForUpdate() as $i => $column) {
|
||||||
if (isset($model_values[$column]) and $old_values[$column] !== $model_values[$column]) {
|
if (isset($model_values[$i]) and $old_values[$i] !== $model_values[$i]) {
|
||||||
$columns []= "`{$column}`";
|
$columns []= "`{$column}` = ?";
|
||||||
$values []= $model_values[$column];
|
$values []= $model_values[$i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (count($columns) === 0) {
|
if (count($columns) === 0) {
|
||||||
@ -101,8 +102,9 @@ abstract class Repository
|
|||||||
} catch (BlankResult $e) {
|
} catch (BlankResult $e) {
|
||||||
$this->insert($model);
|
$this->insert($model);
|
||||||
$model->setId($this->getConnection()->lastInsertId());
|
$model->setId($this->getConnection()->lastInsertId());
|
||||||
} catch (\Error | \Exception $e) {
|
} catch(PDOException $e) {
|
||||||
$this->getLogger()->error($e);
|
$this->getLogger()->error($e);
|
||||||
|
throw $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
abstract public function load(array $row): ModelInterface;
|
abstract public function load(array $row): ModelInterface;
|
||||||
|
@ -63,6 +63,7 @@ class Auth
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$this->getLogger()->debug(sha1($this->getAPIKey()));
|
||||||
$response = $this->getResponseFactory()->createResponse(401);
|
$response = $this->getResponseFactory()->createResponse(401);
|
||||||
$response->getBody()->write(\Safe\json_encode(['error' => 401, 'message' => 'Incorrect token']));
|
$response->getBody()->write(\Safe\json_encode(['error' => 401, 'message' => 'Incorrect token']));
|
||||||
return $response
|
return $response
|
||||||
|
58
api/common/Middleware/CustomExceptions.php
Normal file
58
api/common/Middleware/CustomExceptions.php
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,37 +3,65 @@ namespace ProVM\Common\Service;
|
|||||||
|
|
||||||
use Ddeboer\Imap\Message\AttachmentInterface;
|
use Ddeboer\Imap\Message\AttachmentInterface;
|
||||||
use Ddeboer\Imap\MessageInterface;
|
use Ddeboer\Imap\MessageInterface;
|
||||||
use ProVM\Emails\Model\Attachment;
|
use ProVM\Common\Exception\Message\NoAttachments;
|
||||||
use ProVM\Emails\Model\Message;
|
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->setMessages($messages)
|
||||||
$this->setAttachmentsFolder($attachments_folder);
|
->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 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
|
public function getDecrypt(): Decrypt
|
||||||
{
|
{
|
||||||
return $this->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;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setDecrypt(Decrypt $decrypt): Attachments
|
public function setDecrypt(Decrypt $decrypt): Attachments
|
||||||
@ -41,119 +69,164 @@ class Attachments
|
|||||||
$this->decrypt = $decrypt;
|
$this->decrypt = $decrypt;
|
||||||
return $this;
|
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;
|
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 = [
|
$data = [
|
||||||
'message_id' => $message->getId(),
|
'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
|
if ($this->isFileEncrypted($attachment->getMessage(), $attachment->getFilename())) {
|
||||||
{
|
$attachment->itIsEncrypted();
|
||||||
$filename = $this->buildFilename($attachment);
|
|
||||||
return file_exists($filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function buildFilename(Message $message, AttachmentInterface $attachment): string
|
if ($this->isFileDecrypted($attachment->getMessage(), $attachment->getFilename())) {
|
||||||
{
|
$attachment->itIsDecrypted();
|
||||||
$filename = implode(' - ', [
|
} else {
|
||||||
$message->getSubject(),
|
if ($this->decrypt($attachment->getMessage(), $attachment->getFilename())) {
|
||||||
$message->getDateTime()->format('Y-m-d'),
|
$attachment->itIsDecrypted();
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
$attachments []= $file;
|
$this->getRepository()->save($attachment);
|
||||||
}
|
return true;
|
||||||
return $attachments;
|
} catch (PDOException $e) {
|
||||||
}
|
$this->getLogger()->error($e);
|
||||||
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)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return new \SplFileInfo($new_file);
|
|
||||||
}
|
}
|
||||||
|
public function upload(\ProVM\Emails\Model\Attachment $attachment, AttachmentInterface $remote_attachment): bool
|
||||||
public function removeEncryption(string $filename): \SplFileInfo
|
|
||||||
{
|
{
|
||||||
$attachment = $this->getAttachment($filename);
|
$destination = implode(DIRECTORY_SEPARATOR, [
|
||||||
$new_file = implode(DIRECTORY_SEPARATOR, [$attachment->getPath(), 'decrypted', $attachment->getFilename()]);
|
$this->getFolder(),
|
||||||
if ($this->getDecrypt()->runCommand($attachment->getRealPath(), $new_file)) {
|
$attachment->getFullFilename()
|
||||||
return new \SplFileInfo($new_file);
|
]);
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
71
api/common/Service/Jobs.php
Normal file
71
api/common/Service/Jobs.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,151 +1,143 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Common\Service;
|
namespace ProVM\Common\Service;
|
||||||
|
|
||||||
use Ddeboer\Imap\Message\AttachmentInterface;
|
|
||||||
use Iterator;
|
|
||||||
use Generator;
|
|
||||||
use Ddeboer\Imap\ConnectionInterface;
|
|
||||||
use Ddeboer\Imap\MailboxInterface;
|
use Ddeboer\Imap\MailboxInterface;
|
||||||
use Ddeboer\Imap\MessageInterface;
|
use PDOException;
|
||||||
use ProVM\Common\Exception\EmptyMailbox;
|
use Psr\Log\LoggerInterface;
|
||||||
use ProVM\Common\Exception\Mailbox\Invalid;
|
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
|
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)
|
$this->setRepository($repository)
|
||||||
->setAttachments($attachments)
|
->setRemoteService($remoteService)
|
||||||
->setUsername($username);
|
->setStatesRepository($states)
|
||||||
|
->setLogger($logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ConnectionInterface $connection;
|
protected Mailbox $repository;
|
||||||
protected Attachments $attachments;
|
protected Remote\Mailboxes $remoteService;
|
||||||
protected string $username;
|
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;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setAttachments(Attachments $attachments): Mailboxes
|
public function setRemoteService(Remote\Mailboxes $service): Mailboxes
|
||||||
{
|
{
|
||||||
$this->attachments = $attachments;
|
$this->remoteService = $service;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setUsername(string $username): Mailboxes
|
public function setStatesRepository(State\Mailbox $repository): Mailboxes
|
||||||
{
|
{
|
||||||
$this->username = $username;
|
$this->statesRepository = $repository;
|
||||||
return $this;
|
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
|
public function getAll(): array
|
||||||
{
|
{
|
||||||
if (!isset($this->mailboxes)) {
|
return $this->getRemoteService()->getAll();
|
||||||
$this->mailboxes = $this->getConnection()->getMailboxes();
|
|
||||||
}
|
|
||||||
return $this->mailboxes;
|
|
||||||
}
|
}
|
||||||
public function get(string $mailbox): MailboxInterface
|
public function getRegistered(): array
|
||||||
{
|
{
|
||||||
if (!$this->getConnection()->hasMailbox($mailbox)) {
|
return $this->getRepository()->fetchAll();
|
||||||
throw new Invalid();
|
}
|
||||||
}
|
public function get(int $mailbox_id): \ProVM\Emails\Model\Mailbox
|
||||||
return $this->getConnection()->getMailbox($mailbox);
|
{
|
||||||
|
return $this->getRepository()->fetchById($mailbox_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Messages
|
public function isRegistered(string $mailbox_name): bool
|
||||||
|
|
||||||
protected function advanceIterator(Iterator $iterator, int $up_to): Iterator
|
|
||||||
{
|
{
|
||||||
for ($i = 0; $i < $up_to; $i ++) {
|
try {
|
||||||
$iterator->next();
|
$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) {
|
$remote_mailbox = $this->getRemoteMailbox($mailbox_name);
|
||||||
$this->getLogger()->notice('No mails found.');
|
$name = $remote_mailbox->getName();
|
||||||
throw new EmptyMailbox();
|
$validity = $remote_mailbox->getStatus()->uidvalidity;
|
||||||
}
|
try {
|
||||||
$it = $mailbox->getIterator();
|
$mailbox = $this->getRepository()->create(compact('name', 'validity'));
|
||||||
if ($amount === null) {
|
$this->getRepository()->save($mailbox);
|
||||||
$amount = $mailbox->count() - $start;
|
return true;
|
||||||
}
|
} catch (PDOException $e) {
|
||||||
$it = $this->advanceIterator($it, $start);
|
return false;
|
||||||
for ($i = $start; $i < min($start + $amount, $mailbox->count()); $i ++) {
|
|
||||||
yield $it->key() => $it->current();
|
|
||||||
$it->next();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public function countValid(MailboxInterface $mailbox): int
|
public function updateState(\ProVM\Emails\Model\Mailbox $mailbox, array $messages, \DateTimeInterface $dateTime): bool
|
||||||
{
|
{
|
||||||
$cnt = 0;
|
$data = [
|
||||||
foreach ($mailbox->getIterator() as $message) {
|
'mailbox_id' => $mailbox->getId(),
|
||||||
if ($this->hasAttachments($message) and $this->validAttachments($message)) {
|
'date_time' => $dateTime->format('Y-m-d H:i:s'),
|
||||||
$cnt ++;
|
'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;
|
|
||||||
}
|
}
|
||||||
/**
|
public function unregister(string $mailbox_name): bool
|
||||||
* @param MailboxInterface $mailbox
|
|
||||||
* @param int $uid
|
|
||||||
* @return MessageInterface
|
|
||||||
*/
|
|
||||||
public function getMessage(MailboxInterface $mailbox, int $uid): MessageInterface
|
|
||||||
{
|
{
|
||||||
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
|
if (!$this->getRemoteService()->validate($mailbox_name, $mailbox->getValidity())) {
|
||||||
{
|
$remote_mailbox = $this->getRemoteMailbox($mailbox_name);
|
||||||
foreach ($to as $address) {
|
$mailbox->setValidity($remote_mailbox->getStatus()->uidvalidity);
|
||||||
if (strtolower($address->getAddress()) === strtolower($this->getUsername())) {
|
$this->getRepository()->save($mailbox);
|
||||||
return true;
|
return false;
|
||||||
}
|
|
||||||
}
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,37 +1,177 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Common\Service;
|
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 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);
|
->setLogger($logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Model $factory;
|
protected Mailboxes $mailboxes;
|
||||||
protected LoggerInterface $logger;
|
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;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setLogger(LoggerInterface $logger): Messages
|
public function setRepository(Message $repository): Messages
|
||||||
{
|
{
|
||||||
$this->logger = $logger;
|
$this->repository = $repository;
|
||||||
return $this;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
31
api/common/Service/Remote/Attachments.php
Normal file
31
api/common/Service/Remote/Attachments.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
21
api/common/Service/Remote/Base.php
Normal file
21
api/common/Service/Remote/Base.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
45
api/common/Service/Remote/Mailboxes.php
Normal file
45
api/common/Service/Remote/Mailboxes.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
67
api/common/Service/Remote/Messages.php
Normal file
67
api/common/Service/Remote/Messages.php
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
@ -1,12 +0,0 @@
|
|||||||
<?php
|
|
||||||
use ProVM\Common\Controller\Emails;
|
|
||||||
|
|
||||||
$app->group('/emails', function($app) {
|
|
||||||
$app->get('/mailboxes', [Emails::class, 'mailboxes']);
|
|
||||||
$app->group('/messages', function($app) {
|
|
||||||
$app->post('/attachments[/]', [Emails::class, 'withAttachments']);
|
|
||||||
$app->post('[/]', [Emails::class, 'messages']);
|
|
||||||
});
|
|
||||||
$app->post('/attachments', [Emails::class, 'attachments']);
|
|
||||||
$app->post('/attachment', [Emails::class, 'attachment']);
|
|
||||||
});
|
|
@ -1,13 +1,18 @@
|
|||||||
<?php
|
<?php
|
||||||
use ProVM\Common\Controller\Mailboxes;
|
use ProVM\Common\Controller\Mailboxes;
|
||||||
|
use ProVM\Common\Controller\Messages;
|
||||||
|
|
||||||
$app->group('/mailboxes', function($app) {
|
$app->group('/mailboxes', function($app) {
|
||||||
$app->post('/register', [Mailboxes::class, 'register']);
|
$app->post('/register[/]', [Mailboxes::class, 'register']);
|
||||||
$app->delete('/unregister', [Mailboxes::class, 'unregister']);
|
$app->delete('/unregister[/]', [Mailboxes::class, 'unregister']);
|
||||||
$app->get('/registered', [Mailboxes::class, 'registered']);
|
$app->get('/registered[/]', [Mailboxes::class, 'registered']);
|
||||||
$app->get('[/]', Mailboxes::class);
|
$app->get('[/]', Mailboxes::class);
|
||||||
});
|
});
|
||||||
|
|
||||||
$app->group('/mailbox/{mailbox_id}', function($app) {
|
$app->group('/mailbox/{mailbox_id}', function($app) {
|
||||||
|
$app->group('/messages', function($app) {
|
||||||
|
$app->get('/valid[/]', [Messages::class, 'valid']);
|
||||||
|
$app->get('[/]', Messages::class);
|
||||||
|
});
|
||||||
$app->get('[/]', [Mailboxes::class, 'get']);
|
$app->get('[/]', [Mailboxes::class, 'get']);
|
||||||
});
|
});
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
use ProVM\Common\Controller\Attachments;
|
use ProVM\Common\Controller\Attachments;
|
||||||
|
|
||||||
$app->group('/attachments', function($app) {
|
$app->group('/attachments', function($app) {
|
||||||
$app->post('/get', [Attachments::class, 'get']);
|
$app->put('/grab', [Attachments::class, 'grab']);
|
||||||
$app->post('/decrypt', [Attachments::class, 'decrypt']);
|
$app->post('/decrypt', [Attachments::class, 'decrypt']);
|
||||||
$app->get('[/]', Attachments::class);
|
$app->get('[/]', Attachments::class);
|
||||||
});
|
});
|
@ -1,8 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
use ProVM\Common\Controller\Messages;
|
use ProVM\Common\Controller\Messages;
|
||||||
|
use ProVM\Common\Controller\Jobs;
|
||||||
|
|
||||||
$app->group('/messages', function($app) {
|
$app->group('/messages', function($app) {
|
||||||
$app->post('/valid', [Messages::class, 'valid']);
|
|
||||||
$app->put('/grab', [Messages::class, 'grab']);
|
$app->put('/grab', [Messages::class, 'grab']);
|
||||||
$app->post('[/]', Messages::class);
|
$app->put('/schedule', [Jobs::class, 'schedule']);
|
||||||
|
$app->get('/pending', [Jobs::class, 'pending']);
|
||||||
});
|
});
|
@ -1,4 +0,0 @@
|
|||||||
<?php
|
|
||||||
use ProVM\Common\Controller\Install;
|
|
||||||
|
|
||||||
$app->get('/install', Install::class);
|
|
4
api/resources/routes/99_base.php
Normal file
4
api/resources/routes/99_base.php
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?php
|
||||||
|
use ProVM\Common\Controller\Base;
|
||||||
|
|
||||||
|
$app->get('[/]', Base::class);
|
@ -1,2 +1,2 @@
|
|||||||
<?php
|
<?php
|
||||||
$app->add($app->getContainer()->get(\ProVM\Common\Middleware\CORS::class));
|
$app->add($app->getContainer()->get(ProVM\Common\Middleware\CORS::class));
|
||||||
|
2
api/setup/middleware/03_custom_exceptions.php
Normal file
2
api/setup/middleware/03_custom_exceptions.php
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<?php
|
||||||
|
$app->add($app->getContainer()->get(ProVM\Common\Middleware\CustomExceptions::class));
|
@ -1,2 +1,2 @@
|
|||||||
<?php
|
<?php
|
||||||
$app->add($app->getContainer()->get(\ProVM\Common\Middleware\Auth::class));
|
$app->add($app->getContainer()->get(ProVM\Common\Middleware\Auth::class));
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
<?php
|
<?php
|
||||||
$app->add($app->getContainer()->get(\Zeuxisoo\Whoops\Slim\WhoopsMiddleware::class));
|
$app->add($app->getContainer()->get(Zeuxisoo\Whoops\Slim\WhoopsMiddleware::class));
|
||||||
|
@ -2,40 +2,21 @@
|
|||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
\ProVM\Common\Service\Mailboxes::class => function(ContainerInterface $container) {
|
ProVM\Common\Service\Decrypt::class => function(ContainerInterface $container) {
|
||||||
return (new \ProVM\Common\Service\Mailboxes(
|
return new ProVM\Common\Service\Decrypt(
|
||||||
$container->get(\Ddeboer\Imap\ConnectionInterface::class),
|
$container->get(Psr\Log\LoggerInterface::class),
|
||||||
$container->get(\ProVM\Common\Service\Attachments::class),
|
|
||||||
$container->get('emails')->username
|
|
||||||
))->setLogger($container->get(\Psr\Log\LoggerInterface::class));
|
|
||||||
},
|
|
||||||
\ProVM\Common\Service\Emails::class => function(ContainerInterface $container) {
|
|
||||||
return (new \ProVM\Common\Service\Emails(
|
|
||||||
$container->get(\ProVM\Common\Service\Mailboxes::class),
|
|
||||||
$container->get(\ProVM\Emails\Repository\Message::class),
|
|
||||||
$container->get('attachments_folder')
|
|
||||||
))->setLogger($container->get(\Psr\Log\LoggerInterface::class));
|
|
||||||
},
|
|
||||||
\ProVM\Common\Service\Attachments::class => function(ContainerInterface $container) {
|
|
||||||
return new \ProVM\Common\Service\Attachments(
|
|
||||||
$container->get(\ProVM\Common\Service\Decrypt::class),
|
|
||||||
$container->get(\ProVM\Emails\Repository\Attachment::class),
|
|
||||||
$container->get('attachments_folder')
|
|
||||||
);
|
|
||||||
},
|
|
||||||
\ProVM\Common\Service\Decrypt::class => function(ContainerInterface $container) {
|
|
||||||
return new \ProVM\Common\Service\Decrypt(
|
|
||||||
$container->get(\Psr\Log\LoggerInterface::class),
|
|
||||||
$container->get('base_command'),
|
$container->get('base_command'),
|
||||||
$container->get('passwords')
|
$container->get('passwords')
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
\ProVM\Common\Service\Install::class => function(ContainerInterface $container) {
|
ProVM\Common\Service\Attachments::class => function(ContainerInterface $container) {
|
||||||
$database = $container->get('database');
|
return new ProVM\Common\Service\Attachments(
|
||||||
$pdo = new PDO("mysql:host={$database->host};dbname={$database->name}", $database->username, $database->password);
|
$container->get(ProVM\Common\Service\Messages::class),
|
||||||
return new \ProVM\Common\Service\Install(
|
$container->get(ProVM\Emails\Repository\Attachment::class),
|
||||||
$container->get(\Psr\Log\LoggerInterface::class),
|
$container->get(ProVM\Common\Service\Remote\Attachments::class),
|
||||||
$pdo
|
$container->get(ProVM\Common\Service\Decrypt::class),
|
||||||
|
$container->get('attachments_folder'),
|
||||||
|
$container->get(Psr\Log\LoggerInterface::class)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
11
api/setup/setups/04_middlewares.php
Normal file
11
api/setup/setups/04_middlewares.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
|
return [
|
||||||
|
ProVM\Common\Middleware\CustomExceptions::class => function(ContainerInterface $container) {
|
||||||
|
return new ProVM\Common\Middleware\CustomExceptions(
|
||||||
|
$container->get(Nyholm\Psr7\Factory\Psr17Factory::class),
|
||||||
|
$container->get(Psr\Log\LoggerInterface::class)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
];
|
@ -2,6 +2,7 @@
|
|||||||
namespace ProVM\Emails\Model;
|
namespace ProVM\Emails\Model;
|
||||||
|
|
||||||
use ProVM\Common\Define\Model;
|
use ProVM\Common\Define\Model;
|
||||||
|
use ProVM\Common\Exception\Database\BlankResult;
|
||||||
|
|
||||||
class Attachment implements Model
|
class Attachment implements Model
|
||||||
{
|
{
|
||||||
@ -53,13 +54,22 @@ class Attachment implements Model
|
|||||||
public function getStates(): array
|
public function getStates(): array
|
||||||
{
|
{
|
||||||
if (!isset($this->states)) {
|
if (!isset($this->states)) {
|
||||||
$this->setStates($this->getStateRepository()->fetchByAttachment($this->getId()));
|
try {
|
||||||
|
$this->setStates($this->getStateRepository()->fetchByAttachment($this->getId()));
|
||||||
|
} catch (BlankResult $e) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return $this->states;
|
return $this->states;
|
||||||
}
|
}
|
||||||
public function getState(string $name): State\Attachment
|
public function getState(string $name): State\Attachment
|
||||||
{
|
{
|
||||||
return $this->getStates()[$name];
|
try {
|
||||||
|
return $this->getStates()[$name];
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->newState($name);
|
||||||
|
return $this->getStates()[$name];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public function addState(State\Attachment $state): Attachment
|
public function addState(State\Attachment $state): Attachment
|
||||||
{
|
{
|
||||||
@ -76,19 +86,47 @@ class Attachment implements Model
|
|||||||
protected function newState(string $name): Attachment
|
protected function newState(string $name): Attachment
|
||||||
{
|
{
|
||||||
$this->addState((new State\Attachment())
|
$this->addState((new State\Attachment())
|
||||||
->setName($name)
|
|
||||||
->setAttachment($this)
|
->setAttachment($this)
|
||||||
|
->setName($name)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getFullFilename(): string
|
||||||
|
{
|
||||||
|
return implode(' - ', [
|
||||||
|
$this->getMessage()->getSubject(),
|
||||||
|
$this->getMessage()->getDateTime()->format('Y-m-d His'),
|
||||||
|
$this->getFilename()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isDownloaded(): bool
|
||||||
|
{
|
||||||
|
return $this->getState('downloaded')?->getValue() ?? false;
|
||||||
|
}
|
||||||
public function isEncrypted(): bool
|
public function isEncrypted(): bool
|
||||||
{
|
{
|
||||||
return $this->getState('encrypted')->getValue() ?? false;
|
return $this->getState('encrypted')?->getValue() ?? false;
|
||||||
}
|
}
|
||||||
public function isDecrypted(): bool
|
public function isDecrypted(): bool
|
||||||
{
|
{
|
||||||
return $this->getState('encrypted')->getValue() ?? false;
|
try {
|
||||||
|
return $this->getState('decrypted')?->getValue() ?? false;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->newState('decrypted');
|
||||||
|
return $this->getState('decrypted')?->getValue() ?? false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function itIsDownloaded(): Attachment
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$this->getState('downloaded')->setValue(true);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->newState('downloaded');
|
||||||
|
$this->getState('downloaded')->setValue(true);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
public function itIsEncrypted(): Attachment
|
public function itIsEncrypted(): Attachment
|
||||||
{
|
{
|
||||||
@ -105,7 +143,7 @@ class Attachment implements Model
|
|||||||
try {
|
try {
|
||||||
$this->getState('decrypted')->setValue(true);
|
$this->getState('decrypted')->setValue(true);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$this->newState('encrypted');
|
$this->newState('decrypted');
|
||||||
$this->getState('decrypted')->setValue(true);
|
$this->getState('decrypted')->setValue(true);
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
@ -115,8 +153,17 @@ class Attachment implements Model
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'id' => $this->getId(),
|
'id' => $this->getId(),
|
||||||
'message' => $this->getMessage()->toArray(),
|
'message' => [
|
||||||
|
'id' => $this->getMessage()->getId(),
|
||||||
|
'mailbox' => $this->getMessage()->getMailbox()->toArray(),
|
||||||
|
'position' => $this->getMessage()->getPosition(),
|
||||||
|
'uid' => $this->getMessage()->getUID(),
|
||||||
|
'subject' => $this->getMessage()->getSubject(),
|
||||||
|
'from' => $this->getMessage()->getFrom(),
|
||||||
|
'date_time' => $this->getMessage()->getDateTime()->format('Y-m-d H:i:s')
|
||||||
|
],
|
||||||
'filename' => $this->getFilename(),
|
'filename' => $this->getFilename(),
|
||||||
|
'downloaded' => $this->isDownloaded(),
|
||||||
'encrypted' => $this->isEncrypted(),
|
'encrypted' => $this->isEncrypted(),
|
||||||
'decrypted' => $this->isDecrypted()
|
'decrypted' => $this->isDecrypted()
|
||||||
];
|
];
|
||||||
|
61
api/src/Model/Job.php
Normal file
61
api/src/Model/Job.php
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Emails\Model;
|
||||||
|
|
||||||
|
use DateTimeInterface;
|
||||||
|
use ProVM\Common\Define\Model;
|
||||||
|
|
||||||
|
class Job implements Model
|
||||||
|
{
|
||||||
|
protected int $id;
|
||||||
|
protected Message $message;
|
||||||
|
protected DateTimeInterface $dateTime;
|
||||||
|
protected bool $executed;
|
||||||
|
|
||||||
|
public function getId(): int
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
public function getMessage(): Message
|
||||||
|
{
|
||||||
|
return $this->message;
|
||||||
|
}
|
||||||
|
public function getDateTime(): DateTimeInterface
|
||||||
|
{
|
||||||
|
return $this->dateTime;
|
||||||
|
}
|
||||||
|
public function isExecuted(): bool
|
||||||
|
{
|
||||||
|
return $this->executed ?? false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setId(int $id): Job
|
||||||
|
{
|
||||||
|
$this->id = $id;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function setMessage(Message $message): Job
|
||||||
|
{
|
||||||
|
$this->message = $message;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function setDateTime(DateTimeInterface $dateTime): Job
|
||||||
|
{
|
||||||
|
$this->dateTime = $dateTime;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function wasExecuted(): Job
|
||||||
|
{
|
||||||
|
$this->executed = true;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function toArray(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->getId(),
|
||||||
|
'message' => $this->getMessage()->toArray(),
|
||||||
|
'date_time' => $this->getDateTime()->format('Y-m-d H:i:s'),
|
||||||
|
'executed' => $this->isExecuted()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -4,12 +4,15 @@ namespace ProVM\Emails\Model;
|
|||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
use ProVM\Common\Define\Model;
|
use ProVM\Common\Define\Model;
|
||||||
use ProVM\Common\Exception\Database\BlankResult;
|
use ProVM\Common\Exception\Database\BlankResult;
|
||||||
|
use ProVM\Common\Exception\EmptyMailbox;
|
||||||
|
use ProVM\Common\Exception\Mailbox\Stateless;
|
||||||
use Safe\DateTimeImmutable;
|
use Safe\DateTimeImmutable;
|
||||||
|
|
||||||
class Mailbox implements Model
|
class Mailbox implements Model
|
||||||
{
|
{
|
||||||
protected int $id;
|
protected int $id;
|
||||||
protected string $name;
|
protected string $name;
|
||||||
|
protected int $validity;
|
||||||
|
|
||||||
public function getId(): int
|
public function getId(): int
|
||||||
{
|
{
|
||||||
@ -19,6 +22,10 @@ class Mailbox implements Model
|
|||||||
{
|
{
|
||||||
return $this->name;
|
return $this->name;
|
||||||
}
|
}
|
||||||
|
public function getValidity(): int
|
||||||
|
{
|
||||||
|
return $this->validity;
|
||||||
|
}
|
||||||
|
|
||||||
public function setId(int $id): Mailbox
|
public function setId(int $id): Mailbox
|
||||||
{
|
{
|
||||||
@ -30,6 +37,11 @@ class Mailbox implements Model
|
|||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
public function setValidity(int $uidvalidity): Mailbox
|
||||||
|
{
|
||||||
|
$this->validity = $uidvalidity;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
protected \ProVM\Emails\Repository\State\Mailbox $stateRepository;
|
protected \ProVM\Emails\Repository\State\Mailbox $stateRepository;
|
||||||
|
|
||||||
@ -68,19 +80,31 @@ class Mailbox implements Model
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function lastState(): State\Mailbox
|
||||||
|
{
|
||||||
|
if (count($this->getStates()) === 0) {
|
||||||
|
throw new Stateless($this);
|
||||||
|
}
|
||||||
|
return $this->getStates()[array_key_last($this->getStates())];
|
||||||
|
}
|
||||||
public function lastChecked(): ?DateTimeInterface
|
public function lastChecked(): ?DateTimeInterface
|
||||||
{
|
{
|
||||||
if (count($this->getStates()) == 0) {
|
if (count($this->getStates()) == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return $this->getStates()[array_key_last($this->getStates())]->getDateTime();
|
return $this->lastState()->getDateTime();
|
||||||
}
|
}
|
||||||
public function lastCount(): int
|
public function lastCount(): int
|
||||||
{
|
{
|
||||||
if (count($this->getStates()) == 0) {
|
if (count($this->getStates()) == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return $this->getStates()[array_key_last($this->getStates())]->getCount();
|
return $this->lastState()->getCount();
|
||||||
|
}
|
||||||
|
public function lastPosition(): int
|
||||||
|
{
|
||||||
|
$state = $this->lastState()->getUIDs();
|
||||||
|
return array_key_last($state);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
@ -88,6 +112,7 @@ class Mailbox implements Model
|
|||||||
return [
|
return [
|
||||||
'id' => $this->getId(),
|
'id' => $this->getId(),
|
||||||
'name' => $this->getName(),
|
'name' => $this->getName(),
|
||||||
|
//'validity' => $this->getValidity(),
|
||||||
'last_checked' => [
|
'last_checked' => [
|
||||||
'date' => $this->lastChecked() ?? 'never',
|
'date' => $this->lastChecked() ?? 'never',
|
||||||
'count' => $this->lastCount() ?? 0
|
'count' => $this->lastCount() ?? 0
|
||||||
|
@ -146,7 +146,7 @@ class Message implements Model
|
|||||||
|
|
||||||
public function doesHaveAttachments(): Message
|
public function doesHaveAttachments(): Message
|
||||||
{
|
{
|
||||||
if (!isset($this->getStates()['has_attachments'])) {
|
if (!isset($this->getState()['has_attachments'])) {
|
||||||
$this->newState('has_attachments');
|
$this->newState('has_attachments');
|
||||||
}
|
}
|
||||||
$this->getState('has_attachments')->setValue(true);
|
$this->getState('has_attachments')->setValue(true);
|
||||||
@ -206,7 +206,10 @@ class Message implements Model
|
|||||||
'has_attachments' => $this->hasAttachments(),
|
'has_attachments' => $this->hasAttachments(),
|
||||||
'valid_attachments' => $this->hasValidAttachments(),
|
'valid_attachments' => $this->hasValidAttachments(),
|
||||||
'downloaded_attachments' => $this->hasDownloadedAttachments()
|
'downloaded_attachments' => $this->hasDownloadedAttachments()
|
||||||
]
|
],
|
||||||
|
'attachments' => $this->hasValidAttachments() ? array_map(function(Attachment $attachment) {
|
||||||
|
return $attachment->toArray();
|
||||||
|
}, $this->getAttachments()) : []
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -24,7 +24,7 @@ class Attachment implements Model
|
|||||||
}
|
}
|
||||||
public function getValue(): bool
|
public function getValue(): bool
|
||||||
{
|
{
|
||||||
return $this->value;
|
return $this->value ?? false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setId(int $id): Attachment
|
public function setId(int $id): Attachment
|
||||||
|
@ -2,64 +2,62 @@
|
|||||||
namespace ProVM\Emails\Repository;
|
namespace ProVM\Emails\Repository;
|
||||||
|
|
||||||
use PDO;
|
use PDO;
|
||||||
|
use ProVM\Common\Exception\Database\BlankResult;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use ProVM\Common\Define\Model as ModelInterface;
|
use ProVM\Common\Define\Model as ModelInterface;
|
||||||
|
use ProVM\Common\Factory\Model;
|
||||||
use ProVM\Common\Implement\Repository;
|
use ProVM\Common\Implement\Repository;
|
||||||
|
|
||||||
class Attachment extends Repository
|
class Attachment extends Repository
|
||||||
{
|
{
|
||||||
public function __construct(PDO $connection, LoggerInterface $logger, State\Attachment $stateRepository)
|
public function __construct(PDO $connection, LoggerInterface $logger, Model $factory)
|
||||||
{
|
{
|
||||||
parent::__construct($connection, $logger);
|
parent::__construct($connection, $logger);
|
||||||
$this->setStateRepository($stateRepository)
|
$this->setFactory($factory)
|
||||||
->setTable('attachments');
|
->setTable('attachments');
|
||||||
}
|
}
|
||||||
protected State\Attachment $stateRepository;
|
|
||||||
|
|
||||||
public function getStateRepository(): State\Attachment
|
protected Model $factory;
|
||||||
|
public function getFactory(): Model
|
||||||
{
|
{
|
||||||
return $this->stateRepository;
|
return $this->factory;
|
||||||
}
|
}
|
||||||
|
public function setFactory(Model $factory): Attachment
|
||||||
public function setStateRepository(State\Attachment $repository): Attachment
|
|
||||||
{
|
{
|
||||||
$this->stateRepository = $repository;
|
$this->factory = $factory;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function idField(): string
|
|
||||||
{
|
|
||||||
return 'filename';
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function fieldsForInsert(): array
|
protected function fieldsForInsert(): array
|
||||||
{
|
{
|
||||||
return array_merge($this->fieldsForUpdate(), ['filename']);
|
return [
|
||||||
|
'message_id',
|
||||||
|
'filename'
|
||||||
|
];
|
||||||
}
|
}
|
||||||
protected function fieldsForUpdate(): array
|
protected function fieldsForUpdate(): array
|
||||||
{
|
{
|
||||||
return [
|
return $this->fieldsForInsert();
|
||||||
'message_id'
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
protected function fieldsForCreate(): array
|
protected function fieldsForCreate(): array
|
||||||
{
|
{
|
||||||
return $this->fieldsForInsert();
|
return $this->fieldsForInsert();
|
||||||
}
|
}
|
||||||
protected function valuesForUpdate(ModelInterface $model): array
|
protected function valuesForUpdate(ModelInterface $model): array
|
||||||
|
{
|
||||||
|
|
||||||
|
return $this->valuesForInsert($model);
|
||||||
|
}
|
||||||
|
protected function valuesForInsert(ModelInterface $model): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
$model->getMessage()->getId(),
|
$model->getMessage()->getId(),
|
||||||
$model->getFilename()
|
$model->getFilename()
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
protected function valuesForInsert(ModelInterface $model): array
|
|
||||||
{
|
|
||||||
return $this->valuesForUpdate($model);
|
|
||||||
}
|
|
||||||
protected function defaultFind(ModelInterface $model): ModelInterface
|
protected function defaultFind(ModelInterface $model): ModelInterface
|
||||||
{
|
{
|
||||||
return $this->fetchByFilename($model->getFilename());
|
return $this->fetchByMessageAndFilename($model->getMessage()->getId(), $model->getFilename());
|
||||||
}
|
}
|
||||||
protected function valuesForCreate(array $data): array
|
protected function valuesForCreate(array $data): array
|
||||||
{
|
{
|
||||||
@ -70,22 +68,47 @@ class Attachment extends Repository
|
|||||||
}
|
}
|
||||||
protected function defaultSearch(array $data): ModelInterface
|
protected function defaultSearch(array $data): ModelInterface
|
||||||
{
|
{
|
||||||
return $this->fetchByFilename($data['filename']);
|
return $this->fetchByMessageAndFilename($data['message_id'], $data['filename']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function load(array $row): \ProVM\Emails\Model\Attachment
|
public function load(array $row): \ProVM\Emails\Model\Attachment
|
||||||
{
|
{
|
||||||
$model = new \ProVM\Emails\Model\Attachment();
|
return (new \ProVM\Emails\Model\Attachment())
|
||||||
$model
|
->setId($row['id'])
|
||||||
|
->setMessage($this->getFactory()->find(\ProVM\Emails\Model\Message::class)->fetchById($row['message_id']))
|
||||||
->setFilename($row['filename'])
|
->setFilename($row['filename'])
|
||||||
->setStateRepository($this->getStateRepository());
|
->setStateRepository($this->getFactory()->find(\ProVM\Emails\Model\State\Attachment::class));
|
||||||
return $model;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fetchByFilename(string $filename): \ProVM\Emails\Model\Attachment
|
public function save(ModelInterface &$model): void
|
||||||
{
|
{
|
||||||
$query = "SELECT * FROM {$this->getTable()} WHERE filename = ?";
|
parent::save($model);
|
||||||
return $this->fetchOne($query, [$filename]);
|
$states_names = [
|
||||||
|
'downloaded',
|
||||||
|
'encrypted',
|
||||||
|
'decrypted'
|
||||||
|
];
|
||||||
|
foreach ($states_names as $name) {
|
||||||
|
try {
|
||||||
|
$state = $model->getState($name);
|
||||||
|
$this->getFactory()->find(\ProVM\Emails\Model\State\Attachment::class)->save($state);
|
||||||
|
} catch (BlankResult $e) {
|
||||||
|
$this->getLogger()->error($e);
|
||||||
|
$data = [
|
||||||
|
'attachment_id' => $model->getId(),
|
||||||
|
'name' => $name,
|
||||||
|
'value' => false
|
||||||
|
];
|
||||||
|
$state = $this->getFactory()->find(\ProVM\Emails\Model\State\Attachment::class)->create($data);
|
||||||
|
$this->getFactory()->find(\ProVM\Emails\Model\State\Attachment::class)->save($state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fetchByMessageAndFilename(int $message_id, string $filename): \ProVM\Emails\Model\Attachment
|
||||||
|
{
|
||||||
|
$query = "SELECT * FROM {$this->getTable()} WHERE message_id = ? AND filename = ?";
|
||||||
|
return $this->fetchOne($query, [$message_id, $filename]);
|
||||||
}
|
}
|
||||||
public function fetchByMessage(int $message_id): array
|
public function fetchByMessage(int $message_id): array
|
||||||
{
|
{
|
||||||
|
108
api/src/Repository/Job.php
Normal file
108
api/src/Repository/Job.php
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Emails\Repository;
|
||||||
|
|
||||||
|
use PDO;
|
||||||
|
use ProVM\Common\Factory\Model;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Safe\DateTimeImmutable;
|
||||||
|
use ProVM\Common\Define\Model as ModelInterface;
|
||||||
|
use ProVM\Common\Implement\Repository;
|
||||||
|
use ProVM\Emails\Model\Job as BaseModel;
|
||||||
|
|
||||||
|
class Job extends Repository
|
||||||
|
{
|
||||||
|
public function __construct(PDO $connection, LoggerInterface $logger, Model $factory)
|
||||||
|
{
|
||||||
|
parent::__construct($connection, $logger);
|
||||||
|
$this->setFactory($factory)
|
||||||
|
->setTable('attachments_jobs');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected \ProVM\Common\Factory\Model $factory;
|
||||||
|
public function getFactory(): \ProVM\Common\Factory\Model
|
||||||
|
{
|
||||||
|
return $this->factory;
|
||||||
|
}
|
||||||
|
public function setFactory(\ProVM\Common\Factory\Model $factory): Job
|
||||||
|
{
|
||||||
|
$this->factory = $factory;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function fieldsForUpdate(): array
|
||||||
|
{
|
||||||
|
return $this->fieldsForInsert();
|
||||||
|
}
|
||||||
|
protected function valuesForUpdate(ModelInterface $model): array
|
||||||
|
{
|
||||||
|
return $this->valuesForInsert($model);
|
||||||
|
}
|
||||||
|
protected function fieldsForInsert(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'message_id',
|
||||||
|
'date_time',
|
||||||
|
'executed'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
protected function valuesForInsert(ModelInterface $model): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
$model->getMessage()->getId(),
|
||||||
|
$model->getDateTime()->format('Y-m-d H:i:s'),
|
||||||
|
$model->isExecuted() ? 1 : 0
|
||||||
|
];
|
||||||
|
}
|
||||||
|
protected function defaultFind(ModelInterface $model): ModelInterface
|
||||||
|
{
|
||||||
|
return $this->fetchByMessageAndDate($model->getMessage()->getId(), $model->getDateTime()->format('Y-m-d H:i:s'));
|
||||||
|
}
|
||||||
|
protected function fieldsForCreate(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'message_id',
|
||||||
|
'date_time',
|
||||||
|
'executed'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
protected function valuesForCreate(array $data): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
$data['message_id'],
|
||||||
|
$data['date_time'],
|
||||||
|
$data['executed'] ?? 0
|
||||||
|
];
|
||||||
|
}
|
||||||
|
protected function defaultSearch(array $data): ModelInterface
|
||||||
|
{
|
||||||
|
return $this->fetchByMessageAndDate($data['message_id'], $data['date_time']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function load(array $row): ModelInterface
|
||||||
|
{
|
||||||
|
$model = (new BaseModel())
|
||||||
|
->setId($row['id'])
|
||||||
|
->setMessage($this->getFactory()->find(\ProVM\Emails\Model\Message::class)->fetchById($row['message_id']))
|
||||||
|
->setDateTime(new DateTimeImmutable($row['date_time']));
|
||||||
|
if ($row['executed'] ?? 0 === 1) {
|
||||||
|
$model->wasExecuted();
|
||||||
|
}
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fetchAllPending(): array
|
||||||
|
{
|
||||||
|
$query = "SELECT * FROM {$this->getTable()} WHERE `executed` = 0";
|
||||||
|
return $this->fetchMany($query);
|
||||||
|
}
|
||||||
|
public function fetchByMessageAndDate(int $message_id, string $date_time): \ProVM\Emails\Model\Job
|
||||||
|
{
|
||||||
|
$query = "SELECT * FROM {$this->getTable()} WHERE `message_id` = ? AND `date_time` = ?";
|
||||||
|
return $this->fetchOne($query, [$message_id, $date_time]);
|
||||||
|
}
|
||||||
|
public function fetchPendingByMessage(int $message_id): \ProVM\Emails\Model\Job
|
||||||
|
{
|
||||||
|
$query = "SELECT * FROM {$this->getTable()} WHERE `message_id` = ? AND `executed` = 0";
|
||||||
|
return $this->fetchOne($query, [$message_id]);
|
||||||
|
}
|
||||||
|
}
|
@ -35,7 +35,8 @@ class Mailbox extends Repository
|
|||||||
protected function fieldsForInsert(): array
|
protected function fieldsForInsert(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'name'
|
'name',
|
||||||
|
'validity'
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
protected function fieldsForCreate(): array
|
protected function fieldsForCreate(): array
|
||||||
@ -45,7 +46,8 @@ class Mailbox extends Repository
|
|||||||
protected function valuesForInsert(Model $model): array
|
protected function valuesForInsert(Model $model): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
$model->getName()
|
$model->getName(),
|
||||||
|
$model->getValidity()
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
protected function defaultFind(Model $model): Model
|
protected function defaultFind(Model $model): Model
|
||||||
@ -61,7 +63,8 @@ class Mailbox extends Repository
|
|||||||
protected function valuesForCreate(array $data): array
|
protected function valuesForCreate(array $data): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
$data['name']
|
$data['name'],
|
||||||
|
$data['validity']
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
protected function defaultSearch(array $data): Model
|
protected function defaultSearch(array $data): Model
|
||||||
@ -74,6 +77,7 @@ class Mailbox extends Repository
|
|||||||
return (new \ProVM\Emails\Model\Mailbox())
|
return (new \ProVM\Emails\Model\Mailbox())
|
||||||
->setId($row['id'])
|
->setId($row['id'])
|
||||||
->setName($row['name'])
|
->setName($row['name'])
|
||||||
|
->setValidity($row['validity'])
|
||||||
->setStateRepository($this->getStates());
|
->setStateRepository($this->getStates());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Emails\Repository;
|
namespace ProVM\Emails\Repository;
|
||||||
|
|
||||||
|
use DateTimeInterface;
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOException;
|
use PDOException;
|
||||||
use Exception;
|
use Exception;
|
||||||
use ProVM\Common\Define\Model;
|
use ProVM\Common\Define\Model;
|
||||||
use ProVM\Common\Exception\Database\BlankResult;
|
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use ProVM\Common\Implement\Repository;
|
use ProVM\Common\Implement\Repository;
|
||||||
use Safe\DateTimeImmutable;
|
use Safe\DateTimeImmutable;
|
||||||
@ -75,9 +75,9 @@ class Message extends Repository
|
|||||||
protected function valuesForCreate(array $data): array
|
protected function valuesForCreate(array $data): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
$data['uid'],
|
|
||||||
$data['mailbox_id'],
|
$data['mailbox_id'],
|
||||||
$data['position'],
|
$data['position'],
|
||||||
|
$data['uid'],
|
||||||
$data['subject'],
|
$data['subject'],
|
||||||
$data['from'],
|
$data['from'],
|
||||||
$data['date_time']
|
$data['date_time']
|
||||||
@ -94,21 +94,15 @@ class Message extends Repository
|
|||||||
*/
|
*/
|
||||||
public function load(array $row): \ProVM\Emails\Model\Message
|
public function load(array $row): \ProVM\Emails\Model\Message
|
||||||
{
|
{
|
||||||
$model = new \ProVM\Emails\Model\Message();
|
return (new \ProVM\Emails\Model\Message())
|
||||||
$model
|
|
||||||
->setId($row['id'])
|
->setId($row['id'])
|
||||||
->setUID($row['uid'])
|
->setUID($row['uid'])
|
||||||
->setMailbox($this->getFactory()->find(\ProVM\Emails\Model\Mailbox::class)->fetchById($row['mailbox_id']))
|
->setMailbox($this->getFactory()->find(\ProVM\Emails\Model\Mailbox::class)->fetchById($row['mailbox_id']))
|
||||||
->setPosition($row['position'])
|
->setPosition($row['position'])
|
||||||
->setSubject($row['subject'])
|
->setSubject($row['subject'])
|
||||||
->setFrom($row['from'])
|
->setFrom($row['from'])
|
||||||
|
->setDateTime(new DateTimeImmutable($row['date_time']))
|
||||||
->setFactory($this->getFactory());
|
->setFactory($this->getFactory());
|
||||||
try {
|
|
||||||
$model->setDateTime(new DateTimeImmutable($row['date_time']));
|
|
||||||
} catch (Exception | ErrorfuncException $e) {
|
|
||||||
$this->getLogger()->error($e);
|
|
||||||
}
|
|
||||||
return $model;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function save(Model &$model): void
|
public function save(Model &$model): void
|
||||||
@ -144,12 +138,26 @@ class Message extends Repository
|
|||||||
}
|
}
|
||||||
public function fetchByMailbox(int $mailbox_id): array
|
public function fetchByMailbox(int $mailbox_id): array
|
||||||
{
|
{
|
||||||
$query = "SELECT * FROM {$this->getTable()} WHERE mailbox_id = ?";
|
$query = "SELECT * FROM `{$this->getTable()}` WHERE `mailbox_id` = ?";
|
||||||
return $this->fetchMany($query, [$mailbox_id]);
|
return $this->fetchMany($query, [$mailbox_id]);
|
||||||
}
|
}
|
||||||
public function fetchByMailboxAndPosition(int $mailbox_id, int $start, int $amount): array
|
public function fetchByMailboxAndPosition(int $mailbox_id, int $start, int $amount): array
|
||||||
{
|
{
|
||||||
$query = "SELECT * FROM {$this->getTable()} WHERE mailbox_id = ? AND position BETWEEN ? AND ? LIMIT {$amount}";
|
$query = "SELECT * FROM `{$this->getTable()}` WHERE `mailbox_id` = ? AND `position` BETWEEN ? AND ? LIMIT {$amount}";
|
||||||
return $this->fetchMany($query, [$mailbox_id, $start, $start + $amount]);
|
return $this->fetchMany($query, [$mailbox_id, $start, $start + $amount]);
|
||||||
}
|
}
|
||||||
|
public function fetchValidByMailbox(int $mailbox_id): array
|
||||||
|
{
|
||||||
|
$query = "SELECT a.*
|
||||||
|
FROM `{$this->getTable()}` a
|
||||||
|
JOIN `messages_states` b ON b.`message_id` = a.`id`
|
||||||
|
WHERE a.`mailbox_id` = ? AND b.`name` = 'valid_attachments' AND b.`value` = 1";
|
||||||
|
return $this->fetchMany($query, [$mailbox_id]);
|
||||||
|
}
|
||||||
|
public function fetchByMailboxSubjectFromAndDate(int $mailbox_id, string $subject, string $from, DateTimeInterface $dateTime): \ProVM\Emails\Model\Message
|
||||||
|
{
|
||||||
|
$query = "SELECT * FROM `{$this->getTable()}`
|
||||||
|
WHERE `mailbox_id` = ? `subject` = ? AND `from` = ? AND `date_time` = ?";
|
||||||
|
return $this->fetchOne($query, [$mailbox_id, $subject, $from, $dateTime->format('Y-m-d H:i:s')]);
|
||||||
|
}
|
||||||
}
|
}
|
@ -8,10 +8,22 @@ use Psr\Log\LoggerInterface;
|
|||||||
|
|
||||||
class Attachment extends Repository
|
class Attachment extends Repository
|
||||||
{
|
{
|
||||||
public function __construct(PDO $connection, LoggerInterface $logger)
|
public function __construct(PDO $connection, LoggerInterface $logger, \ProVM\Common\Factory\Model $factory)
|
||||||
{
|
{
|
||||||
parent::__construct($connection, $logger);
|
parent::__construct($connection, $logger);
|
||||||
$this->setTable('attachments_states');
|
$this->setTable('attachments_states')
|
||||||
|
->setFactory($factory);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected \ProVM\Common\Factory\Model $factory;
|
||||||
|
public function getFactory(): \ProVM\Common\Factory\Model
|
||||||
|
{
|
||||||
|
return $this->factory;
|
||||||
|
}
|
||||||
|
public function setFactory(\ProVM\Common\Factory\Model $factory): Attachment
|
||||||
|
{
|
||||||
|
$this->factory = $factory;
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function fieldsForInsert(): array
|
protected function fieldsForInsert(): array
|
||||||
@ -40,7 +52,7 @@ class Attachment extends Repository
|
|||||||
}
|
}
|
||||||
protected function valuesForUpdate(Model $model): array
|
protected function valuesForUpdate(Model $model): array
|
||||||
{
|
{
|
||||||
return array_merge($this->valuesForInsert($model), [$model->getId()]);
|
return $this->valuesForInsert($model);
|
||||||
}
|
}
|
||||||
protected function fieldsForCreate(): array
|
protected function fieldsForCreate(): array
|
||||||
{
|
{
|
||||||
@ -63,6 +75,7 @@ class Attachment extends Repository
|
|||||||
{
|
{
|
||||||
return (new \ProVM\Emails\Model\State\Attachment())
|
return (new \ProVM\Emails\Model\State\Attachment())
|
||||||
->setId($row['id'])
|
->setId($row['id'])
|
||||||
|
->setAttachment($this->getFactory()->find(\ProVM\Emails\Model\Attachment::class)->fetchById($row['attachment_id']))
|
||||||
->setName($row['name'])
|
->setName($row['name'])
|
||||||
->setValue($row['value'] !== 0);
|
->setValue($row['value'] !== 0);
|
||||||
}
|
}
|
||||||
|
@ -8,10 +8,22 @@ use ProVM\Common\Implement\Repository;
|
|||||||
|
|
||||||
class Message extends Repository
|
class Message extends Repository
|
||||||
{
|
{
|
||||||
public function __construct(PDO $connection, LoggerInterface $logger)
|
public function __construct(PDO $connection, LoggerInterface $logger, \ProVM\Common\Factory\Model $factory)
|
||||||
{
|
{
|
||||||
parent::__construct($connection, $logger);
|
parent::__construct($connection, $logger);
|
||||||
$this->setTable('messages_states');
|
$this->setTable('messages_states')
|
||||||
|
->setFactory($factory);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected \ProVM\Common\Factory\Model $factory;
|
||||||
|
public function getFactory(): \ProVM\Common\Factory\Model
|
||||||
|
{
|
||||||
|
return $this->factory;
|
||||||
|
}
|
||||||
|
public function setFactory(\ProVM\Common\Factory\Model $factory): Message
|
||||||
|
{
|
||||||
|
$this->factory = $factory;
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function fieldsForUpdate(): array
|
protected function fieldsForUpdate(): array
|
||||||
@ -64,6 +76,7 @@ class Message extends Repository
|
|||||||
return (new \ProVM\Emails\Model\State\Message())
|
return (new \ProVM\Emails\Model\State\Message())
|
||||||
->setId($row['id'])
|
->setId($row['id'])
|
||||||
->setName($row['name'])
|
->setName($row['name'])
|
||||||
|
->setMessage($this->getFactory()->find(\ProVM\Emails\Model\Message::class)->fetchById($row['message_id']))
|
||||||
->setValue(($row['value'] ?? 0) !== 0);
|
->setValue(($row['value'] ?? 0) !== 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,9 +53,10 @@ class GrabAttachments extends Command
|
|||||||
$messages = $this->getMessages();
|
$messages = $this->getMessages();
|
||||||
$io->text('Found ' . count($messages) . ' messages.');
|
$io->text('Found ' . count($messages) . ' messages.');
|
||||||
$io->section('Grabbing Attachments');
|
$io->section('Grabbing Attachments');
|
||||||
foreach ($messages as $message) {
|
foreach ($messages as $job) {
|
||||||
$attachments = $this->grabAttachments($message);
|
$message = $job->message;
|
||||||
$io->text("Found {$attachments} attachments for message UID:{$message}.");
|
$attachments = $this->grabAttachments($message->uid);
|
||||||
|
$io->text("Found {$attachments} attachments for message UID:{$message->uid}.");
|
||||||
}
|
}
|
||||||
$io->success('Done.');
|
$io->success('Done.');
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
this.id = props.id
|
this.id = props.id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
draw() {
|
draw(tr) {
|
||||||
const name = $('<td></td>')
|
const name = $('<td></td>')
|
||||||
const register = $('<td></td>')
|
const register = $('<td></td>')
|
||||||
if (this.registered) {
|
if (this.registered) {
|
||||||
@ -42,27 +42,32 @@
|
|||||||
register.append(
|
register.append(
|
||||||
$('<button></button>').addClass('ui mini circular icon button').append(
|
$('<button></button>').addClass('ui mini circular icon button').append(
|
||||||
$('<i></i>').addClass('save icon')
|
$('<i></i>').addClass('save icon')
|
||||||
).click(() => {
|
).click((e) => {
|
||||||
this.register()
|
this.register($(e.currentTarget))
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return $('<tr></tr>').append(name).append(register)
|
return tr.append(name).append(register)
|
||||||
}
|
}
|
||||||
register() {
|
register(button) {
|
||||||
const uri = '/mailboxes/register'
|
const uri = '/mailboxes/register'
|
||||||
const data = {
|
const data = {
|
||||||
mailboxes: [this.name]
|
mailboxes: [this.name]
|
||||||
}
|
}
|
||||||
|
button.html('').append(
|
||||||
|
$('<i></i>').addClass('redo loading icon')
|
||||||
|
)
|
||||||
return Send.post({
|
return Send.post({
|
||||||
uri,
|
uri,
|
||||||
data
|
data
|
||||||
}).then(response => {
|
}).then(response => {
|
||||||
response.mailboxes.forEach(mb => {
|
response.registered.mailboxes.forEach(mb => {
|
||||||
if (mb.name === this.name && mb.created) {
|
if (mb.name === this.name && mb.registered) {
|
||||||
this.id = mb.id
|
this.id = mb.id
|
||||||
this.registered = true
|
this.registered = true
|
||||||
mailboxes.draw().table()
|
const tr = button.parent().parent()
|
||||||
|
tr.html('')
|
||||||
|
this.draw(tr)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -91,7 +96,7 @@
|
|||||||
mailboxes: [],
|
mailboxes: [],
|
||||||
get: function() {
|
get: function() {
|
||||||
this.draw().loading()
|
this.draw().loading()
|
||||||
return Send.get('/mailboxes').then(response => {
|
return Send.get('/mailboxes').then((response, status, jqXHR) => {
|
||||||
response.mailboxes.forEach(mb => {
|
response.mailboxes.forEach(mb => {
|
||||||
this.mailboxes.push(new Mailbox(mb))
|
this.mailboxes.push(new Mailbox(mb))
|
||||||
})
|
})
|
||||||
@ -138,7 +143,7 @@
|
|||||||
body: () => {
|
body: () => {
|
||||||
const body = $('<tbody></tbody>')
|
const body = $('<tbody></tbody>')
|
||||||
this.mailboxes.forEach(mb => {
|
this.mailboxes.forEach(mb => {
|
||||||
body.append(mb.draw())
|
body.append(mb.draw($('<tr></tr>')))
|
||||||
})
|
})
|
||||||
return body
|
return body
|
||||||
}
|
}
|
||||||
|
@ -19,14 +19,16 @@
|
|||||||
valid: false,
|
valid: false,
|
||||||
downloaded: false
|
downloaded: false
|
||||||
}
|
}
|
||||||
|
attachments
|
||||||
|
|
||||||
constructor({id, uid, subject, date_time, from, states}) {
|
constructor({id, uid, subject, date_time, from, states, attachments}) {
|
||||||
this.set().id(id)
|
this.set().id(id)
|
||||||
.set().uid(uid)
|
.set().uid(uid)
|
||||||
.set().subject(subject)
|
.set().subject(subject)
|
||||||
.set().date(date_time)
|
.set().date(date_time)
|
||||||
.set().from(from)
|
.set().from(from)
|
||||||
.set().states(states)
|
.set().states(states)
|
||||||
|
.set().attachments(attachments)
|
||||||
}
|
}
|
||||||
get() {
|
get() {
|
||||||
return {
|
return {
|
||||||
@ -47,6 +49,9 @@
|
|||||||
},
|
},
|
||||||
states: () => {
|
states: () => {
|
||||||
return this.states
|
return this.states
|
||||||
|
},
|
||||||
|
attachments: () => {
|
||||||
|
return this.attachments
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -82,6 +87,9 @@
|
|||||||
this.states[map_keys[k]] = states[k]
|
this.states[map_keys[k]] = states[k]
|
||||||
})
|
})
|
||||||
return this
|
return this
|
||||||
|
},
|
||||||
|
attachments: attachments => {
|
||||||
|
this.attachments = attachments
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -123,25 +131,35 @@
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.debug(e)
|
console.debug(e)
|
||||||
}
|
}
|
||||||
|
const valid = $('<td></td>').html($('<i></i>').addClass(this.has().valid() ? 'green check circle icon' : 'red times circle icon'))
|
||||||
|
if (this.has().valid() && this.get().attachments().length > 0) {
|
||||||
|
const list = $('<div></div>').addClass('ui list')
|
||||||
|
this.get().attachments().forEach(attachment => {
|
||||||
|
list.append($('<div></div>').addClass('item').html(attachment.filename))
|
||||||
|
})
|
||||||
|
valid.append(
|
||||||
|
$('<div></div>').addClass('ui popup').append(list)
|
||||||
|
)
|
||||||
|
}
|
||||||
return $('<tr></tr>').append(
|
return $('<tr></tr>').append(
|
||||||
$('<td></td>').html(this.get().subject())
|
$('<td></td>').html(this.get().subject())
|
||||||
).append(
|
).append(
|
||||||
$('<td></td>').html(format.format(date))
|
$('<td></td>').html(format.format(date))
|
||||||
).append(
|
).append(
|
||||||
$('<td></td>').html(this.get().from())
|
$('<td></td>').html(this.get().from().full)
|
||||||
).append(
|
).append(
|
||||||
$('<td></td>').html($('<i></i>').addClass(this.has().attachments() ? 'green check circle icon' : 'red times circle icon'))
|
$('<td></td>').html($('<i></i>').addClass(this.has().attachments() ? 'green check circle icon' : 'red times circle icon'))
|
||||||
).append(
|
).append(
|
||||||
$('<td></td>').html($('<i></i>').addClass(this.has().valid() ? 'green check circle icon' : 'red times circle icon'))
|
valid
|
||||||
).append(
|
).append(
|
||||||
$('<td></td>').html($('<i></i>').addClass(this.has().downloaded() ? 'green check circle icon' : 'red times circle icon'))
|
$('<td></td>').html($('<i></i>').addClass(this.has().downloaded() ? 'green check circle icon' : 'red times circle icon'))
|
||||||
).append(
|
).append(
|
||||||
$('<td></td>').append(
|
$('<td></td>').append(
|
||||||
(this.has().attachments() && this.has().valid() && !this.has().downloaded()) ?
|
(this.has().attachments() && this.has().valid() && !this.has().downloaded()) ?
|
||||||
$('<button></button>').addClass('ui mini green circular icon button').append(
|
$('<button></button>').addClass('ui mini green circular icon button').append(
|
||||||
$('<i></i>').addClass('paperclip icon')
|
$('<i></i>').addClass('clock icon')
|
||||||
).click(() => {
|
).click(() => {
|
||||||
this.download().attachments(this.get().uid())
|
this.download().attachments(this.get().id())
|
||||||
}) : ''
|
}) : ''
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -149,21 +167,19 @@
|
|||||||
|
|
||||||
download() {
|
download() {
|
||||||
return {
|
return {
|
||||||
attachments: uid => {
|
attachments: id => {
|
||||||
const uri = '/emails/attachment'
|
const uri = '/messages/schedule'
|
||||||
const data = {
|
const data = {
|
||||||
mailbox: '{{$mailbox_id}}',
|
|
||||||
messages: [
|
messages: [
|
||||||
uid
|
id
|
||||||
],
|
]
|
||||||
extension: 'pdf'
|
|
||||||
}
|
}
|
||||||
return Send.post({
|
return Send.put({
|
||||||
uri,
|
uri,
|
||||||
data
|
data
|
||||||
}).then(response => {
|
}).then(response => {
|
||||||
if (response.attachments_count > 0) {
|
if (response.scheduled > 0) {
|
||||||
alert('Grabbed Attachments')
|
alert('Scheduled Attachments Job')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -191,14 +207,8 @@
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
messages: () => {
|
messages: () => {
|
||||||
const uri = '/messages/valid'
|
const uri = '/mailbox/{{$mailbox_id}}/messages/valid'
|
||||||
const data = {
|
return Send.get(uri).then(response => {
|
||||||
mailboxes: ['{{$mailbox_id}}']
|
|
||||||
}
|
|
||||||
return Send.post({
|
|
||||||
uri,
|
|
||||||
data
|
|
||||||
}).then(response => {
|
|
||||||
if (this.total === null) {
|
if (this.total === null) {
|
||||||
$(this.id.count).html(' (' + response.total + ')')
|
$(this.id.count).html(' (' + response.total + ')')
|
||||||
this.total = response.total
|
this.total = response.total
|
||||||
|
@ -13,8 +13,13 @@
|
|||||||
get: function() {
|
get: function() {
|
||||||
const uri = '/mailboxes/registered'
|
const uri = '/mailboxes/registered'
|
||||||
this.draw().loading()
|
this.draw().loading()
|
||||||
return Send.get(uri).then(response => {
|
return Send.get(uri).then((response, status, jqXHR) => {
|
||||||
this.mailboxes = response.mailboxes
|
if (parseInt(jqXHR.status / 100) !== 2) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (jqXHR.status === 200) {
|
||||||
|
this.mailboxes = response.mailboxes
|
||||||
|
}
|
||||||
this.draw().list()
|
this.draw().list()
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
@ -38,6 +43,10 @@
|
|||||||
list: () => {
|
list: () => {
|
||||||
const parent = $(this.div_id)
|
const parent = $(this.div_id)
|
||||||
parent.html('')
|
parent.html('')
|
||||||
|
if (this.mailboxes.length === 0) {
|
||||||
|
parent.html('No mailboxes registered.')
|
||||||
|
return
|
||||||
|
}
|
||||||
const list = $('<div></div>').addClass('ui list')
|
const list = $('<div></div>').addClass('ui list')
|
||||||
this.mailboxes.forEach(mb => {
|
this.mailboxes.forEach(mb => {
|
||||||
list.append(
|
list.append(
|
||||||
|
Reference in New Issue
Block a user