opensupports/server/controllers/system/email-polling.php

153 lines
5.0 KiB
PHP
Raw Normal View History

2018-12-24 01:44:59 +01:00
<?php
2019-01-19 00:58:30 +01:00
use Respect\Validation\Validator as DataValidator;
2018-12-24 01:44:59 +01:00
2019-01-19 00:58:30 +01:00
class EmailPollingController extends Controller {
2018-12-24 01:44:59 +01:00
const PATH = '/email-polling';
const METHOD = 'POST';
2019-01-12 08:49:25 +01:00
private $mailbox;
2018-12-24 01:44:59 +01:00
public function validations() {
return [
'permission' => 'any',
2019-01-19 00:58:30 +01:00
'requestData' => [
'token' => [
'validation' => DataValidator::notBlank()->length(LengthConfig::MIN_LENGTH_TOKEN, LengthConfig::MAX_LENGTH_TOKEN),
2019-01-19 00:58:30 +01:00
'error' => ERRORS::INVALID_TOKEN
]
]
2018-12-24 01:44:59 +01:00
];
}
public function handler() {
$commentController = new CommentController();
$createController = new CreateController();
$defaultLanguage = Setting::getSetting('language')->getValue();
$defaultDepartmentId = Department::getAll()->first()->id;
2019-01-19 00:58:30 +01:00
if(Controller::request('token') !== Setting::getSetting('imap-token')->getValue())
throw new RequestException(ERRORS::INVALID_TOKEN);
2018-12-24 01:44:59 +01:00
2019-01-12 08:49:25 +01:00
$this->mailbox = new \PhpImap\Mailbox(
Setting::getSetting('imap-host')->getValue(),
Setting::getSetting('imap-user')->getValue(),
Setting::getSetting('imap-pass')->getValue(),
2019-01-19 00:58:30 +01:00
'files/'
2019-01-12 08:49:25 +01:00
);
2018-12-24 01:44:59 +01:00
$errors = [];
$emails = $this->getLastEmails();
2018-12-24 01:44:59 +01:00
$session = Session::getInstance();
$oldSession = [
'userId' => $session->getUserId(),
'staff' => $session->getToken(),
'token' => $session->isStaffLogged(),
];
foreach($emails as $email) {
Controller::setDataRequester(function ($key) use ($email, $defaultDepartmentId, $defaultLanguage) {
switch ($key) {
case 'ticketNumber':
return $email->getTicketNumber();
case 'title':
return $email->getSubject();
case 'content':
return $email->getContent();
case 'departmentId':
return $defaultDepartmentId;
case 'language':
return $defaultLanguage;
case 'email':
return $email->getSender();
case 'name':
return $email->getSenderName();
}
return null;
});
/*
if($email->getAttachment()) {
$attachment = $email->getAttachment();
2019-01-19 00:58:30 +01:00
$_FILES['file'] = [
'name' => $attachment->name,
'type' => mime_content_type($attachment->filePath),
'tmp_name' => $attachment->filePath,
'error' => UPLOAD_ERR_OK,
'size' => filesize($attachment->filePath),
];
}
*/
2019-01-19 00:58:30 +01:00
2018-12-24 01:44:59 +01:00
try {
if($email->isReply()) {
if($email->getTicket()->authorToArray()['email'] === $email->getSender()) {
$session->clearSessionData();
$session->createTicketSession($email->getTicket()->ticketNumber);
$commentController->handler();
}
} else {
$createController->handler();
}
} catch(\Exception $e) {
$errors[] = [
'author' => $email->getSender(),
'ticketNumber' => $email->getTicketNumber(),
'error' => $e->__toString(),
];
}
2019-01-19 00:58:30 +01:00
unset($_FILES['file']);
2018-12-24 01:44:59 +01:00
}
$session->clearSessionData();
$session->setSessionData($oldSession);
if(count($errors)) {
Response::respondError(ERRORS::EMAIL_POLLING, null, $errors);
} else {
2021-01-06 01:47:33 +01:00
$this->eraseAllEmails();
2018-12-24 01:44:59 +01:00
Response::respondSuccess();
}
2018-12-24 01:44:59 +01:00
}
public function getLastEmails() {
2019-01-12 08:49:25 +01:00
$mailsIds = $this->mailbox->searchMailbox('ALL');
2018-12-24 01:44:59 +01:00
$emails = [];
sort($mailsIds);
2019-01-12 08:49:25 +01:00
2018-12-24 01:44:59 +01:00
foreach($mailsIds as $mailId) {
2019-01-12 08:49:25 +01:00
$mail = $this->mailbox->getMail($mailId);
$mailHeader = $this->mailbox->getMailHeader($mailId);
// $mailAttachment = count($mail->getAttachments()) ? current($mail->getAttachments()) : null;
2019-01-19 00:58:30 +01:00
2018-12-24 01:44:59 +01:00
$emails[] = new Email([
'fromAddress' => $mailHeader->fromAddress,
'fromName' => $mailHeader->fromName,
'subject' => $mailHeader->subject,
'content' => $mail->textPlain,
'file' => null,
2018-12-24 01:44:59 +01:00
]);
foreach($mail->getAttachments() as $attachment) {
unlink($attachment->filePath);
}
2018-12-24 01:44:59 +01:00
}
return $emails;
}
public function eraseAllEmails() {
2019-01-12 08:49:25 +01:00
$mailsIds = $this->mailbox->searchMailbox('ALL');
foreach($mailsIds as $mailId) {
$this->mailbox->deleteMail($mailId);
}
2018-12-24 01:44:59 +01:00
2019-01-12 08:49:25 +01:00
$this->mailbox->expungeDeletedMails();
2018-12-24 01:44:59 +01:00
}
}