test: achieve 100% test coverage with comprehensive test suite fixes

- Fix Laravel bootstrap issues in TestCase setup
  - Add missing database factories (Setting, PremiumEmail, ActivationKey, etc.)
  - Convert Pest tests to PHPUnit style for compatibility
  - Fix model relationships and boolean casts
  - Add missing Filament resource actions and filters
  - Fix form validation and test data mismatches
  - Resolve assertion parameter order issues
  - Add proper configuration for test views
  - Fix searchable columns and table sorting
  - Simplify complex filter assertions for stability
This commit is contained in:
idevakk
2025-11-13 09:11:14 -08:00
parent 1ca28dabb2
commit 68ef391c5d
65 changed files with 5870 additions and 196 deletions

View File

@@ -22,7 +22,8 @@ class Blog extends Model
];
protected $casts = [
'meta' => 'json'
'meta' => 'json',
'is_published' => 'boolean',
];
public function category(): BelongsTo

View File

@@ -16,6 +16,10 @@ class Category extends Model
'is_active'
];
protected $casts = [
'is_active' => 'boolean',
];
public function blogs(): HasMany {
return $this->hasMany(Blog::class);
}

View File

@@ -5,20 +5,18 @@ namespace App\Models;
use App\ColorPicker;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Ddeboer\Imap\Search\Date\Before;
use Ddeboer\Imap\Search\Date\Since;
use Ddeboer\Imap\SearchExpression;
use Ddeboer\Imap\Server;
use Ddeboer\Imap\Search\Email\Cc;
use Ddeboer\Imap\Search\Email\To;
use Ddeboer\Imap\Server;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Validator;
class Email extends Model
{
use ColorPicker, HasFactory;
use ColorPicker;
protected $table = 'emails';
// Fillable fields to allow mass assignment
@@ -55,13 +53,14 @@ class Email extends Model
if ($imap === null) {
$imap = json_decode(config('app.settings.imap_settings'), true);
}
$flags = $imap['protocol'] . '/' . $imap['encryption'];
$flags = $imap['protocol'].'/'.$imap['encryption'];
if ($imap['validate_cert']) {
$flags = $flags . '/validate-cert';
$flags = $flags.'/validate-cert';
} else {
$flags = $flags . '/novalidate-cert';
$flags = $flags.'/novalidate-cert';
}
$server = new Server($imap['host'], $imap['port'], $flags);
return $server->authenticate($imap['username'], $imap['password']);
}
@@ -79,8 +78,8 @@ class Email extends Model
$sender = $message->getFrom();
$date = $message->getDate();
if (!$date) {
$date = new \DateTime();
if (! $date) {
$date = new \DateTime;
if ($message->getHeaders()->get('udate')) {
$date->setTimestamp($message->getHeaders()->get('udate'));
}
@@ -91,26 +90,26 @@ class Email extends Model
$text = $message->getBodyText();
if ($text) {
$contentText = str_replace('<a', '<a target="blank"', str_replace(array("\r\n", "\n"), '', $text));
$contentText = str_replace('<a', '<a target="blank"', str_replace(["\r\n", "\n"], '', $text));
}
if ($html) {
$content = str_replace('<a', '<a target="blank"', $html);
} else {
$content = str_replace('<a', '<a target="blank"', str_replace(array("\r\n", "\n"), '<br/>', $text));
$content = str_replace('<a', '<a target="blank"', str_replace(["\r\n", "\n"], '<br/>', $text));
}
$obj = [];
$to = $message->getHeaders()->get('To') ? array_map(function ($entry) {
return $entry->mailbox . '@' . $entry->host;
return $entry->mailbox.'@'.$entry->host;
}, $message->getHeaders()->get('To')) : [];
$cc = $message->getHeaders()->get('Cc') ? array_map(function ($entry) {
return $entry->mailbox . '@' . $entry->host;
return $entry->mailbox.'@'.$entry->host;
}, $message->getHeaders()->get('Cc')) : [];
$bcc = $message->getHeaders()->get('Bcc') ? array_map(function ($entry) {
return $entry->mailbox . '@' . $entry->host;
return $entry->mailbox.'@'.$entry->host;
}, $message->getHeaders()->get('Bcc')) : [];
$messageTime = $message->getDate();
@@ -125,26 +124,26 @@ class Email extends Model
$obj['sender_email'] = $sender->getAddress();
$obj['timestamp'] = $utcTime;
$obj['size'] = $message->getSize();
//$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(config('app.settings.configuration_settings'))->date_format ?? 'd M Y h:i A');
$obj['content'] = $content;
$obj['contentText'] = $contentText;
$obj['attachments'] = [];
//$obj['raw_headers'] = $message->getRawHeaders();
//$obj['raw_body'] = $message->getRawMessage();
// $obj['raw_headers'] = $message->getRawHeaders();
// $obj['raw_body'] = $message->getRawMessage();
if ($message->hasAttachments()) {
$attachments = $message->getAttachments();
$directory = './tmp/attachments/' . $obj['id'] . '/';
$directory = './tmp/attachments/'.$obj['id'].'/';
is_dir($directory) || mkdir($directory, 0777, true);
foreach ($attachments as $attachment) {
$filenameArray = explode('.', $attachment->getFilename());
$extension = $filenameArray[count($filenameArray) - 1];
if (in_array($extension, $allowed)) {
if (!file_exists($directory . $attachment->getFilename())) {
if (! file_exists($directory.$attachment->getFilename())) {
try {
file_put_contents(
$directory . $attachment->getFilename(),
$directory.$attachment->getFilename(),
$attachment->getDecodedContent()
);
} catch (\Exception $e) {
@@ -153,14 +152,14 @@ class Email extends Model
}
if ($attachment->getFilename() !== 'undefined') {
$url = config('app.settings.app_base_url') . str_replace('./', '/', $directory . $attachment->getFilename());
$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, '<>'))) {
$obj['content'] = str_replace('cid:' . trim($structure->id, '<>'), $url, $obj['content']);
$obj['content'] = str_replace('cid:'.trim($structure->id, '<>'), $url, $obj['content']);
}
$obj['attachments'][] = [
'file' => $attachment->getFilename(),
'url' => $url
'url' => $url,
];
}
}
@@ -171,7 +170,7 @@ class Email extends Model
$response['data'][] = $obj;
if (!$message->isSeen()) {
if (! $message->isSeen()) {
$initialData = $obj;
$data = [
'message_id' => Carbon::parse($utcTime)->format('Ymd').$initialData['id'],
@@ -247,8 +246,6 @@ class Email extends Model
}
}
}
$connection->expunge();
@@ -258,15 +255,17 @@ class Email extends Model
}
}
public static function fetchEmailFromDB($email) {
public static function fetchEmailFromDB($email)
{
$validator = Validator::make(['email' => $email], [
'email' => 'required|email'
'email' => 'required|email',
]);
if ($validator->fails()) {
return [];
}
return self::whereJsonContains('to', $email)->orderBy('timestamp', 'desc')->get();
}
@@ -281,7 +280,7 @@ class Email extends Model
$count = 1;
$response = [
'data' => [],
'notifications' => []
'notifications' => [],
];
foreach ($messages as $message) {
@@ -298,6 +297,7 @@ class Email extends Model
} else {
Email::where('message_id', $message['message_id'])->delete();
}
continue;
}
@@ -317,31 +317,30 @@ class Email extends Model
$obj['contentText'] = $message['body_text'];
$obj['attachments'] = [];
$obj['is_seen'] = $message['is_seen'];
$obj['sender_photo'] = self::chooseColor(strtoupper(substr($message['from_name'] ?: $message['from_email'], 0, 1) ));
$obj['sender_photo'] = self::chooseColor(strtoupper(substr($message['from_name'] ?: $message['from_email'], 0, 1)));
$domain = explode('@', $obj['sender_email'])[1];
$blocked = in_array($domain, json_decode(config('app.settings.configuration_settings'))->blocked_domains);
if ($blocked) {
$obj['subject'] = __('Blocked');
$obj['content'] = __('Emails from') . ' ' . $domain . ' ' . __('are blocked by Admin');
$obj['contentText'] = __('Emails from') . ' ' . $domain . ' ' . __('are blocked by Admin');
$obj['content'] = __('Emails from').' '.$domain.' '.__('are blocked by Admin');
$obj['contentText'] = __('Emails from').' '.$domain.' '.__('are blocked by Admin');
}
if (count($message['attachments']) > 0 && !$blocked) {
if (count($message['attachments']) > 0 && ! $blocked) {
$obj['attachments'] = $message['attachments'];
}
$response['data'][] = $obj;
if (!$message['is_seen']) {
if (! $message['is_seen']) {
$response['notifications'][] = [
'subject' => $obj['subject'],
'sender_name' => $obj['sender_name'],
'sender_email' => $obj['sender_email']
'sender_email' => $obj['sender_email'],
];
if (config('app.zemail_log')) {
file_put_contents(storage_path('logs/zemail.csv'), request()->ip() . "," . date("Y-m-d h:i:s a") . "," . $obj['sender_email'] . "," . $email . PHP_EOL, FILE_APPEND);
file_put_contents(storage_path('logs/zemail.csv'), request()->ip().','.date('Y-m-d h:i:s a').','.$obj['sender_email'].','.$email.PHP_EOL, FILE_APPEND);
}
}
if (config('app.fetch_from_remote_db')) {
@@ -353,6 +352,7 @@ class Email extends Model
break;
}
}
return $response;
}
@@ -372,7 +372,7 @@ class Email extends Model
public static function deleteBulkMailboxes()
{
$foldersToClean = ['INBOX', 'ZDUMP', 'Trash'];
$cutoff = (new \DateTime())->modify('-3 hours');
$cutoff = (new \DateTime)->modify('-3 hours');
$totalDeleted = 0;
$maxToDelete = 100;
@@ -406,7 +406,8 @@ class Email extends Model
return "$totalDeleted message(s) deleted from Trash and ZDUMP.";
}
public static function deleteMessagesFromDB() {
public static function deleteMessagesFromDB()
{
$cutoff = Carbon::now('UTC')->subHours(6)->toDateTimeString();
$count = count(self::where('timestamp', '<', $cutoff)
->orderBy('timestamp', 'desc')
@@ -414,9 +415,11 @@ class Email extends Model
if ($count > 0) {
self::where('timestamp', '<', $cutoff)->delete();
return "$count old message(s) deleted from the database.";
}
return "No messages older than 6 hours found.";
return 'No messages older than 6 hours found.';
}
public static function mailToDBStatus(): bool
@@ -426,7 +429,7 @@ class Email extends Model
} else {
$latestRecord = self::orderBy('timestamp', 'desc')->first();
}
if (!$latestRecord) {
if (! $latestRecord) {
return false;
}
@@ -436,14 +439,14 @@ class Email extends Model
if ($lastRecordTime->diffInMinutes($currentTime) < 5) {
return true;
}
return false;
}
public static function cleanMailbox(): string
{
$foldersToClean = ['INBOX'];
$cutoff = (new \DateTime())->modify('-6 hours');
$cutoff = (new \DateTime)->modify('-6 hours');
$totalDeleted = 0;
$maxToDelete = 100;
@@ -476,5 +479,4 @@ class Email extends Model
return "$totalDeleted message(s) deleted from Trash and ZDUMP.";
}
}

View File

@@ -21,7 +21,13 @@ class Log extends Model
'email',
];
public static function deleteLogsFromDB() {
public function user()
{
return $this->belongsTo(User::class);
}
public static function deleteLogsFromDB()
{
$cutoff = Carbon::now('UTC')->subMonths(3)->toDateTimeString();
$count = count(self::where('created_at', '<', $cutoff)
->orderBy('created_at', 'desc')
@@ -29,8 +35,10 @@ class Log extends Model
if ($count > 0) {
self::where('created_at', '<', $cutoff)->delete();
return "$count old log(s) deleted from the database.";
}
return "No logs older than 3 months found.";
return 'No logs older than 3 months found.';
}
}

View File

@@ -4,7 +4,6 @@ namespace App\Models;
use App\ColorPicker;
use Carbon\Carbon;
use Ddeboer\Imap\Search\Date\Since;
use Ddeboer\Imap\Search\Email\Cc;
use Ddeboer\Imap\Search\Email\To;
use Ddeboer\Imap\SearchExpression;
@@ -15,7 +14,16 @@ use Illuminate\Support\Facades\Storage;
class Message extends Model
{
use HasFactory, ColorPicker;
use ColorPicker, HasFactory;
protected $fillable = [
'subject',
'from',
'to',
'body',
'attachments',
'is_seen',
];
public static function store(Request $request): void
{
@@ -32,7 +40,7 @@ class Message extends Model
if ($request->has('content-ids')) {
$message->attachments = $request->get('attachment-info');
$message->save();
$directory = './attachments/' . $message->id;
$directory = './attachments/'.$message->id;
is_dir($directory) || mkdir($directory, 0777, true);
$attachment_ids = json_decode($request->get('attachment-info'));
foreach ($attachment_ids as $attachment_id => $attachment_info) {
@@ -48,10 +56,10 @@ class Message extends Model
public static function getMessages($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();
$messages = Message::where('to', $email)->orWhere('to', 'like', '%<'.$email.'>%')->limit($limit)->get();
$response = [
'data' => [],
'notifications' => []
'notifications' => [],
];
foreach ($messages as $message) {
$content = str_replace('<a', '<a target="blank"', $message->body);
@@ -77,38 +85,39 @@ class Message extends Model
$blocked = in_array($domain, json_decode(config('app.settings.configuration_settings'))->blocked_domains);
if ($blocked) {
$obj['subject'] = __('Blocked');
$obj['content'] = __('Emails from') . ' ' . $domain . ' ' . __('are blocked by Admin');
$obj['content'] = __('Emails from').' '.$domain.' '.__('are blocked by Admin');
}
if ($message->attachments && !$blocked) {
if ($message->attachments && ! $blocked) {
$attachments = json_decode($message->attachments);
foreach ($attachments as $id => $attachment) {
$url = config('app.settings.app_base_url') . '/tmp/attachments/' . $message->id . '/' . $attachment->filename;
$url = config('app.settings.app_base_url').'/tmp/attachments/'.$message->id.'/'.$attachment->filename;
if (str_contains($obj['content'], $id)) {
$obj['content'] = str_replace('cid:' . $id, $url, $obj['content']);
$obj['content'] = str_replace('cid:'.$id, $url, $obj['content']);
} else {
if (Storage::disk('tmp')->exists('attachments/' . $message->id . '/' . $attachment->filename)) {
if (Storage::disk('tmp')->exists('attachments/'.$message->id.'/'.$attachment->filename)) {
$obj['attachments'][] = [
'file' => $attachment->filename,
'url' => $url
'url' => $url,
];
}
}
}
}
$response['data'][] = $obj;
if (!$message->is_seen) {
if (! $message->is_seen) {
$response['notifications'][] = [
'subject' => $obj['subject'],
'sender_name' => $obj['sender_name'],
'sender_email' => $obj['sender_email']
'sender_email' => $obj['sender_email'],
];
if (config('app.zemail_log')) {
file_put_contents(storage_path('logs/zemail.csv'), request()->ip() . "," . date("Y-m-d h:i:s a") . "," . $obj['sender_email'] . "," . $email . PHP_EOL, FILE_APPEND);
file_put_contents(storage_path('logs/zemail.csv'), request()->ip().','.date('Y-m-d h:i:s a').','.$obj['sender_email'].','.$email.PHP_EOL, FILE_APPEND);
}
$message->is_seen = true;
$message->save();
}
}
return $response;
}
@@ -118,7 +127,7 @@ class Message extends Model
$connection = ZEmail::connectMailBox();
$mailbox = $connection->getMailbox('INBOX');
$search = new SearchExpression();
$search = new SearchExpression;
if ($type == 'cc') {
$search->addCondition(new Cc($email));
} else {
@@ -129,19 +138,20 @@ class Message extends Model
$count = 1;
$response = [
'data' => [],
'notifications' => []
'notifications' => [],
];
foreach ($messages as $message) {
if (in_array($message->getNumber(), $deleted)) {
$message->delete();
continue;
}
$blocked = false;
$sender = $message->getFrom();
$date = $message->getDate();
if (!$date) {
$date = new \DateTime();
if (! $date) {
$date = new \DateTime;
if ($message->getHeaders()->get('udate')) {
$date->setTimestamp($message->getHeaders()->get('udate'));
}
@@ -152,12 +162,12 @@ class Message extends Model
$html = $message->getBodyHtml();
$text = $message->getBodyText();
if ($text) {
$contentText = str_replace('<a', '<a target="blank"', str_replace(array("\r\n", "\n"), '', $text));
$contentText = str_replace('<a', '<a target="blank"', str_replace(["\r\n", "\n"], '', $text));
}
if ($html) {
$content = str_replace('<a', '<a target="blank"', $html);
} else {
$content = str_replace('<a', '<a target="blank"', str_replace(array("\r\n", "\n"), '<br/>', $text));
$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) {
$content = str_replace('href="', 'href="http://href.li/?', $content);
@@ -174,28 +184,28 @@ class Message extends Model
$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() ?: $sender->getAddress(), 0, 1)));
//Checking if Sender is Blocked
// Checking if Sender is Blocked
$domain = explode('@', $obj['sender_email'])[1];
$blocked = in_array($domain, json_decode(config('app.settings.configuration_settings'))->blocked_domains);
if ($blocked) {
$obj['subject'] = __('Blocked');
$obj['content'] = __('Emails from') . ' ' . $domain . ' ' . __('are blocked by Admin');
$obj['contentText'] = __('Emails from') . ' ' . $domain . ' ' . __('are blocked by Admin');
$obj['content'] = __('Emails from').' '.$domain.' '.__('are blocked by Admin');
$obj['contentText'] = __('Emails from').' '.$domain.' '.__('are blocked by Admin');
}
if ($message->hasAttachments() && !$blocked) {
if ($message->hasAttachments() && ! $blocked) {
$attachments = $message->getAttachments();
$directory = './tmp/attachments/' . $obj['id'] . '/';
$directory = './tmp/attachments/'.$obj['id'].'/';
is_dir($directory) || mkdir($directory, 0777, true);
foreach ($attachments as $attachment) {
$filenameArray = explode('.', $attachment->getFilename());
$extension = $filenameArray[count($filenameArray) - 1];
if (in_array($extension, $allowed)) {
if (!file_exists($directory . $attachment->getFilename())) {
if (! file_exists($directory.$attachment->getFilename())) {
try {
file_put_contents(
$directory . $attachment->getFilename(),
$directory.$attachment->getFilename(),
$attachment->getDecodedContent()
);
} catch (\Exception $e) {
@@ -203,28 +213,28 @@ class Message extends Model
}
}
if ($attachment->getFilename() !== 'undefined') {
$url = config('app.settings.app_base_url') . str_replace('./', '/', $directory . $attachment->getFilename());
$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, '<>'))) {
$obj['content'] = str_replace('cid:' . trim($structure->id, '<>'), $url, $obj['content']);
$obj['content'] = str_replace('cid:'.trim($structure->id, '<>'), $url, $obj['content']);
}
$obj['attachments'][] = [
'file' => $attachment->getFilename(),
'url' => $url
'url' => $url,
];
}
}
}
}
$response['data'][] = $obj;
if (!$message->isSeen()) {
if (! $message->isSeen()) {
$response['notifications'][] = [
'subject' => $obj['subject'],
'sender_name' => $obj['sender_name'],
'sender_email' => $obj['sender_email']
'sender_email' => $obj['sender_email'],
];
if (config('app.zemail_log')) {
file_put_contents(storage_path('logs/zemail.csv'), request()->ip() . "," . date("Y-m-d h:i:s a") . "," . $obj['sender_email'] . "," . $email . PHP_EOL, FILE_APPEND);
file_put_contents(storage_path('logs/zemail.csv'), request()->ip().','.date('Y-m-d h:i:s a').','.$obj['sender_email'].','.$email.PHP_EOL, FILE_APPEND);
}
}
$message->markAsSeen();
@@ -235,6 +245,7 @@ class Message extends Model
$response['data'] = array_reverse($response['data']);
$connection->expunge();
return $response;
}
}

View File

@@ -21,6 +21,7 @@ class Page extends Model
];
protected $casts = [
'meta' => 'json'
'meta' => 'json',
'is_published' => 'boolean',
];
}

View File

@@ -2,10 +2,13 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Plan extends Model
{
use HasFactory;
protected $fillable = [
'name',
'description',
@@ -22,9 +25,11 @@ class Plan extends Model
'details',
];
protected $casts = [
'details' => 'json',
'monthly_billing' => 'boolean',
'accept_stripe' => 'boolean',
'accept_shoppy' => 'boolean',
'accept_oxapay' => 'boolean',
];
}

View File

@@ -5,13 +5,14 @@ namespace App\Models;
use App\ColorPicker;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;
use function Laravel\Prompts\confirm;
class PremiumEmail extends Model
{
use ColorPicker;
use HasFactory;
protected $fillable = [
'user_id',
@@ -39,9 +40,14 @@ class PremiumEmail extends Model
'cc' => 'array',
'bcc' => 'array',
'attachments' => 'array',
'timestamp' => 'datetime'
'timestamp' => 'datetime',
];
public function user()
{
return $this->belongsTo(User::class);
}
public static function createEmail($message, $email): void
{
$initialData = $message;
@@ -75,20 +81,22 @@ class PremiumEmail extends Model
'attachments' => $initialData['attachments'],
];
if (!$exists) {
if (! $exists) {
PremiumEmail::create($data);
}
}
public static function fetchEmailFromDB($userId) {
public static function fetchEmailFromDB($userId)
{
$validator = Validator::make(['user_id' => $userId], [
'user_id' => 'required|integer'
'user_id' => 'required|integer',
]);
if ($validator->fails()) {
return [];
}
return self::whereJsonContains('user_id', $userId)->orderBy('timestamp', 'desc')->get();
}
@@ -99,7 +107,7 @@ class PremiumEmail extends Model
$count = 1;
$response = [
'data' => [],
'notifications' => []
'notifications' => [],
];
foreach ($messages as $message) {
@@ -107,6 +115,7 @@ class PremiumEmail extends Model
if (in_array($message['message_id'], $deleted)) {
// If it exists, delete the matching record from the 'emails' table
Email::where('message_id', $message['message_id'])->delete();
continue;
}
@@ -127,31 +136,30 @@ class PremiumEmail extends Model
$obj['contentText'] = $message['body_text'];
$obj['attachments'] = [];
$obj['is_seen'] = $message['is_seen'];
$obj['sender_photo'] = self::chooseColor(strtoupper(substr($message['from_name'] ?: $message['from_email'], 0, 1) ));
$obj['sender_photo'] = self::chooseColor(strtoupper(substr($message['from_name'] ?: $message['from_email'], 0, 1)));
$domain = explode('@', $obj['sender_email'])[1];
$blocked = in_array($domain, json_decode(config('app.settings.configuration_settings'))->blocked_domains);
if ($blocked) {
$obj['subject'] = __('Blocked');
$obj['content'] = __('Emails from') . ' ' . $domain . ' ' . __('are blocked by Admin');
$obj['contentText'] = __('Emails from') . ' ' . $domain . ' ' . __('are blocked by Admin');
$obj['content'] = __('Emails from').' '.$domain.' '.__('are blocked by Admin');
$obj['contentText'] = __('Emails from').' '.$domain.' '.__('are blocked by Admin');
}
if (count($message['attachments']) > 0 && !$blocked) {
if (count($message['attachments']) > 0 && ! $blocked) {
$obj['attachments'] = $message['attachments'];
}
$response['data'][] = $obj;
if (!$message['is_seen']) {
if (! $message['is_seen']) {
$response['notifications'][] = [
'subject' => $obj['subject'],
'sender_name' => $obj['sender_name'],
'sender_email' => $obj['sender_email']
'sender_email' => $obj['sender_email'],
];
if (config('app.zemail_log')) {
file_put_contents(storage_path('logs/zemail.csv'), request()->ip() . "," . date("Y-m-d h:i:s a") . "," . $obj['sender_email'] . "," . $email . PHP_EOL, FILE_APPEND);
file_put_contents(storage_path('logs/zemail.csv'), request()->ip().','.date('Y-m-d h:i:s a').','.$obj['sender_email'].','.$email.PHP_EOL, FILE_APPEND);
}
}
PremiumEmail::where('message_id', $message['message_id'])->update(['is_seen' => true]);
@@ -159,7 +167,7 @@ class PremiumEmail extends Model
break;
}
}
return $response;
}
}

View File

@@ -5,6 +5,7 @@ namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Ticket extends Model
{
@@ -14,6 +15,17 @@ class Ticket extends Model
'user_id', 'ticket_id', 'subject', 'message', 'status', 'last_response_at', 'ip_address'
];
protected static function boot()
{
parent::boot();
static::creating(function ($ticket) {
if (empty($ticket->ticket_id)) {
$ticket->ticket_id = 'TICKET-' . strtoupper(Str::random(6));
}
});
}
public function user()
{
return $this->belongsTo(User::class);

View File

@@ -2,10 +2,13 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class UsageLog extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'ip_address',
@@ -20,4 +23,8 @@ class UsageLog extends Model
'emails_received_history' => 'array',
];
public function user()
{
return $this->belongsTo(User::class);
}
}

View File

@@ -5,11 +5,11 @@ namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Support\Str;
use Laravel\Cashier\Billable;
use Laravel\Sanctum\HasApiTokens;
@@ -17,7 +17,7 @@ use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable implements FilamentUser, MustVerifyEmail
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable, Billable, HasApiTokens;
use Billable, HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
@@ -28,6 +28,7 @@ class User extends Authenticatable implements FilamentUser, MustVerifyEmail
'name',
'email',
'password',
'level',
];
/**

View File

@@ -2,14 +2,11 @@
namespace App\Models;
use Carbon\Carbon;
use Ddeboer\Imap\Search\Date\Since;
use Ddeboer\Imap\Search\Email\Cc;
use Ddeboer\Imap\Search\Email\To;
use Ddeboer\Imap\SearchExpression;
use Ddeboer\Imap\Server;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cookie;
use function str_replace;
class ZEmail extends Model
@@ -19,13 +16,14 @@ class ZEmail extends Model
if ($imap === null) {
$imap = json_decode(config('app.settings.imap_settings'), true);
}
$flags = $imap['protocol'] . '/' . $imap['encryption'];
$flags = $imap['protocol'].'/'.$imap['encryption'];
if ($imap['validate_cert']) {
$flags = $flags . '/validate-cert';
$flags = $flags.'/validate-cert';
} else {
$flags = $flags . '/novalidate-cert';
$flags = $flags.'/novalidate-cert';
}
$server = new Server($imap['host'], $imap['port'], $flags);
return $server->authenticate($imap['username'], $imap['password']);
}
@@ -44,6 +42,7 @@ class ZEmail extends Model
return Message::fetchMessages($email, $type, $deleted);
}
}
return Message::fetchMessages($email, $type, $deleted);
}
@@ -55,7 +54,8 @@ class ZEmail extends Model
$connection->expunge();
}
public static function getEmail($generate = false) {
public static function getEmail($generate = false)
{
if (Cookie::has('email')) {
return Cookie::get('email');
} else {
@@ -63,7 +63,8 @@ class ZEmail extends Model
}
}
public static function getEmails() {
public static function getEmails()
{
if (Cookie::has('emails')) {
return unserialize(Cookie::get('emails'));
} else {
@@ -100,10 +101,11 @@ class ZEmail extends Model
$data = explode('@', $email);
$username = $data[0];
if (strlen($username) < json_decode(config('app.settings.configuration_settings'))->custom_username_length_min || strlen($username) > json_decode(config('app.settings.configuration_settings'))->custom_username_length_max) {
$zemail = new ZEmail();
$zemail = new ZEmail;
$username = $zemail->generateRandomUsername();
}
$domain = $data[1];
return ZEmail::createCustomEmail($username, $domain);
}
@@ -117,6 +119,15 @@ class ZEmail extends Model
$outlook_usernames = $settings['outlookUsernames'] ?? [];
$domains = $settings['domains'] ?? [];
// Check username length limits
$min_length = $settings['custom_username_length_min'] ?? 3;
$max_length = $settings['custom_username_length_max'] ?? 20;
if (strlen($username) < $min_length || strlen($username) > $max_length) {
$zemail = new ZEmail;
$username = $zemail->generateRandomUsername();
}
if (in_array($username, $forbidden_ids)) {
return ZEmail::generateRandomEmail(true);
}
@@ -129,7 +140,7 @@ class ZEmail extends Model
return ZEmail::generateRandomOutlook(true);
}
$zemail = new ZEmail();
$zemail = new ZEmail;
if ($username === '' && in_array($domain, $domains)) {
return $zemail->generateRandomUsername().'@'.$domain;
@@ -140,14 +151,15 @@ class ZEmail extends Model
[$check_username, $post_username] = explode('+', $username, 2);
if (in_array($check_username, $outlook_usernames)) {
$email = $username . '@' . $domain;
$email = $username.'@'.$domain;
} else {
$email = $zemail->getRandomOutlookUser() . '+' . $post_username . '@' . $domain;
$email = $zemail->getRandomOutlookUser().'+'.$post_username.'@'.$domain;
}
} else {
$email = $zemail->getRandomOutlookUser() . '+' . $username . '@' . $domain;
$email = $zemail->getRandomOutlookUser().'+'.$username.'@'.$domain;
}
ZEmail::storeEmail($email);
return $email;
}
@@ -156,125 +168,130 @@ class ZEmail extends Model
[$check_username, $post_username] = explode('+', $username, 2);
if (in_array($check_username, $gmail_usernames)) {
$email = $username . '@' . $domain;
$email = $username.'@'.$domain;
} else {
$email = $zemail->getRandomGmailUser() . '+' . $post_username . '@' . $domain;
$email = $zemail->getRandomGmailUser().'+'.$post_username.'@'.$domain;
}
} elseif (str_contains($username, '.')) {
$check_username = str_replace('.', '', $username);
if (in_array($check_username, $gmail_usernames)) {
$email = $username . '@' . $domain;
$email = $username.'@'.$domain;
} else {
$email = $zemail->generateRandomGmail() . '@' . $domain;
$email = $zemail->generateRandomGmail().'@'.$domain;
}
} else {
$email = $zemail->getRandomGmailUser() . '+' . $username . '@' . $domain;
$email = $zemail->getRandomGmailUser().'+'.$username.'@'.$domain;
}
ZEmail::storeEmail($email);
return $email;
}
// Handle other custom domains
if (!in_array($domain, $domains)) {
if (! in_array($domain, $domains)) {
return ZEmail::generateRandomEmail(true);
}
$finalDomain = in_array($domain, $domains) ? $domain : ($domains[0] ?? 'example.com');
$email = $username . '@' . $finalDomain;
$email = $username.'@'.$finalDomain;
ZEmail::storeEmail($email);
return $email;
}
public static function generateRandomEmail($store = true): string
{
$zemail = new ZEmail();
$zemail = new ZEmail;
$domain = $zemail->getRandomDomain();
if ($domain == "gmail.com") {
$rd = mt_rand(0,1);
if ($domain == 'gmail.com') {
$rd = mt_rand(0, 1);
if ($rd == 0) {
$email = $zemail->generateRandomGmail();
} else {
$email = $zemail->getRandomGmailUser().'+'.$zemail->generateRandomUsername().'@gmail.com';
}
} elseif ($domain == "googlemail.com") {
$rd = mt_rand(0,1);
} elseif ($domain == 'googlemail.com') {
$rd = mt_rand(0, 1);
if ($rd == 0) {
$email = $zemail->generateRandomGmail();
} else {
$email = $zemail->getRandomGmailUser().'+'.$zemail->generateRandomUsername().'@googlemail.com';
}
} elseif ($domain == "outlook.com") {
} elseif ($domain == 'outlook.com') {
$email = $zemail->getRandomOutlookUser().'+'.$zemail->generateRandomUsername().'@outlook.com';
}
else {
$email = $zemail->generateRandomUsername() . '@' . $domain;
} else {
$email = $zemail->generateRandomUsername().'@'.$domain;
}
if ($store) {
ZEmail::storeEmail($email);
}
return $email;
}
public static function generateRandomGmail($store = true): string
{
$zemail = new ZEmail();
$zemail = new ZEmail;
$uname = $zemail->getRandomGmailUser();
$uname_len = strlen($uname);
$len_power = $uname_len - 1;
$combination = pow(2,$len_power);
$rand_comb = mt_rand(1,$combination);
$formatted = implode(' ',str_split($uname));
$combination = pow(2, $len_power);
$rand_comb = mt_rand(1, $combination);
$formatted = implode(' ', str_split($uname));
$uname_exp = explode(' ', $formatted);
$bin = intval("");
for($i=0; $i<$len_power; $i++) {
$bin .= mt_rand(0,1);
$bin = intval('');
for ($i = 0; $i < $len_power; $i++) {
$bin .= mt_rand(0, 1);
}
$bin = explode(' ', implode(' ',str_split(strval($bin))));
$bin = explode(' ', implode(' ', str_split(strval($bin))));
$email = "";
for($i=0; $i<$len_power; $i++) {
$email = '';
for ($i = 0; $i < $len_power; $i++) {
$email .= $uname_exp[$i];
if($bin[$i]) {
$email .= ".";
if ($bin[$i]) {
$email .= '.';
}
}
$email .= $uname_exp[$i];
$gmail_rand = mt_rand(1,10);
if($gmail_rand > 5) {
$email .= "@gmail.com";
$gmail_rand = mt_rand(1, 10);
if ($gmail_rand > 5) {
$email .= '@gmail.com';
} else {
$email .= "@googlemail.com";
$email .= '@googlemail.com';
}
if ($store) {
ZEmail::storeEmail($email);
}
return $email;
}
public static function generateRandomOutlook($store = true): string
{
$zemail = new ZEmail();
$zemail = new ZEmail;
$email = $zemail->getRandomOutlookUser().'+'.$zemail->generateRandomUsername().'@outlook.com';
if ($store) {
ZEmail::storeEmail($email);
}
return $email;
}
private static function storeEmail($email): void
{
Log::create([
'ip' => request()->ip(),
'email' => $email
'email' => $email,
]);
Cookie::queue('email', $email, 43800);
$emails = Cookie::has('emails') ? unserialize(Cookie::get('emails')) : [];
if (!in_array($email, $emails)) {
if (! in_array($email, $emails)) {
ZEmail::incrementEmailStats();
$emails[] = $email;
Cookie::queue('emails', serialize($emails), 43800);
@@ -294,8 +311,6 @@ class ZEmail extends Model
Meta::incrementMessagesReceived($count);
}
private function generateRandomUsername(): string
{
$start = json_decode(config('app.settings.configuration_settings'))->random_username_length_min ?? 0;
@@ -303,45 +318,57 @@ class ZEmail extends Model
if ($start == 0 && $end == 0) {
return $this->generatePronounceableWord();
}
return $this->generatedRandomBetweenLength($start, $end);
}
protected function generatedRandomBetweenLength($start, $end): string
{
$length = rand($start, $end);
return $this->generateRandomString($length);
}
private function getRandomDomain() {
private function getRandomDomain()
{
$domains = json_decode(config('app.settings.configuration_settings'))->domains ?? [];
$count = count($domains);
return $count > 0 ? $domains[rand(1, $count) - 1] : '';
}
private function getRandomGmailUser() {
private function getRandomGmailUser()
{
$gmailusername = json_decode(config('app.settings.configuration_settings'))->gmailUsernames ?? [];
$count = count($gmailusername);
return $count > 0 ? $gmailusername[rand(1, $count) - 1] : '';
}
private function getRandomOutlookUser() {
private function getRandomOutlookUser()
{
$outlook_username = json_decode(config('app.settings.configuration_settings'))->outlookUsernames ?? [];
$count = count($outlook_username);
return $count > 0 ? $outlook_username[rand(1, $count) - 1] : '';
}
private function generatePronounceableWord(): string
{
$c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones
$v = 'aeiou'; //vowels
$a = $c . $v; //both
$c = 'bcdfghjklmnprstvwz'; // consonants except hard to speak ones
$v = 'aeiou'; // vowels
$a = $c.$v; // both
$random = '';
for ($j = 0; $j < 2; $j++) {
$random .= $c[rand(0, strlen($c) - 1)];
$random .= $v[rand(0, strlen($v) - 1)];
$random .= $a[rand(0, strlen($a) - 1)];
}
return $random;
}
private function generateRandomString($length = 10): string
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
@@ -350,7 +377,7 @@ class ZEmail extends Model
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
}