Files
zemailnator/app/Livewire/Frontend/App.php

110 lines
3.5 KiB
PHP

<?php
namespace App\Livewire\Frontend;
use App\Models\Message;
use App\Models\ZEmail;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
class App extends Component
{
public $messages = [];
public $deleted = [];
public $error = '';
public $email;
public $initial;
public $overflow = false;
protected $listeners = ['fetchMessages' => 'fetch', 'syncEmail'];
public function mount()
{
$this->email = ZEmail::getEmails();
$this->initial = false;
}
public function syncEmail($email) {
$this->email = $email;
}
public function fetch() {
try {
$count = count($this->messages);
$responses = [];
if (config('app.beta_feature') || !json_decode(config('app.settings.imap_settings'))->cc_check) {
$responses = [
'to' => ZEmail::getMessages($this->email, 'to', $this->deleted),
'cc' => [
'data' => [],
'notifications' => []
]
];
} else {
$responses = [
'to' => ZEmail::getMessages($this->email, 'to', $this->deleted),
'cc' => ZEmail::getMessages($this->email, 'cc', $this->deleted)
];
}
$this->deleted = [];
$this->messages = array_merge($responses['to']['data'], $responses['cc']['data']);
$notifications = array_merge($responses['to']['notifications'], $responses['cc']['notifications']);
if (count($notifications)) {
if ($this->overflow == false && count($this->messages) == $count) {
$this->overflow = true;
}
} else {
$this->overflow = false;
}
foreach ($notifications as $notification) {
$this->dispatchBrowserEvent('showNewMailNotification', $notification);
}
ZEmail::incrementMessagesStats(count($notifications));
} catch (\Exception $e) {
if (Auth::check() && Auth::user()->level == 9) {
$this->error = $e->getMessage();
} else {
$this->error = 'Not able to connect to Mail Server';
}
}
$this->dispatchBrowserEvent('stopLoader');
$this->dispatchBrowserEvent('loadDownload');
$this->initial = true;
}
public function delete($messageId) {
if (config('app.beta_feature')) {
Message::find($messageId)->delete();
}
$this->deleted[] = $messageId;
foreach ($this->messages as $key => $message) {
if ($message['id'] == $messageId) {
$directory = './tmp/attachments/' . $messageId;
$this->rrmdir($directory);
unset($this->messages[$key]);
}
}
}
public function render()
{
return view('livewire.frontend.app');
}
private function rrmdir($dir): void
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir . DIRECTORY_SEPARATOR . $object) && !is_link($dir . "/" . $object))
$this->rrmdir($dir . DIRECTORY_SEPARATOR . $object);
else
unlink($dir . DIRECTORY_SEPARATOR . $object);
}
}
rmdir($dir);
}
}
}