diff --git a/api/common/Controller/Attachments.php b/api/common/Controller/Attachments.php index 5b33445..68d3dd6 100644 --- a/api/common/Controller/Attachments.php +++ b/api/common/Controller/Attachments.php @@ -7,6 +7,7 @@ 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; class Attachments { @@ -51,4 +52,13 @@ class Attachments } 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); + + $response->withHeader('Content-Type', 'application/pdf'); + $response->withHeader('Content-Disposition', "'attachment;filename='{$attachment->getFullFilename()}'"); + $response->getBody()->write($service->getFile($attachment_id)); + return $response; + } } \ No newline at end of file diff --git a/api/common/Service/Attachments.php b/api/common/Service/Attachments.php index 0c2a6a0..6c92915 100644 --- a/api/common/Service/Attachments.php +++ b/api/common/Service/Attachments.php @@ -3,9 +3,11 @@ namespace ProVM\Common\Service; use Ddeboer\Imap\Message\AttachmentInterface; use Ddeboer\Imap\MessageInterface; +use Nyholm\Psr7\Stream; use ProVM\Common\Exception\Message\NoAttachments; use ProVM\Emails\Model\Message; use ProVM\Emails\Repository\Attachment; +use Psr\Http\Message\StreamInterface; use Psr\Log\LoggerInterface; use Safe\Exceptions\FilesystemException; @@ -84,6 +86,22 @@ class Attachments extends Base $remote_message = $this->getMessages()->getRemoteMessage($message->getUID()); return $this->getRemoteService()->get($remote_message, $relative_filename); } + public function getFile(int $attachment_id): string + { + $attachment = $this->getRepository()->fetchById($attachment_id); + $filename = implode(DIRECTORY_SEPARATOR, [ + $this->getFolder(), + $attachment->getFullFilename() + ]); + if ($attachment->isDecrypted()) { + $filename = implode(DIRECTORY_SEPARATOR, [ + $this->getFolder(), + 'decrypted', + $attachment->getFullFilename() + ]); + } + return \Safe\file_get_contents($filename); + } public function getAll(): array { diff --git a/api/public/index.php b/api/public/index.php index aed6193..9659116 100644 --- a/api/public/index.php +++ b/api/public/index.php @@ -8,6 +8,6 @@ Monolog\ErrorHandler::register($app->getContainer()->get(Psr\Log\LoggerInterface try { $app->run(); } catch (Error | Exception $e) { - $app->getContainer()->get(\Psr\Log\LoggerInterface::class)->error($e); + $app->getContainer()->get(Psr\Log\LoggerInterface::class)->error($e); throw $e; } diff --git a/api/resources/routes/02_attachments.php b/api/resources/routes/02_attachments.php index 56c26c1..62969b7 100644 --- a/api/resources/routes/02_attachments.php +++ b/api/resources/routes/02_attachments.php @@ -5,4 +5,7 @@ $app->group('/attachments', function($app) { $app->put('/grab', [Attachments::class, 'grab']); $app->post('/decrypt', [Attachments::class, 'decrypt']); $app->get('[/]', Attachments::class); +}); +$app->group('/attachment/{attachment_id}', function($app) { + $app->get('[/]', [Attachments::class, 'get']); }); \ No newline at end of file diff --git a/api/resources/routes/02_messages.php b/api/resources/routes/02_messages.php index 9c3c07f..ed080d5 100644 --- a/api/resources/routes/02_messages.php +++ b/api/resources/routes/02_messages.php @@ -6,4 +6,7 @@ $app->group('/messages', function($app) { $app->put('/grab', [Messages::class, 'grab']); $app->put('/schedule', [Jobs::class, 'schedule']); $app->get('/pending', [Jobs::class, 'pending']); +}); +$app->group('/message/{message_id}', function($app) { + $app->get('[/]', [Messages::class, 'get']); }); \ No newline at end of file diff --git a/api/setup/setups/98_log.php b/api/setup/setups/98_log.php index b81de42..0aedf37 100644 --- a/api/setup/setups/98_log.php +++ b/api/setup/setups/98_log.php @@ -2,21 +2,21 @@ use Psr\Container\ContainerInterface; return [ - \Monolog\Handler\DeduplicationHandler::class => function(ContainerInterface $container) { - return new \Monolog\Handler\DeduplicationHandler($container->get(\Monolog\Handler\RotatingFileHandler::class)); + Monolog\Handler\DeduplicationHandler::class => function(ContainerInterface $container) { + return new Monolog\Handler\DeduplicationHandler($container->get(Monolog\Handler\RotatingFileHandler::class)); }, - \Monolog\Handler\RotatingFileHandler::class => function(ContainerInterface $container) { - $handler = new \Monolog\Handler\RotatingFileHandler($container->get('log_file')); - $handler->setFormatter($container->get(\Monolog\Formatter\SyslogFormatter::class)); + Monolog\Handler\RotatingFileHandler::class => function(ContainerInterface $container) { + $handler = new Monolog\Handler\RotatingFileHandler($container->get('log_file')); + $handler->setFormatter($container->get(Monolog\Formatter\SyslogFormatter::class)); return $handler; }, - \Psr\Log\LoggerInterface::class => function(ContainerInterface $container) { - $logger = new \Monolog\Logger('file_logger'); - $logger->pushHandler($container->get(\Monolog\Handler\DeduplicationHandler::class)); - //$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)); + Psr\Log\LoggerInterface::class => function(ContainerInterface $container) { + $logger = new Monolog\Logger('file_logger'); + $logger->pushHandler($container->get(Monolog\Handler\DeduplicationHandler::class)); + //$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; } ]; \ No newline at end of file diff --git a/api/src/Model/Attachment.php b/api/src/Model/Attachment.php index 2fe5570..49bf820 100644 --- a/api/src/Model/Attachment.php +++ b/api/src/Model/Attachment.php @@ -163,6 +163,7 @@ class Attachment implements Model 'date_time' => $this->getMessage()->getDateTime()->format('Y-m-d H:i:s') ], 'filename' => $this->getFilename(), + 'fullname' => $this->getFullFilename(), 'downloaded' => $this->isDownloaded(), 'encrypted' => $this->isEncrypted(), 'decrypted' => $this->isDecrypted() diff --git a/ui/common/Controller/Attachments.php b/ui/common/Controller/Attachments.php new file mode 100644 index 0000000..70c01df --- /dev/null +++ b/ui/common/Controller/Attachments.php @@ -0,0 +1,21 @@ +get("/attachment/{$attachment_id}", [ + 'stream' => true, + 'sink' => \Safe\fopen('php://stdout', 'wb') + ]); + $response->withHeader('Content-Type', 'application/pdf'); + $response->getBody()->write($rs->getBody()->getContents()); + return $response; + } +} \ No newline at end of file diff --git a/ui/common/Controller/Emails.php b/ui/common/Controller/Emails.php index a7c7efa..dcbb11c 100644 --- a/ui/common/Controller/Emails.php +++ b/ui/common/Controller/Emails.php @@ -11,8 +11,12 @@ class Emails { return $view->render($response, 'emails.mailboxes'); } - public function messages(ServerRequestInterface $request, ResponseInterface $response, View $view, string $mailbox): ResponseInterface + public function messages(ServerRequestInterface $request, ResponseInterface $response, View $view, int $mailbox_id): ResponseInterface { - return $view->render($response, 'emails.messages', ['mailbox_id' => $mailbox]); + return $view->render($response, 'emails.messages', compact('mailbox_id')); + } + public function show(ServerRequestInterface $request, ResponseInterface $response, View $view, int $message_id): ResponseInterface + { + return $view->render($response, 'emails.show', compact('message_id')); } } \ No newline at end of file diff --git a/ui/composer.json b/ui/composer.json index 8d7e8da..aefa104 100644 --- a/ui/composer.json +++ b/ui/composer.json @@ -3,6 +3,7 @@ "type": "project", "require": { "berrnd/slim-blade-view": "^1.0", + "guzzlehttp/guzzle": "^7.5", "monolog/monolog": "^3.2", "nyholm/psr7": "^1.5", "nyholm/psr7-server": "^1.0", diff --git a/ui/public/index.php b/ui/public/index.php index 0606ece..9659116 100644 --- a/ui/public/index.php +++ b/ui/public/index.php @@ -4,4 +4,10 @@ $app = require_once implode(DIRECTORY_SEPARATOR, [ 'setup', 'app.php' ]); -$app->run(); +Monolog\ErrorHandler::register($app->getContainer()->get(Psr\Log\LoggerInterface::class)); +try { + $app->run(); +} catch (Error | Exception $e) { + $app->getContainer()->get(Psr\Log\LoggerInterface::class)->error($e); + throw $e; +} diff --git a/ui/resources/routes/01_emails.php b/ui/resources/routes/01_emails.php index a5d9f2d..e8addbd 100644 --- a/ui/resources/routes/01_emails.php +++ b/ui/resources/routes/01_emails.php @@ -2,9 +2,12 @@ use ProVM\Common\Controller\Emails; $app->group('/emails', function($app) { - $app->group('/mailbox/{mailbox}', function ($app) { + $app->group('/mailbox/{mailbox_id}', function($app) { $app->get('[/]', [Emails::class, 'messages']); }); + $app->group('/message/{message_id}', function($app) { + $app->get('[/]', [Emails::class, 'show']); + }); $app->get('/mailboxes', Emails::class); $app->get('[/]', Emails::class); }); diff --git a/ui/resources/routes/02_attachments.php b/ui/resources/routes/02_attachments.php new file mode 100644 index 0000000..c95452a --- /dev/null +++ b/ui/resources/routes/02_attachments.php @@ -0,0 +1,6 @@ +group('/attachment/{attachment_id}', function($app) { + $app->get('[/]', [Attachments::class, 'get']); +}); \ No newline at end of file diff --git a/ui/resources/views/emails/messages.blade.php b/ui/resources/views/emails/messages.blade.php index 138ac7a..2542956 100644 --- a/ui/resources/views/emails/messages.blade.php +++ b/ui/resources/views/emails/messages.blade.php @@ -141,7 +141,7 @@ $('
').addClass('ui popup').append(list) ) } - return $('