chore: code refactor via rector
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use DateTimeImmutable;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\ColorPicker;
|
||||
use Carbon\Carbon;
|
||||
use DateTime;
|
||||
@@ -13,15 +16,16 @@ use Exception;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Cookie;
|
||||
use Log;
|
||||
|
||||
class Premium extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasFactory;
|
||||
use ColorPicker;
|
||||
|
||||
public static function connectMailBox($imap = null): ConnectionInterface
|
||||
{
|
||||
$imapDB = json_decode(config('app.settings.imap_settings'), true);
|
||||
$imapDB = json_decode((string) config('app.settings.imap_settings'), true);
|
||||
$imap = [
|
||||
'host' => $imapDB['premium_host'],
|
||||
'port' => $imapDB['premium_port'],
|
||||
@@ -37,7 +41,7 @@ class Premium extends Model
|
||||
return ZEmail::connectMailBox($imap);
|
||||
}
|
||||
|
||||
public static function fetchMessages($email, $type = 'to', $deleted = []): array
|
||||
public static function fetchMessages(?string $email, $type = 'to', $deleted = []): array
|
||||
{
|
||||
if ($email == null) {
|
||||
return [
|
||||
@@ -56,7 +60,7 @@ class Premium 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' => [],
|
||||
@@ -71,7 +75,7 @@ class Premium 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'));
|
||||
@@ -90,7 +94,7 @@ class Premium 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);
|
||||
}
|
||||
|
||||
@@ -99,7 +103,7 @@ class Premium 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['size'] = $message->getSize();
|
||||
@@ -107,11 +111,11 @@ class Premium 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() ?: (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');
|
||||
@@ -120,9 +124,11 @@ class Premium extends Model
|
||||
if ($message->hasAttachments() && ! $blocked) {
|
||||
$attachments = $message->getAttachments();
|
||||
$directory = './tmp/premium/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())) {
|
||||
@@ -132,13 +138,13 @@ class Premium extends Model
|
||||
$attachment->getDecodedContent()
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
\Illuminate\Support\Facades\Log::error($e->getMessage());
|
||||
Log::error($e->getMessage());
|
||||
}
|
||||
}
|
||||
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'][] = [
|
||||
@@ -172,12 +178,12 @@ class Premium extends Model
|
||||
return $response;
|
||||
}
|
||||
|
||||
public static function getMessages($email, $type = 'to', $deleted = []): array
|
||||
public static function getMessages(?string $email, $type = 'to', $deleted = []): array
|
||||
{
|
||||
return self::fetchMessages($email, $type, $deleted);
|
||||
}
|
||||
|
||||
public static function deleteMessage($id): void
|
||||
public static function deleteMessage(int $id): void
|
||||
{
|
||||
$connection = self::connectMailBox();
|
||||
$mailbox = $connection->getMailbox('INBOX');
|
||||
@@ -189,18 +195,16 @@ class Premium extends Model
|
||||
{
|
||||
if (Cookie::has('p_email')) {
|
||||
return Cookie::get('p_email');
|
||||
} else {
|
||||
return $generate ? self::generateRandomEmail() : null;
|
||||
}
|
||||
return $generate ? self::generateRandomEmail() : null;
|
||||
}
|
||||
|
||||
public static function getEmails()
|
||||
{
|
||||
if (Cookie::has('p_emails')) {
|
||||
return unserialize(Cookie::get('p_emails'));
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function setEmail($email): void
|
||||
@@ -213,7 +217,7 @@ class Premium extends Model
|
||||
|
||||
public static function setEmailP($email): void
|
||||
{
|
||||
$usageLogs = UsageLog::where(['user_id' => auth()->user()->id])->first();
|
||||
$usageLogs = UsageLog::query()->where(['user_id' => auth()->user()->id])->first();
|
||||
$emails = $usageLogs->emails_created_history;
|
||||
if (is_array($emails) && in_array($email, $emails)) {
|
||||
Cookie::queue('p_email', $email, 43800);
|
||||
@@ -238,9 +242,9 @@ class Premium extends Model
|
||||
|
||||
public static function createCustomEmailFull($email): string
|
||||
{
|
||||
$data = explode('@', $email);
|
||||
$data = explode('@', (string) $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) {
|
||||
if (strlen($username) < json_decode((string) config('app.settings.configuration_settings'))->custom_username_length_min || strlen($username) > json_decode((string) config('app.settings.configuration_settings'))->custom_username_length_max) {
|
||||
$zemail = new Premium;
|
||||
$username = $zemail->generateRandomUsername();
|
||||
}
|
||||
@@ -249,11 +253,11 @@ class Premium extends Model
|
||||
return self::createCustomEmail($username, $domain);
|
||||
}
|
||||
|
||||
public static function createCustomEmail($username, $domain): string
|
||||
public static function createCustomEmail($username, string $domain): string
|
||||
{
|
||||
$username = preg_replace('/[^a-zA-Z0-9+.]/', '', strtolower($username));
|
||||
$username = preg_replace('/[^a-zA-Z0-9+.]/', '', strtolower((string) $username));
|
||||
|
||||
$settings = json_decode(config('app.settings.configuration_settings'), true);
|
||||
$settings = json_decode((string) config('app.settings.configuration_settings'), true);
|
||||
$forbidden_ids = $settings['forbidden_ids'] ?? [];
|
||||
$gmail_usernames = $settings['premium_gmailUsernames'] ?? [];
|
||||
$outlook_usernames = $settings['premium_outlookUsernames'] ?? [];
|
||||
@@ -267,7 +271,7 @@ class Premium extends Model
|
||||
return self::generateRandomGmail(true);
|
||||
}
|
||||
|
||||
if ($username === '' && in_array($domain, ['outlook.com'])) {
|
||||
if ($username === '' && $domain === 'outlook.com') {
|
||||
return self::generateRandomOutlook(true);
|
||||
}
|
||||
|
||||
@@ -277,9 +281,9 @@ class Premium extends Model
|
||||
return $zemail->generateRandomUsername().'@'.$domain;
|
||||
}
|
||||
|
||||
if (in_array($domain, ['outlook.com'])) {
|
||||
if (str_contains($username, '+')) {
|
||||
[$check_username, $post_username] = explode('+', $username, 2);
|
||||
if ($domain === 'outlook.com') {
|
||||
if (str_contains((string) $username, '+')) {
|
||||
[$check_username, $post_username] = explode('+', (string) $username, 2);
|
||||
|
||||
if (in_array($check_username, $outlook_usernames)) {
|
||||
$email = $username.'@'.$domain;
|
||||
@@ -295,8 +299,8 @@ class Premium extends Model
|
||||
}
|
||||
|
||||
if (in_array($domain, ['gmail.com', 'googlemail.com'])) {
|
||||
if (str_contains($username, '+')) {
|
||||
[$check_username, $post_username] = explode('+', $username, 2);
|
||||
if (str_contains((string) $username, '+')) {
|
||||
[$check_username, $post_username] = explode('+', (string) $username, 2);
|
||||
|
||||
if (in_array($check_username, $gmail_usernames)) {
|
||||
$email = $username.'@'.$domain;
|
||||
@@ -304,7 +308,7 @@ class Premium extends Model
|
||||
$email = $zemail->getRandomGmailUser().'+'.$post_username.'@'.$domain;
|
||||
}
|
||||
|
||||
} elseif (str_contains($username, '.')) {
|
||||
} elseif (str_contains((string) $username, '.')) {
|
||||
$check_username = str_replace('.', '', $username);
|
||||
|
||||
if (in_array($check_username, $gmail_usernames)) {
|
||||
@@ -341,14 +345,14 @@ class Premium extends Model
|
||||
$domain = $zemail->getRandomDomain();
|
||||
if ($domain == 'gmail.com') {
|
||||
$rd = mt_rand(0, 1);
|
||||
if ($rd == 0) {
|
||||
if ($rd === 0) {
|
||||
$email = $zemail->generateRandomGmail(false);
|
||||
} else {
|
||||
$email = $zemail->getRandomGmailUser().'+'.$zemail->generateRandomUsername().'@gmail.com';
|
||||
}
|
||||
} elseif ($domain == 'googlemail.com') {
|
||||
$rd = mt_rand(0, 1);
|
||||
if ($rd == 0) {
|
||||
if ($rd === 0) {
|
||||
$email = $zemail->generateRandomGmail(false);
|
||||
} else {
|
||||
$email = $zemail->getRandomGmailUser().'+'.$zemail->generateRandomUsername().'@googlemail.com';
|
||||
@@ -369,11 +373,11 @@ class Premium extends Model
|
||||
{
|
||||
$zemail = new Premium;
|
||||
$uname = $zemail->getRandomGmailUser();
|
||||
$uname_len = strlen($uname);
|
||||
$uname_len = strlen((string) $uname);
|
||||
$len_power = $uname_len - 1;
|
||||
$combination = pow(2, $len_power);
|
||||
$rand_comb = mt_rand(1, $combination);
|
||||
$formatted = implode(' ', str_split($uname));
|
||||
$combination = 2 ** $len_power;
|
||||
mt_rand(1, $combination);
|
||||
$formatted = implode(' ', str_split((string) $uname));
|
||||
$uname_exp = explode(' ', $formatted);
|
||||
|
||||
$bin = intval('');
|
||||
@@ -385,7 +389,7 @@ class Premium extends Model
|
||||
$email = '';
|
||||
for ($i = 0; $i < $len_power; $i++) {
|
||||
$email .= $uname_exp[$i];
|
||||
if ($bin[$i]) {
|
||||
if ($bin[$i] !== '' && $bin[$i] !== '0') {
|
||||
$email .= '.';
|
||||
}
|
||||
}
|
||||
@@ -414,7 +418,7 @@ class Premium extends Model
|
||||
return $email;
|
||||
}
|
||||
|
||||
private static function storeEmail($email): void
|
||||
private static function storeEmail(string $email): void
|
||||
{
|
||||
Log::create([
|
||||
'user_id' => auth()->user()->id,
|
||||
@@ -447,8 +451,8 @@ class Premium extends Model
|
||||
|
||||
private function generateRandomUsername(): string
|
||||
{
|
||||
$start = json_decode(config('app.settings.configuration_settings'))->random_username_length_min ?? 0;
|
||||
$end = json_decode(config('app.settings.configuration_settings'))->random_username_length_max ?? 0;
|
||||
$start = json_decode((string) config('app.settings.configuration_settings'))->random_username_length_min ?? 0;
|
||||
$end = json_decode((string) config('app.settings.configuration_settings'))->random_username_length_max ?? 0;
|
||||
if ($start == 0 && $end == 0) {
|
||||
return $this->generatePronounceableWord();
|
||||
}
|
||||
@@ -458,33 +462,33 @@ class Premium extends Model
|
||||
|
||||
protected function generatedRandomBetweenLength($start, $end): string
|
||||
{
|
||||
$length = rand($start, $end);
|
||||
$length = random_int($start, $end);
|
||||
|
||||
return $this->generateRandomString($length);
|
||||
}
|
||||
|
||||
private function getRandomDomain()
|
||||
{
|
||||
$domains = json_decode(config('app.settings.configuration_settings'))->premium_domains ?? [];
|
||||
$domains = json_decode((string) config('app.settings.configuration_settings'))->premium_domains ?? [];
|
||||
$count = count($domains);
|
||||
|
||||
return $count > 0 ? $domains[rand(1, $count) - 1] : '';
|
||||
return $count > 0 ? $domains[random_int(1, $count) - 1] : '';
|
||||
}
|
||||
|
||||
private function getRandomGmailUser()
|
||||
{
|
||||
$gmailusername = json_decode(config('app.settings.configuration_settings'))->premium_gmailUsernames ?? [];
|
||||
$gmailusername = json_decode((string) config('app.settings.configuration_settings'))->premium_gmailUsernames ?? [];
|
||||
$count = count($gmailusername);
|
||||
|
||||
return $count > 0 ? $gmailusername[rand(1, $count) - 1] : '';
|
||||
return $count > 0 ? $gmailusername[random_int(1, $count) - 1] : '';
|
||||
}
|
||||
|
||||
private function getRandomOutlookUser()
|
||||
{
|
||||
$outlook_username = json_decode(config('app.settings.configuration_settings'))->premium_outlookUsernames ?? [];
|
||||
$outlook_username = json_decode((string) config('app.settings.configuration_settings'))->premium_outlookUsernames ?? [];
|
||||
$count = count($outlook_username);
|
||||
|
||||
return $count > 0 ? $outlook_username[rand(1, $count) - 1] : '';
|
||||
return $count > 0 ? $outlook_username[random_int(1, $count) - 1] : '';
|
||||
}
|
||||
|
||||
private function generatePronounceableWord(): string
|
||||
@@ -494,21 +498,21 @@ class Premium extends Model
|
||||
$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)];
|
||||
$random .= $c[random_int(0, strlen($c) - 1)];
|
||||
$random .= $v[random_int(0, strlen($v) - 1)];
|
||||
$random .= $a[random_int(0, strlen($a) - 1)];
|
||||
}
|
||||
|
||||
return $random;
|
||||
}
|
||||
|
||||
private function generateRandomString($length = 10): string
|
||||
private function generateRandomString(int $length = 10): string
|
||||
{
|
||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||
$charactersLength = strlen($characters);
|
||||
$randomString = '';
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
||||
$randomString .= $characters[random_int(0, $charactersLength - 1)];
|
||||
}
|
||||
|
||||
return $randomString;
|
||||
@@ -524,10 +528,7 @@ class Premium extends Model
|
||||
return;
|
||||
}
|
||||
|
||||
$usageLog = UsageLog::firstOrCreate(
|
||||
['user_id' => $user->id],
|
||||
['ip_address' => request()->ip()]
|
||||
);
|
||||
$usageLog = UsageLog::query()->firstOrCreate(['user_id' => $user->id], ['ip_address' => request()->ip()]);
|
||||
|
||||
$usageLog->increment('emails_created_count', $count);
|
||||
}
|
||||
@@ -539,10 +540,7 @@ class Premium extends Model
|
||||
return;
|
||||
}
|
||||
|
||||
$usageLog = UsageLog::firstOrCreate(
|
||||
['user_id' => $user->id],
|
||||
['ip_address' => request()->ip()]
|
||||
);
|
||||
$usageLog = UsageLog::query()->firstOrCreate(['user_id' => $user->id], ['ip_address' => request()->ip()]);
|
||||
|
||||
$usageLog->increment('emails_received_count', $count);
|
||||
}
|
||||
@@ -556,10 +554,7 @@ class Premium extends Model
|
||||
}
|
||||
|
||||
$ip = request()->ip();
|
||||
$usageLog = UsageLog::firstOrCreate(
|
||||
['user_id' => $user->id],
|
||||
['ip_address' => $ip]
|
||||
);
|
||||
$usageLog = UsageLog::query()->firstOrCreate(['user_id' => $user->id], ['ip_address' => $ip]);
|
||||
|
||||
$history = $usageLog->emails_created_history ?? [];
|
||||
if (! in_array($email, $history)) {
|
||||
|
||||
Reference in New Issue
Block a user