Files
zemailnator/app/Livewire/Dashboard/Mailbox/Inbox.php

296 lines
10 KiB
PHP

<?php
namespace App\Livewire\Dashboard\Mailbox;
use App\ColorPicker;
use App\Models\Log;
use App\Models\Premium;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;
use Livewire\Component;
class Inbox extends Component
{
use ColorPicker;
public $messages = [];
public $deleted = [];
public $error = '';
public $email;
public $initial = false;
public $type;
public $overflow = false;
public $messageId;
public $list = false;
public $emails;
public $username, $domain, $domains, $action;
public $email_limit = 20;
protected $listeners = ['updateEmail' => 'syncEmail', 'getEmail' => 'generateEmail', 'fetchMessages' => 'fetch', 'setMessageId' => 'setMessageId'];
public function mount(): void
{
$this->domains = json_decode(config('app.settings.configuration_settings'))->premium_domains ?? [];
$this->email = Premium::getEmail();
$this->emails = Premium::getEmails();
$this->initial = false;
$this->checkMultipleEmails();
$this->validateDomainInEmail();
if(auth()->user()->subscribedToProduct(config('app.plans')[0]['product_id'])) {
try {
$result = auth()->user()->subscriptions()->where(['stripe_status' => 'active'])->orderByDesc('updated_at')->first();
$userPriceID = $result['items'][0]['stripe_price'];
$mailboxLimit = $this->email_limit;
foreach (config('app.plans') as $plan) {
if ($plan['pricing_id'] === $userPriceID) {
$mailboxLimit = $plan['mailbox_limit'];
break;
}
}
$this->email_limit = $mailboxLimit;
} catch (\Exception $e) {
\Log::error($e->getMessage());
}
}
}
private function checkMultipleEmails(): void
{
if (count($this->emails) == 0) {
$this->emails = [$this->email];
}
if (count($this->emails) > 1) {
$this->list = true;
} else {
$this->list = false;
}
}
public function switchEmail($email)
{
return redirect()->route('switch', ['email' => $email]);
}
public function syncEmail(): void
{
$this->email = Premium::getEmail();
$this->emails = Premium::getEmails();
if (count($this->emails) == 0) {
$this->dispatch('getEmail');
}
$this->checkMultipleEmails();
$this->dispatch('syncMailbox', $this->email);
$this->dispatch('fetchMessages');
}
public function generateEmail(): void
{
if ($this->email == null) {
Premium::generateRandomEmail();
}
$this->checkMultipleEmails();
$this->dispatch('updateEmail');
}
/* Actions */
public function create() {
if (!$this->username) {
return $this->showAlert('error', __('Please enter Username'));
}
$this->checkDomainInUsername();
if (strlen($this->username) < json_decode(config('app.settings.configuration_settings'))->custom_username_length_min || strlen($this->username) > json_decode(config('app.settings.configuration_settings'))->custom_username_length_max) {
return $this->showAlert('error', __('Username length cannot be less than') . ' ' . json_decode(config('app.settings.configuration_settings'))->custom_username_length_min . ' ' . __('and greater than') . ' ' . json_decode(config('app.settings.configuration_settings'))->custom_username_length_max);
}
if (!$this->domain) {
return $this->showAlert('error', __('Please Select a Domain'));
}
if (in_array($this->username, json_decode(config('app.settings.configuration_settings'))->forbidden_ids)) {
return $this->showAlert('error', __('Username not allowed'));
}
if (!$this->checkEmailLimit()) {
return $this->showAlert('error', __('You have reached daily limit of MAX ') . $this->email_limit . __(' temp mail'));
}
if (!$this->checkUsedEmail()) {
return $this->showAlert('error', __('Sorry! That email is already been used by someone else. Please try a different email address.'));
}
$this->email = Premium::createCustomEmail($this->username, $this->domain);
return redirect()->route('dashboard.premium');
}
public function random() {
if (!$this->checkEmailLimit()) {
return $this->showAlert('error', __('You have reached daily limit of maximum ') . $this->email_limit . __(' temp mail addresses.'));
}
$this->email = Premium::generateRandomEmail();
return redirect()->route('dashboard.premium');
}
public function gmail() {
if (!$this->checkEmailLimit()) {
return $this->showAlert('error', __('You have reached daily limit of maximum ') . $this->email_limit . __(' temp mail addresses.'));
}
$this->email = Premium::generateRandomGmail();
return redirect()->route('dashboard.premium');
}
public function deleteEmail()
{
return redirect()->route('deleteP', ['email' => $this->email]);
}
private function showAlert($type, $message): void
{
$this->dispatch('showAlert', ['type' => $type, 'message' => $message]);
}
private function checkEmailLimit(): bool
{
$logs = Log::select('ip', 'email')->where('user_id', auth()->user()->id)->where('created_at', '>', Carbon::now()->subDay())->groupBy('email')->groupBy('ip')->get();
if (count($logs) >= $this->email_limit) {
return false;
}
return true;
}
private function checkUsedEmail(): bool
{
if (json_decode(config('app.settings.configuration_settings'))->disable_used_email) {
$check = Log::where('email', $this->user . '@' . $this->domain)->where('ip', '<>', request()->ip())->count();
if ($check > 0) {
return false;
}
return true;
}
return true;
}
private function checkDomainInUsername(): void
{
$parts = explode('@', $this->username);
if (isset($parts[1])) {
if (in_array($parts[1], $this->domains)) {
$this->domain = $parts[1];
}
$this->username = $parts[0];
}
}
private function validateDomainInEmail(): void
{
$data = explode('@', $this->email);
if (isset($data[1])) {
$domain = $data[1];
$domains = json_decode(config('app.settings.configuration_settings'))->premium_domains ?? [];
if (!in_array($domain, $domains)) {
$key = array_search($this->email, $this->emails);
Premium::removeEmail($this->email);
if ($key == 0 && count($this->emails) == 1 && json_decode(config('app.settings.configuration_settings'))->after_last_email_delete == 'redirect_to_homepage') {
redirect()->route('dashboard.premium');
} else {
redirect()->route('dashboard.premium');
}
}
}
}
/* Mailbox */
public function fetch(): void
{
try {
$count = count($this->messages);
if ($count > 0) {
$this->messages = [];
}
$responses = [];
if (config('app.beta_feature') || !json_decode(config('app.settings.imap_settings'))->premium_cc_check) {
$responses = [
'to' => Premium::getMessages($this->email, 'to', $this->deleted),
'cc' => [
'data' => [],
'notifications' => []
]
];
} else {
$responses = [
'to' => Premium::getMessages($this->email, 'to', $this->deleted),
'cc' => Premium::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 && count($this->messages) == $count) {
$this->overflow = true;
}
} else {
$this->overflow = false;
}
foreach ($notifications as $notification) {
$this->dispatch('showNewMailNotification', $notification);
}
Premium::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->dispatch('stopLoader');
$this->initial = true;
}
public function delete($messageId) {
try {
$this->deleted[] = $messageId;
foreach ($this->messages as $key => $message) {
if ($message['id'] == $messageId) {
$directory = public_path('tmp/premium/attachments/') . $messageId;
$this->rrmdir($directory);
unset($this->messages[$key]);
}
}
} catch (
\Exception $exception
) {
\Illuminate\Support\Facades\Log::error($exception->getMessage());
}
}
public function render()
{
return view('livewire.dashboard.mailbox.inbox')->layout('components.layouts.dashboard');
}
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);
}
}
}