Merge branch 'feature/jobs' into develop

This commit is contained in:
2023-06-12 21:14:31 -04:00
78 changed files with 1510 additions and 698 deletions

View File

@ -7,8 +7,14 @@
* [ ] Download `attachments` (*encrypted* & *decrypted*).
## CLI
* [x] Get `mailboxes` from **[API]** then run `grab messages` job in **[API]** for each one.
* [x] Get `pending attachments` jobs from **[API]** and run.
#### Automatic
* [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
* [x] Grab all `mailboxes` from `Email Provider`, identifying those that are registered.
@ -21,18 +27,17 @@
## Workflow
* **[User]** Choose `mailboxes` to register or unregister
-> **[API]** Register selected `mailboxes` and get `messages` for recently registered.
* **[Cron]** Get registered `mailboxes` -> **[API]** Get `messages`
* **[User]** Check messages found -> **[API]** Schedule `attachments`
* **[Cron]** Get `attachment download` jobs -> **[API]** grab `attachments`
* **[User]** Choose `mailboxes` to register or unregister.
-> **[API]** Register selected `mailboxes`, register new `messages:grab` job.
* **[Cron]** Get `jobs`, run `jobs`.
* **[User]** Check messages found -> **[API]** Schedule `attachments`.
### Jobs
## Jobs
#### Automatic
* [ ] Check registered `mailboxes` for new `messages`, logging last check.
* [ ] Check if `attachments` are `encrypted`.
* [ ] Check for new scheduled jobs.
* [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`.
* [ ] Grab `attachments`.
* [ ] Decrypt `attachments`.
* [ ] Grab `messages` for `mailbox` id.
* [ ] Grab `attachments` for `message` id.
* [ ] Decrypt `attachment`.

View File

@ -1,19 +1,20 @@
<?php
namespace ProVM\Common\Controller;
use ProVM\Common\Exception\Request\MissingArgument;
use Psr\Http\Message\ResponseInterface;
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 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
{
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) {
return $attachment->toArray();
@ -24,35 +25,7 @@ class Attachments
];
return $this->withJson($response, $output);
}
public function grab(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Jobs $jobsService): 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
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Attachments $service, LoggerInterface $logger, int $attachment_id): ResponseInterface
{
$attachment = $service->getRepository()->fetchById($attachment_id);
@ -61,4 +34,25 @@ class Attachments
$response->getBody()->write($service->getFile($attachment_id));
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
{
return $this->withJson($response, [
'version' => '1.0.0'
'version' => '1.0.0',
'app' => 'emails'
]);
}
}
}

View File

@ -1,7 +1,6 @@
<?php
namespace ProVM\Common\Controller;
use ProVM\Emails\Model\Job;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use ProVM\Common\Exception\Request\MissingArgument;
@ -13,23 +12,25 @@ class Jobs
{
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();
$json = json_decode($body->getContents());
if (!isset($json->messages)) {
throw new MissingArgument('messages', 'array', 'messages ids');
if (!isset($json->jobs)) {
throw new MissingArgument('jobs', 'array', 'job commands with arguments');
}
$output = [
'messages' => $json->messages,
'total' => count($json->messages),
'jobs' => $json->jobs,
'total' => count($json->jobs),
'scheduled' => 0
];
foreach ($json->messages as $message_id) {
if ($service->schedule($message_id)) {
$message = $messagesService->getRepository()->fetchById($message_id);
$message->doesHaveScheduledDownloads();
$messagesService->getRepository()->save($message);
foreach ($json->jobs as $job) {
if ($service->queue($job->command, $job->arguments)) {
$output['scheduled'] ++;
}
}
@ -37,12 +38,47 @@ class Jobs
}
public function pending(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
{
$pending = array_map(function(Job $job) {
return $job->toArray();
}, $service->getPending());
$pending = $service->getPending();
$output = [
'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);
}

View File

@ -1,11 +1,11 @@
<?php
namespace ProVM\Common\Controller;
use ProVM\Common\Exception\Request\MissingArgument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use ProVM\Common\Exception\Request\MissingArgument;
use ProVM\Common\Implement\Controller\Json;
use ProVM\Common\Service\Messages as Service;
use ProVM\Common\Service;
use ProVM\Emails\Model\Message;
use function Safe\json_decode;
@ -13,7 +13,7 @@ class Messages
{
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);
$messages = array_map(function(Message $message) {
@ -37,7 +37,7 @@ class Messages
];
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);
$messages = array_values(array_filter(array_map(function(Message $message) use ($service, $attachments) {
@ -63,32 +63,48 @@ class Messages
];
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);
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();
$json = json_decode($body->getContents());
if (!isset($json->mailboxes)) {
throw new MissingArgument('mailboxes', 'array', 'mailboxes names');
$json = json_decode($body);
if (!isset($json->messages)) {
throw new MissingArgument('messages', 'array', 'messages IDs');
}
$output = [
'mailboxes' => $json->mailboxes,
'messages' => [],
'message_count' => 0
'messages' => $json->messages,
'scheduled' => 0
];
foreach ($json->mailboxes as $mailbox_name) {
$messages = $service->grab($mailbox_name);
foreach ($messages as $message) {
if ($message->hasValidAttachments()) {
$attachmentsService->create($message);
}
foreach ($json->messages as $message_id) {
if ($jobsService->queue('attachments:grab', [$message_id])) {
$message = $service->getRepository()->fetchById($message_id);
$message->doesHaveScheduledDownloads();
$service->getRepository()->save($message);
$output['scheduled'] ++;
}
$output['messages'] = array_merge($output['messages'], $messages);
$output['message_count'] += count($messages);
}
return $this->withJson($response, $output);
}

View File

@ -0,0 +1,15 @@
<?php
namespace ProVM\Common\Exception\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
namespace ProVM\Common\Exception;
use Ddeboer\Imap\MailboxInterface;
use Exception;
use Throwable;
use Ddeboer\Imap\MailboxInterface;
class EmptyMailbox extends Exception
{
@ -12,4 +13,4 @@ class EmptyMailbox extends Exception
$code = 101;
parent::__construct($message, $code, $previous);
}
}
}

View File

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

View File

@ -2,6 +2,7 @@
namespace ProVM\Common\Exception\Mailbox;
use Exception;
use Throwable;
use ProVM\Emails\Model\Mailbox;
class Stateless extends Exception
@ -12,4 +13,4 @@ class Stateless extends Exception
$code = 102;
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

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

View File

@ -1,24 +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;
use ProVM\Common\Exception\Database\BlankResult;
use ProVM\Common\Service;
class Messages
{
public function __construct(protected Service\Messages $service, protected LoggerInterface $logger) {}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$this->service->checkSchedule();
} catch (BlankResult $e) {
$this->logger->notice($e);
}
return $handler->handle($request);
}
}

View File

@ -117,7 +117,7 @@ class Attachments extends Base
continue;
}
$name = $file->getBasename(".{$file->getExtension()}");
list($subject, $date, $filename) = explode(' - ', $name);
list($date, $subject, $filename) = explode(' - ', $name);
try {
$message = $this->getMessages()->find($subject, $date)[0];
$filename = "{$filename}.{$file->getExtension()}";

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

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

@ -154,7 +154,6 @@ class Messages extends Base
$message->doesHaveValidAttachments();
}
}
error_log(json_encode(compact('message')).PHP_EOL,3,'/logs/debug');
$this->getRepository()->save($message);
return true;
} catch (PDOException $e) {
@ -196,20 +195,7 @@ class Messages extends Base
$registered = $this->getMailboxes()->getRegistered();
foreach ($registered as $mailbox) {
if (!$this->getMailboxes()->isUpdated($mailbox)) {
$this->logger->info("Updating messages from {$mailbox->getName()}");
$this->grab($mailbox->getName());
}
}
}
public function checkSchedule(): void
{
$messages = $this->getRepository()->fetchAll();
foreach ($messages as $message) {
if ($message->hasAttachments() and $message->hasValidAttachments() and !$message->hasDownloadedAttachments() and !$message->hasScheduledDownloads()) {
if ($this->getJobsService()->schedule($message->getId())) {
$message->doesHaveDownloadedAttachments();
$this->getRepository()->save($message);
}
$this->getJobsService()->queue('messages:grab', [$mailbox->getId()]);
}
}
}

View File

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

View File

@ -1,15 +1,15 @@
server {
listen 0.0.0.0:8080;
listen 81;
root /app/api/public;
index index.php index.html index.htm;
access_log /var/logs/nginx/access.log;
error_log /var/logs/nginx/error.log;
access_log /var/logs/nginx/api.access.log;
error_log /var/logs/nginx/api.error.log;
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') {
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';
@ -26,11 +26,12 @@ server {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_pass api:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
}

View File

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

View File

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

View File

@ -4,9 +4,9 @@ use ProVM\Common\Controller\Jobs;
$app->group('/messages', function($app) {
$app->put('/grab', [Messages::class, 'grab']);
$app->put('/schedule', [Jobs::class, 'schedule']);
$app->get('/pending', [Jobs::class, 'pending']);
$app->put('/schedule', [Messages::class, 'schedule']);
//$app->get('/pending', [Jobs::class, 'pending']);
});
$app->group('/message/{message_id}', function($app) {
$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
use DI\ContainerBuilder;
use DI\Bridge\Slim\Bridge;
require_once 'composer.php';
$builder = new \DI\ContainerBuilder();
$builder = new ContainerBuilder();
$folders = [
'settings',
@ -24,7 +27,7 @@ foreach ($folders as $f) {
$builder->addDefinitions($file->getRealPath());
}
}
$app = \DI\Bridge\Slim\Bridge::create($builder->build());
$app = Bridge::create($builder->build());
$folder = implode(DIRECTORY_SEPARATOR, [
__DIR__,

View File

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

View File

@ -33,5 +33,8 @@ return [
$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

@ -8,9 +8,11 @@ return [
'Mailbox' => ProVM\Emails\Repository\Mailbox::class,
'Message' => ProVM\Emails\Repository\Message::class,
'Attachment' => ProVM\Emails\Repository\Attachment::class,
'Job' => ProVM\Emails\Repository\Job::class,
"State\\Mailbox" => ProVM\Emails\Repository\State\Mailbox::class,
"State\\Message" => ProVM\Emails\Repository\State\Message::class,
"State\\Attachment" => ProVM\Emails\Repository\State\Attachment::class
"State\\Attachment" => ProVM\Emails\Repository\State\Attachment::class,
"State\\Job" => ProVM\Emails\Repository\State\Job::class,
]);
}
];

View File

@ -6,7 +6,7 @@ return [
return new ProVM\Common\Middleware\Auth(
$container->get(Nyholm\Psr7\Factory\Psr17Factory::class),
$container->get(Psr\Log\LoggerInterface::class),
$container->get('api_key')
$container->get(ProVM\Common\Service\Auth::class)
);
}
];

View File

@ -6,12 +6,12 @@ return [
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),
];
},
'request_log_handler' => function(ContainerInterface $container) {
return (new Monolog\Handler\RotatingFileHandler(implode(DIRECTORY_SEPARATOR, [$container->get('logs_folder'), 'requests.log'])))
->setFormatter(new Monolog\Formatter\LineFormatter(null, null, true));
return (new Monolog\Handler\RotatingFileHandler(implode(DIRECTORY_SEPARATOR, [$container->get('logs_folder'), 'requests.log'])));
},
'request_logger' => function(ContainerInterface $container) {
return new Monolog\Logger(
@ -36,7 +36,7 @@ return [
);
},
Psr\Log\LoggerInterface::class => function(ContainerInterface $container) {
return $container->get('elk_logger');
return $container->get('file_logger');
},
'file_logger' => function(ContainerInterface $container) {
return new Monolog\Logger(

View File

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

View File

@ -3,29 +3,27 @@ namespace ProVM\Emails\Model;
use DateTimeInterface;
use ProVM\Common\Define\Model;
use ProVM\Common\Exception\Database\BlankResult;
use ProVM\Common\Exception\Job\Stateless;
use ProVM\Emails;
class Job implements Model
{
protected int $id;
protected Message $message;
protected DateTimeInterface $dateTime;
protected bool $executed;
protected string $command;
protected string $arguments;
public function getId(): int
{
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;
}
public function isExecuted(): bool
{
return $this->executed ?? false;
return $this->arguments ?? '';
}
public function setId(int $id): Job
@ -33,29 +31,72 @@ class Job implements Model
$this->id = $id;
return $this;
}
public function setMessage(Message $message): Job
public function setCommand(string $command): Job
{
$this->message = $message;
$this->command = $command;
return $this;
}
public function setDateTime(DateTimeInterface $dateTime): Job
public function setArguments(string $arguments): Job
{
$this->dateTime = $dateTime;
$this->arguments = $arguments;
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;
}
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
{
return [
'id' => $this->getId(),
'message' => $this->getMessage()->toArray(),
'date_time' => $this->getDateTime()->format('Y-m-d H:i:s'),
'executed' => $this->isExecuted()
'command' => $this->getCommand(),
'arguments' => $this->getArguments(),
'states' => $this->getStates()
];
}
public function jsonSerialize(): mixed

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

@ -2,28 +2,28 @@
namespace ProVM\Emails\Repository;
use PDO;
use ProVM\Common\Factory\Model;
use ProVM\Common\Factory;
use Psr\Log\LoggerInterface;
use Safe\DateTimeImmutable;
use ProVM\Common\Define\Model as ModelInterface;
use ProVM\Common\Implement\Repository;
use ProVM\Emails\Model\Job as BaseModel;
use ProVM\Emails;
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);
$this->setFactory($factory)
->setTable('attachments_jobs');
->setTable('jobs');
}
protected \ProVM\Common\Factory\Model $factory;
public function getFactory(): \ProVM\Common\Factory\Model
protected Factory\Model $factory;
public function getFactory(): Factory\Model
{
return $this->factory;
}
public function setFactory(\ProVM\Common\Factory\Model $factory): Job
public function setFactory(Factory\Model $factory): Job
{
$this->factory = $factory;
return $this;
@ -34,12 +34,9 @@ class Job extends Repository
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`message_id` INT UNSIGNED NOT NULL,
`date_time` DATETIME NOT NULL,
`executed` INT(1) UNSIGNED DEFAULT 0,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_messages_{$this->getTable()}` (`message_id`)
REFERENCES `messages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
`command` VARCHAR(100) NOT NULL,
`arguments` TEXT NOT NULL,
PRIMARY KEY (`id`)
)";
$this->getConnection()->query($query);
}
@ -55,69 +52,68 @@ CREATE TABLE {$this->getTable()} (
protected function fieldsForInsert(): array
{
return [
'message_id',
'date_time',
'executed'
'command',
'arguments',
];
}
protected function valuesForInsert(ModelInterface $model): array
{
return [
$model->getMessage()->getId(),
$model->getDateTime()->format('Y-m-d H:i:s'),
$model->isExecuted() ? 1 : 0
$model->getCommand(),
$model->getArguments(),
];
}
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
{
return [
'message_id',
'date_time',
'executed'
'command',
'arguments',
];
}
protected function valuesForCreate(array $data): array
{
return [
$data['message_id'],
$data['date_time'],
$data['executed'] ?? 0
$data['command'],
$data['arguments'],
];
}
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
{
$model = (new BaseModel())
return (new BaseModel())
->setId($row['id'])
->setMessage($this->getFactory()->find(\ProVM\Emails\Model\Message::class)->fetchById($row['message_id']))
->setDateTime(new DateTimeImmutable($row['date_time']));
if ($row['executed'] ?? 0 === 1) {
$model->wasExecuted();
}
return $model;
->setCommand($row['command'])
->setArguments($row['arguments'])
->setStateRepository($this->getFactory()->find(Emails\Model\State\Job::class));
}
public function fetchAllPending(): array
{
$query = "SELECT * FROM {$this->getTable()} WHERE `executed` = 0";
return $this->fetchMany($query);
$query = "SELECT a.*
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` = ?";
return $this->fetchOne($query, [$message_id, $date_time]);
$query = "SELECT * FROM {$this->getTable()} WHERE `command` = ? AND `arguments` = ?";
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";
return $this->fetchOne($query, [$message_id]);
$query = "SELECT a.*
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

@ -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

@ -3,7 +3,7 @@ FROM php:8-cli
ENV PATH ${PATH}:/app/bin
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/* \
&& docker-php-ext-install zip

View File

@ -1,68 +0,0 @@
<?php
namespace ProVM\Common\Command;
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\Common\Service\Communicator;
use function Safe\json_decode;
#[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 json_decode($response->getBody()->getContents())->attachments;
}
protected function decrypt(string $attachment): bool
{
$response = $this->getCommunicator()->put('/attachments/decrypt', ['attachments' => [$attachment]]);
return 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,66 +0,0 @@
<?php
namespace ProVM\Common\Command;
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\Common\Service\Communicator;
use function Safe\json_decode;
#[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 json_decode($response->getBody()->getContents())->messages;
}
protected function grabAttachments(int $message_uid): int
{
$response = $this->getCommunicator()->put('/attachments/grab', ['messages' => [$message_uid]]);
return 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,67 +0,0 @@
<?php
namespace ProVM\Common\Command;
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\Common\Service\Communicator;
use function Safe\json_decode;
#[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 json_decode($response->getBody()->getContents())->mailboxes;
}
protected function grabMessages(string $mailbox): int
{
$response = $this->getCommunicator()->put('/messages/grab', ['mailboxes' => [$mailbox]]);
$body = 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
namespace ProVM\Common\Service;
namespace ProVM\Service;
use HttpResponseException;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ResponseInterface;
use Safe\Exceptions\JsonException;
use function Safe\json_encode;
class Communicator
{
public function __construct(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;
}
public function __construct(protected ClientInterface $client) {}
/**
* @throws HttpResponseException
*/
protected function handleResponse(ResponseInterface $response): ResponseInterface
{
if ($response->getStatusCode() < 200 or $response->getStatusCode() >= 300) {
@ -33,6 +21,11 @@ class Communicator
}
return $response;
}
/**
* @throws HttpResponseException
* @throws JsonException
*/
protected function request(string $method, string $uri, ?array $body = null): ResponseInterface
{
$options = [];
@ -42,23 +35,42 @@ class Communicator
];
$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
{
return $this->request('get', $uri);
}
/**
* @throws HttpResponseException
* @throws JsonException
*/
public function post(string $uri, array $data): ResponseInterface
{
return $this->request('post', $uri, $data);
}
/**
* @throws HttpResponseException
* @throws JsonException
*/
public function put(string $uri, array $data): ResponseInterface
{
return $this->request('put', $uri, $data);
}
/**
* @throws HttpResponseException
* @throws JsonException
*/
public function delete(string $uri, array $data): ResponseInterface
{
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
namespace ProVM\Common\Wrapper;
namespace ProVM\Wrapper;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Application as Base;
class Application extends Base
{
public function __construct(ContainerInterface $container, string $name = 'UNKNOWN', string $version = 'UNKNOWN')
@ -22,4 +21,4 @@ class Application extends Base
$this->container = $container;
return $this;
}
}
}

View File

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

View File

@ -1,3 +1,10 @@
# minutes hour day_of_month month day_of_week command
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 2 * * 2-6 /app/bin/emails messages:grab >> /logs/messages.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
volumes:
- ${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));
try {
$app->run();
} catch (Error | Exception $e) {
$logger = $app->getContainer()->get(Psr\Log\LoggerInterface::class);
$logger->debug(Safe\json_encode(compact('_SERVER')));
$logger->error($e);
throw $e;
} catch (Error $e) {
$app->getContainer()->get(Psr\Log\LoggerInterface::class)->error($e);
} catch (Exception $e) {
$app->getContainer()->get(Psr\Log\LoggerInterface::class)->warning($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
$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
$app->add($app->getContainer()->get(\ProVM\Common\Command\GrabAttachments::class));
$app->add($app->getContainer()->get(\ProVM\Common\Command\DecryptPdf::class));
$app->add($app->getContainer()->get(ProVM\Command\Attachments\Check::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
require_once 'composer.php';
$builder = new \DI\ContainerBuilder();
$builder = new DI\ContainerBuilder();
$folders = [
'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, [
__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
return [
'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;
return [
\Psr\Http\Client\ClientInterface::class => function(ContainerInterface $container) {
return new \GuzzleHttp\Client([
Psr\Http\Client\ClientInterface::class => function(ContainerInterface $container) {
return new GuzzleHttp\Client([
'base_uri' => $container->get('api_uri'),
'headers' => [
'Authorization' => "Bearer {$container->get('api_key')}"
]
]);
}
];
];

View File

@ -2,7 +2,7 @@
use Psr\Container\ContainerInterface;
return [
ProVM\Common\Middleware\Logging::class => function(ContainerInterface $container) {
return new ProVM\Common\Middleware\Logging($container->get('request_logger'));
ProVM\Middleware\Logging::class => function(ContainerInterface $container) {
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;
return [
Monolog\Handler\RotatingFileHandler::class => function(ContainerInterface $container) {
$handler = new Monolog\Handler\RotatingFileHandler($container->get('log_file'));
$handler->setFormatter($container->get(Monolog\Formatter\LineFormatter::class));
return $handler;
'log_processors' => function(ContainerInterface $container) {
return [
$container->get(Monolog\Processor\PsrLogMessageProcessor::class),
$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) {
$logger = new Monolog\Logger('request_logger');
$handler = new Monolog\Handler\RotatingFileHandler(implode(DIRECTORY_SEPARATOR, [$container->get('logs_folder'), 'requests.log']));
$handler->setFormatter($container->get(Monolog\Formatter\SyslogFormatter::class));
$dedupHandler = new Monolog\Handler\DeduplicationHandler($handler, null, Monolog\Level::Info);
$logger->pushHandler($dedupHandler);
$logger->pushProcessor($container->get(Monolog\Processor\PsrLogMessageProcessor::class));
$logger->pushProcessor($container->get(Monolog\Processor\IntrospectionProcessor::class));
$logger->pushProcessor($container->get(Monolog\Processor\MemoryUsageProcessor::class));
return $logger;
return new Monolog\Logger(
'request_logger',
[$container->get('request_log_handler')],
$container->get('log_processors')
);
},
'file_log_handler' => function(ContainerInterface $container) {
return new Monolog\Handler\FilterHandler(
(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) {
$logger = new Monolog\Logger('file_logger');
$logger->pushHandler($container->get(Monolog\Handler\RotatingFileHandler::class));
$logger->pushProcessor($container->get(Monolog\Processor\PsrLogMessageProcessor::class));
$logger->pushProcessor($container->get(Monolog\Processor\IntrospectionProcessor::class));
$logger->pushProcessor($container->get(Monolog\Processor\MemoryUsageProcessor::class));
return $logger;
return $container->get('file_logger');
},
'file_logger' => function(ContainerInterface $container) {
return new Monolog\Logger(
'file',
[
$container->get('file_log_handler'),
$container->get('debug_log_handler')
],
$container->get('log_processors')
);
},
];

View File

@ -1,94 +0,0 @@
-- Adminer 4.8.1 MySQL 5.5.5-10.9.3-MariaDB-1:10.9.3+maria~ubu2204 dump
SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
SET NAMES utf8mb4;
DROP TABLE IF EXISTS `attachments`;
CREATE TABLE `attachments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`message_id` int(10) unsigned NOT NULL,
`filename` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `message_id` (`message_id`),
CONSTRAINT `attachments_ibfk_2` FOREIGN KEY (`message_id`) REFERENCES `messages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
DROP TABLE IF EXISTS `attachments_jobs`;
CREATE TABLE `attachments_jobs` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`message_id` int(10) unsigned NOT NULL,
`date_time` datetime NOT NULL,
`executed` int(1) DEFAULT 0,
PRIMARY KEY (`id`),
KEY `message_id` (`message_id`),
CONSTRAINT `attachments_jobs_ibfk_2` FOREIGN KEY (`message_id`) REFERENCES `messages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
DROP TABLE IF EXISTS `attachments_states`;
CREATE TABLE `attachments_states` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`attachment_id` int(10) unsigned NOT NULL,
`name` varchar(100) NOT NULL,
`value` int(1) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
KEY `attachment_id` (`attachment_id`),
CONSTRAINT `attachments_states_ibfk_1` FOREIGN KEY (`attachment_id`) REFERENCES `attachments` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
DROP TABLE IF EXISTS `mailboxes`;
CREATE TABLE `mailboxes` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`validity` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
DROP TABLE IF EXISTS `mailboxes_states`;
CREATE TABLE `mailboxes_states` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`mailbox_id` int(10) unsigned NOT NULL,
`date_time` datetime NOT NULL,
`count` int(10) unsigned NOT NULL,
`uids` text NOT NULL,
PRIMARY KEY (`id`),
KEY `mailbox_id` (`mailbox_id`),
CONSTRAINT `mailboxes_states_ibfk_2` FOREIGN KEY (`mailbox_id`) REFERENCES `mailboxes` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
DROP TABLE IF EXISTS `messages`;
CREATE TABLE `messages` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`mailbox_id` int(10) unsigned NOT NULL,
`position` int(10) unsigned NOT NULL,
`uid` varchar(255) NOT NULL,
`subject` varchar(255) NOT NULL,
`from` varchar(100) NOT NULL,
`date_time` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `mailbox_id` (`mailbox_id`),
CONSTRAINT `messages_ibfk_1` FOREIGN KEY (`mailbox_id`) REFERENCES `mailboxes` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
DROP TABLE IF EXISTS `messages_states`;
CREATE TABLE `messages_states` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`message_id` int(10) unsigned NOT NULL,
`name` varchar(100) NOT NULL,
`value` int(1) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
KEY `message_id` (`message_id`),
CONSTRAINT `messages_states_ibfk_2` FOREIGN KEY (`message_id`) REFERENCES `messages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- 2022-11-30 03:59:49

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);
$output = [
'uri' => var_export($request->getUri(), true),
'uri' => print_r($request->getUri(), true),
'body' => $request->getParsedBody()
];
$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;
index index.php index.html index.htm;
access_log /var/logs/nginx/ui.access.log;
error_log /var/logs/nginx/ui.error.log;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
@ -16,4 +19,4 @@ server {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
}

View File

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

View File

@ -50,8 +50,12 @@
}
const list = $('<div></div>').addClass('ui list')
this.mailboxes.forEach(mb => {
let count = ''
if (typeof mb.last_checked !== 'undefined') {
count = ' (' + mb.last_checked.count + ')'
}
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)

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

View File

@ -0,0 +1,165 @@
@extends('jobs.base')
@section('jobs_content')
<div id="jobs" class="ui basic segment"></div>
@endsection
@push('page_styles')
<link rel="stylesheet" href="//cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css" />
@endpush
@push('page_scripts')
<script type="text/javascript" src="//cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
class Job
{
id
command
arguments
states
constructor(props)
{
this.id = props.id
this.command = props.command
this.arguments = props.arguments
this.states = props.states
}
lastState()
{
return this.states.sort((a, b) => {
const d1 = new Date(a.date.date)
const d2 = new Date(b.date.date)
return d1 - d2
}).at(-1)
}
isPending()
{
return this.lastState().status === 1
}
draw()
{
return {
row: () => {
const status = this.isPending() ? 'yellow clock' : 'green check'
const formatter = new Intl.DateTimeFormat('es-CL', {dateStyle: 'full', timeStyle: 'long'})
const d = new Date(this.lastState().date.date)
return $('<tr></tr>').attr('data-id', this.id).append(
$('<td></td>').html(this.id)
).append(
$('<td></td>').html(this.command)
).append(
$('<td></td>').html(this.arguments)
).append(
$('<td></td>').append(
$('<i></i>').addClass('ui icon ' + status)
)
).append(
$('<td></td>').html(formatter.format(d))
)
}
}
}
execute()
{
const uri = '/job/' + this.id + '/execute'
Send.get(uri).then((response, status, jqXHR) => {
console.debug(jqXHR.status)
console.debug(response)
})
}
}
const jobs = {
id: '',
data: [],
get: function() {
return {
jobs: () => {
this.draw().loader()
const uri = '/jobs'
return Send.get(uri).then((response, status, jqXHR) => {
if (parseInt(jqXHR.status/100) !== 2 || jqXHR.status === 204) {
$(this.id).html('').append(
this.draw().empty()
)
return
}
if (response.jobs.length === 0) {
$(this.id).html('').append(
this.draw().empty()
)
return
}
this.data = response.jobs.map(job => {
return new Job(job)
})
this.draw().jobs()
})
}
}
},
draw: function() {
return {
loader: () => {
$(this.id).html('').append(
$('<div></div>').addClass('ui segment').append(
$('<p></p>').html('&nbsp;')
).append(
$('<div></div>').addClass('ui active dimmer').append(
$('<div></div>').addClass('ui indeterminate elastic loader')
)
).append(
$('<p></p>').html('&nbsp;')
)
)
},
empty: () => {
return $('<div></div>').addClass('ui message').html('No messages found.')
},
jobs: () => {
$(this.id).html('')
const table = $('<table></table>').addClass('ui table').attr('id', 'jobs_table')
table.append(
this.draw().head()
).append(
this.draw().body()
)
$(this.id).append(table)
const t = new DataTable('#jobs_table', {
order: [[4, 'desc']]
})
},
head: () => {
return $('<thead></thead>').append(
$('<tr></tr>').append(
$('<th></th>').html('ID')
).append(
$('<th></th>').html('Command')
).append(
$('<th></th>').html('Arguments')
).append(
$('<th></th>').html('Status')
).append(
$('<th></th>').html('Date')
)
)
},
body: () => {
const body = $('<tbody></tbody>')
this.data.forEach(job => {
body.append(job.draw().row())
})
return body
}
}
},
setup: function() {
this.get().jobs()
}
}
$().ready(() => {
jobs.id = '#jobs'
jobs.setup()
})
</script>
@endpush

View File

@ -1,5 +1,6 @@
<nav class="ui menu">
<a class="item" href="{{$urls->base}}">Inicio</a>
<a class="item" href="{{$urls->base}}/emails/mailboxes">Register Mailboxes</a>
<a class="item" href="{{$urls->base}}/jobs">Jobs</a>
</nav>
<br />

View File

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