update 1.0.8.0
Commits for version update
This commit is contained in:
@@ -2,9 +2,8 @@
|
||||
|
||||
namespace App\Http\Controllers\Agent\helpdesk;
|
||||
|
||||
// controllers
|
||||
use App;
|
||||
// models
|
||||
use App\Http\Controllers\Admin\MailFetch as Fetch;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Model\helpdesk\Email\Emails;
|
||||
use App\Model\helpdesk\Manage\Help_topic;
|
||||
@@ -14,13 +13,8 @@ use App\Model\helpdesk\Settings\Ticket;
|
||||
use App\Model\helpdesk\Ticket\Ticket_attachments;
|
||||
use App\Model\helpdesk\Ticket\Ticket_source;
|
||||
use App\Model\helpdesk\Ticket\Ticket_Thread;
|
||||
use App\Model\helpdesk\Ticket\Tickets;
|
||||
// classes
|
||||
use App\Model\helpdesk\Utility\MailboxProtocol;
|
||||
use Crypt;
|
||||
use File;
|
||||
use ForceUTF8\Encoding;
|
||||
use PhpImap\Mailbox as ImapMailbox;
|
||||
use App\Model\helpdesk\Ticket\Tickets;
|
||||
|
||||
/**
|
||||
* MailController.
|
||||
@@ -48,200 +42,13 @@ class MailController extends Controller
|
||||
*/
|
||||
public function readmails(Emails $emails, Email $settings_email, System $system, Ticket $ticket)
|
||||
{
|
||||
// $path_url = $system->first()->url;
|
||||
//dd($emails);
|
||||
if ($settings_email->first()->email_fetching == 1) {
|
||||
if ($settings_email->first()->all_emails == 1) {
|
||||
// $helptopic = $this->TicketController->default_helptopic();
|
||||
// $sla = $this->TicketController->default_sla();
|
||||
$email = $emails->get();
|
||||
foreach ($email as $e_mail) {
|
||||
if ($e_mail->fetching_status == 1) {
|
||||
$auto_response = $e_mail->auto_response;
|
||||
$priority = $e_mail->priority;
|
||||
$dept = $e_mail->department;
|
||||
$helptopic = $e_mail->help_topic;
|
||||
if ($priority == null) {
|
||||
$priority = $ticket->first()->priority;
|
||||
}
|
||||
if ($dept == null) {
|
||||
$dept = $system->first()->department;
|
||||
}
|
||||
if ($helptopic == null) {
|
||||
$helptopic = $ticket->first()->help_topic;
|
||||
}
|
||||
$get_helptopic = Help_topic::where('id', '=', $helptopic)->first();
|
||||
$sla = $get_helptopic->sla_plan;
|
||||
$host = $e_mail->fetching_host;
|
||||
$port = $e_mail->fetching_port;
|
||||
if ($e_mail->mailbox_protocol) {
|
||||
$protocol_value = $e_mail->mailbox_protocol;
|
||||
$get_mailboxprotocol = MailboxProtocol::where('id', '=', $protocol_value)->first();
|
||||
$protocol = $get_mailboxprotocol->value;
|
||||
} elseif ($e_mail->fetching_encryption == '/none') {
|
||||
$fetching_encryption2 = '/novalidate-cert';
|
||||
$protocol = $fetching_encryption2;
|
||||
} else {
|
||||
if ($e_mail->fetching_protocol) {
|
||||
$fetching_protocol = '/'.$e_mail->fetching_protocol;
|
||||
} else {
|
||||
$fetching_protocol = '';
|
||||
}
|
||||
if ($e_mail->fetching_encryption) {
|
||||
$fetching_encryption = $e_mail->fetching_encryption;
|
||||
} else {
|
||||
$fetching_encryption = '';
|
||||
}
|
||||
$protocol = $fetching_protocol.$fetching_encryption;
|
||||
}
|
||||
$imap_config = '{'.$host.':'.$port.$protocol.'}INBOX';
|
||||
$password = Crypt::decrypt($e_mail->password);
|
||||
try {
|
||||
$mailbox = new ImapMailbox($imap_config, $e_mail->email_address, $password, __DIR__);
|
||||
} catch (\PhpImap\Exception $e) {
|
||||
echo 'Connection error';
|
||||
}
|
||||
$mails = [];
|
||||
try {
|
||||
$mailsIds = $mailbox->searchMailBox('SINCE '.date('d-M-Y', strtotime('-1 day')));
|
||||
} catch (\PhpImap\Exception $e) {
|
||||
echo 'Connection error';
|
||||
}
|
||||
if (!$mailsIds) {
|
||||
die('Mailbox is empty');
|
||||
}
|
||||
foreach ($mailsIds as $mailId) {
|
||||
try {
|
||||
$overview = $mailbox->get_overview($mailId);
|
||||
} catch (Exception $e) {
|
||||
return \Lang::get('lang.unable_to_fetch_emails');
|
||||
}
|
||||
$var = $overview[0]->seen ? 'read' : 'unread';
|
||||
if ($var == 'unread') {
|
||||
$mail = $mailbox->getMail($mailId);
|
||||
try {
|
||||
$mail = $mailbox->getMail($mailId);
|
||||
} catch (\PhpImap\Exception $e) {
|
||||
echo 'Connection error';
|
||||
}
|
||||
if ($settings_email->first()->email_collaborator == 1) {
|
||||
$collaborator = $mail->cc;
|
||||
} else {
|
||||
$collaborator = null;
|
||||
}
|
||||
$body = $mail->textHtml;
|
||||
if ($body != null) {
|
||||
$body = self::trimTableTag($body);
|
||||
}
|
||||
// if mail body has no messages fetch backup mail
|
||||
if ($body == null) {
|
||||
$body = $mail->textPlain;
|
||||
}
|
||||
if ($body == null) {
|
||||
$attach = $mail->getAttachments();
|
||||
if (is_array($attach)) {
|
||||
if (array_key_exists('html-body', $attach)) {
|
||||
$path = $attach['html-body']->filePath;
|
||||
}
|
||||
if ($path == null) {
|
||||
if (array_key_exists('text-body', $attach)) {
|
||||
$path = $attach['text-body']->filePath;
|
||||
}
|
||||
}
|
||||
if ($path) {
|
||||
$body = file_get_contents($path);
|
||||
}
|
||||
if ($body) {
|
||||
$body = self::trimTableTag($body);
|
||||
} else {
|
||||
$body = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
// if ($body == null) {
|
||||
// $body = $mailbox->backup_getmail($mailId);
|
||||
// $body = str_replace('\r\n', '<br/>', $body);
|
||||
// }
|
||||
$date = $mail->date;
|
||||
$datetime = $overview[0]->date;
|
||||
$date_time = explode(' ', $datetime);
|
||||
$date = $date_time[1].'-'.$date_time[2].'-'.$date_time[3].' '.$date_time[4];
|
||||
$date = date('Y-m-d H:i:s', strtotime($date));
|
||||
if (isset($mail->subject)) {
|
||||
$subject = $mail->subject;
|
||||
} else {
|
||||
$subject = 'No Subject';
|
||||
}
|
||||
|
||||
$to = $mail->to;
|
||||
$fromname = $mail->fromName;
|
||||
$fromaddress = $mail->fromAddress;
|
||||
$ticket_source = Ticket_source::where('name', '=', 'email')->first();
|
||||
$source = $ticket_source->id;
|
||||
$phone = '';
|
||||
$phonecode = '';
|
||||
$mobile_number = '';
|
||||
$assign = $get_helptopic->auto_assign;
|
||||
$form_data = null;
|
||||
$team_assign = null;
|
||||
$ticket_status = null;
|
||||
$result = $this->TicketWorkflowController->workflow($fromaddress, $fromname, $to, $subject, $body, $phone, $phonecode, $mobile_number, $helptopic, $sla, $priority, $source, $collaborator, $dept, $assign, $team_assign, $ticket_status, $form_data, $auto_response, $mail->getAttachments());
|
||||
|
||||
if ($result[1] == true) {
|
||||
$ticket_table = Tickets::where('ticket_number', '=', $result[0])->first();
|
||||
$thread_id = Ticket_Thread::where('ticket_id', '=', $ticket_table->id)->max('id');
|
||||
|
||||
$thread_id = $thread_id;
|
||||
foreach ($mail->getAttachments() as $attachment) {
|
||||
$support = 'support';
|
||||
|
||||
$dir_img_paths = __DIR__;
|
||||
$dir_img_path = explode('/code', $dir_img_paths);
|
||||
|
||||
$filepath = explode('..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'public', $attachment->filePath);
|
||||
|
||||
if ($filepath[1]) {
|
||||
$path = public_path().$filepath[1];
|
||||
|
||||
$filesize = filesize($path);
|
||||
$file_data = file_get_contents($path);
|
||||
$ext = pathinfo($attachment->filePath, PATHINFO_EXTENSION);
|
||||
$imageid = $attachment->id;
|
||||
$string = str_replace('-', '', $attachment->name);
|
||||
$filename = explode('src', $attachment->filePath);
|
||||
$filename = str_replace('\\', '', $filename);
|
||||
$body = str_replace('cid:'.$imageid, $filepath[1], $body);
|
||||
$pos = strpos($body, $filepath[1]);
|
||||
if ($pos == false) {
|
||||
if ($settings_email->first()->attachment == 1) {
|
||||
$upload = new Ticket_attachments();
|
||||
$upload->file = $file_data;
|
||||
$upload->thread_id = $thread_id;
|
||||
$upload->name = $filepath[1];
|
||||
$upload->type = $ext;
|
||||
$upload->size = $filesize;
|
||||
$upload->poster = 'ATTACHMENT';
|
||||
$upload->save();
|
||||
}
|
||||
} else {
|
||||
$upload = new Ticket_attachments();
|
||||
$upload->file = $file_data;
|
||||
$upload->thread_id = $thread_id;
|
||||
$upload->name = $filepath[1];
|
||||
$upload->type = $ext;
|
||||
$upload->size = $filesize;
|
||||
$upload->poster = 'INLINE';
|
||||
$upload->save();
|
||||
}
|
||||
unlink($path);
|
||||
}
|
||||
}
|
||||
$body = $body;
|
||||
$thread = Ticket_Thread::where('id', '=', $thread_id)->first();
|
||||
$thread->body = $this->separate_reply($body);
|
||||
$thread->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($email->count() > 0) {
|
||||
foreach ($email as $e_mail) {
|
||||
$this->fetch($e_mail);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -264,40 +71,263 @@ class MailController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode Imap text.
|
||||
* @param object $email
|
||||
*
|
||||
* @param type $str
|
||||
*
|
||||
* @return type string
|
||||
* @return int
|
||||
*/
|
||||
public function decode_imap_text($str)
|
||||
public function priority($email)
|
||||
{
|
||||
$result = '';
|
||||
$decode_header = imap_mime_header_decode($str);
|
||||
foreach ($decode_header as $obj) {
|
||||
$result .= htmlspecialchars(rtrim($obj->text, "\t"));
|
||||
$priority = $email->priority;
|
||||
if (!$priority) {
|
||||
$priority = $this->ticketController()->getSystemDefaultPriority();
|
||||
}
|
||||
|
||||
return $result;
|
||||
return $priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* fetch_attachments.
|
||||
* get department.
|
||||
*
|
||||
* @return type
|
||||
* @param object $email
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function fetch_attachments()
|
||||
public function department($email)
|
||||
{
|
||||
$uploads = Upload::all();
|
||||
foreach ($uploads as $attachment) {
|
||||
$image = @imagecreatefromstring($attachment->file);
|
||||
ob_start();
|
||||
imagejpeg($image, null, 80);
|
||||
$data = ob_get_contents();
|
||||
ob_end_clean();
|
||||
$var = '<a href="" target="_blank"><img src="data:image/jpg;base64,'.base64_encode($data).'"/></a>';
|
||||
echo '<br/><span class="mailbox-attachment-icon has-img">'.$var.'</span>';
|
||||
$department = $email->department;
|
||||
if (!$department) {
|
||||
$department = $this->ticketController()->getSystemDefaultDepartment();
|
||||
}
|
||||
|
||||
return $department;
|
||||
}
|
||||
|
||||
/**
|
||||
* get help topic.
|
||||
*
|
||||
* @param object $email
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function helptopic($email)
|
||||
{
|
||||
//dd($email);
|
||||
$helptopic = $email->help_topic;
|
||||
if (!$helptopic) {
|
||||
$helptopic = $this->ticketController()->getSystemDefaultHelpTopic();
|
||||
}
|
||||
|
||||
return $helptopic;
|
||||
}
|
||||
|
||||
/**
|
||||
* get sla.
|
||||
*
|
||||
* @param object $email
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function sla($email)
|
||||
{
|
||||
$helptopic = $this->helptopic($email);
|
||||
$help = Help_topic::where('id', '=', $helptopic)->first();
|
||||
if ($help) {
|
||||
$sla = $help->sla_plan;
|
||||
}
|
||||
if (!$sla) {
|
||||
$sla = $this->ticketController()->getSystemDefaultSla();
|
||||
}
|
||||
|
||||
return $sla;
|
||||
}
|
||||
|
||||
/**
|
||||
* get ticket controller.
|
||||
*
|
||||
* @return \App\Http\Controllers\Agent\helpdesk\TicketController
|
||||
*/
|
||||
public function ticketController()
|
||||
{
|
||||
$PhpMailController = new \App\Http\Controllers\Common\PhpMailController();
|
||||
$NotificationController = new \App\Http\Controllers\Common\NotificationController();
|
||||
$controller = new TicketController($PhpMailController, $NotificationController);
|
||||
|
||||
return $controller;
|
||||
}
|
||||
|
||||
public function fetch($email)
|
||||
{
|
||||
// dd($email);
|
||||
if ($email) {
|
||||
$username = $email->email_address;
|
||||
$password = $email->password;
|
||||
$service = $email->fetching_protocol;
|
||||
$host = $email->fetching_host;
|
||||
$port = $email->fetching_port;
|
||||
$encryption = $email->fetching_encryption;
|
||||
$cert = $email->mailbox_protocol;
|
||||
$server = new Fetch($host, $port, $service);
|
||||
if ($encryption != null || $encryption != '') {
|
||||
$server->setFlag($encryption);
|
||||
}
|
||||
$server->setFlag($cert);
|
||||
$server->setAuthentication($username, $password);
|
||||
$messages = $server->search('UNSEEN', 10);
|
||||
$this->message($messages, $email);
|
||||
}
|
||||
}
|
||||
|
||||
public function message($messages, $email)
|
||||
{
|
||||
foreach ($messages as $message) {
|
||||
$this->getMessageContent($message, $email);
|
||||
}
|
||||
}
|
||||
|
||||
public function getMessageContent($message, $email)
|
||||
{
|
||||
$body = $message->getMessageBody(true);
|
||||
if (!$body) {
|
||||
$body = $message->getMessageBody();
|
||||
}
|
||||
$subject = $message->getSubject();
|
||||
$address = $message->getAddresses('reply-to');
|
||||
if (!$address) {
|
||||
$address = $message->getAddresses('from');
|
||||
}
|
||||
$collaborators = $this->collaburators($message, $email);
|
||||
$attachments = $message->getAttachments();
|
||||
//dd(['body' => $body, 'subject' => $subject, 'address' => $address, 'cc' => $collaborator, 'attachments' => $attachments]);
|
||||
$this->workflow($address, $subject, $body, $collaborators, $attachments, $email);
|
||||
}
|
||||
|
||||
public function workflow($address, $subject, $body, $collaborator, $attachments, $email)
|
||||
{
|
||||
$fromaddress = checkArray('address', $address[0]);
|
||||
$fromname = checkArray('name', $address[0]);
|
||||
$helptopic = $this->helptopic($email);
|
||||
$sla = $this->sla($email);
|
||||
$priority = $this->priority($email);
|
||||
$ticket_source = Ticket_source::where('name', '=', 'email')->first();
|
||||
$source = $ticket_source->id;
|
||||
$dept = $this->department($email);
|
||||
$get_helptopic = Help_topic::where('id', '=', $helptopic)->first();
|
||||
$assign = $get_helptopic->auto_assign;
|
||||
$form_data = null;
|
||||
$team_assign = null;
|
||||
$ticket_status = null;
|
||||
$auto_response = $email->auto_response;
|
||||
$result = $this->TicketWorkflowController->workflow($fromaddress, $fromname, $subject, $body, $phone = '', $phonecode = '', $mobile_number = '', $helptopic, $sla, $priority, $source, $collaborator, $dept, $assign, $team_assign, $ticket_status, $form_data = [], $auto_response);
|
||||
if ($result[1] == true) {
|
||||
$this->updateThread($result[0], $body, $attachments);
|
||||
}
|
||||
}
|
||||
|
||||
public function updateThread($ticket_number, $body, $attachments)
|
||||
{
|
||||
$ticket_table = Tickets::where('ticket_number', '=', $ticket_number)->first();
|
||||
$thread_id = Ticket_Thread::where('ticket_id', '=', $ticket_table->id)->max('id');
|
||||
$thread = Ticket_Thread::where('id', '=', $thread_id)->first();
|
||||
$thread->body = $this->separate_reply($body);
|
||||
$thread->save();
|
||||
$this->saveAttachments($thread->id, $attachments);
|
||||
\Log::info('Ticket has created : ', ['id' => $thread->ticket_id]);
|
||||
}
|
||||
|
||||
public function saveAttachments($thread_id, $attachments = [])
|
||||
{
|
||||
if (is_array($attachments) && count($attachments) > 0) {
|
||||
foreach ($attachments as $attachment) {
|
||||
$structure = $attachment->getStructure();
|
||||
$disposition = 'ATTACHMENT';
|
||||
if (isset($structure->disposition)) {
|
||||
$disposition = $structure->disposition;
|
||||
}
|
||||
|
||||
$filename = str_random(16).'-'.$attachment->getFileName();
|
||||
$type = $attachment->getMimeType();
|
||||
$size = $attachment->getSize();
|
||||
$data = $attachment->getData();
|
||||
$this->manageAttachment($data, $filename, $type, $size, $disposition, $thread_id);
|
||||
$this->updateBody($attachment, $thread_id, $filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function manageAttachment($data, $filename, $type, $size, $disposition, $thread_id)
|
||||
{
|
||||
$upload = new Ticket_attachments();
|
||||
$upload->file = $data;
|
||||
$upload->thread_id = $thread_id;
|
||||
$upload->name = $filename;
|
||||
$upload->type = $type;
|
||||
$upload->size = $size;
|
||||
$upload->poster = $disposition;
|
||||
if ($data && $size && $disposition) {
|
||||
$upload->save();
|
||||
}
|
||||
}
|
||||
|
||||
public function updateBody($attachment, $thread_id, $filename)
|
||||
{
|
||||
$structure = $attachment->getStructure();
|
||||
$disposition = 'ATTACHMENT';
|
||||
if (isset($structure->disposition)) {
|
||||
$disposition = $structure->disposition;
|
||||
}
|
||||
if ($disposition == 'INLINE') {
|
||||
$id = str_replace('>', '', str_replace('<', '', $structure->id));
|
||||
//$filename = $attachment->getFileName();
|
||||
$path = public_path('attachments');
|
||||
$filepath = asset('attachments/'.$filename);
|
||||
$threads = new Ticket_Thread();
|
||||
$thread = $threads->find($thread_id);
|
||||
$body = $thread->body;
|
||||
$body = str_replace('cid:'.$id, $filename, $body);
|
||||
$thread->body = $body;
|
||||
$thread->save();
|
||||
$attachment->saveToDirectory($path);
|
||||
}
|
||||
}
|
||||
|
||||
public function collaburators($message, $email)
|
||||
{
|
||||
$this_address = $email->email_address;
|
||||
$collaborator_cc = $message->getAddresses('cc');
|
||||
//dd($collaborator_cc);
|
||||
$collaborator_bcc = $message->getAddresses('bcc');
|
||||
$collaborator_to = $message->getAddresses('to');
|
||||
$cc_array = [];
|
||||
$bcc_array = [];
|
||||
$to_array = [];
|
||||
if ($collaborator_cc) {
|
||||
foreach ($collaborator_cc as $cc) {
|
||||
$name = checkArray('name', $cc);
|
||||
$address = checkArray('address', $cc);
|
||||
$cc_array[$address] = $name;
|
||||
}
|
||||
}
|
||||
if ($collaborator_bcc) {
|
||||
foreach ($collaborator_bcc as $bcc) {
|
||||
$name = checkArray('name', $bcc);
|
||||
$address = checkArray('address', $bcc);
|
||||
$bcc_array[$address] = $name;
|
||||
}
|
||||
}
|
||||
if ($collaborator_to) {
|
||||
foreach ($collaborator_to as $to) {
|
||||
$name = checkArray('name', $to);
|
||||
$address = checkArray('address', $to);
|
||||
$to_array[$address] = $name;
|
||||
}
|
||||
}
|
||||
$array = array_merge($bcc_array, $cc_array);
|
||||
$array = array_merge($array, $to_array);
|
||||
if (array_key_exists($this_address, $array)) {
|
||||
unset($array[$this_address]);
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -309,47 +339,22 @@ class MailController extends Controller
|
||||
*/
|
||||
public function get_data($id)
|
||||
{
|
||||
$attachments = App\Model\helpdesk\Ticket\Ticket_attachments::where('id', '=', $id)->get();
|
||||
$attachments = \App\Model\helpdesk\Ticket\Ticket_attachments::where('id', '=', $id)->get();
|
||||
foreach ($attachments as $attachment) {
|
||||
header('Content-type: application/'.$attachment->type.'');
|
||||
header('Content-Disposition: inline; filename='.$attachment->name.'');
|
||||
header('Content-Transfer-Encoding: binary');
|
||||
echo $attachment->file;
|
||||
$headers = [
|
||||
'Content-type: application/'.$attachment->type.'',
|
||||
'Content-Disposition: inline; filename='.$attachment->name.'',
|
||||
'Content-Transfer-Encoding: binary',
|
||||
];
|
||||
$file = $attachment->file;
|
||||
echo $file;
|
||||
// return response($file)
|
||||
// ->header('Content-Type', $attachment->type)
|
||||
// ->header('Content-Disposition', 'inline; filename='.$attachment->name.'')
|
||||
// ->header('Content-Transfer-Encoding', 'binary');
|
||||
}
|
||||
}
|
||||
|
||||
public static function trimTableTag($html)
|
||||
{
|
||||
if (strpos('<table>', $html) != false) {
|
||||
$first_pos = strpos($html, '<table');
|
||||
$fist_string = substr_replace($html, '', 0, $first_pos);
|
||||
$last_pos = strrpos($fist_string, '</table>', -1);
|
||||
$total = strlen($fist_string);
|
||||
$diff = $total - $last_pos;
|
||||
$str = substr_replace($fist_string, '', $last_pos, -1);
|
||||
$final_str = str_finish($str, '</table>');
|
||||
|
||||
return $final_str;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
public static function trim3D($html)
|
||||
{
|
||||
$body = str_replace('=3D', '', $html);
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
public static function trimInjections($html, $tags = ['<script>', '</script>', '<style>', '</style>', '<?php', '?>'])
|
||||
{
|
||||
$replace = [];
|
||||
foreach ($tags as $key => $tag) {
|
||||
$replace[$key] = htmlspecialchars($tag);
|
||||
}
|
||||
$body = str_replace($tags, $replace, $html);
|
||||
|
||||
return $body;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user