159 lines
5.0 KiB
PHP
159 lines
5.0 KiB
PHP
<?php
|
|
namespace ProVM\Common\Service;
|
|
|
|
use Ddeboer\Imap\Message\AttachmentInterface;
|
|
use Ddeboer\Imap\MessageInterface;
|
|
use ProVM\Emails\Model\Attachment;
|
|
use ProVM\Emails\Model\Message;
|
|
|
|
class Attachments
|
|
{
|
|
public function __construct(Decrypt $decrypt, \ProVM\Emails\Repository\Attachment $repository, string $attachments_folder)
|
|
{
|
|
$this->setDecrypt($decrypt);
|
|
$this->setAttachmentsFolder($attachments_folder);
|
|
}
|
|
|
|
protected string $attachments_folder;
|
|
protected Decrypt $decrypt;
|
|
protected \ProVM\Emails\Repository\Attachment $repository;
|
|
|
|
public function getAttachmentsFolder(): string
|
|
{
|
|
return $this->attachments_folder;
|
|
}
|
|
public function getDecrypt(): Decrypt
|
|
{
|
|
return $this->decrypt;
|
|
}
|
|
public function getRepository(): \ProVM\Emails\Repository\Attachment
|
|
{
|
|
return $this->repository;
|
|
}
|
|
|
|
public function setAttachmentsFolder(string $folder): Attachments
|
|
{
|
|
$this->attachments_folder = $folder;
|
|
return $this;
|
|
}
|
|
public function setDecrypt(Decrypt $decrypt): Attachments
|
|
{
|
|
$this->decrypt = $decrypt;
|
|
return $this;
|
|
}
|
|
public function setRepository(\ProVM\Emails\Repository\Attachment $repository): Attachments
|
|
{
|
|
$this->repository = $repository;
|
|
return $this;
|
|
}
|
|
|
|
public function validateAttachment(AttachmentInterface $remote_attachment): bool
|
|
{
|
|
return str_contains($remote_attachment->getFilename(), '.pdf');
|
|
}
|
|
|
|
public function buildAttachment(Message $message, AttachmentInterface $remote_attachment): Attachment
|
|
{
|
|
$data = [
|
|
'message_id' => $message->getId(),
|
|
'filename' => $this->buildFilename($message, $remote_attachment)
|
|
];
|
|
return $this->getRepository()->create($data);
|
|
}
|
|
|
|
public function exists(Attachment $attachment): bool
|
|
{
|
|
$filename = $this->buildFilename($attachment);
|
|
return file_exists($filename);
|
|
}
|
|
|
|
public function buildFilename(Message $message, AttachmentInterface $attachment): string
|
|
{
|
|
$filename = implode(' - ', [
|
|
$message->getSubject(),
|
|
$message->getDateTime()->format('Y-m-d'),
|
|
$attachment->getFilename()
|
|
]);
|
|
return implode(DIRECTORY_SEPARATOR, [
|
|
$this->getAttachmentsFolder(),
|
|
$filename
|
|
]);
|
|
}
|
|
|
|
public function checkEncrypted(): array
|
|
{
|
|
$output = [];
|
|
foreach ($this->fetchAttachments() as $attachment) {
|
|
$output[$attachment->getFilename()] = $this->getDecrypt()->isEncrypted($attachment->getRealPath());
|
|
}
|
|
return $output;
|
|
}
|
|
|
|
public function fetchAttachments(): array
|
|
{
|
|
$files = new \FilesystemIterator($this->getAttachmentsFolder());
|
|
$attachments = [];
|
|
foreach ($files as $file) {
|
|
if ($file->isDir()) {
|
|
continue;
|
|
}
|
|
$attachments []= $file;
|
|
}
|
|
return $attachments;
|
|
}
|
|
public function fetchDecryptedAttachments(): array
|
|
{
|
|
$folder = implode(DIRECTORY_SEPARATOR, [$this->getAttachmentsFolder(), 'decrypted']);
|
|
$files = new \FilesystemIterator($folder);
|
|
$attachments = [];
|
|
foreach ($files as $file) {
|
|
if ($file->isDir()) {
|
|
continue;
|
|
}
|
|
$attachments []= $file;
|
|
}
|
|
return $attachments;
|
|
}
|
|
public function fetchFullAttachments(): array
|
|
{
|
|
$attachments = $this->fetchAttachments();
|
|
$output = [];
|
|
foreach ($attachments as $attachment) {
|
|
$att = [
|
|
'original_attachment' => $attachment,
|
|
'encrypted' => $this->getDecrypt()->isEncrypted($attachment->getRealPath())
|
|
];
|
|
if ($att['encrypted']) {
|
|
$att['decrypted_attachment'] = $this->getDecryptedAttachment($attachment);
|
|
}
|
|
$output []= $att;
|
|
}
|
|
return $output;
|
|
}
|
|
|
|
public function getAttachment(string $filename): \SplFileInfo
|
|
{
|
|
if (!str_contains($filename, $this->getAttachmentsFolder())) {
|
|
$filename = implode(DIRECTORY_SEPARATOR, [$this->getAttachmentsFolder(), $filename]);
|
|
}
|
|
return new \SplFileInfo($filename);
|
|
}
|
|
public function getDecryptedAttachment(\SplFileInfo $info): \SplFileInfo|bool
|
|
{
|
|
$new_file = implode(DIRECTORY_SEPARATOR, [$info->getPath(), 'decrypted', $info->getFilename()]);
|
|
if (!file_exists($new_file)) {
|
|
return false;
|
|
}
|
|
return new \SplFileInfo($new_file);
|
|
}
|
|
|
|
public function removeEncryption(string $filename): \SplFileInfo
|
|
{
|
|
$attachment = $this->getAttachment($filename);
|
|
$new_file = implode(DIRECTORY_SEPARATOR, [$attachment->getPath(), 'decrypted', $attachment->getFilename()]);
|
|
if ($this->getDecrypt()->runCommand($attachment->getRealPath(), $new_file)) {
|
|
return new \SplFileInfo($new_file);
|
|
}
|
|
return $attachment;
|
|
}
|
|
} |