4 Commits

Author SHA1 Message Date
3124d09480 Merge branch 'feature/jobs' into develop 2023-06-12 21:14:31 -04:00
88f91c4bd5 Jobs setup 2023-06-12 21:14:07 -04:00
03c1dac2f2 Cleanup of cli 2023-06-09 00:54:34 -04:00
9307ba330c Various updates 2023-06-08 20:49:27 -04:00
103 changed files with 2205 additions and 717 deletions

View File

@ -7,8 +7,14 @@
* [ ] Download `attachments` (*encrypted* & *decrypted*). * [ ] Download `attachments` (*encrypted* & *decrypted*).
## CLI ## CLI
* [x] Get `mailboxes` from **[API]** then run `grab messages` job in **[API]** for each one. #### Automatic
* [x] Get `pending attachments` jobs from **[API]** and run. * [x] `mailboxes:check`: Get *registered* `mailboxes` and schedule `messages:grab` for `mailbox_id`.
* [x] `attachments:check`: Check *saved* `attachments` and schedule `attachments:decrypt` for `attachment_id`.
* [x] `jobs:check`: Get *pending* `jobs` and run them.
#### Scheduled
* [x] `messages:grab`: Grab `messages` for `mailbox`. Arguments: `mailbox_id`.
* [x] `attachments:grab`: Grab `attachments` for `message`. Arguments: `message_id`.
* [x] `attachments:decrypt`: Decrypt `attachment`. Arguments: `attachment_id`.
## 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.
@ -21,8 +27,17 @@
## Workflow ## Workflow
* **[User]** Choose `mailboxes` to register or unregister * **[User]** Choose `mailboxes` to register or unregister.
-> **[API]** Register selected `mailboxes` and get `messages` for recently registered. -> **[API]** Register selected `mailboxes`, register new `messages:grab` job.
* **[Cron]** Get registered `mailboxes` -> **[API]** Get `messages` * **[Cron]** Get `jobs`, run `jobs`.
* **[User]** Check messages found -> **[API]** Schedule `attachments` * **[User]** Check messages found -> **[API]** Schedule `attachments`.
* **[Cron]** Get `attachment download` jobs -> **[API]** grab `attachments`
## Jobs
#### Automatic
* [x] Check *registered* `mailboxes` for new `messages`. Every weekday.
* [x] Check if `attachments` are *encrypted*. Every weekday.
* [x] Check for new *scheduled* `jobs`. Every minute.
#### Scheduled
* [ ] Grab `messages` for `mailbox` id.
* [ ] Grab `attachments` for `message` id.
* [ ] Decrypt `attachment`.

View File

@ -1,19 +1,20 @@
<?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 ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service\Attachments as Service;
use ProVM\Emails\Model\Attachment;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service;
use ProVM\Emails\Model\Attachment;
use ProVM\Common\Exception\Request\MissingArgument;
use function Safe\json_decode;
class Attachments class Attachments
{ {
use Json; use Json;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Attachments $service): ResponseInterface
{ {
$attachments = array_map(function(Attachment $attachment) { $attachments = array_map(function(Attachment $attachment) {
return $attachment->toArray(); return $attachment->toArray();
@ -24,35 +25,7 @@ class Attachments
]; ];
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Jobs $jobsService): ResponseInterface public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Attachments $service, LoggerInterface $logger, int $attachment_id): ResponseInterface
{
$body = $request->getBody();
$json = \Safe\json_decode($body->getContents());
if (!isset($json->messages)) {
throw new MissingArgument('messages', 'array', 'message UIDs');
}
$output = [
'messages' => $json->messages,
'total' => count($json->messages),
'saved' => [
'attachments' => [],
'total' => 0
]
];
foreach ($json->messages as $message_id) {
if (!$jobsService->isPending($message_id)) {
continue;
}
if ($service->grab($message_id)) {
$job = $jobsService->find($message_id);
$jobsService->execute($job->getId());
$output['saved']['attachments'] []= $job->toArray();
$output['saved']['total'] ++;
}
}
return $this->withJson($response, $output);
}
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service, LoggerInterface $logger, int $attachment_id): ResponseInterface
{ {
$attachment = $service->getRepository()->fetchById($attachment_id); $attachment = $service->getRepository()->fetchById($attachment_id);
@ -61,4 +34,25 @@ class Attachments
$response->getBody()->write($service->getFile($attachment_id)); $response->getBody()->write($service->getFile($attachment_id));
return $response; return $response;
} }
} public function grab(ServerRequestInterface $request, ResponseInterface $response, Service\Attachments $service, Service\Messages $messagesService): ResponseInterface
{
$body = $request->getBody();
$json = json_decode($body->getContents());
if (!isset($json->messages)) {
throw new MissingArgument('messages', 'array', 'Messages UIDs');
}
$output = [
'messages' => $json->messages,
'attachments' => [],
'total' => 0
];
foreach ($json->messages as $message_uid) {
$message = $messagesService->getLocalMessage($message_uid);
$attachments = $service->grab($message->getId());
$output['attachments'] = array_merge($output['attachments'], $attachments);
$output['total'] += count($attachments);
}
return $this->withJson($response, $output);
}
}

View File

@ -12,7 +12,8 @@ class Base
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{ {
return $this->withJson($response, [ return $this->withJson($response, [
'version' => '1.0.0' 'version' => '1.0.0',
'app' => 'emails'
]); ]);
} }
} }

View File

@ -1,34 +1,36 @@
<?php <?php
namespace ProVM\Common\Controller; namespace ProVM\Common\Controller;
use ProVM\Emails\Model\Job;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use ProVM\Common\Exception\Request\MissingArgument; use ProVM\Common\Exception\Request\MissingArgument;
use ProVM\Common\Implement\Controller\Json; use ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service\Jobs as Service; use ProVM\Common\Service\Jobs as Service;
use function Safe\json_decode;
class Jobs class Jobs
{ {
use Json; use Json;
public function schedule(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Messages $messagesService): ResponseInterface public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$jobs = $service->getRepository()->fetchAll();
return $this->withJson($response, compact('jobs'));
}
public function schedule(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{ {
$body = $request->getBody(); $body = $request->getBody();
$json = \Safe\json_decode($body->getContents()); $json = json_decode($body->getContents());
if (!isset($json->messages)) { if (!isset($json->jobs)) {
throw new MissingArgument('messages', 'array', 'messages ids'); throw new MissingArgument('jobs', 'array', 'job commands with arguments');
} }
$output = [ $output = [
'messages' => $json->messages, 'jobs' => $json->jobs,
'total' => count($json->messages), 'total' => count($json->jobs),
'scheduled' => 0 'scheduled' => 0
]; ];
foreach ($json->messages as $message_id) { foreach ($json->jobs as $job) {
if ($service->schedule($message_id)) { if ($service->queue($job->command, $job->arguments)) {
$message = $messagesService->getRepository()->fetchById($message_id);
$message->doesHaveScheduledDownloads();
$messagesService->getRepository()->save($message);
$output['scheduled'] ++; $output['scheduled'] ++;
} }
} }
@ -36,12 +38,47 @@ class Jobs
} }
public function pending(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface public function pending(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{ {
$pending = array_map(function(Job $job) { $pending = $service->getPending();
return $job->toArray();
}, $service->getPending());
$output = [ $output = [
'total' => count($pending), 'total' => count($pending),
'pending' => $pending 'jobs' => $pending
];
return $this->withJson($response, $output);
}
public function pendingCommands(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$body = $response->getBody();
$json = json_decode($body->getContents());
if (!isset($json->commands)) {
throw new MissingArgument('commands', 'array', 'job commands');
}
$output = [
'commands' => $json->commands,
'total' => count($json->commands),
'pending' => []
];
foreach ($json->commands as $command) {
$pending = $service->getPendingByCommand($command);
if (count($pending) === 0) {
continue;
}
$output['pending'][$command] = $pending;
}
return $this->withJson($response, $output);
}
public function finish(ServerRequestInterface $request, ResponseInterface $response, Service $service, $job_id): ResponseInterface
{
$output = [
'job_id' => $job_id,
'status' => $service->finish($job_id)
];
return $this->withJson($response, $output);
}
public function failed(ServerRequestInterface $request, ResponseInterface $response, Service $service, $job_id): ResponseInterface
{
$output = [
'job_id' => $job_id,
'status' => $service->failed($job_id)
]; ];
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }

View File

@ -1,24 +1,35 @@
<?php <?php
namespace ProVM\Common\Controller; namespace ProVM\Common\Controller;
use Ddeboer\Imap\Exception\MessageDoesNotExistException; 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 ProVM\Common\Exception\Request\MissingArgument;
use ProVM\Common\Implement\Controller\Json; use ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service\Messages as Service; use ProVM\Common\Service;
use ProVM\Emails\Model\Message; use ProVM\Emails\Model\Message;
use function Safe\json_decode;
class Messages class Messages
{ {
use Json; use Json;
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service, int $mailbox_id): ResponseInterface public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, int $mailbox_id): ResponseInterface
{ {
$mailbox = $service->getMailboxes()->get($mailbox_id); $mailbox = $service->getMailboxes()->get($mailbox_id);
$messages = array_map(function(Message $message) { $messages = array_map(function(Message $message) {
return $message->toArray(); return $message->toArray();
}, $service->getAll($mailbox->getName())); }, $service->getAll($mailbox->getName()));
usort($messages, function($a, $b) {
$d = $a['date_time'] - $b['date_time'];
if ($d->days === 0) {
$f = strcmp($a['from'], $b['from']);
if ($f === 0) {
return strcmp($a['subject'], $b['subject']);
}
return $f;
}
return $d->format('%r%a');
});
$output = [ $output = [
'mailbox' => $mailbox->toArray(), 'mailbox' => $mailbox->toArray(),
'total' => count($messages), 'total' => count($messages),
@ -26,7 +37,7 @@ class Messages
]; ];
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
public function valid(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Attachments $attachments, int $mailbox_id): ResponseInterface public function valid(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, Service\Attachments $attachments, int $mailbox_id): ResponseInterface
{ {
$mailbox = $service->getMailboxes()->get($mailbox_id); $mailbox = $service->getMailboxes()->get($mailbox_id);
$messages = array_values(array_filter(array_map(function(Message $message) use ($service, $attachments) { $messages = array_values(array_filter(array_map(function(Message $message) use ($service, $attachments) {
@ -34,6 +45,17 @@ class Messages
}, $service->getValid($mailbox->getName())), function($message) { }, $service->getValid($mailbox->getName())), function($message) {
return $message !== null; return $message !== null;
})); }));
usort($messages, function($a, $b) {
$d = strcmp($a['date_time'], $b['date_time']);
if ($d === 0) {
$f = strcmp($a['from'], $b['from']);
if ($f === 0) {
return strcmp($a['subject'], $b['subject']);
}
return $f;
}
return $d;
});
$output = [ $output = [
'mailbox' => $mailbox->toArray(), 'mailbox' => $mailbox->toArray(),
'total' => count($messages), 'total' => count($messages),
@ -41,33 +63,49 @@ class Messages
]; ];
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
public function get(ServerRequestInterface $request, ResponseInterface $response, Service $service, int $message_id): ResponseInterface public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, int $message_id): ResponseInterface
{ {
$message = $service->getRepository()->fetchById($message_id); $message = $service->getRepository()->fetchById($message_id);
return $this->withJson($response, ['message' => $message->toArray()]); return $this->withJson($response, ['message' => $message->toArray()]);
} }
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Attachments $attachmentsService): ResponseInterface public function grab(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, \ProVM\Common\Service\Attachments $attachmentsService, $mailbox_id): ResponseInterface
{
$output = [
'mailbox_id' => $mailbox_id,
'messages' => [],
'count' => 0
];
$mailbox = $service->getMailboxes()->get($mailbox_id);
$messages = $service->grab($mailbox->getName());
foreach ($messages as $message_id) {
$message = $service->getLocalMessage($message_id);
if ($message->hasValidAttachments()) {
$attachmentsService->create($message->getId());
}
}
$output['messages'] = $messages;
$output['count'] = count($messages);
return $this->withJson($response, $output);
}
public function schedule(ServerRequestInterface $request, ResponseInterface $response, Service\Messages $service, Service\Jobs $jobsService): ResponseInterface
{ {
$body = $request->getBody(); $body = $request->getBody();
$json = \Safe\json_decode($body->getContents()); $json = json_decode($body);
if (!isset($json->mailboxes)) { if (!isset($json->messages)) {
throw new MissingArgument('mailboxes', 'array', 'mailboxes names'); throw new MissingArgument('messages', 'array', 'messages IDs');
} }
$output = [ $output = [
'mailboxes' => $json->mailboxes, 'messages' => $json->messages,
'messages' => [], 'scheduled' => 0
'message_count' => 0
]; ];
foreach ($json->mailboxes as $mailbox_name) { foreach ($json->messages as $message_id) {
$messages = $service->grab($mailbox_name); if ($jobsService->queue('attachments:grab', [$message_id])) {
foreach ($messages as $message) { $message = $service->getRepository()->fetchById($message_id);
if ($message->hasValidAttachments()) { $message->doesHaveScheduledDownloads();
$attachmentsService->create($message); $service->getRepository()->save($message);
} $output['scheduled'] ++;
} }
$output['messages'] = array_merge($output['messages'], $messages);
$output['message_count'] += count($messages);
} }
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
} }

View File

@ -1,6 +1,8 @@
<?php <?php
namespace ProVM\Common\Define; namespace ProVM\Common\Define;
interface Model use JsonSerializable;
interface Model extends JsonSerializable
{ {
} }

View File

@ -0,0 +1,28 @@
<?php
namespace ProVM\Common\Define;
use PDO;
use Psr\Log\LoggerInterface;
interface Repository
{
public function getConnection(): PDO;
public function getTable(): string;
public function getLogger(): LoggerInterface;
public function setConnection(PDO $pdo): Repository;
public function setTable(string $table): Repository;
public function setLogger(LoggerInterface $logger): Repository;
public function isInstalled(): bool;
public function install();
public function resetIndex(): void;
public function create(array $data): Model;
public function save(Model &$model): void;
public function fetchAll(): array;
public function fetchById(int $id): Model;
public function update(Model $model, Model $old): void;
public function optimize(): void;
public function delete(Model $model): void;
}

View File

@ -0,0 +1,15 @@
<?php
namespace ProVM\Common\Exception\Job;
use Exception;
use Throwable;
class Stateless extends Exception
{
public function __construct(int $job_id, ?Throwable $previous = null)
{
$message = "Job {$job_id} does not have any state";
$code = 2002;
parent::__construct($message, $code, $previous);
}
}

View File

@ -1,8 +1,9 @@
<?php <?php
namespace ProVM\Common\Exception; namespace ProVM\Common\Exception;
use Ddeboer\Imap\MailboxInterface;
use Exception; use Exception;
use Throwable;
use Ddeboer\Imap\MailboxInterface;
class EmptyMailbox extends Exception class EmptyMailbox extends Exception
{ {
@ -12,4 +13,4 @@ class EmptyMailbox extends Exception
$code = 101; $code = 101;
parent::__construct($message, $code, $previous); parent::__construct($message, $code, $previous);
} }
} }

View File

@ -2,6 +2,7 @@
namespace ProVM\Common\Exception\Mailbox; namespace ProVM\Common\Exception\Mailbox;
use Exception; use Exception;
use Throwable;
class Invalid extends Exception class Invalid extends Exception
{ {
@ -11,4 +12,4 @@ class Invalid extends Exception
$code = 100; $code = 100;
parent::__construct($message, $code, $previous); parent::__construct($message, $code, $previous);
} }
} }

View File

@ -2,6 +2,7 @@
namespace ProVM\Common\Exception\Mailbox; namespace ProVM\Common\Exception\Mailbox;
use Exception; use Exception;
use Throwable;
use ProVM\Emails\Model\Mailbox; use ProVM\Emails\Model\Mailbox;
class Stateless extends Exception class Stateless extends Exception
@ -12,4 +13,4 @@ class Stateless extends Exception
$code = 102; $code = 102;
parent::__construct($message, $code, $previous); parent::__construct($message, $code, $previous);
} }
} }

View File

@ -0,0 +1,15 @@
<?php
namespace ProVM\Common\Exception\Request\Auth;
use Exception;
use Throwable;
class Forbidden extends Exception
{
public function __construct(?Throwable $previous = null)
{
$message = 'Forbidden';
$code = 413;
parent::__construct($message, $code, $previous);
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace ProVM\Common\Exception\Request\Auth;
use Exception;
use Throwable;
class Unauthorized extends Exception
{
public function __construct(?Throwable $previous = null)
{
$message = 'Unauthorized';
$code = 401;
parent::__construct($message, $code, $previous);
}
}

View File

@ -55,4 +55,4 @@ class Model
} }
return $this->getContainer()->get($repository_class); return $this->getContainer()->get($repository_class);
} }
} }

View File

@ -3,11 +3,11 @@ namespace ProVM\Common\Implement;
use PDO; use PDO;
use PDOException; use PDOException;
use ProVM\Common\Exception\Database\BlankResult;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use ProVM\Common\Define\Model as ModelInterface; use ProVM\Common\Exception\Database\BlankResult;
use ProVM\Common\Define;
abstract class Repository abstract class Repository implements Define\Repository
{ {
public function __construct(PDO $connection, LoggerInterface $logger) public function __construct(PDO $connection, LoggerInterface $logger)
{ {
@ -32,33 +32,32 @@ abstract class Repository
return $this->logger; return $this->logger;
} }
public function setConnection(PDO $pdo): Repository public function setConnection(PDO $pdo): Define\Repository
{ {
$this->connection = $pdo; $this->connection = $pdo;
return $this; return $this;
} }
public function setTable(string $table): Repository public function setTable(string $table): Define\Repository
{ {
$this->table = $table; $this->table = $table;
return $this; return $this;
} }
public function setLogger(LoggerInterface $logger): Repository public function setLogger(LoggerInterface $logger): Define\Repository
{ {
$this->logger = $logger; $this->logger = $logger;
return $this; return $this;
} }
abstract protected function fieldsForUpdate(): array; public function isInstalled(): bool
abstract protected function valuesForUpdate(ModelInterface $model): array;
protected function idProperty(): string
{ {
return 'getId'; $query = "SHOW TABLES LIKE '{$this->getTable()}'";
$st = $this->getConnection()->query($query);
if ($st === false) {
throw new PDOException("Could not run query {$query}");
}
return $st->rowCount() > 0;
} }
protected function idField(): string public function update(Define\Model $model, Define\Model $old): void
{
return 'id';
}
public function update(ModelInterface $model, ModelInterface $old): void
{ {
$query = "UPDATE `{$this->getTable()}` SET "; $query = "UPDATE `{$this->getTable()}` SET ";
$model_values = $this->valuesForUpdate($model); $model_values = $this->valuesForUpdate($model);
@ -79,22 +78,7 @@ abstract class Repository
$st = $this->getConnection()->prepare($query); $st = $this->getConnection()->prepare($query);
$st->execute($values); $st->execute($values);
} }
abstract protected function fieldsForInsert(): array; public function save(Define\Model &$model): void
abstract protected function valuesForInsert(ModelInterface $model): array;
protected function insert(ModelInterface $model): void
{
$fields = $this->fieldsForInsert();
$fields_string = implode(', ', array_map(function($field) {
return "`{$field}`";
}, $fields));
$fields_questions = implode(', ', array_fill(0, count($fields), '?'));
$query = "INSERT INTO `{$this->getTable()}` ({$fields_string}) VALUES ({$fields_questions})";
$values = $this->valuesForInsert($model);
$st = $this->getConnection()->prepare($query);
$st->execute($values);
}
abstract protected function defaultFind(ModelInterface $model): ModelInterface;
public function save(ModelInterface &$model): void
{ {
try { try {
$old = $this->defaultFind($model); $old = $this->defaultFind($model);
@ -107,12 +91,7 @@ abstract class Repository
throw $e; throw $e;
} }
} }
abstract public function load(array $row): ModelInterface; public function create(array $data): Define\Model
abstract protected function fieldsForCreate(): array;
abstract protected function valuesForCreate(array $data): array;
abstract protected function defaultSearch(array $data): ModelInterface;
public function create(array $data): ModelInterface
{ {
try { try {
return $this->defaultSearch($data); return $this->defaultSearch($data);
@ -121,11 +100,6 @@ abstract class Repository
return $this->load($data); return $this->load($data);
} }
} }
protected function getId(ModelInterface $model): int
{
return $model->getId();
}
public function resetIndex(): void public function resetIndex(): void
{ {
$query = "ALTER TABLE `{$this->getTable()}` AUTO_INCREMENT = 1"; $query = "ALTER TABLE `{$this->getTable()}` AUTO_INCREMENT = 1";
@ -136,7 +110,7 @@ abstract class Repository
$query = "OPTIMIZE TABLE `{$this->getTable()}`"; $query = "OPTIMIZE TABLE `{$this->getTable()}`";
$this->getConnection()->query($query); $this->getConnection()->query($query);
} }
public function delete(ModelInterface $model): void public function delete(Define\Model $model): void
{ {
$query = "DELETE FROM `{$this->getTable()}` WHERE `{$this->idField()}` = ?"; $query = "DELETE FROM `{$this->getTable()}` WHERE `{$this->idField()}` = ?";
$st = $this->getConnection()->prepare($query); $st = $this->getConnection()->prepare($query);
@ -144,8 +118,42 @@ abstract class Repository
$this->resetIndex(); $this->resetIndex();
$this->optimize(); $this->optimize();
} }
public function fetchAll(): array
{
$query = "SELECT * FROM `{$this->getTable()}`";
return $this->fetchMany($query);
}
public function fetchById(int $id): Define\Model
{
$query = "SELECT * FROM `{$this->getTable()}` WHERE `{$this->idField()}` = ?";
return $this->fetchOne($query, [$id]);
}
protected function fetchOne(string $query, ?array $values = null): ModelInterface protected function idProperty(): string
{
return 'getId';
}
protected function idField(): string
{
return 'id';
}
protected function insert(Define\Model $model): void
{
$fields = $this->fieldsForInsert();
$fields_string = implode(', ', array_map(function($field) {
return "`{$field}`";
}, $fields));
$fields_questions = implode(', ', array_fill(0, count($fields), '?'));
$query = "INSERT INTO `{$this->getTable()}` ({$fields_string}) VALUES ({$fields_questions})";
$values = $this->valuesForInsert($model);
$st = $this->getConnection()->prepare($query);
$st->execute($values);
}
protected function getId(Define\Model $model): int
{
return $model->getId();
}
protected function fetchOne(string $query, ?array $values = null): Define\Model
{ {
if ($values !== null) { if ($values !== null) {
$st = $this->getConnection()->prepare($query); $st = $this->getConnection()->prepare($query);
@ -174,14 +182,14 @@ abstract class Repository
return array_map([$this, 'load'], $rows); return array_map([$this, 'load'], $rows);
} }
public function fetchAll(): array abstract public function install(): void;
{ abstract public function load(array $row): Define\Model;
$query = "SELECT * FROM `{$this->getTable()}`"; abstract protected function fieldsForUpdate(): array;
return $this->fetchMany($query); abstract protected function valuesForUpdate(Define\Model $model): array;
} abstract protected function fieldsForInsert(): array;
public function fetchById(int $id): ModelInterface abstract protected function valuesForInsert(Define\Model $model): array;
{ abstract protected function defaultFind(Define\Model $model): Define\Model;
$query = "SELECT * FROM `{$this->getTable()}` WHERE `{$this->idField()}` = ?"; abstract protected function fieldsForCreate(): array;
return $this->fetchOne($query, [$id]); abstract protected function valuesForCreate(array $data): array;
} abstract protected function defaultSearch(array $data): Define\Model;
} }

View File

@ -0,0 +1,25 @@
<?php
namespace ProVM\Common\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use ProVM\Common\Exception\Database\BlankResult;
use ProVM\Common\Service;
class Attachments
{
public function __construct(protected Service\Attachments $service, protected LoggerInterface $logger) {}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$this->service->checkDownloaded();
$this->service->checkEncryption();
} catch (BlankResult $e) {
$this->logger->notice($e);
}
return $handler->handle($request);
}
}

View File

@ -6,19 +6,19 @@ use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use ProVM\Common\Exception\Request\Auth\Unauthorized;
use ProVM\Common\Service\Auth as Service;
class Auth class Auth
{ {
public function __construct(ResponseFactoryInterface $factory, LoggerInterface $logger, string $api_key) public function __construct(ResponseFactoryInterface $factory, LoggerInterface $logger, protected Service $service)
{ {
$this->setResponseFactory($factory); $this->setResponseFactory($factory);
$this->setLogger($logger); $this->setLogger($logger);
$this->setAPIKey($api_key);
} }
protected ResponseFactoryInterface $factory; protected ResponseFactoryInterface $factory;
protected LoggerInterface $logger; protected LoggerInterface $logger;
protected string $api_key;
public function getResponseFactory(): ResponseFactoryInterface public function getResponseFactory(): ResponseFactoryInterface
{ {
@ -28,10 +28,6 @@ class Auth
{ {
return $this->logger; return $this->logger;
} }
public function getAPIKey(): string
{
return $this->api_key;
}
public function setResponseFactory(ResponseFactoryInterface $factory): Auth public function setResponseFactory(ResponseFactoryInterface $factory): Auth
{ {
@ -43,30 +39,23 @@ class Auth
$this->logger = $logger; $this->logger = $logger;
return $this; return $this;
} }
public function setAPIKey(string $key): Auth
{
$this->api_key = $key;
return $this;
}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{ {
if ($request->getMethod() === 'OPTIONS') { if ($request->getMethod() === 'OPTIONS') {
return $handler->handle($request); return $handler->handle($request);
} }
$auths = $request->getHeader('Authorization'); try {
foreach ($auths as $auth) { if ($this->service->validate($request)) {
if (str_contains($auth, 'Bearer')) { return $handler->handle($request);
$key = str_replace('Bearer ', '', $auth);
if (sha1($this->getAPIKey()) === $key) {
return $handler->handle($request);
}
} }
} catch (Unauthorized $e) {
$response = $this->getResponseFactory()->createResponse($e->getCode());
$response->getBody()->write(json_encode(['error' => $e->getCode(), 'message' => $e->getMessage()]));
} }
$this->getLogger()->debug(sha1($this->getAPIKey())); $response = $this->getResponseFactory()->createResponse(413);
$response = $this->getResponseFactory()->createResponse(401); $response->getBody()->write(\Safe\json_encode(['error' => 413, 'message' => 'Incorrect token']));
$response->getBody()->write(\Safe\json_encode(['error' => 401, 'message' => 'Incorrect token']));
return $response return $response
->withHeader('Content-Type', 'application/json'); ->withHeader('Content-Type', 'application/json');
} }
} }

View File

@ -0,0 +1,20 @@
<?php
namespace ProVM\Common\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use ProVM\Common\Service\Install as Service;
class Install
{
public function __construct(protected Service $service) {}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (!$this->service->check()) {
$this->service->install();
}
return $handler->handle($request);
}
}

View File

@ -5,34 +5,21 @@ use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use function Safe\json_encode;
class Logging class Logging
{ {
public function __construct(LoggerInterface $logger) { public function __construct(protected LoggerInterface $logger) {}
$this->setLogger($logger);
}
protected LoggerInterface $logger;
public function getLogger(): LoggerInterface
{
return $this->logger;
}
public function setLogger(LoggerInterface $logger): Logging
{
$this->logger = $logger;
return $this;
}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{ {
$response = $handler->handle($request); $response = $handler->handle($request);
$output = [ $output = [
'uri' => var_export($request->getUri(), true), 'uri' => var_export($request->getUri(), true),
'body' => $request->getBody()->getContents() 'body' => $request->getBody()->getContents(),
'response' => (clone $response)->getBody()->getContents()
]; ];
$this->getLogger()->info(\Safe\json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); $this->logger->info(json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
return $response; return $response;
} }
} }

View File

@ -0,0 +1,24 @@
<?php
namespace ProVM\Common\Middleware;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use ProVM\Common\Exception\Database\BlankResult;
use ProVM\Common\Service;
class Mailboxes
{
public function __construct(protected Service\Messages $service, protected LoggerInterface $logger) {}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$this->service->checkUpdate();
} catch (BlankResult $e) {
$this->logger->notice($e);
}
return $handler->handle($request);
}
}

View File

@ -1,15 +1,15 @@
<?php <?php
namespace ProVM\Common\Service; namespace ProVM\Common\Service;
use ProVM\Common\Exception\Database\BlankResult;
use Psr\Log\LoggerInterface;
use Ddeboer\Imap\Message\AttachmentInterface; use Ddeboer\Imap\Message\AttachmentInterface;
use Ddeboer\Imap\MessageInterface; use Ddeboer\Imap\MessageInterface;
use Nyholm\Psr7\Stream; use Safe\Exceptions\FilesystemException;
use ProVM\Common\Exception\Message\NoAttachments; use ProVM\Common\Exception\Message\NoAttachments;
use ProVM\Emails\Model\Message; use ProVM\Emails\Model\Message;
use ProVM\Emails\Repository\Attachment; use ProVM\Emails\Repository\Attachment;
use Psr\Http\Message\StreamInterface; use function Safe\{file_get_contents,file_put_contents};
use Psr\Log\LoggerInterface;
use Safe\Exceptions\FilesystemException;
class Attachments extends Base class Attachments extends Base
{ {
@ -100,13 +100,33 @@ class Attachments extends Base
$attachment->getFullFilename() $attachment->getFullFilename()
]); ]);
} }
return \Safe\file_get_contents($filename); return file_get_contents($filename);
} }
public function getAll(): array public function getAll(): array
{ {
return $this->getRepository()->fetchAll(); return $this->getRepository()->fetchAll();
} }
public function getDownloadedFiles(): array
{
$downloaded = [];
$folder = $this->getFolder();
$files = new \FilesystemIterator($folder);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
$name = $file->getBasename(".{$file->getExtension()}");
list($date, $subject, $filename) = explode(' - ', $name);
try {
$message = $this->getMessages()->find($subject, $date)[0];
$filename = "{$filename}.{$file->getExtension()}";
$downloaded []= compact('message', 'filename');
} catch (BlankResult $e) {
}
}
return $downloaded;
}
public function create(int $message_id): array public function create(int $message_id): array
{ {
$message = $this->getMessages()->getRepository()->fetchById($message_id); $message = $this->getMessages()->getRepository()->fetchById($message_id);
@ -183,7 +203,7 @@ class Attachments extends Base
$attachment->getFullFilename() $attachment->getFullFilename()
]); ]);
try { try {
\Safe\file_put_contents($destination, $remote_attachment->getDecodedContent()); file_put_contents($destination, $remote_attachment->getDecodedContent());
return true; return true;
} catch (FilesystemException $e) { } catch (FilesystemException $e) {
$this->getLogger()->error($e); $this->getLogger()->error($e);
@ -236,6 +256,9 @@ class Attachments extends Base
if (!$message->hasValidAttachments()) { if (!$message->hasValidAttachments()) {
return false; return false;
} }
if ($message->hasDownloadedAttachments()) {
return true;
}
foreach ($remote_message->getAttachments() as $attachment) { foreach ($remote_message->getAttachments() as $attachment) {
if (!str_contains($attachment->getFilename(), '.pdf')) { if (!str_contains($attachment->getFilename(), '.pdf')) {
continue; continue;
@ -247,4 +270,59 @@ class Attachments extends Base
} }
return true; return true;
} }
} public function find(Message $message, string $filename): \ProVM\Emails\Model\Attachment
{
return $this->getRepository()->fetchByMessageAndFilename($message->getId(), $filename);
}
public function exists(Message $message, string $filename): bool
{
try {
$this->find($message, $filename);
return true;
} catch (BlankResult $e) {
return false;
}
}
public function add(Message $message, string $filename): bool
{
$data = [
'message_id' => $message->getId(),
'filename' => $filename
];
try {
$attachment = $this->getRepository()->create($data);
$attachment->itIsDownloaded();
$this->getRepository()->save($attachment);
$message->doesHaveDownloadedAttachments();
$this->getMessages()->getRepository()->save($message);
return true;
} catch (PDOException $e) {
$this->getLogger()->error($e);
return false;
}
}
public function checkDownloaded(): void
{
$data = $this->getDownloadedFiles();
foreach ($data as $info) {
if (!$this->exists($info['message'], $info['filename'])) {
$this->logger->info("Updating attachment {$info['filename']} for message {$info['message']->getSubject()}");
$this->add($info['message'], $info['filename']);
}
}
}
public function getDownloaded(): array
{
return $this->getRepository()->fetchDownloaded();
}
public function checkEncryption(): void
{
$attachments = $this->getDownloaded();
foreach ($attachments as $attachment) {
if ($attachment->isEncrypted() and !$attachment->isDecrypted()) {
$this->logger->notice("Schedule decrypt for {$attachment->getFullFilename()}");
$this->decrypt($attachment->getMessage(), $attachment->getFilename());
}
}
}
}

View File

@ -0,0 +1,33 @@
<?php
namespace ProVM\Common\Service;
use Psr\Http\Message\ServerRequestInterface;
use ProVM\Common\Exception\Request\Auth\Unauthorized;
class Auth
{
public function __construct(protected string $api_key) {}
public function validate(ServerRequestInterface $request): bool
{
$key = $this->getHeaderKey($request);
if (sha1($this->api_key) === $key) {
return true;
}
return false;
}
protected function getHeaderKey(ServerRequestInterface $request): string
{
if (!$request->hasHeader('Authorization')) {
throw new Unauthorized();
}
$auths = $request->getHeader('Authorization');
foreach ($auths as $auth) {
if (str_contains($auth, 'Bearer')) {
return str_replace('Bearer ', '', $auth);
}
}
throw new Unauthorized();
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace ProVM\Common\Service;
use ProVM\Common\Factory\Model;
class Install
{
public function __construct(protected Model $factory, protected array $model_list) {}
public function check(): bool
{
foreach ($this->model_list as $model_class) {
$repository = $this->factory->find($model_class);
if (!$repository->isInstalled()) {
return false;
}
}
return true;
}
public function install(): void
{
$check = true;
$repository = null;
foreach ($this->model_list as $model_class) {
$repository = $this->factory->find($model_class);
if ($check) {
$query = "SET FOREIGN_KEY_CHECKS = 0";
$repository->getConnection()->query($query);
$check = false;
}
if (!$repository->isInstalled()) {
$repository->install();
}
}
if (!$check) {
$query = "SET FOREIGN_KEY_CHECKS = 1";
$repository->getConnection()->query($query);
}
}
}

View File

@ -1,40 +1,49 @@
<?php <?php
namespace ProVM\Common\Service; namespace ProVM\Common\Service;
use Illuminate\Support\Facades\Date;
use PDOException; use PDOException;
use ProVM\Common\Exception\Database\BlankResult; use ProVM\Common\Exception\Database\BlankResult;
use Safe\DateTimeImmutable; use Safe\DateTimeImmutable;
use ProVM\Emails\Repository\Job; use ProVM\Emails\Repository;
use ProVM\Emails\Model;
class Jobs extends Base class Jobs extends Base
{ {
public function __construct(Job $repository) public function __construct(Repository\Job $repository, protected Repository\State\Job $stateRepository)
{ {
$this->setRepository($repository); $this->setRepository($repository);
} }
protected Job $repository; protected Repository\Job $repository;
public function getRepository(): Job public function getRepository(): Repository\Job
{ {
return $this->repository; return $this->repository;
} }
public function setRepository(Job $repository): Jobs public function setRepository(Repository\Job $repository): Jobs
{ {
$this->repository = $repository; $this->repository = $repository;
return $this; return $this;
} }
public function schedule(int $message_id): bool public function queue(string $command, ?array $arguments = null): bool
{ {
$data = [ $data = [
'message_id' => $message_id, 'command' => $command,
'date_time' => (new DateTimeImmutable())->format('Y-m-d H:i:s') 'arguments' => implode(' ', $arguments)
]; ];
try { try {
$job = $this->getRepository()->create($data); $job = $this->getRepository()->create($data);
$this->getRepository()->save($job); $this->getRepository()->save($job);
$data = [
'job_id' => $job->getId(),
'date_time' => (new DateTimeImmutable())->format('Y-m-d H:i:s'),
'status' => Model\State\Job::Pending
];
$state = $this->stateRepository->create($data);
$this->stateRepository->save($state);
return true; return true;
} catch (PDOException $e) { } catch (PDOException $e) {
return false; return false;
@ -44,28 +53,42 @@ class Jobs extends Base
{ {
return $this->getRepository()->fetchAllPending(); return $this->getRepository()->fetchAllPending();
} }
public function isPending(int $message_id): bool public function getPendingByCommand(string $command): array
{ {
try { try {
$this->getRepository()->fetchPendingByMessage($message_id); return $this->getRepository()->fetchAllPendingByCommand($command);
return true;
} catch (BlankResult $e) { } catch (BlankResult $e) {
return false; return [];
} }
} }
public function find(int $message_id): \ProVM\Emails\Model\Job public function finish(int $job_id): bool
{
return $this->getRepository()->fetchPendingByMessage($message_id);
}
public function execute(int $job_id): bool
{ {
$data = [
'job_id' => $job_id,
'date_time' => (new DateTimeImmutable())->format('Y-m-d H:i:s'),
'status' => Model\State\Job::Executed
];
try { try {
$job = $this->getRepository()->fetchById($job_id); $state = $this->stateRepository->create($data);
$job->wasExecuted(); $this->stateRepository->save($state);
$this->getRepository()->save($job);
return true; return true;
} catch (PDOException $e) { } catch (PDOException $e) {
return false; return false;
} }
} }
} public function failed(int $job_id): bool
{
$data = [
'job_id' => $job_id,
'date_time' => (new DateTimeImmutable())->format('Y-m-d H:i:s'),
'status' => Model\State\Job::Failure
];
try {
$state = $this->stateRepository->create($data);
$this->stateRepository->save($state);
return true;
} catch (PDOException $e) {
return false;
}
}
}

View File

@ -11,7 +11,7 @@ use ProVM\Emails\Repository\State;
class Mailboxes extends Base class Mailboxes extends Base
{ {
public function __construct(Mailbox $repository, Remote\Mailboxes $remoteService, State\Mailbox $states, LoggerInterface $logger) public function __construct(Mailbox $repository, Remote\Mailboxes $remoteService, State\Mailbox $states, LoggerInterface $logger, protected int $max_update_days)
{ {
$this->setRepository($repository) $this->setRepository($repository)
->setRemoteService($remoteService) ->setRemoteService($remoteService)
@ -35,7 +35,6 @@ class Mailboxes extends Base
{ {
return $this->statesRepository; return $this->statesRepository;
} }
public function setRepository(Mailbox $repository): Mailboxes public function setRepository(Mailbox $repository): Mailboxes
{ {
$this->repository = $repository; $this->repository = $repository;
@ -110,6 +109,7 @@ class Mailboxes extends Base
$this->getStatesRepository()->save($state); $this->getStatesRepository()->save($state);
return true; return true;
} catch (PDOException $e) { } catch (PDOException $e) {
$this->getLogger()->error($e);
return false; return false;
} }
} }
@ -141,4 +141,13 @@ class Mailboxes extends Base
} }
return true; return true;
} }
} public function isUpdated(\ProVM\Emails\Model\Mailbox $mailbox): bool
{
$states = $mailbox->getStates();
if (count($states) === 0) {
return false;
}
$last = $states[count($states) - 1];
return abs((int) $last->getDateTime()->diff(new \DateTimeImmutable())->format('%r%a')) < $this->max_update_days;
}
}

View File

@ -4,6 +4,7 @@ namespace ProVM\Common\Service;
use Ddeboer\Imap\Exception\MessageDoesNotExistException; use Ddeboer\Imap\Exception\MessageDoesNotExistException;
use Ddeboer\Imap\MailboxInterface; use Ddeboer\Imap\MailboxInterface;
use PDOException; use PDOException;
use ProVM\Common\Exception\Database\BlankResult;
use ProVM\Common\Exception\Mailbox\Stateless; use ProVM\Common\Exception\Mailbox\Stateless;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Ddeboer\Imap\MessageInterface; use Ddeboer\Imap\MessageInterface;
@ -14,17 +15,19 @@ use Safe\DateTimeImmutable;
class Messages extends Base class Messages extends Base
{ {
public function __construct(Mailboxes $mailboxes, Message $repository, Remote\Messages $remoteService, LoggerInterface $logger) public function __construct(Mailboxes $mailboxes, Message $repository, Remote\Messages $remoteService, Jobs $jobsService, LoggerInterface $logger)
{ {
$this->setMailboxes($mailboxes) $this->setMailboxes($mailboxes)
->setRepository($repository) ->setRepository($repository)
->setRemoteService($remoteService) ->setRemoteService($remoteService)
->setJobsService($jobsService)
->setLogger($logger); ->setLogger($logger);
} }
protected Mailboxes $mailboxes; protected Mailboxes $mailboxes;
protected Message $repository; protected Message $repository;
protected Remote\Messages $remoteService; protected Remote\Messages $remoteService;
protected Jobs $jobsService;
public function getMailboxes(): Mailboxes public function getMailboxes(): Mailboxes
{ {
@ -38,6 +41,10 @@ class Messages extends Base
{ {
return $this->remoteService; return $this->remoteService;
} }
public function getJobsService(): Jobs
{
return $this->jobsService;
}
public function setMailboxes(Mailboxes $mailboxes): Messages public function setMailboxes(Mailboxes $mailboxes): Messages
{ {
@ -54,6 +61,11 @@ class Messages extends Base
$this->remoteService = $service; $this->remoteService = $service;
return $this; return $this;
} }
public function setJobsService(Jobs $service): Messages
{
$this->jobsService = $service;
return $this;
}
public function getLocalMessage(string $message_uid): \ProVM\Emails\Model\Message public function getLocalMessage(string $message_uid): \ProVM\Emails\Model\Message
{ {
@ -174,4 +186,17 @@ class Messages extends Base
} }
return false; return false;
} }
} public function find(string $subject, string $date): array
{
return $this->repository->fetchAllBySubjectAndDate($subject, new DateTimeImmutable($date));
}
public function checkUpdate(): void
{
$registered = $this->getMailboxes()->getRegistered();
foreach ($registered as $mailbox) {
if (!$this->getMailboxes()->isUpdated($mailbox)) {
$this->getJobsService()->queue('messages:grab', [$mailbox->getId()]);
}
}
}
}

View File

@ -7,7 +7,7 @@ services:
- ${API_PATH:-.}:/app/api - ${API_PATH:-.}:/app/api
- ${API_PATH:-.}/nginx.conf:/etc/nginx/conf.d/api.conf - ${API_PATH:-.}/nginx.conf:/etc/nginx/conf.d/api.conf
ports: ports:
- "${API_PORT:-8080}:8080" - "${API_PORT:-8080}:81"
api: api:
profiles: profiles:
- api - api

View File

@ -1,15 +1,15 @@
server { server {
listen 0.0.0.0:8080; listen 81;
root /app/api/public; root /app/api/public;
index index.php index.html index.htm; index index.php index.html index.htm;
access_log /var/logs/nginx/access.log; access_log /var/logs/nginx/api.access.log;
error_log /var/logs/nginx/error.log; error_log /var/logs/nginx/api.error.log;
location / { location / {
try_files $uri $uri/ /index.php?$query_string; try_files $uri /index.php$is_args$args;
} }
location ~ \.php$ { location ~ \.php {
if ($request_method = 'OPTIONS') { if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range'; add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
@ -26,11 +26,12 @@ server {
try_files $uri =404; try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_pass api:9000; fastcgi_pass api:9000;
fastcgi_index index.php; fastcgi_index index.php;
include fastcgi_params;
fastcgi_param REQUEST_URI $request_uri; fastcgi_param REQUEST_URI $request_uri;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_INFO $fastcgi_path_info;
} }
} }

View File

@ -7,12 +7,8 @@ $app = require_once implode(DIRECTORY_SEPARATOR, [
Monolog\ErrorHandler::register($app->getContainer()->get(Psr\Log\LoggerInterface::class)); Monolog\ErrorHandler::register($app->getContainer()->get(Psr\Log\LoggerInterface::class));
try { try {
$app->run(); $app->run();
} catch (Error | Exception $e) { } catch (Error $e) {
$logger = $app->getContainer()->get(Psr\Log\LoggerInterface::class); $app->getContainer()->get(Psr\Log\LoggerInterface::class)->error($e);
if (isset($_REQUEST)) { } catch (Exception $e) {
$logger->debug(Safe\json_encode(compact('_REQUEST'))); $app->getContainer()->get(Psr\Log\LoggerInterface::class)->debug($e);
}
$logger->debug(Safe\json_encode(compact('_SERVER')));
$logger->error($e);
throw $e;
} }

View File

@ -11,6 +11,7 @@ $app->group('/mailboxes', function($app) {
$app->group('/mailbox/{mailbox_id}', function($app) { $app->group('/mailbox/{mailbox_id}', function($app) {
$app->group('/messages', function($app) { $app->group('/messages', function($app) {
$app->get('/grab[/]', [Messages::class, 'grab']);
$app->get('/valid[/]', [Messages::class, 'valid']); $app->get('/valid[/]', [Messages::class, 'valid']);
$app->get('[/]', Messages::class); $app->get('[/]', Messages::class);
}); });

View File

@ -2,10 +2,10 @@
use ProVM\Common\Controller\Attachments; use ProVM\Common\Controller\Attachments;
$app->group('/attachments', function($app) { $app->group('/attachments', function($app) {
$app->put('/grab', [Attachments::class, 'grab']); $app->put('/grab[/]', [Attachments::class, 'grab']);
$app->post('/decrypt', [Attachments::class, 'decrypt']); $app->get('/pending[/]', [Attachments::class, 'pending']);
$app->get('[/]', Attachments::class); $app->get('[/]', Attachments::class);
}); });
$app->group('/attachment/{attachment_id}', function($app) { $app->group('/attachment/{attachment_id}', function($app) {
$app->get('[/]', [Attachments::class, 'get']); $app->get('[/]', [Attachments::class, 'get']);
}); });

View File

@ -4,9 +4,9 @@ use ProVM\Common\Controller\Jobs;
$app->group('/messages', function($app) { $app->group('/messages', function($app) {
$app->put('/grab', [Messages::class, 'grab']); $app->put('/grab', [Messages::class, 'grab']);
$app->put('/schedule', [Jobs::class, 'schedule']); $app->put('/schedule', [Messages::class, 'schedule']);
$app->get('/pending', [Jobs::class, 'pending']); //$app->get('/pending', [Jobs::class, 'pending']);
}); });
$app->group('/message/{message_id}', function($app) { $app->group('/message/{message_id}', function($app) {
$app->get('[/]', [Messages::class, 'get']); $app->get('[/]', [Messages::class, 'get']);
}); });

View File

@ -0,0 +1,15 @@
<?php
use ProVM\Common\Controller\Jobs;
$app->group('/jobs', function($app) {
$app->group('/pending', function($app) {
$app->put('/command[/]', [Jobs::class, 'pendingCommands']);
$app->get('[/]', [Jobs::class, 'pending']);
});
$app->post('/schedule[/]', [Jobs::class, 'schedule']);
$app->get('[/]', Jobs::class);
});
$app->group('/job/{job_id}', function($app) {
$app->get('/finish[/]', [Jobs::class, 'finish']);
$app->get('/failed[/]', [Jobs::class, 'failed']);
});

View File

@ -1,7 +1,10 @@
<?php <?php
use DI\ContainerBuilder;
use DI\Bridge\Slim\Bridge;
require_once 'composer.php'; require_once 'composer.php';
$builder = new \DI\ContainerBuilder(); $builder = new ContainerBuilder();
$folders = [ $folders = [
'settings', 'settings',
@ -24,7 +27,7 @@ foreach ($folders as $f) {
$builder->addDefinitions($file->getRealPath()); $builder->addDefinitions($file->getRealPath());
} }
} }
$app = \DI\Bridge\Slim\Bridge::create($builder->build()); $app = Bridge::create($builder->build());
$folder = implode(DIRECTORY_SEPARATOR, [ $folder = implode(DIRECTORY_SEPARATOR, [
__DIR__, __DIR__,

View File

@ -0,0 +1,4 @@
<?php
$app->add($app->getContainer()->get(ProVM\Common\Middleware\Attachments::class));
$app->add($app->getContainer()->get(ProVM\Common\Middleware\Mailboxes::class));
$app->add($app->getContainer()->get(ProVM\Common\Middleware\Install::class));

View File

@ -5,7 +5,7 @@ return [
'host' => $_ENV['EMAIL_HOST'], 'host' => $_ENV['EMAIL_HOST'],
'username' => $_ENV['EMAIL_USERNAME'], 'username' => $_ENV['EMAIL_USERNAME'],
'password' => $_ENV['EMAIL_PASSWORD'], 'password' => $_ENV['EMAIL_PASSWORD'],
'folder' => $_ENV['EMAIL_FOLDER'], //'folder' => $_ENV['EMAIL_FOLDER'],
]; ];
if (isset($_ENV['EMAIL_PORT'])) { if (isset($_ENV['EMAIL_PORT'])) {
$data['port'] = $_ENV['EMAIL_PORT']; $data['port'] = $_ENV['EMAIL_PORT'];
@ -27,5 +27,6 @@ return [
$arr['port'] = $_ENV['MYSQL_PORT']; $arr['port'] = $_ENV['MYSQL_PORT'];
} }
return (object) $arr; return (object) $arr;
} },
'max_update_days' => 7
]; ];

View File

@ -0,0 +1,33 @@
<?php
return [
'model_list' => function() {
function getClassesFromFolder(string $folder): array {
$classes = [];
$files = new FilesystemIterator($folder);
foreach ($files as $file) {
if ($file->isDir()) {
$classes = array_merge($classes, getClassesFromFolder($file->getRealPath()));
continue;
}
$classes []= ltrim(str_replace("\\\\", "\\", implode("\\", [
'ProVM',
'Emails',
'Model',
str_replace([implode(DIRECTORY_SEPARATOR, [
dirname(__FILE__, 3),
'src',
'Model'
]), '/'], ['', "\\"], $folder),
$file->getBasename(".{$file->getExtension()}")
])), "\\");
}
return $classes;
}
$folder = implode(DIRECTORY_SEPARATOR, [
dirname(__FILE__, 3),
'src',
'Model'
]);
return getClassesFromFolder($folder);
}
];

View File

@ -1,4 +1,5 @@
<?php <?php
return [ return [
'log_file' => '/logs/php.log' 'log_file' => '/logs/php.log',
]; 'logstash_socket' => 'localhost:50000'
];

View File

@ -5,13 +5,13 @@ return [
Ddeboer\Imap\ServerInterface::class => function(ContainerInterface $container) { Ddeboer\Imap\ServerInterface::class => function(ContainerInterface $container) {
$emails = $container->get('emails'); $emails = $container->get('emails');
if (isset($emails->port)) { if (isset($emails->port)) {
return new \Ddeboer\Imap\Server($emails->host, $emails->port); return new Ddeboer\Imap\Server($emails->host, $emails->port);
} }
return new \Ddeboer\Imap\Server($emails->host); return new Ddeboer\Imap\Server($emails->host);
}, },
\Ddeboer\Imap\ConnectionInterface::class => function(ContainerInterface $container) { Ddeboer\Imap\ConnectionInterface::class => function(ContainerInterface $container) {
$emails = $container->get('emails'); $emails = $container->get('emails');
$server = $container->get(\Ddeboer\Imap\ServerInterface::class); $server = $container->get(Ddeboer\Imap\ServerInterface::class);
return $server->authenticate($emails->username, $emails->password); return $server->authenticate($emails->username, $emails->password);
}, },
PDO::class => function(ContainerInterface $container) { PDO::class => function(ContainerInterface $container) {

View File

@ -18,5 +18,23 @@ return [
$container->get('attachments_folder'), $container->get('attachments_folder'),
$container->get(Psr\Log\LoggerInterface::class) $container->get(Psr\Log\LoggerInterface::class)
); );
},
ProVM\Common\Service\Mailboxes::class => function(ContainerInterface $container) {
return new ProVM\Common\Service\Mailboxes(
$container->get(ProVM\Emails\Repository\Mailbox::class),
$container->get(ProVM\Common\Service\Remote\Mailboxes::class),
$container->get(ProVM\Emails\Repository\State\Mailbox::class),
$container->get(Psr\Log\LoggerInterface::class),
$container->get('max_update_days')
);
},
ProVM\Common\Service\Install::class => function(ContainerInterface $container) {
return new ProVM\Common\Service\Install(
$container->get(ProVM\Common\Factory\Model::class),
$container->get('model_list')
);
},
ProVM\Common\Service\Auth::class => function(ContainerInterface $container) {
return new ProVM\Common\Service\Auth($container->get('api_key'));
} }
]; ];

View File

@ -2,16 +2,17 @@
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
return [ return [
\ProVM\Common\Factory\Model::class => function(ContainerInterface $container) { ProVM\Common\Factory\Model::class => function(ContainerInterface $container) {
$factory = new \ProVM\Common\Factory\Model($container); $factory = new ProVM\Common\Factory\Model($container);
$repositories = [ return $factory->setRepositories([
'Mailbox' => \ProVM\Emails\Repository\Mailbox::class, 'Mailbox' => ProVM\Emails\Repository\Mailbox::class,
'Message' => \ProVM\Emails\Repository\Message::class, 'Message' => ProVM\Emails\Repository\Message::class,
'Attachment' => \ProVM\Emails\Repository\Attachment::class, 'Attachment' => ProVM\Emails\Repository\Attachment::class,
"State\\Mailbox" => \ProVM\Emails\Repository\State\Mailbox::class, 'Job' => ProVM\Emails\Repository\Job::class,
"State\\Message" => \ProVM\Emails\Repository\State\Message::class, "State\\Mailbox" => ProVM\Emails\Repository\State\Mailbox::class,
"State\\Attachment" => \ProVM\Emails\Repository\State\Attachment::class "State\\Message" => ProVM\Emails\Repository\State\Message::class,
]; "State\\Attachment" => ProVM\Emails\Repository\State\Attachment::class,
return $factory->setRepositories($repositories); "State\\Job" => ProVM\Emails\Repository\State\Job::class,
]);
} }
]; ];

View File

@ -10,5 +10,5 @@ return [
}, },
ProVM\Common\Middleware\Logging::class => function(ContainerInterface $container) { ProVM\Common\Middleware\Logging::class => function(ContainerInterface $container) {
return new ProVM\Common\Middleware\Logging($container->get('request_logger')); return new ProVM\Common\Middleware\Logging($container->get('request_logger'));
} },
]; ];

View File

@ -2,11 +2,11 @@
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
return [ return [
\ProVM\Common\Middleware\Auth::class => function(ContainerInterface $container) { ProVM\Common\Middleware\Auth::class => function(ContainerInterface $container) {
return new \ProVM\Common\Middleware\Auth( return new ProVM\Common\Middleware\Auth(
$container->get(\Nyholm\Psr7\Factory\Psr17Factory::class), $container->get(Nyholm\Psr7\Factory\Psr17Factory::class),
$container->get(\Psr\Log\LoggerInterface::class), $container->get(Psr\Log\LoggerInterface::class),
$container->get('api_key') $container->get(ProVM\Common\Service\Auth::class)
); );
} }
]; ];

View File

@ -2,31 +2,61 @@
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
return [ return [
Monolog\Handler\DeduplicationHandler::class => function(ContainerInterface $container) { 'log_processors' => function(ContainerInterface $container) {
return new Monolog\Handler\DeduplicationHandler($container->get(Monolog\Handler\RotatingFileHandler::class)); return [
$container->get(Monolog\Processor\PsrLogMessageProcessor::class),
$container->get(Monolog\Processor\IntrospectionProcessor::class),
$container->get(Monolog\Processor\WebProcessor::class),
$container->get(Monolog\Processor\MemoryPeakUsageProcessor::class),
];
}, },
Monolog\Handler\RotatingFileHandler::class => function(ContainerInterface $container) { 'request_log_handler' => function(ContainerInterface $container) {
$handler = new Monolog\Handler\RotatingFileHandler($container->get('log_file')); return (new Monolog\Handler\RotatingFileHandler(implode(DIRECTORY_SEPARATOR, [$container->get('logs_folder'), 'requests.log'])));
$handler->setFormatter($container->get(Monolog\Formatter\SyslogFormatter::class));
return $handler;
}, },
'request_logger' => function(ContainerInterface $container) { 'request_logger' => function(ContainerInterface $container) {
$logger = new Monolog\Logger('request_logger'); return new Monolog\Logger(
$handler = new Monolog\Handler\RotatingFileHandler(implode(DIRECTORY_SEPARATOR, [$container->get('logs_folder'), 'requests.log'])); 'request_logger',
$handler->setFormatter($container->get(Monolog\Formatter\SyslogFormatter::class)); [$container->get('request_log_handler')],
$dedupHandler = new Monolog\Handler\DeduplicationHandler($handler, null, Monolog\Level::Info); $container->get('log_processors')
$logger->pushHandler($dedupHandler); );
$logger->pushProcessor($container->get(Monolog\Processor\PsrLogMessageProcessor::class)); },
$logger->pushProcessor($container->get(Monolog\Processor\IntrospectionProcessor::class)); 'file_log_handler' => function(ContainerInterface $container) {
$logger->pushProcessor($container->get(Monolog\Processor\MemoryUsageProcessor::class)); return new Monolog\Handler\FilterHandler(
return $logger; (new Monolog\Handler\RotatingFileHandler($container->get('log_file')))
->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
Monolog\Level::Error
);
},
'debug_log_handler' => function(ContainerInterface $container) {
return new Monolog\Handler\FilterHandler(
(new Monolog\Handler\RotatingFileHandler(implode(DIRECTORY_SEPARATOR, [$container->get('logs_folder'), 'debug.log'])))
->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
Monolog\Level::Debug,
Monolog\Level::Warning
);
}, },
Psr\Log\LoggerInterface::class => function(ContainerInterface $container) { Psr\Log\LoggerInterface::class => function(ContainerInterface $container) {
$logger = new Monolog\Logger('file_logger'); return $container->get('file_logger');
$logger->pushHandler($container->get(Monolog\Handler\DeduplicationHandler::class)); },
$logger->pushProcessor($container->get(Monolog\Processor\PsrLogMessageProcessor::class)); 'file_logger' => function(ContainerInterface $container) {
$logger->pushProcessor($container->get(Monolog\Processor\IntrospectionProcessor::class)); return new Monolog\Logger(
$logger->pushProcessor($container->get(Monolog\Processor\MemoryUsageProcessor::class)); 'file',
return $logger; [
$container->get('file_log_handler'),
$container->get('debug_log_handler')
],
$container->get('log_processors')
);
},
'elk_logger' => function(ContainerInterface $container) {
return new Monolog\Logger('elk', [
(new Monolog\Handler\SocketHandler($container->get('logstash_socket')))
->setFormatter(new Monolog\Formatter\LogstashFormatter('emails', 'docker'))
], [
new Monolog\Processor\PsrLogMessageProcessor(),
new Monolog\Processor\WebProcessor(),
new Monolog\Processor\IntrospectionProcessor(),
new Monolog\Processor\MemoryPeakUsageProcessor()
]);
} }
]; ];

View File

@ -95,9 +95,9 @@ class Attachment implements Model
public function getFullFilename(): string public function getFullFilename(): string
{ {
return implode(' - ', [ return implode(' - ', [
$this->getMessage()->getSubject(),
$this->getMessage()->getDateTime()->format('Y-m-d His'), $this->getMessage()->getDateTime()->format('Y-m-d His'),
$this->getFilename() $this->getMessage()->getSubject(),
$this->getFilename(),
]); ]);
} }
@ -169,4 +169,8 @@ class Attachment implements Model
'decrypted' => $this->isDecrypted() 'decrypted' => $this->isDecrypted()
]; ];
} }
} public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@ -3,29 +3,27 @@ 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\Job\Stateless;
use ProVM\Emails;
class Job implements Model class Job implements Model
{ {
protected int $id; protected int $id;
protected Message $message; protected string $command;
protected DateTimeInterface $dateTime; protected string $arguments;
protected bool $executed;
public function getId(): int public function getId(): int
{ {
return $this->id; return $this->id;
} }
public function getMessage(): Message public function getCommand(): string
{ {
return $this->message; return $this->command;
} }
public function getDateTime(): DateTimeInterface public function getArguments(): string
{ {
return $this->dateTime; return $this->arguments ?? '';
}
public function isExecuted(): bool
{
return $this->executed ?? false;
} }
public function setId(int $id): Job public function setId(int $id): Job
@ -33,29 +31,76 @@ class Job implements Model
$this->id = $id; $this->id = $id;
return $this; return $this;
} }
public function setMessage(Message $message): Job public function setCommand(string $command): Job
{ {
$this->message = $message; $this->command = $command;
return $this; return $this;
} }
public function setDateTime(DateTimeInterface $dateTime): Job public function setArguments(string $arguments): Job
{ {
$this->dateTime = $dateTime; $this->arguments = $arguments;
return $this; return $this;
} }
public function wasExecuted(): Job
protected Emails\Repository\State\Job $stateRepository;
public function getStateRepository(): Emails\Repository\State\Job
{ {
$this->executed = true; return $this->stateRepository;
}
public function setStateRepository(Emails\Repository\State\Job $repository): Job
{
$this->stateRepository = $repository;
return $this; return $this;
} }
protected array $states;
public function getStates(): array
{
if (!isset($this->states)) {
try {
$this->setStates($this->getStateRepository()->fetchByJob($this->getId()));
} catch (BlankResult $e) {
return [];
}
}
return $this->states;
}
public function addState(State\Job $state): Job
{
$this->states []= $state;
return $this;
}
public function setStates(array $states): Job
{
foreach ($states as $state) {
$this->addState($state);
}
return $this;
}
public function isExecuted(): bool
{
return $this->lastState()->getStatus() === State\Job::Executed;
}
public function lastState(): State\Job
{
if (count($this->getStates()) === 0) {
throw new Stateless($this->getId());
}
return $this->getStates()[array_key_last($this->getStates())];
}
public function toArray(): array public function toArray(): array
{ {
return [ return [
'id' => $this->getId(), 'id' => $this->getId(),
'message' => $this->getMessage()->toArray(), 'command' => $this->getCommand(),
'date_time' => $this->getDateTime()->format('Y-m-d H:i:s'), 'arguments' => $this->getArguments(),
'executed' => $this->isExecuted() 'states' => $this->getStates()
]; ];
} }
} public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@ -104,6 +104,9 @@ class Mailbox implements Model
public function lastPosition(): int public function lastPosition(): int
{ {
$state = $this->lastState()->getUIDs(); $state = $this->lastState()->getUIDs();
if (count($state) === 0) {
return 0;
}
return array_key_last($state); return array_key_last($state);
} }
@ -119,4 +122,8 @@ class Mailbox implements Model
] ]
]; ];
} }
} public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@ -99,14 +99,14 @@ class Message implements Model
{ {
if (!isset($this->states)) { if (!isset($this->states)) {
try { try {
$this->setStates($this->getFactory()->find(\ProVM\Emails\Model\State\Message::class)->fetchByMessage($this->getId())); $this->setStates($this->getFactory()->find(State\Message::class)->fetchByMessage($this->getId()));
} catch (BlankResult $e) { } catch (BlankResult $e) {
return []; return [];
} }
} }
return $this->states; return $this->states;
} }
public function getState(string $name): \ProVM\Emails\Model\State\Message public function getState(string $name): State\Message
{ {
try { try {
return $this->getStates()[$name]; return $this->getStates()[$name];
@ -115,7 +115,7 @@ class Message implements Model
return $this->getStates()[$name]; return $this->getStates()[$name];
} }
} }
public function addState(\ProVM\Emails\Model\State\Message $state): Message public function addState(State\Message $state): Message
{ {
$this->states[$state->getName()] = $state; $this->states[$state->getName()] = $state;
return $this; return $this;
@ -129,7 +129,7 @@ class Message implements Model
} }
protected function newState(string $name): Message protected function newState(string $name): Message
{ {
$this->addState((new \ProVM\Emails\Model\State\Message()) $this->addState((new State\Message())
->setName($name) ->setName($name)
->setMessage($this) ->setMessage($this)
->setValue(false) ->setValue(false)
@ -219,4 +219,8 @@ class Message implements Model
}, $this->getAttachments()) : [] }, $this->getAttachments()) : []
]; ];
} }
public function jsonSerialize(): mixed
{
return $this->toArray();
}
} }

View File

@ -47,4 +47,18 @@ class Attachment implements Model
$this->value = $value; $this->value = $value;
return $this; return $this;
} }
}
public function toArray(): array
{
return [
'id' => $this->getId(),
'attachment_id' => $this->getAttachment()->getId(),
'name' => $this->getName(),
'value' => $this->getValue()
];
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@ -0,0 +1,65 @@
<?php
namespace ProVM\Emails\Model\State;
use ProVM\Common\Define\Model;
use ProVM\Emails;
class Job implements Model
{
const Executed = 0;
const Pending = 1;
const Failure = -1;
protected int $id;
protected Emails\Model\Job $job;
protected \DateTimeInterface $dateTime;
protected int $status;
public function getId(): int
{
return $this->id;
}
public function getJob(): Emails\Model\Job
{
return $this->job;
}
public function getDateTime(): \DateTimeInterface
{
return $this->dateTime;
}
public function getStatus(): int
{
return $this->status;
}
public function setId(int $id): Job
{
$this->id = $id;
return $this;
}
public function setJob(Emails\Model\Job $job): Job
{
$this->job = $job;
return $this;
}
public function setDateTime(\DateTimeInterface $dateTime): Job
{
$this->dateTime = $dateTime;
return $this;
}
public function setStatus(int $status): Job
{
$this->status = $status;
return $this;
}
public function jsonSerialize(): mixed
{
return [
'id' => $this->getId(),
'job_id' => $this->getJob()->getId(),
'date' => $this->getDateTime(),
'status' => $this->getStatus()
];
}
}

View File

@ -30,7 +30,7 @@ class Mailbox implements Model
} }
public function getUIDs(): array public function getUIDs(): array
{ {
return $this->uids; return $this->uids ?? [];
} }
public function setId(int $id): Mailbox public function setId(int $id): Mailbox
@ -65,4 +65,19 @@ class Mailbox implements Model
} }
return $this; return $this;
} }
}
public function toArray(): array
{
return [
'id' => $this->getId(),
'mailbox_id' => $this->getMailbox()->getId(),
'date_time' => $this->getDateTime()->format('Y-m-d H:i:s'),
'count' => $this->getCount(),
'uids' => $this->getUIDs()
];
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@ -47,4 +47,18 @@ class Message implements Model
$this->value = $value; $this->value = $value;
return $this; return $this;
} }
}
public function toArray(): array
{
return [
'id' => $this->getId(),
'message_id' => $this->getMessage()->getId(),
'name' => $this->getName(),
'value' => $this->getValue()
];
}
public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

View File

@ -28,6 +28,20 @@ class Attachment extends Repository
return $this; return $this;
} }
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`message_id` INT UNSIGNED NOT NULL,
`filename` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_messages_{$this->getTable()}` (`message_id`)
REFERENCES `messages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)";
$this->getConnection()->query($query);
}
protected function fieldsForInsert(): array protected function fieldsForInsert(): array
{ {
return [ return [
@ -115,4 +129,11 @@ class Attachment extends Repository
$query = "SELECT * FROM {$this->getTable()} WHERE message_id = ?"; $query = "SELECT * FROM {$this->getTable()} WHERE message_id = ?";
return $this->fetchMany($query, [$message_id]); return $this->fetchMany($query, [$message_id]);
} }
} public function fetchDownloaded(): array
{
$query = "SELECT a.*
FROM `{$this->getTable()}` a JOIN `attachments_states` `as` ON `as`.attachment_id = a.id
WHERE `as`.name = 'downloaded' AND `as`.value = 1";
return $this->fetchMany($query);
}
}

View File

@ -2,33 +2,45 @@
namespace ProVM\Emails\Repository; namespace ProVM\Emails\Repository;
use PDO; use PDO;
use ProVM\Common\Factory\Model; use ProVM\Common\Factory;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Safe\DateTimeImmutable;
use ProVM\Common\Define\Model as ModelInterface; use ProVM\Common\Define\Model as ModelInterface;
use ProVM\Common\Implement\Repository; use ProVM\Common\Implement\Repository;
use ProVM\Emails\Model\Job as BaseModel; use ProVM\Emails\Model\Job as BaseModel;
use ProVM\Emails;
class Job extends Repository class Job extends Repository
{ {
public function __construct(PDO $connection, LoggerInterface $logger, Model $factory) public function __construct(PDO $connection, LoggerInterface $logger, Factory\Model $factory)
{ {
parent::__construct($connection, $logger); parent::__construct($connection, $logger);
$this->setFactory($factory) $this->setFactory($factory)
->setTable('attachments_jobs'); ->setTable('jobs');
} }
protected \ProVM\Common\Factory\Model $factory; protected Factory\Model $factory;
public function getFactory(): \ProVM\Common\Factory\Model public function getFactory(): Factory\Model
{ {
return $this->factory; return $this->factory;
} }
public function setFactory(\ProVM\Common\Factory\Model $factory): Job public function setFactory(Factory\Model $factory): Job
{ {
$this->factory = $factory; $this->factory = $factory;
return $this; return $this;
} }
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`command` VARCHAR(100) NOT NULL,
`arguments` TEXT NOT NULL,
PRIMARY KEY (`id`)
)";
$this->getConnection()->query($query);
}
protected function fieldsForUpdate(): array protected function fieldsForUpdate(): array
{ {
return $this->fieldsForInsert(); return $this->fieldsForInsert();
@ -40,69 +52,68 @@ class Job extends Repository
protected function fieldsForInsert(): array protected function fieldsForInsert(): array
{ {
return [ return [
'message_id', 'command',
'date_time', 'arguments',
'executed'
]; ];
} }
protected function valuesForInsert(ModelInterface $model): array protected function valuesForInsert(ModelInterface $model): array
{ {
return [ return [
$model->getMessage()->getId(), $model->getCommand(),
$model->getDateTime()->format('Y-m-d H:i:s'), $model->getArguments(),
$model->isExecuted() ? 1 : 0
]; ];
} }
protected function defaultFind(ModelInterface $model): ModelInterface protected function defaultFind(ModelInterface $model): ModelInterface
{ {
return $this->fetchByMessageAndDate($model->getMessage()->getId(), $model->getDateTime()->format('Y-m-d H:i:s')); return $this->fetchByCommandAndArguments($model->getCommand(), $model->getArguments());
} }
protected function fieldsForCreate(): array protected function fieldsForCreate(): array
{ {
return [ return [
'message_id', 'command',
'date_time', 'arguments',
'executed'
]; ];
} }
protected function valuesForCreate(array $data): array protected function valuesForCreate(array $data): array
{ {
return [ return [
$data['message_id'], $data['command'],
$data['date_time'], $data['arguments'],
$data['executed'] ?? 0
]; ];
} }
protected function defaultSearch(array $data): ModelInterface protected function defaultSearch(array $data): ModelInterface
{ {
return $this->fetchByMessageAndDate($data['message_id'], $data['date_time']); return $this->fetchByCommandAndArguments($data['command'], $data['arguments']);
} }
public function load(array $row): ModelInterface public function load(array $row): ModelInterface
{ {
$model = (new BaseModel()) return (new BaseModel())
->setId($row['id']) ->setId($row['id'])
->setMessage($this->getFactory()->find(\ProVM\Emails\Model\Message::class)->fetchById($row['message_id'])) ->setCommand($row['command'])
->setDateTime(new DateTimeImmutable($row['date_time'])); ->setArguments($row['arguments'])
if ($row['executed'] ?? 0 === 1) { ->setStateRepository($this->getFactory()->find(Emails\Model\State\Job::class));
$model->wasExecuted();
}
return $model;
} }
public function fetchAllPending(): array public function fetchAllPending(): array
{ {
$query = "SELECT * FROM {$this->getTable()} WHERE `executed` = 0"; $query = "SELECT a.*
return $this->fetchMany($query); FROM `{$this->getTable()}` a
JOIN (SELECT s1.* FROM `jobs_states` s1 JOIN (SELECT MAX(id) AS id, job_id FROM `jobs_states` GROUP BY job_id) s2 ON s2.id = s1.id) b ON b.`job_id` = a.`id`
WHERE b.`status` = ?";
return $this->fetchMany($query, [Emails\Model\State\Job::Pending]);
} }
public function fetchByMessageAndDate(int $message_id, string $date_time): \ProVM\Emails\Model\Job public function fetchByCommandAndArguments(string $command, string $arguments): Emails\Model\Job
{ {
$query = "SELECT * FROM {$this->getTable()} WHERE `message_id` = ? AND `date_time` = ?"; $query = "SELECT * FROM {$this->getTable()} WHERE `command` = ? AND `arguments` = ?";
return $this->fetchOne($query, [$message_id, $date_time]); return $this->fetchOne($query, [$command, $arguments]);
} }
public function fetchPendingByMessage(int $message_id): \ProVM\Emails\Model\Job public function fetchAllPendingByCommand(string $command): array
{ {
$query = "SELECT * FROM {$this->getTable()} WHERE `message_id` = ? AND `executed` = 0"; $query = "SELECT a.*
return $this->fetchOne($query, [$message_id]); FROM `{$this->getTable()}` a
JOIN (SELECT s1.* FROM `jobs_states` s1 JOIN (SELECT MAX(id) AS id, job_id FROM `jobs_states` GROUP BY job_id) s2 ON s2.id = s1.id) b ON b.`job_id` = a.`id`
WHERE a.`command` = ? AND b.`status` = ?";
return $this->fetchMany($query, [$command, Emails\Model\State\Job::Pending]);
} }
} }

View File

@ -28,6 +28,18 @@ class Mailbox extends Repository
return $this; return $this;
} }
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`validity` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
)";
$this->getConnection()->query($query);
}
protected function fieldsForUpdate(): array protected function fieldsForUpdate(): array
{ {
return $this->fieldsForInsert(); return $this->fieldsForInsert();
@ -86,4 +98,4 @@ class Mailbox extends Repository
$query = "SELECT * FROM `{$this->getTable()}` WHERE `name` = ?"; $query = "SELECT * FROM `{$this->getTable()}` WHERE `name` = ?";
return $this->fetchOne($query, [$name]); return $this->fetchOne($query, [$name]);
} }
} }

View File

@ -3,34 +3,50 @@ namespace ProVM\Emails\Repository;
use DateTimeInterface; use DateTimeInterface;
use PDO; use PDO;
use PDOException;
use Exception;
use ProVM\Common\Define\Model;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use ProVM\Common\Implement\Repository;
use Safe\DateTimeImmutable; use Safe\DateTimeImmutable;
use Safe\Exceptions\ErrorfuncException; use ProVM\Common\Define\Model;
use ProVM\Common\Implement\Repository;
use ProVM\Common\Factory;
class Message extends Repository class Message extends Repository
{ {
public function __construct(PDO $connection, LoggerInterface $logger, \ProVM\Common\Factory\Model $factory) public function __construct(PDO $connection, LoggerInterface $logger, Factory\Model $factory)
{ {
parent::__construct($connection, $logger); parent::__construct($connection, $logger);
$this->setTable('messages') $this->setTable('messages')
->setFactory($factory); ->setFactory($factory);
} }
protected \ProVM\Common\Factory\Model $factory; protected Factory\Model $factory;
public function getFactory(): \ProVM\Common\Factory\Model public function getFactory(): Factory\Model
{ {
return $this->factory; return $this->factory;
} }
public function setFactory(\ProVM\Common\Factory\Model $factory): Message public function setFactory(Factory\Model $factory): Message
{ {
$this->factory = $factory; $this->factory = $factory;
return $this; return $this;
} }
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`uid` VARCHAR(255) NOT NULL,
`mailbox_id` INT UNSIGNED NOT NULL,
`position` INT UNSIGNED NOT NULL,
`subject` VARCHAR(255) NOT NULL,
`from` VARCHAR(255) NOT NULL,
`date_time` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_mailboxes_{$this->getTable()}` (`mailbox_id`)
REFERENCES `mailboxes` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)";
$this->getConnection()->query($query);
}
protected function fieldsForUpdate(): array protected function fieldsForUpdate(): array
{ {
return $this->fieldsForInsert(); return $this->fieldsForInsert();
@ -114,21 +130,23 @@ class Message extends Repository
'downloaded_attachments', 'downloaded_attachments',
'scheduled_downloads' 'scheduled_downloads'
]; ];
$stateRepository = $this->getFactory()->find(\ProVM\Emails\Model\State\Message::class);
foreach ($valid_states as $state_name) { foreach ($valid_states as $state_name) {
try { try {
$model->getState($state_name); $model->getState($state_name);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->getLogger()->warning($e);
$data = [ $data = [
'message_id' => $model->getId(), 'message_id' => $model->getId(),
'name' => $state_name 'name' => $state_name
]; ];
$state = $this->getFactory()->find(\ProVM\Emails\Model\State\Message::class)->create($data); $state = $stateRepository->create($data);
$model->addState($state); $model->addState($state);
} }
} }
foreach ($model->getStates() as $state) { foreach ($model->getStates() as $state) {
$state->setMessage($model); //$state->setMessage($model);
$this->getFactory()->find(\ProVM\Emails\Model\State\Message::class)->save($state); $stateRepository->save($state);
} }
} }
@ -161,4 +179,9 @@ class Message extends Repository
WHERE `mailbox_id` = ? `subject` = ? AND `from` = ? AND `date_time` = ?"; 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')]); return $this->fetchOne($query, [$mailbox_id, $subject, $from, $dateTime->format('Y-m-d H:i:s')]);
} }
public function fetchAllBySubjectAndDate(string $subject, DateTimeInterface $dateTime): array
{
$query = "SELECT * FROM `{$this->getTable()}` WHERE `subject` = ? AND `date_time` BETWEEN ? AND ?";
return $this->fetchMany($query, [$subject, $dateTime->format('Y-m-d 00:00:00'), $dateTime->format('Y-m-d 23:59:59')]);
}
} }

View File

@ -26,6 +26,22 @@ class Attachment extends Repository
return $this; return $this;
} }
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`attachment_id` INT UNSIGNED NOT NULL,
`name` VARCHAR(100) NOT NULL,
`value` INT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_attachments_{$this->getTable()}` (`attachment_id`)
REFERENCES `attachments` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
";
$this->getConnection()->query($query);
}
protected function fieldsForInsert(): array protected function fieldsForInsert(): array
{ {
return [ return [
@ -90,4 +106,4 @@ class Attachment extends Repository
$query = "SELECT * FROM `{$this->getTable()}` WHERE `attachment_id` = ? AND `name` = ?"; $query = "SELECT * FROM `{$this->getTable()}` WHERE `attachment_id` = ? AND `name` = ?";
return $this->fetchOne($query, [$attachment_id, $name]); return $this->fetchOne($query, [$attachment_id, $name]);
} }
} }

View File

@ -0,0 +1,113 @@
<?php
namespace ProVM\Emails\Repository\State;
use PDO;
use Psr\Log\LoggerInterface;
use ProVM\Common\Define;
use ProVM\Common\Factory;
use ProVM\Common\Implement\Repository;
use ProVM\Emails;
class Job extends Repository
{
public function __construct(PDO $connection, LoggerInterface $logger, Factory\Model $factory)
{
parent::__construct($connection, $logger);
$this->setTable('jobs_states')
->setFactory($factory);
}
protected Factory\Model $factory;
public function getFactory(): Factory\Model
{
return $this->factory;
}
public function setFactory(Factory\Model $factory): Job
{
$this->factory = $factory;
return $this;
}
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`job_id` INT UNSIGNED NOT NULL,
`date_time` DATETIME NOT NULL,
`status` INT NOT NULL DEFAULT 1,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_jobs_{$this->getTable()}` (`job_id`)
REFERENCES `jobs` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)";
$this->getConnection()->query($query);
}
public function load(array $row): Define\Model
{
return (new Emails\Model\State\Job())
->setId($row['id'])
->setJob($this->getFactory()->find(Emails\Model\Job::class)->fetchById($row['job_id']))
->setDateTime(new \DateTimeImmutable($row['date_time']))
->setStatus($row['status']);
}
public function fetchByJob(int $job_id): array
{
$query = "SELECT * FROM `{$this->getTable()}` WHERE `job_id` = ?";
return $this->fetchMany($query, [$job_id]);
}
public function fetchByJobAndStatus(int $job_id, int $status): Emails\Model\State\Job
{
$query = "SELECT * FROM `{$this->getTable()}` WHERE `job_id` = ? AND `status` = ?";
return $this->fetchOne($query, [$job_id, $status]);
}
protected function fieldsForInsert(): array
{
return [
'job_id',
'date_time',
'status'
];
}
protected function valuesForInsert(Define\Model $model): array
{
return [
$model->getJob()->getId(),
$model->getDateTime()->format('Y-m-d H:i:s'),
$model->getStatus()
];
}
protected function fieldsForUpdate(): array
{
return $this->fieldsForInsert();
}
protected function fieldsForCreate(): array
{
return $this->fieldsForInsert();
}
protected function valuesForUpdate(Define\Model $model): array
{
return $this->valuesForInsert($model);
}
protected function valuesForCreate(array $data): array
{
return [
$data['job_id'],
$data['date_time'],
$data['status']
];
}
protected function defaultFind(Define\Model $model): Define\Model
{
return $this->fetchByJobAndStatus($model->getJob()->getId(), $model->getStatus());
}
protected function defaultSearch(array $data): Define\Model
{
return $this->fetchByJobAndStatus($data['job_id'], $data['status']);
}
}

View File

@ -28,6 +28,23 @@ class Mailbox extends Repository
return $this; return $this;
} }
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`mailbox_id` INT UNSIGNED NOT NULL,
`date_time` DATETIME NOT NULL,
`count` INT UNSIGNED NOT NULL,
`uids` TEXT NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_mailboxes_{$this->getTable()}` (`mailbox_id`)
REFERENCES `mailboxes` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
";
$this->getConnection()->query($query);
}
protected function fieldsForUpdate(): array protected function fieldsForUpdate(): array
{ {
return $this->fieldsForInsert(); return $this->fieldsForInsert();
@ -99,4 +116,4 @@ class Mailbox extends Repository
$query = "SELECT * FROM `{$this->getTable()}` WHERE `mailbox_id` = ? AND `date_time` = ?"; $query = "SELECT * FROM `{$this->getTable()}` WHERE `mailbox_id` = ? AND `date_time` = ?";
return $this->fetchOne($query, [$mailbox_id, $date_time]); return $this->fetchOne($query, [$mailbox_id, $date_time]);
} }
} }

View File

@ -26,6 +26,22 @@ class Message extends Repository
return $this; return $this;
} }
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`message_id` INT UNSIGNED NOT NULL,
`name` VARCHAR(100) NOT NULL,
`value` INT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_messages_{$this->getTable()}` (`message_id`)
REFERENCES `messages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
";
$this->getConnection()->query($query);
}
protected function fieldsForUpdate(): array protected function fieldsForUpdate(): array
{ {
return $this->fieldsForInsert(); return $this->fieldsForInsert();
@ -90,4 +106,4 @@ class Message extends Repository
$query = "SELECT * FROM `{$this->getTable()}` WHERE `message_id` = ? AND `name` = ?"; $query = "SELECT * FROM `{$this->getTable()}` WHERE `message_id` = ? AND `name` = ?";
return $this->fetchOne($query, [$message_id, $name]); return $this->fetchOne($query, [$message_id, $name]);
} }
} }

View File

@ -3,7 +3,7 @@ FROM php:8-cli
ENV PATH ${PATH}:/app/bin ENV PATH ${PATH}:/app/bin
RUN apt-get update \ RUN apt-get update \
&& apt-get install -y cron git libzip-dev unzip \ && apt-get install -y cron git libzip-dev unzip qpdf \
&& rm -r /var/lib/apt/lists/* \ && rm -r /var/lib/apt/lists/* \
&& docker-php-ext-install zip && docker-php-ext-install zip

View File

@ -1,67 +0,0 @@
<?php
namespace ProVM\Common\Command;
use ProVM\Common\Service\Communicator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'attachments:decrypt',
description: 'Decrypt attachments pending',
hidden: false
)]
class DecryptPdf extends Command
{
public function __construct(Communicator $communicator, string $name = null)
{
$this->setCommunicator($communicator);
parent::__construct($name);
}
protected Communicator $communicator;
public function getCommunicator(): Communicator
{
return $this->communicator;
}
public function setCommunicator(Communicator $communicator): DecryptPdf
{
$this->communicator = $communicator;
return $this;
}
protected function getAttachments(): array
{
$response = $this->getCommunicator()->get('/attachments/pending');
return \Safe\json_decode($response->getBody()->getContents())->attachments;
}
protected function decrypt(string $attachment): bool
{
$response = $this->getCommunicator()->put('/attachments/decrypt', ['attachments' => [$attachment]]);
return \Safe\json_decode($response->getBody()->getContents())->status;
}
public function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$io->title('Decrypt Attachments');
$io->section('Grabbing Attachments');
$attachments = $this->getAttachments();
$io->text('Found ' . count($attachments) . ' attachments.');
$io->section('Decrypting Attachments');
foreach ($attachments as $attachment) {
$status = $this->decrypt($attachment);
if ($status) {
$io->success("{$attachment} decrypted correctly.");
} else {
$io->error("Problem decrypting {$attachment}.");
}
}
$io->success('Done.');
return Command::SUCCESS;
}
}

View File

@ -1,65 +0,0 @@
<?php
namespace ProVM\Common\Command;
use ProVM\Common\Service\Communicator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'attachments:grab',
description: 'Grab attachments from pending messages',
aliases: ['attachments:get'],
hidden: false
)]
class GrabAttachments extends Command
{
public function __construct(Communicator $communicator, string $name = null)
{
$this->setCommunicator($communicator);
parent::__construct($name);
}
protected Communicator $service;
public function getCommunicator(): Communicator
{
return $this->service;
}
public function setCommunicator(Communicator $service): GrabAttachments
{
$this->service = $service;
return $this;
}
protected function getMessages(): array
{
$response = $this->getCommunicator()->get('/messages/pending');
return \Safe\json_decode($response->getBody()->getContents())->messages;
}
protected function grabAttachments(int $message_uid): int
{
$response = $this->getCommunicator()->put('/attachments/grab', ['messages' => [$message_uid]]);
return \Safe\json_decode($response->getBody()->getContents())->attachment_count;
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->title('Grab Attachments');
$io->section('Grabbing Messages');
$messages = $this->getMessages();
$io->text('Found ' . count($messages) . ' messages.');
$io->section('Grabbing Attachments');
foreach ($messages as $job) {
$message = $job->message;
$attachments = $this->grabAttachments($message->uid);
$io->text("Found {$attachments} attachments for message UID:{$message->uid}.");
}
$io->success('Done.');
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,52 @@
<?php
namespace ProVM\Command\Jobs;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use ProVM\Service\Jobs;
#[AsCommand(
name: 'jobs:check',
description: 'Check for pending jobs',
hidden: false
)]
class Check extends Command
{
public function __construct(protected Jobs $service, string $name = null)
{
parent::__construct($name);
}
public function execute(InputInterface $input, OutputInterface $output)
{
$section1 = $output->section();
$section2 = $output->section();
$io1 = new SymfonyStyle($input, $section1);
$io2 = new SymfonyStyle($input, $section2);
$io1->title('Checking Pending Jobs');
$pending_jobs = $this->service->getPending();
$notice = 'Found ' . count($pending_jobs) . ' jobs';
$io1->text($notice);
if (count($pending_jobs) > 0) {
$io1->section('Running Jobs');
$io1->progressStart(count($pending_jobs));
foreach ($pending_jobs as $job) {
$section2->clear();
$io2->text("Running {$job->command}");
if ($this->service->run($job)) {
$io2->success('Success');
} else {
$io2->error('Failure');
}
$io1->progressAdvance();
}
}
$section2->clear();
$io2->success('Done');
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace ProVM\Command\Jobs;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use ProVM\Service\Jobs;
#[AsCommand(
name: 'jobs:execute',
description: 'Execute job by job_id',
hidden: false
)]
class Execute extends Command
{
public function __construct(protected Jobs $service, string $name = null)
{
parent::__construct($name);
}
protected function configure()
{
$this->addArgument('job_id', InputArgument::REQUIRED, 'Job ID to be executed');
}
public function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$job_id = $input->getArgument('job_id');
$job = $this->service->get($job_id);
if ($this->service->run($job)) {
$io->success('Success');
} else {
$io->error('Failed');
}
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace ProVM\Command\Mailboxes;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use ProVM\Service\Mailboxes;
#[AsCommand(
name: 'mailboxes:check',
description: 'Check registered mailboxes for new emails',
hidden: false
)]
class Check extends Command
{
public function __construct(protected Mailboxes $service, string $name = null)
{
parent::__construct($name);
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$section1 = $output->section();
$section2 = $output->section();
$io1 = new SymfonyStyle($input, $section1);
$io2 = new SymfonyStyle($input, $section2);
$io1->title('Checking for New Messages');
$mailboxes = $this->service->getAll();
$notice = 'Found ' . count($mailboxes) . ' mailboxes';
$io1->text($notice);
if (count($mailboxes) > 0) {
$io1->section('Checking for new messages');
$io1->progressStart(count($mailboxes));
foreach ($mailboxes as $mailbox) {
$section2->clear();
$io2->text("Checking {$mailbox->name}");
if ($this->service->check($mailbox)) {
$io2->success("Found new emails in {$mailbox->name}");
} else {
$io2->info("No new emails in {$mailbox->name}");
}
$io1->progressAdvance();
}
$io1->progressFinish();
}
$section2->clear();
$io2->success('Done');
return Command::SUCCESS;
}
}

View File

@ -1,66 +0,0 @@
<?php
namespace ProVM\Common\Command;
use ProVM\Common\Service\Communicator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'messages:grab',
description: 'Run grab messages job for registered mailboxes',
aliases: ['messages'],
hidden: false
)]
class Messages extends Command
{
public function __construct(Communicator $communicator, string $name = null)
{
$this->setCommunicator($communicator);
parent::__construct($name);
}
protected Communicator $communicator;
public function getCommunicator(): Communicator
{
return $this->communicator;
}
public function setCommunicator(Communicator $communicator): Messages
{
$this->communicator = $communicator;
return $this;
}
protected function getMailboxes(): array
{
$response = $this->getCommunicator()->get('/mailboxes/registered');
return \Safe\json_decode($response->getBody()->getContents())->mailboxes;
}
protected function grabMessages(string $mailbox): int
{
$response = $this->getCommunicator()->put('/messages/grab', ['mailboxes' => [$mailbox]]);
$body = \Safe\json_decode($response->getBody()->getContents());
return $body->message_count;
}
public function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$io->title('Messages');
$io->section('Grabbing Registered Mailboxes');
$mailboxes = $this->getMailboxes();
$io->text('Found ' . count($mailboxes) . ' registered mailboxes.');
$io->section('Grabbing Messages');
foreach ($mailboxes as $mailbox) {
$message_count = $this->grabMessages($mailbox->name);
$io->text("Found {$message_count} messages in {$mailbox->name}.");
}
$io->success('Done.');
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace ProVM\Command\Messages;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use ProVM\Service\Mailboxes;
#[AsCommand(
name: 'messages:grab',
description: 'Run grab messages job for mailbox',
hidden: false
)]
class Grab extends Command
{
public function __construct(protected Mailboxes $service, string $name = null)
{
parent::__construct($name);
}
protected function configure()
{
$this->addArgument('mailbox_id', InputArgument::REQUIRED, 'Mailbox ID to grab emails');
}
public function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$mailbox_id = $input->getArgument('mailbox_id');
$io->title("Grabbing Messages for Mailbox ID {$mailbox_id}");
$io->section('Grabbing Messages');
$count = $this->service->grabMessages($mailbox_id);
$io->info("Found {$count} messages");
$io->success('Done.');
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace ProVM\Exception\Response;
use Exception;
use Throwable;
class EmptyResponse extends Exception
{
public function __construct(string $uri, string $method = 'get', ?Throwable $previous = null)
{
$method = strtoupper($method);
$message = "Received empty response for request {$method} '{$uri}'";
$code = 410;
parent::__construct($message, $code, $previous);
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace ProVM\Exception\Response;
use Exception;
use Throwable;
class MissingResponse extends Exception
{
public function __construct(string $expected, ?Throwable $previous = null)
{
$message = "Response is missing parameter(s) {$expected}";
$code = 406;
parent::__construct($message, $code, $previous);
}
}

View File

@ -1,38 +0,0 @@
<?php
namespace ProVM\Common\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
class Logging
{
public function __construct(LoggerInterface $logger) {
$this->setLogger($logger);
}
protected LoggerInterface $logger;
public function getLogger(): LoggerInterface
{
return $this->logger;
}
public function setLogger(LoggerInterface $logger): Logging
{
$this->logger = $logger;
return $this;
}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
$output = [
'uri' => var_export($request->getUri(), true),
'body' => $request->getBody()->getContents()
];
$this->getLogger()->info(\Safe\json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
return $response;
}
}

View File

@ -0,0 +1,114 @@
<?php
namespace ProVM\Service;
use ProVM\Exception\Response\EmptyResponse;
use ProVM\Exception\Response\MissingResponse;
use Psr\Log\LoggerInterface;
use function Safe\json_decode;
class Attachments
{
public function __construct(protected Communicator $communicator, protected LoggerInterface $logger, protected array $passwords, protected string $base_command = 'qpdf') {}
protected array $attachments;
public function findAll(): \Generator
{
$this->logger->info('Finding all downloaded attachment files');
$folder = '/attachments';
$files = new \FilesystemIterator($folder);
foreach ($files as $file) {
if ($file->isDir()) {
continue;
}
yield $file->getRealPath();
}
}
public function getAll(): array
{
if (!isset($this->attachments)) {
$this->logger->info('Grabbing all attachments');
$response = $this->communicator->get('/attachments');
$body = $response->getBody()->getContents();
if (trim($body) === '') {
$this->attachments = [];
return $this->attachments;
}
$this->attachments = json_decode($body)->attachments;
}
return $this->attachments;
}
public function get(int $attachment_id): object
{
$this->logger->info("Getting attachment {$attachment_id}");
$uri = "/attachment/{$attachment_id}";
$response = $this->communicator->get($uri);
$body = $response->getBody()->getContents();
if (trim($body) === '') {
throw new EmptyResponse($uri);
}
$json = json_decode($body);
if (!isset($json->attachment)) {
throw new MissingResponse('attachment');
}
return $json->attachment;
}
public function find(string $filename): int
{
$this->logger->info("Finding attachment {$filename}");
foreach ($this->getAll() as $attachment) {
if ($attachment->fullfilename === $filename) {
return $attachment->id;
}
}
throw new \Exception("{$filename} is not in the database");
}
public function isEncrypted(string $filename): bool
{
if (!file_exists($filename)) {
throw new \InvalidArgumentException("File not found {$filename}");
}
$escaped_filename = escapeshellarg($filename);
$cmd = "{$this->base_command} --is-encrypted {$escaped_filename}";
exec($cmd, $output, $retcode);
return $retcode == 0;
}
public function scheduleDecrypt(int $attachment_id): bool
{
$this->logger->info("Scheduling decryption of attachment {$attachment_id}");
$uri = "/attachment/{$attachment_id}/decrypt";
$response = $this->communicator->get($uri);
$body = $response->getBody()->getContents();
if (trim($body) === '') {
throw new EmptyResponse($uri);
}
$json = json_decode($body);
if (!isset($json->status)) {
throw new MissingResponse('status');
}
return $json->status;
}
public function decrypt(string $basename): bool
{
$this->logger->info("Decrypting {$basename}");
$in_filename = implode('/', ['attachments', $basename]);
$out_filename = implode('/', ['attachments', 'decrypted', $basename]);
if (file_exists($out_filename)) {
throw new \Exception("{$basename} already decrypted");
}
foreach ($this->passwords as $password) {
$cmd = $this->base_command . ' -password=' . escapeshellarg($password) . ' -decrypt ' . escapeshellarg($in_filename) . ' ' . escapeshellarg($out_filename);
exec($cmd, $output, $retcode);
$success = $retcode == 0;
if ($success) {
return true;
}
if (file_exists($out_filename)) {
unlink($out_filename);
}
unset($output);
}
return false;
}
}

View File

@ -1,31 +1,19 @@
<?php <?php
namespace ProVM\Common\Service; namespace ProVM\Service;
use HttpResponseException; use HttpResponseException;
use Psr\Http\Client\ClientInterface; use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Safe\Exceptions\JsonException;
use function Safe\json_encode; use function Safe\json_encode;
class Communicator class Communicator
{ {
public function __construct(ClientInterface $client) public function __construct(protected ClientInterface $client) {}
{
$this->setClient($client);
}
protected ClientInterface $client;
public function getClient(): ClientInterface
{
return $this->client;
}
public function setClient(ClientInterface $client): Communicator
{
$this->client = $client;
return $this;
}
/**
* @throws HttpResponseException
*/
protected function handleResponse(ResponseInterface $response): ResponseInterface protected function handleResponse(ResponseInterface $response): ResponseInterface
{ {
if ($response->getStatusCode() < 200 or $response->getStatusCode() >= 300) { if ($response->getStatusCode() < 200 or $response->getStatusCode() >= 300) {
@ -33,6 +21,11 @@ class Communicator
} }
return $response; return $response;
} }
/**
* @throws HttpResponseException
* @throws JsonException
*/
protected function request(string $method, string $uri, ?array $body = null): ResponseInterface protected function request(string $method, string $uri, ?array $body = null): ResponseInterface
{ {
$options = []; $options = [];
@ -42,23 +35,42 @@ class Communicator
]; ];
$options['body'] = json_encode($body); $options['body'] = json_encode($body);
} }
return $this->handleResponse($this->getClient()->request($method, $uri, $options)); return $this->handleResponse($this->client->request($method, $uri, $options));
} }
/**
* @throws HttpResponseException
* @throws JsonException
*/
public function get(string $uri): ResponseInterface public function get(string $uri): ResponseInterface
{ {
return $this->request('get', $uri); return $this->request('get', $uri);
} }
/**
* @throws HttpResponseException
* @throws JsonException
*/
public function post(string $uri, array $data): ResponseInterface public function post(string $uri, array $data): ResponseInterface
{ {
return $this->request('post', $uri, $data); return $this->request('post', $uri, $data);
} }
/**
* @throws HttpResponseException
* @throws JsonException
*/
public function put(string $uri, array $data): ResponseInterface public function put(string $uri, array $data): ResponseInterface
{ {
return $this->request('put', $uri, $data); return $this->request('put', $uri, $data);
} }
/**
* @throws HttpResponseException
* @throws JsonException
*/
public function delete(string $uri, array $data): ResponseInterface public function delete(string $uri, array $data): ResponseInterface
{ {
return $this->request('delete', $uri, $data); return $this->request('delete', $uri, $data);
} }
} }

View File

@ -0,0 +1,71 @@
<?php
namespace ProVM\Service;
use Psr\Log\LoggerInterface;
use ProVM\Exception\Response\{EmptyResponse, MissingResponse};
use function Safe\json_decode;
class Jobs
{
public function __construct(protected Communicator $communicator, protected LoggerInterface $logger) {}
public function getPending(): array
{
$this->logger->info('Getting pending jobs');
$response = $this->communicator->get('/jobs/pending');
$body = $response->getBody()->getContents();
if (trim($body) === '') {
return [];
}
$json = json_decode($body);
if (!isset($json->jobs)) {
return [];
}
return $json->jobs;
}
public function get(int $job_id): object
{
$this->logger->info("Getting Job {$job_id}");
$uri = "/job/{$job_id}";
return $this->send($uri, 'job');
}
public function run(object $job): bool
{
$this->logger->debug("Running Job {$job->id}");
$base_command = '/app/bin/emails';
$cmd = [$base_command, $job->command];
if ($job->arguments !== '') {
$cmd []= $job->arguments;
}
$cmd = implode(' ', $cmd);
$response = shell_exec($cmd);
if ($response !== false) {
return $this->finished($job->id);
}
return $this->failure($job->id);
}
protected function finished(int $job_id): bool
{
$uri = "/job/{$job_id}/finish";
return $this->send($uri, 'status');
}
protected function failure(int $job_id): bool
{
$uri = "/job/{$job_id}/failed";
return $this->send($uri, 'status');
}
protected function send(string $uri, string $param): mixed
{
$response = $this->communicator->get($uri);
$body = $response->getBody()->getContents();
if (trim($body) === '') {
throw new EmptyResponse($uri);
}
$json = json_decode($body);
if (!isset($json->{$param})) {
throw new MissingResponse($param);
}
return $json->{$param};
}
}

View File

@ -0,0 +1,61 @@
<?php
namespace ProVM\Service;
use DateTimeImmutable;
use ProVM\Exception\Response\EmptyResponse;
use ProVM\Exception\Response\MissingResponse;
use Psr\Log\LoggerInterface;
use function Safe\json_decode;
class Mailboxes
{
public function __construct(protected Communicator $communicator, protected LoggerInterface $logger, protected int $min_check_days) {}
public function getAll(): array
{
$this->logger->info('Getting all registered mailboxes');
$response = $this->communicator->get('/mailboxes/registered');
$body = $response->getBody()->getContents();
if (trim($body) === '') {
return [];
}
$json = json_decode($body);
if (!isset($json->mailboxes)) {
return [];
}
return $json->mailboxes;
}
public function check(object $mailbox): bool
{
$this->logger->info("Checking mailbox {$mailbox->id}");
if ((new DateTimeImmutable())->diff(new DateTimeImmutable($mailbox->last_checked->date->date))->days < $this->min_check_days) {
return true;
}
$uri = "/mailbox/{$mailbox->id}/check";
$response = $this->communicator->get($uri);
$body = $response->getBody()->getContents();
if (trim($body) === '') {
throw new EmptyResponse($uri);
}
$json = json_decode($body);
if (!isset($json->status)) {
throw new MissingResponse('status');
}
return $json->status;
}
public function grabMessages(int $mailbox_id): int
{
$this->logger->info("Grabbing messages for {$mailbox_id}");
$uri = "/mailbox/{$mailbox_id}/messages/grab";
$response = $this->communicator->get($uri);
$body = $response->getBody()->getContents();
if (trim($body) === '') {
return 0;
}
$json = json_decode($body);
if (!isset($json->count)) {
return 0;
}
return $json->count;
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace ProVM\Service;
use ProVM\Exception\Response\EmptyResponse;
use ProVM\Exception\Response\MissingResponse;
use Psr\Log\LoggerInterface;
use function Safe\json_decode;
class Messages
{
public function __construct(protected Communicator $communicator, protected LoggerInterface $logger) {}
public function get(int $message_id): object
{
$this->logger->info("Getting message {$message_id}");
$uri = "/message/{$message_id}";
$response = $this->communicator->get($uri);
$body = $response->getBody()->getContents();
if (trim($body) === '') {
throw new EmptyResponse($uri);
}
$json = json_decode($body);
if (!isset($json->message)) {
throw new MissingResponse('message');
}
return $json->message;
}
public function grabAttachments(string $message_uid): int
{
$this->logger->info("Grabbing attachments for message UID {$message_uid}");
$uri = '/attachments/grab';
$response = $this->communicator->put($uri, ['messages' => [$message_uid]]);
$body = $response->getBody()->getContents();
if (trim($body) === '') {
return 0;
}
$json = json_decode($body);
if (!isset($json->total)) {
return 0;
}
return $json->total;
}
}

View File

@ -1,9 +1,8 @@
<?php <?php
namespace ProVM\Common\Wrapper; namespace ProVM\Wrapper;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Application as Base; use Symfony\Component\Console\Application as Base;
class Application extends Base class Application extends Base
{ {
public function __construct(ContainerInterface $container, string $name = 'UNKNOWN', string $version = 'UNKNOWN') public function __construct(ContainerInterface $container, string $name = 'UNKNOWN', string $version = 'UNKNOWN')
@ -22,4 +21,4 @@ class Application extends Base
$this->container = $container; $this->container = $container;
return $this; return $this;
} }
} }

View File

@ -15,7 +15,7 @@
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"ProVM\\Common\\": "common/" "ProVM\\": "common/"
} }
}, },
"authors": [ "authors": [

View File

@ -1,3 +1,10 @@
# minutes hour day_of_month month day_of_week command # minutes hour day_of_month month day_of_week command
0 2 * * 2-6 /app/bin/emails messages:grab >> /logs/messages.log #0 2 * * 2-6 /app/bin/emails messages:grab >> /logs/messages.log
0 3 * * 2-6 /app/bin/emails attachments:grab >> /logs/attachments.log #0 3 * * 2-6 /app/bin/emails attachments:grab >> /logs/attachments.log
# Pending jobs every minute
* * * * * /app/bin/emails jobs:check >> /logs/jobs.log
# Check mailboxes for new emails every weekday
0 0 * * 2-6 /app/bin/emails mailboxes:check >> /logs/mailboxes.log
# Check attachments every weekday
0 1 * * 2-6 /app/bin/emails attachments:check >> /logs/attachments.log

View File

@ -12,4 +12,6 @@ services:
- .key.env - .key.env
volumes: volumes:
- ${CLI_PATH:-.}/:/app - ${CLI_PATH:-.}/:/app
- ./logs/cli:/logs - ${CLI_PATH}/crontab:/var/spool/cron/crontabs/root
- ${LOGS_PATH}/cli:/logs
- ${ATT_PATH}:/attachments

View File

@ -7,9 +7,8 @@ $app = require_once implode(DIRECTORY_SEPARATOR, [
Monolog\ErrorHandler::register($app->getContainer()->get(Psr\Log\LoggerInterface::class)); Monolog\ErrorHandler::register($app->getContainer()->get(Psr\Log\LoggerInterface::class));
try { try {
$app->run(); $app->run();
} catch (Error | Exception $e) { } catch (Error $e) {
$logger = $app->getContainer()->get(Psr\Log\LoggerInterface::class); $app->getContainer()->get(Psr\Log\LoggerInterface::class)->error($e);
$logger->debug(Safe\json_encode(compact('_SERVER'))); } catch (Exception $e) {
$logger->error($e); $app->getContainer()->get(Psr\Log\LoggerInterface::class)->warning($e);
throw $e;
} }

View File

@ -0,0 +1,2 @@
<?php
$app->add($app->getContainer()->get(ProVM\Command\Mailboxes\Check::class));

View File

@ -1,2 +1,2 @@
<?php <?php
$app->add($app->getContainer()->get(\ProVM\Common\Command\Messages::class)); $app->add($app->getContainer()->get(ProVM\Command\Messages\Grab::class));

View File

@ -1,3 +1,4 @@
<?php <?php
$app->add($app->getContainer()->get(\ProVM\Common\Command\GrabAttachments::class)); $app->add($app->getContainer()->get(ProVM\Command\Attachments\Check::class));
$app->add($app->getContainer()->get(\ProVM\Common\Command\DecryptPdf::class)); $app->add($app->getContainer()->get(ProVM\Command\Attachments\Grab::class));
$app->add($app->getContainer()->get(ProVM\Command\Attachments\Decrypt::class));

View File

@ -0,0 +1,3 @@
<?php
$app->add($app->getContainer()->get(ProVM\Command\Jobs\Check::class));
$app->add($app->getContainer()->get(ProVM\Command\Jobs\Execute::class));

View File

@ -1,7 +1,7 @@
<?php <?php
require_once 'composer.php'; require_once 'composer.php';
$builder = new \DI\ContainerBuilder(); $builder = new DI\ContainerBuilder();
$folders = [ $folders = [
'settings', 'settings',
@ -24,7 +24,7 @@ foreach ($folders as $f) {
} }
} }
$app = new \ProVM\Common\Wrapper\Application($builder->build()); $app = new ProVM\Wrapper\Application($builder->build());
$folder = implode(DIRECTORY_SEPARATOR, [ $folder = implode(DIRECTORY_SEPARATOR, [
__DIR__, __DIR__,

View File

@ -1,2 +0,0 @@
<?php
$app->add($app->getContainer()->get(ProVM\Common\Middleware\Logging::class));

View File

@ -1,5 +1,10 @@
<?php <?php
return [ return [
'api_uri' => $_ENV['API_URI'], 'api_uri' => $_ENV['API_URI'],
'api_key' => sha1($_ENV['API_KEY']) 'api_key' => sha1($_ENV['API_KEY']),
'base_command' => 'qpdf',
'passwords' => function() {
return explode($_ENV['PASSWORDS_SEPARATOR'] ?? ',', $_ENV['PASSWORDS'] ?? '');
},
'min_check_days' => 1
]; ];

View File

@ -2,12 +2,12 @@
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
return [ return [
\Psr\Http\Client\ClientInterface::class => function(ContainerInterface $container) { Psr\Http\Client\ClientInterface::class => function(ContainerInterface $container) {
return new \GuzzleHttp\Client([ return new GuzzleHttp\Client([
'base_uri' => $container->get('api_uri'), 'base_uri' => $container->get('api_uri'),
'headers' => [ 'headers' => [
'Authorization' => "Bearer {$container->get('api_key')}" 'Authorization' => "Bearer {$container->get('api_key')}"
] ]
]); ]);
} }
]; ];

View File

@ -2,7 +2,7 @@
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
return [ return [
ProVM\Common\Middleware\Logging::class => function(ContainerInterface $container) { ProVM\Middleware\Logging::class => function(ContainerInterface $container) {
return new ProVM\Common\Middleware\Logging($container->get('request_logger')); return new ProVM\Middleware\Logging($container->get('request_logger'));
} }
]; ];

View File

@ -0,0 +1,21 @@
<?php
use Psr\Container\ContainerInterface;
return [
ProVM\Service\Mailboxes::class => function(ContainerInterface $container) {
return new ProVM\Service\Mailboxes(
$container->get(ProVM\Service\Communicator::class),
$container->get(Psr\Log\LoggerInterface::class),
$container->get('min_check_days')
);
},
ProVM\Service\Attachments::class => function(ContainerInterface $container) {
return new ProVM\Service\Attachments(
$container->get(ProVM\Service\Communicator::class),
$container->get(Psr\Log\LoggerInterface::class),
$container->get('passwords'),
$container->get('base_command')
);
},
];

View File

@ -2,28 +2,49 @@
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
return [ return [
Monolog\Handler\RotatingFileHandler::class => function(ContainerInterface $container) { 'log_processors' => function(ContainerInterface $container) {
$handler = new Monolog\Handler\RotatingFileHandler($container->get('log_file')); return [
$handler->setFormatter($container->get(Monolog\Formatter\LineFormatter::class)); $container->get(Monolog\Processor\PsrLogMessageProcessor::class),
return $handler; $container->get(Monolog\Processor\IntrospectionProcessor::class),
$container->get(Monolog\Processor\MemoryPeakUsageProcessor::class),
];
},
'request_log_handler' => function(ContainerInterface $container) {
return (new Monolog\Handler\RotatingFileHandler(implode(DIRECTORY_SEPARATOR, [$container->get('logs_folder'), 'requests.log'])));
}, },
'request_logger' => function(ContainerInterface $container) { 'request_logger' => function(ContainerInterface $container) {
$logger = new Monolog\Logger('request_logger'); return new Monolog\Logger(
$handler = new Monolog\Handler\RotatingFileHandler(implode(DIRECTORY_SEPARATOR, [$container->get('logs_folder'), 'requests.log'])); 'request_logger',
$handler->setFormatter($container->get(Monolog\Formatter\SyslogFormatter::class)); [$container->get('request_log_handler')],
$dedupHandler = new Monolog\Handler\DeduplicationHandler($handler, null, Monolog\Level::Info); $container->get('log_processors')
$logger->pushHandler($dedupHandler); );
$logger->pushProcessor($container->get(Monolog\Processor\PsrLogMessageProcessor::class)); },
$logger->pushProcessor($container->get(Monolog\Processor\IntrospectionProcessor::class)); 'file_log_handler' => function(ContainerInterface $container) {
$logger->pushProcessor($container->get(Monolog\Processor\MemoryUsageProcessor::class)); return new Monolog\Handler\FilterHandler(
return $logger; (new Monolog\Handler\RotatingFileHandler($container->get('log_file')))
->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
Monolog\Level::Error
);
},
'debug_log_handler' => function(ContainerInterface $container) {
return new Monolog\Handler\FilterHandler(
(new Monolog\Handler\RotatingFileHandler(implode(DIRECTORY_SEPARATOR, [$container->get('logs_folder'), 'debug.log'])))
->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true)),
Monolog\Level::Debug,
Monolog\Level::Warning
);
}, },
Psr\Log\LoggerInterface::class => function(ContainerInterface $container) { Psr\Log\LoggerInterface::class => function(ContainerInterface $container) {
$logger = new Monolog\Logger('file_logger'); return $container->get('file_logger');
$logger->pushHandler($container->get(Monolog\Handler\RotatingFileHandler::class)); },
$logger->pushProcessor($container->get(Monolog\Processor\PsrLogMessageProcessor::class)); 'file_logger' => function(ContainerInterface $container) {
$logger->pushProcessor($container->get(Monolog\Processor\IntrospectionProcessor::class)); return new Monolog\Logger(
$logger->pushProcessor($container->get(Monolog\Processor\MemoryUsageProcessor::class)); 'file',
return $logger; [
$container->get('file_log_handler'),
$container->get('debug_log_handler')
],
$container->get('log_processors')
);
}, },
]; ];

View File

@ -0,0 +1,14 @@
<?php
namespace ProVM\Common\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Views\Blade as View;
class Jobs
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, View $view): ResponseInterface
{
return $view->render($response, 'jobs.list');
}
}

View File

@ -29,7 +29,7 @@ class Logging
{ {
$response = $handler->handle($request); $response = $handler->handle($request);
$output = [ $output = [
'uri' => var_export($request->getUri(), true), 'uri' => print_r($request->getUri(), true),
'body' => $request->getParsedBody() 'body' => $request->getParsedBody()
]; ];
$this->getLogger()->info(\Safe\json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); $this->getLogger()->info(\Safe\json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));

View File

@ -3,6 +3,9 @@ server {
root /app/ui/public; root /app/ui/public;
index index.php index.html index.htm; index index.php index.html index.htm;
access_log /var/logs/nginx/ui.access.log;
error_log /var/logs/nginx/ui.error.log;
location / { location / {
try_files $uri $uri/ /index.php?$query_string; try_files $uri $uri/ /index.php?$query_string;
} }
@ -16,4 +19,4 @@ server {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_INFO $fastcgi_path_info;
} }
} }

View File

@ -8,11 +8,5 @@ Monolog\ErrorHandler::register($app->getContainer()->get(Psr\Log\LoggerInterface
try { try {
$app->run(); $app->run();
} catch (Error | Exception $e) { } catch (Error | Exception $e) {
$logger = $app->getContainer()->get(Psr\Log\LoggerInterface::class); $app->getContainer()->get(Psr\Log\LoggerInterface::class)->error($e);
if (isset($_REQUEST)) {
$logger->debug(Safe\json_encode(compact('_REQUEST')));
}
$logger->debug(Safe\json_encode(compact('_SERVER')));
$logger->error($e);
throw $e;
} }

View File

@ -0,0 +1,4 @@
<?php
use ProVM\Common\Controller\Jobs;
$app->get('/jobs', Jobs::class);

View File

@ -5,7 +5,12 @@
<div id="messages" class="ui basic segment"></div> <div id="messages" class="ui basic segment"></div>
@endsection @endsection
@push('page_styles')
<link rel="stylesheet" href="//cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css" />
@endpush
@push('page_scripts') @push('page_scripts')
<script type="text/javascript" src="//cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">
class Message class Message
{ {
@ -22,7 +27,8 @@
} }
attachments attachments
constructor({id, uid, subject, date_time, from, states, attachments}) { 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)
@ -31,7 +37,8 @@
.set().states(states) .set().states(states)
.set().attachments(attachments) .set().attachments(attachments)
} }
get() { get()
{
return { return {
id: () => { id: () => {
return this.id return this.id
@ -56,7 +63,8 @@
} }
} }
} }
set() { set()
{
return { return {
id: id => { id: id => {
this.id = id this.id = id
@ -99,7 +107,8 @@
} }
} }
} }
has() { has()
{
return { return {
attachments: () => { attachments: () => {
return this.states.attachments return this.states.attachments
@ -115,7 +124,8 @@
} }
} }
} }
doesHave() { doesHave()
{
return { return {
attachments: () => { attachments: () => {
this.states.attachments = true this.states.attachments = true
@ -136,7 +146,8 @@
} }
} }
draw() { draw()
{
return { return {
row: () => { row: () => {
const format = Intl.DateTimeFormat('es-CL', {dateStyle: 'full', timeStyle: 'short'}) const format = Intl.DateTimeFormat('es-CL', {dateStyle: 'full', timeStyle: 'short'})
@ -196,14 +207,15 @@
}) })
}, },
scheduledButton: () => { scheduledButton: () => {
return $('<i></i>').addClass('ui green circular inverted check icon') return $('<i></i>').addClass('ui green circle check icon')
}, },
schedulingButton: () => { schedulingButton: () => {
return $('<i></i>').addClass('ui circular inverted redo loading icon') return $('<i></i>').addClass('ui circle redo loading icon')
} }
} }
} }
download() { download()
{
return { return {
attachments: event => { attachments: event => {
const td = $(event.currentTarget).parent() const td = $(event.currentTarget).parent()
@ -218,7 +230,11 @@
return Send.put({ return Send.put({
uri, uri,
data data
}).then(response => { }).then((response, status, jqXHR) => {
if (parseInt(jqXHR.status/100) !== 2 || jqXHR.status === 204) {
td.html('')
return
}
if (response.scheduled > 0) { if (response.scheduled > 0) {
td.html('').append(this.draw().scheduledButton()) td.html('').append(this.draw().scheduledButton())
} }
@ -265,6 +281,7 @@
$(this.id.results).html('').append( $(this.id.results).html('').append(
this.draw().table() this.draw().table()
) )
let table = new DataTable('#messages_table')
} else { } else {
$(this.id.results).html('').append( $(this.id.results).html('').append(
this.draw().empty() this.draw().empty()
@ -290,7 +307,7 @@
) )
}, },
table: () => { table: () => {
return $('<table></table>').addClass('ui table').append( return $('<table></table>').attr('id', 'messages_table').addClass('ui table').append(
this.draw().head() this.draw().head()
).append( ).append(
this.draw().body() this.draw().body()
@ -325,7 +342,7 @@
).append( ).append(
$('<th></th>').html('Downloaded Attachments') $('<th></th>').html('Downloaded Attachments')
).append( ).append(
$('<th></th>') $('<th></th>').html('Schedule?')
) )
) )
}, },
@ -334,7 +351,7 @@
this.visible.forEach((m, i) => { this.visible.forEach((m, i) => {
const row = m.draw().row() const row = m.draw().row()
row.prepend( row.prepend(
$('<td></td>').html(i + this.current + 1) $('<td></td>').html(i + 1)
) )
tbody.append(row) tbody.append(row)
}) })

View File

@ -50,8 +50,12 @@
} }
const list = $('<div></div>').addClass('ui list') const list = $('<div></div>').addClass('ui list')
this.mailboxes.forEach(mb => { this.mailboxes.forEach(mb => {
let count = ''
if (typeof mb.last_checked !== 'undefined') {
count = ' (' + mb.last_checked.count + ')'
}
list.append( list.append(
$('<a></a>').addClass('item').attr('href', '{{$urls->base}}/emails/mailbox/' + mb.id).html(mb.name) $('<a></a>').addClass('item').attr('href', '{{$urls->base}}/emails/mailbox/' + mb.id).html(mb.name + count)
) )
}) })
parent.append(list) parent.append(list)

View File

@ -0,0 +1,14 @@
@extends('layout.base')
@section('page_title')
Jobs
@hasSection('jobs_title')
-
@yield('jobs_title')
@endif
@endsection
@section('page_content')
<h1>Jobs</h1>
@yield('jobs_content')
@endsection

Some files were not shown because too many files have changed in this diff Show More