Skip to content

Commit

Permalink
Email fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
EvolutionPHP committed Jan 14, 2021
1 parent 554b82d commit 1eecac1
Show file tree
Hide file tree
Showing 239 changed files with 54,029 additions and 135 deletions.
6 changes: 3 additions & 3 deletions hdz/app/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@
]);

#Imap
$routes->add('imap_fetcher', 'MailFetcher::imap',[
'as' => 'imap_fetcher'
]);
$routes->add('imap_fetcher', 'MailFetcher::imap');
#Pipe
$routes->add('mail_pipe', 'MailFetcher::pipe');

#Install
$routes->add('install', 'Install::home',[
Expand Down
61 changes: 17 additions & 44 deletions hdz/app/Controllers/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,50 +51,23 @@ private function checkSetup()
if ( !function_exists('mysqli_connect') ){
$error_msg[] = lang('Install.error.mysql');
}
$attachment_path = WRITEPATH.'attachments';
$logs_path = WRITEPATH.'logs';
$session_path = WRITEPATH.'session';
$upload_path = WRITEPATH.'uploads';
$site_upload_path = FCPATH.'upload';
if ( ! is_really_writable($attachment_path) )
{
$error_msg[] = lang_replace('Install.error.writable', [
'%folder%' => $attachment_path,
'[b]' => '<span class="font-weight-bold">',
'[/b]' => '</span>'
]);
}
if ( ! is_really_writable($logs_path) )
{
$error_msg[] = lang_replace('Install.error.writable', [
'%folder%' => $logs_path,
'[b]' => '<span class="font-weight-bold">',
'[/b]' => '</span>'
]);
}
if ( ! is_really_writable($session_path) )
{
$error_msg[] = lang_replace('Install.error.writable', [
'%folder%' => $session_path,
'[b]' => '<span class="font-weight-bold">',
'[/b]' => '</span>'
]);
}
if ( ! is_really_writable($upload_path) )
{
$error_msg[] = lang_replace('Install.error.writable', [
'%folder%' => $upload_path,
'[b]' => '<span class="font-weight-bold">',
'[/b]' => '</span>'
]);
}
if ( ! is_really_writable($site_upload_path) )
{
$error_msg[] = lang_replace('Install.error.writable', [
'%folder%' => $site_upload_path,
'[b]' => '<span class="font-weight-bold">',
'[/b]' => '</span>'
]);
$path_list = [
WRITEPATH.'attachments',
WRITEPATH.'logs',
WRITEPATH.'mails',
WRITEPATH.'session',
WRITEPATH.'uploads',
FCPATH.'upload',
FCPATH.'upload'.DIRECTORY_SEPARATOR.'thumbs'
];
foreach ($path_list as $path){
if(!is_really_writable($path)){
$error_msg[] = lang_replace('Install.error.writable', [
'%folder%' => $path,
'[b]' => '<span class="font-weight-bold">',
'[/b]' => '</span>'
]);
}
}
$db = Database::connect();
try {
Expand Down
84 changes: 7 additions & 77 deletions hdz/app/Controllers/MailFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,87 +8,17 @@

namespace App\Controllers;


use App\Libraries\Attachments;
use App\Libraries\Emails;
use App\Libraries\Tickets;
use CodeIgniter\Files\File;
use PhpImap\Exceptions\ConnectionException;
use PhpImap\Mailbox;

class MailFetcher extends BaseController
{
public function imap()
{
$config = \HTMLPurifier_Config::createDefault();
$html_purifier = new \HTMLPurifier($config);
$attach_dir = WRITEPATH.'attachments';
$emails = new Emails();
$tickets = new Tickets();
if($list = $emails->getFetcher()){
foreach ($list as $email)
{
$mailbox = new Mailbox(
'{'.$email->imap_host.':'.$email->imap_port.'/'.$email->incoming_type.'/ssl/novalidate-cert}INBOX', // IMAP server and mailbox folder
$email->imap_username, // Username for the before configured mailbox
$email->imap_password // Password for the before configured username
);
try{
$mailsIds = $mailbox->searchMailbox('ALL');
}catch (ConnectionException $ex){
log_message('error','IMAP connection failed: '.$ex);
return;
}
if(!$mailsIds){
return;
}

$mailbox->setAttachmentsDir($attach_dir);
foreach ($mailsIds as $k => $v){
$mail = $mailbox->getMail($mailsIds[$k]);
$message = ($mail->textHtml) ? $html_purifier->purify($mail->textHtml) : $mail->textPlain;
$client_id = $this->client->getClientID($mail->fromName, $mail->fromAddress);
if(!$ticket = $tickets->getTicketFromEmail($client_id, $mail->subject)){
$ticket_id = $tickets->createTicket(
$client_id,
$mail->subject,
$email->department_id
);
$message_id = $tickets->addMessage($ticket_id, $message);
$ticket = $tickets->getTicket(['id' => $ticket_id]);
}else{
$ticket_id = $ticket->id;
$message_id = $tickets->addMessage($ticket_id, $message);
$tickets->updateTicketReply($ticket_id, $ticket->status);
}
$tickets->staffNotification($ticket);
//Attachments
$attachments = new Attachments();
if(!empty($mail->getAttachments())){
foreach ($mail->getAttachments() as $file){
if(file_exists($file->filePath)){
$mailFetcherLib = new \App\Libraries\MailFetcher();
return $mailFetcherLib->parse_imap();
}

$fileInfo = new File($file->filePath);
$size = $fileInfo->getSize();
$file_type = $fileInfo->getMimeType();
$filename = $fileInfo->getRandomName();
$fileInfo->move($attach_dir, $filename);
$original_name = $file->name;
$attachments->addFromTicket(
$ticket_id,
$message_id,
$original_name,
$filename,
$size,
$file_type
);
}
}
}
$mailbox->deleteMail($mail->id);
}
$mailbox->disconnect();
}
}
public function pipe()
{
$mailFetcherLib = new \App\Libraries\MailFetcher();
return $mailFetcherLib->parse_pipe();
}
}
5 changes: 5 additions & 0 deletions hdz/app/Helpers/helpdesk_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,9 @@ function parseCustomFieldsForm($customField)
{
$customFields = new \App\Libraries\CustomFields();
return $customFields->parseForm($customField);
}

function mail_piping()
{
return \Config\Services::mailFetcher()->pipe();
}
17 changes: 8 additions & 9 deletions hdz/app/Libraries/Emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ public function getFetcher()

public function getDefault()
{
$q = $this->emailModel->where('default', 1)
->get(1);
if($q->resultID->num_rows == 0){
return null;
}
return $q->getRow();
return $this->getRow(['default' => 1]);
}

public function getByID($id)
Expand All @@ -63,16 +58,20 @@ public function getByID($id)
return null;
}

public function getByDepartment($id)
public function getRow($where=array())
{
$q = $this->emailModel->where('department_id', $id)
->get(1);
$q = $this->emailModel->where($where)->get(1);
if($q->resultID->num_rows == 0){
return null;
}
return $q->getRow();
}

public function getByDepartment($id)
{
return $this->getRow(['department_id' => $id]);
}

public function set_default($id)
{
$count = $this->emailModel->where('id', $id)
Expand Down
Loading

0 comments on commit 1eecac1

Please sign in to comment.