chore: code refactor via rector
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use App\ColorPicker;
|
||||
use Carbon\Carbon;
|
||||
use DateTime;
|
||||
@@ -34,21 +35,19 @@ class Message extends Model
|
||||
$message->subject = $request->subject;
|
||||
$message->from = $request->from;
|
||||
$message->to = $request->to;
|
||||
if ($request->has('html')) {
|
||||
$message->body = $request->html;
|
||||
} else {
|
||||
$message->body = $request->text;
|
||||
}
|
||||
$message->body = $request->has('html') ? $request->html : $request->text;
|
||||
$message->save();
|
||||
if ($request->has('content-ids')) {
|
||||
$message->attachments = $request->get('attachment-info');
|
||||
$message->save();
|
||||
$directory = './attachments/'.$message->id;
|
||||
is_dir($directory) || mkdir($directory, 0777, true);
|
||||
$attachment_ids = json_decode($request->get('attachment-info'));
|
||||
if (!is_dir($directory)) {
|
||||
mkdir($directory, 0777, true);
|
||||
}
|
||||
$attachment_ids = json_decode((string) $request->get('attachment-info'));
|
||||
foreach ($attachment_ids as $attachment_id => $attachment_info) {
|
||||
$allowed = explode(',', 'csv,doc,docx,xls,xlsx,ppt,pptx,xps,pdf,dxf,ai,psd,eps,ps,svg,ttf,zip,rar,tar,gzip,mp3,mpeg,wav,ogg,jpeg,jpg,png,gif,bmp,tif,webm,mpeg4,3gpp,mov,avi,mpegs,wmv,flx,txt');
|
||||
$file = explode('.', $attachment_info->filename);
|
||||
$file = explode('.', (string) $attachment_info->filename);
|
||||
if (in_array($file[count($file) - 1], $allowed)) {
|
||||
Storage::disk('tmp')->putFileAs($directory, $request->file($attachment_id), $attachment_info->filename);
|
||||
}
|
||||
@@ -56,53 +55,47 @@ class Message extends Model
|
||||
}
|
||||
}
|
||||
|
||||
public static function getMessages($email): array
|
||||
public static function getMessages(string $email): array
|
||||
{
|
||||
$limit = json_decode(config('app.settings.configuration_settings'))->fetch_messages_limit ?? 15;
|
||||
$messages = Message::where('to', $email)->orWhere('to', 'like', '%<'.$email.'>%')->limit($limit)->get();
|
||||
$limit = json_decode((string) config('app.settings.configuration_settings'))->fetch_messages_limit ?? 15;
|
||||
$messages = Message::query()->where('to', $email)->orWhere('to', 'like', '%<'.$email.'>%')->limit($limit)->get();
|
||||
$response = [
|
||||
'data' => [],
|
||||
'notifications' => [],
|
||||
];
|
||||
foreach ($messages as $message) {
|
||||
$content = str_replace('<a', '<a target="blank"', $message->body);
|
||||
if (json_decode(config('app.settings.configuration_settings'))->enable_masking_external_link) {
|
||||
if (json_decode((string) config('app.settings.configuration_settings'))->enable_masking_external_link) {
|
||||
$content = str_replace('href="', 'href="https://href.li/?', $content);
|
||||
}
|
||||
$obj = [];
|
||||
$obj['subject'] = $message->subject;
|
||||
$sender = explode('<', $message->from);
|
||||
$sender = explode('<', (string) $message->from);
|
||||
$obj['sender_name'] = $sender[0];
|
||||
if (isset($sender[1])) {
|
||||
$obj['sender_email'] = str_replace('>', '', $sender[1]);
|
||||
} else {
|
||||
$obj['sender_email'] = $obj['sender_name'];
|
||||
}
|
||||
$obj['sender_email'] = isset($sender[1]) ? str_replace('>', '', $sender[1]) : $obj['sender_name'];
|
||||
$obj['timestamp'] = $message->created_at;
|
||||
$obj['date'] = $message->created_at->format(json_decode(config('app.settings.configuration_settings'))->date_format ?? 'd M Y h:i A');
|
||||
$obj['date'] = $message->created_at->format(json_decode((string) config('app.settings.configuration_settings'))->date_format ?? 'd M Y h:i A');
|
||||
$obj['datediff'] = $message->created_at->diffForHumans();
|
||||
$obj['id'] = $message->id;
|
||||
$obj['content'] = $content;
|
||||
$obj['attachments'] = [];
|
||||
$domain = explode('@', $obj['sender_email'])[1];
|
||||
$blocked = in_array($domain, json_decode(config('app.settings.configuration_settings'))->blocked_domains);
|
||||
$blocked = in_array($domain, json_decode((string) config('app.settings.configuration_settings'))->blocked_domains);
|
||||
if ($blocked) {
|
||||
$obj['subject'] = __('Blocked');
|
||||
$obj['content'] = __('Emails from').' '.$domain.' '.__('are blocked by Admin');
|
||||
}
|
||||
if ($message->attachments && ! $blocked) {
|
||||
$attachments = json_decode($message->attachments);
|
||||
$attachments = json_decode((string) $message->attachments);
|
||||
foreach ($attachments as $id => $attachment) {
|
||||
$url = config('app.settings.app_base_url').'/tmp/attachments/'.$message->id.'/'.$attachment->filename;
|
||||
if (str_contains($obj['content'], $id)) {
|
||||
if (str_contains($obj['content'], (string) $id)) {
|
||||
$obj['content'] = str_replace('cid:'.$id, $url, $obj['content']);
|
||||
} else {
|
||||
if (Storage::disk('tmp')->exists('attachments/'.$message->id.'/'.$attachment->filename)) {
|
||||
$obj['attachments'][] = [
|
||||
'file' => $attachment->filename,
|
||||
'url' => $url,
|
||||
];
|
||||
}
|
||||
} elseif (Storage::disk('tmp')->exists('attachments/'.$message->id.'/'.$attachment->filename)) {
|
||||
$obj['attachments'][] = [
|
||||
'file' => $attachment->filename,
|
||||
'url' => $url,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -124,7 +117,7 @@ class Message extends Model
|
||||
return $response;
|
||||
}
|
||||
|
||||
public static function fetchMessages($email, $type = 'to', $deleted = []): array
|
||||
public static function fetchMessages(string $email, $type = 'to', $deleted = []): array
|
||||
{
|
||||
$allowed = explode(',', 'doc,docx,xls,xlsx,ppt,pptx,xps,pdf,dxf,ai,psd,eps,ps,svg,ttf,zip,rar,tar,gzip,mp3,mpeg,wav,ogg,jpeg,jpg,png,gif,bmp,tif,webm,mpeg4,3gpp,mov,avi,mpegs,wmv,flx,txt');
|
||||
$connection = ZEmail::connectMailBox();
|
||||
@@ -137,7 +130,7 @@ class Message extends Model
|
||||
$search->addCondition(new To($email));
|
||||
}
|
||||
$messages = $mailbox->getMessages($search, \SORTDATE, true);
|
||||
$limit = json_decode(config('app.settings.configuration_settings'))->fetch_messages_limit ?? 15;
|
||||
$limit = json_decode((string) config('app.settings.configuration_settings'))->fetch_messages_limit ?? 15;
|
||||
$count = 1;
|
||||
$response = [
|
||||
'data' => [],
|
||||
@@ -153,7 +146,7 @@ class Message extends Model
|
||||
$blocked = false;
|
||||
$sender = $message->getFrom();
|
||||
$date = $message->getDate();
|
||||
if (! $date) {
|
||||
if (!$date instanceof DateTimeImmutable) {
|
||||
$date = new DateTime;
|
||||
if ($message->getHeaders()->get('udate')) {
|
||||
$date->setTimestamp($message->getHeaders()->get('udate'));
|
||||
@@ -172,7 +165,7 @@ class Message extends Model
|
||||
} else {
|
||||
$content = str_replace('<a', '<a target="blank"', str_replace(["\r\n", "\n"], '<br/>', $text));
|
||||
}
|
||||
if (json_decode(config('app.settings.configuration_settings'))->enable_masking_external_link) {
|
||||
if (json_decode((string) config('app.settings.configuration_settings'))->enable_masking_external_link) {
|
||||
$content = str_replace('href="', 'href="http://href.li/?', $content);
|
||||
}
|
||||
$obj = [];
|
||||
@@ -180,18 +173,18 @@ class Message extends Model
|
||||
$obj['sender_name'] = $sender->getName();
|
||||
$obj['sender_email'] = $sender->getAddress();
|
||||
$obj['timestamp'] = $message->getDate();
|
||||
$obj['date'] = $date->format(json_decode(config('app.settings.configuration_settings'))->date_format ?? 'd M Y h:i A');
|
||||
$obj['date'] = $date->format(json_decode((string) config('app.settings.configuration_settings'))->date_format ?? 'd M Y h:i A');
|
||||
$obj['datediff'] = $datediff->diffForHumans();
|
||||
$obj['id'] = $message->getNumber();
|
||||
$obj['content'] = $content;
|
||||
$obj['contentText'] = $contentText;
|
||||
$obj['attachments'] = [];
|
||||
$obj['is_seen'] = true;
|
||||
$obj['sender_photo'] = self::chooseColor(strtoupper(substr($sender->getName() ?: $sender->getAddress(), 0, 1)));
|
||||
$obj['sender_photo'] = self::chooseColor(strtoupper(substr($sender->getName() ?: (string) $sender->getAddress(), 0, 1)));
|
||||
|
||||
// Checking if Sender is Blocked
|
||||
$domain = explode('@', $obj['sender_email'])[1];
|
||||
$blocked = in_array($domain, json_decode(config('app.settings.configuration_settings'))->blocked_domains);
|
||||
$domain = explode('@', (string) $obj['sender_email'])[1];
|
||||
$blocked = in_array($domain, json_decode((string) config('app.settings.configuration_settings'))->blocked_domains);
|
||||
if ($blocked) {
|
||||
$obj['subject'] = __('Blocked');
|
||||
$obj['content'] = __('Emails from').' '.$domain.' '.__('are blocked by Admin');
|
||||
@@ -200,9 +193,11 @@ class Message extends Model
|
||||
if ($message->hasAttachments() && ! $blocked) {
|
||||
$attachments = $message->getAttachments();
|
||||
$directory = './tmp/attachments/'.$obj['id'].'/';
|
||||
is_dir($directory) || mkdir($directory, 0777, true);
|
||||
if (!is_dir($directory)) {
|
||||
mkdir($directory, 0777, true);
|
||||
}
|
||||
foreach ($attachments as $attachment) {
|
||||
$filenameArray = explode('.', $attachment->getFilename());
|
||||
$filenameArray = explode('.', (string) $attachment->getFilename());
|
||||
$extension = $filenameArray[count($filenameArray) - 1];
|
||||
if (in_array($extension, $allowed)) {
|
||||
if (! file_exists($directory.$attachment->getFilename())) {
|
||||
@@ -218,7 +213,7 @@ class Message extends Model
|
||||
if ($attachment->getFilename() !== 'undefined') {
|
||||
$url = config('app.settings.app_base_url').str_replace('./', '/', $directory.$attachment->getFilename());
|
||||
$structure = $attachment->getStructure();
|
||||
if (isset($structure->id) && str_contains($obj['content'], trim($structure->id, '<>'))) {
|
||||
if (isset($structure->id) && str_contains((string) $obj['content'], trim($structure->id, '<>'))) {
|
||||
$obj['content'] = str_replace('cid:'.trim($structure->id, '<>'), $url, $obj['content']);
|
||||
}
|
||||
$obj['attachments'][] = [
|
||||
|
||||
Reference in New Issue
Block a user