Thanasoft-H2F/calendar/lib/Controller/EmailBoxController.php
2025-03-28 17:59:51 +03:00

218 lines
5.6 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\Calendar\Controller;
use OCP\IRequest;
use Horde_Imap_Client;
use OCA\Calendar\Db\MailRepository;
use OCP\IURLGenerator;
use OCP\AppFramework\Http;
use OCP\AppFramework\Controller;
use OCP\Files\IMimeTypeDetector;
use OCA\Mail\Contracts\IMailSearch;
use OCP\AppFramework\Http\Response;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\IMAP\IMAPClientFactory;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\Sync\SyncService;
use OCP\AppFramework\Http\JSONResponse;
use OCA\Mail\Contracts\IUserPreferences;
use OCP\AppFramework\Db\DoesNotExistException;
class EmailBoxController extends Controller {
private ?string $currentUserId;
private IMailManager $mailManager;
private IUserPreferences $preferences;
private IMailSearch $mailSearch;
private AccountService $accountService;
private IURLGenerator $urlGenerator;
private IMimeTypeDetector $mimeTypeDetector;
private IMAPClientFactory $clientFactory;
private SyncService $syncService;
private MailRepository $mailRepository;
public function __construct(string $appName,
IRequest $request,
?string $UserId,
IMailManager $mailManager,
IUserPreferences $preferences,
IMailSearch $mailSearch,
AccountService $accountService,
IURLGenerator $urlGenerator,
IMimeTypeDetector $mimeTypeDetector,
IMAPClientFactory $clientFactory,
SyncService $syncService,
MailRepository $mailRepository,
) {
parent::__construct($appName, $request);
$this->currentUserId = $UserId;
$this->mailManager = $mailManager;
$this->preferences = $preferences;
$this->mailSearch = $mailSearch;
$this->accountService = $accountService;
$this->urlGenerator = $urlGenerator;
$this->mimeTypeDetector = $mimeTypeDetector;
$this->clientFactory = $clientFactory;
$this->syncService = $syncService;
$this->mailRepository = $mailRepository;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
*/
public function loadEmailboxMessage(): JSONResponse {
$mailboxId = $this->mailRepository->getCurrentUserMailBoxId($this->currentUserId);
if($mailboxId == null){
return new JSONResponse([
"success" => false,
"message" => "MAILBOX_NOT_FOUND",
], Http::STATUS_OK);
}
$lastMessageTimestamp = null;
$init = true;
$sortOrder = 'newest';
$query = null;
try {
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $mailboxId);
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
// try {
// if (boolval($this->request->getParam('withSync'))) {
// $this->syncService->syncMailbox(
// $account,
// $mailbox,
// Horde_Imap_Client::SYNC_NEWMSGSUIDS | Horde_Imap_Client::SYNC_FLAGSUIDS | Horde_Imap_Client::SYNC_VANISHEDUIDS,
// null,
// $lastMessageTimestamp,
// $init,
// $sortOrder,
// $query
// );
// }
// } catch (\Throwable $th) {
// // on ignioir car il n'est pas est deja synchronise
// }
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$order = $this->preferences->getPreference($this->currentUserId, 'sort-order', 'newest') === 'newest' ? 'DESC': 'ASC';
return new JSONResponse(
$this->mailSearch->findMessages(
$account,
$mailbox,
$order,
null,
null,
400,
)
);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
*/
public function loadEmailboxAttachement(int $messageId = 0): Response {
$data = [];
try {
$message = $this->mailManager->getMessage($this->currentUserId, $messageId);
$mailbox = $this->mailManager->getMailbox($this->currentUserId, $message->getMailboxId());
$account = $this->accountService->find($this->currentUserId, $mailbox->getAccountId());
// $data["messageObject"] = $message ?? null;
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}
$client = $this->clientFactory->getClient($account);
try {
$imapMessage = $this->mailManager->getImapMessage(
$client,
$account,
$mailbox,
$message->getUid(), true
);
$json = $imapMessage->getFullMessage($messageId);
} finally {
$client->logout();
}
$data['fullMessage'] = $json;
$data['attachments'] = array_map(function ($a) use ($messageId) {
return $this->enrichDownloadUrl(
$messageId,
$a
);
}, $json['attachments']);
if(isset( $json['attachments'])) {
unset($json['attachments']);
}
return new JSONResponse(($data));
}
/**
* @param int $id
* @param array $attachment
*
* @return array
*/
private function enrichDownloadUrl(int $id,
array $attachment) {
$downloadUrl = $this->urlGenerator->linkToRoute('mail.messages.downloadAttachment',
[
'id' => $id,
'attachmentId' => $attachment['id'],
]);
$downloadUrl = $this->urlGenerator->getAbsoluteURL($downloadUrl);
$attachment['downloadUrl'] = $downloadUrl;
$attachment['mimeUrl'] = $this->mimeTypeDetector->mimeTypeIcon($attachment['mime']);
$attachment['isImage'] = $this->attachmentIsImage($attachment);
$attachment['isCalendarEvent'] = $this->attachmentIsCalendarEvent($attachment);
return $attachment;
}
/**
* Determines if the content of this attachment is an image
*
* @param array $attachment
*
* @return boolean
*/
private function attachmentIsImage(array $attachment): bool {
return in_array(
$attachment['mime'], [
'image/jpeg',
'image/png',
'image/gif'
]);
}
/**
* @param array $attachment
*
* @return boolean
*/
private function attachmentIsCalendarEvent(array $attachment): bool {
return in_array($attachment['mime'], ['text/calendar', 'application/ics'], true);
}
}