chore: code refactor via rector
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Ddeboer\Imap\ConnectionInterface;
|
||||
use Ddeboer\Imap\Search\Email\To;
|
||||
use Ddeboer\Imap\Server;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Cookie;
|
||||
@@ -12,23 +12,21 @@ use function str_replace;
|
||||
|
||||
class ZEmail extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasFactory;
|
||||
public static function connectMailBox($imap = null): ConnectionInterface
|
||||
{
|
||||
if ($imap === null) {
|
||||
$imap = json_decode(config('app.settings.imap_settings'), true);
|
||||
$imap = json_decode((string) config('app.settings.imap_settings'), true);
|
||||
}
|
||||
$flags = $imap['protocol'].'/'.$imap['encryption'];
|
||||
if ($imap['validate_cert']) {
|
||||
$flags = $flags.'/validate-cert';
|
||||
} else {
|
||||
$flags = $flags.'/novalidate-cert';
|
||||
}
|
||||
$flags = $imap['validate_cert'] ? $flags.'/validate-cert' : $flags.'/novalidate-cert';
|
||||
$server = new Server($imap['host'], $imap['port'], $flags);
|
||||
|
||||
return $server->authenticate($imap['username'], $imap['password']);
|
||||
}
|
||||
|
||||
public static function getMessages($email, $type = 'to', $deleted = []): array
|
||||
public static function getMessages(string $email, $type = 'to', $deleted = []): array
|
||||
{
|
||||
if (config('app.beta_feature')) {
|
||||
return Message::getMessages($email);
|
||||
@@ -39,15 +37,14 @@ class ZEmail extends Model
|
||||
if (config('app.fetch_from_db')) {
|
||||
if (Email::mailToDBStatus()) {
|
||||
return Email::parseEmail($email, $deleted);
|
||||
} else {
|
||||
return Message::fetchMessages($email, $type, $deleted);
|
||||
}
|
||||
return Message::fetchMessages($email, $type, $deleted);
|
||||
}
|
||||
|
||||
return Message::fetchMessages($email, $type, $deleted);
|
||||
}
|
||||
|
||||
public static function deleteMessage($id): void
|
||||
public static function deleteMessage(int $id): void
|
||||
{
|
||||
$connection = ZEmail::connectMailBox();
|
||||
$mailbox = $connection->getMailbox('INBOX');
|
||||
@@ -59,18 +56,16 @@ class ZEmail extends Model
|
||||
{
|
||||
if (Cookie::has('email')) {
|
||||
return Cookie::get('email');
|
||||
} else {
|
||||
return $generate ? ZEmail::generateRandomEmail() : null;
|
||||
}
|
||||
return $generate ? ZEmail::generateRandomEmail() : null;
|
||||
}
|
||||
|
||||
public static function getEmails()
|
||||
{
|
||||
if (Cookie::has('emails')) {
|
||||
return unserialize(Cookie::get('emails'));
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
public static function setEmail($email): void
|
||||
@@ -99,9 +94,9 @@ class ZEmail 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 ZEmail;
|
||||
$username = $zemail->generateRandomUsername();
|
||||
}
|
||||
@@ -110,11 +105,11 @@ class ZEmail extends Model
|
||||
return ZEmail::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['gmailUsernames'] ?? [];
|
||||
$outlook_usernames = $settings['outlookUsernames'] ?? [];
|
||||
@@ -124,7 +119,7 @@ class ZEmail extends Model
|
||||
$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) {
|
||||
if (strlen((string) $username) < $min_length || strlen((string) $username) > $max_length) {
|
||||
$zemail = new ZEmail;
|
||||
$username = $zemail->generateRandomUsername();
|
||||
}
|
||||
@@ -137,7 +132,7 @@ class ZEmail extends Model
|
||||
return ZEmail::generateRandomGmail(true);
|
||||
}
|
||||
|
||||
if ($username === '' && in_array($domain, ['outlook.com'])) {
|
||||
if ($username === '' && $domain === 'outlook.com') {
|
||||
return ZEmail::generateRandomOutlook(true);
|
||||
}
|
||||
|
||||
@@ -147,9 +142,9 @@ class ZEmail 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;
|
||||
@@ -165,8 +160,8 @@ class ZEmail 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;
|
||||
@@ -174,7 +169,7 @@ class ZEmail 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)) {
|
||||
@@ -211,14 +206,14 @@ class ZEmail extends Model
|
||||
$domain = $zemail->getRandomDomain();
|
||||
if ($domain == 'gmail.com') {
|
||||
$rd = mt_rand(0, 1);
|
||||
if ($rd == 0) {
|
||||
if ($rd === 0) {
|
||||
$email = $zemail->generateRandomGmail();
|
||||
} 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();
|
||||
} else {
|
||||
$email = $zemail->getRandomGmailUser().'+'.$zemail->generateRandomUsername().'@googlemail.com';
|
||||
@@ -239,11 +234,11 @@ class ZEmail extends Model
|
||||
{
|
||||
$zemail = new ZEmail;
|
||||
$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('');
|
||||
@@ -255,7 +250,7 @@ class ZEmail extends Model
|
||||
$email = '';
|
||||
for ($i = 0; $i < $len_power; $i++) {
|
||||
$email .= $uname_exp[$i];
|
||||
if ($bin[$i]) {
|
||||
if ($bin[$i] !== '' && $bin[$i] !== '0') {
|
||||
$email .= '.';
|
||||
}
|
||||
}
|
||||
@@ -284,9 +279,9 @@ class ZEmail extends Model
|
||||
return $email;
|
||||
}
|
||||
|
||||
private static function storeEmail($email): void
|
||||
private static function storeEmail(string $email): void
|
||||
{
|
||||
Log::create([
|
||||
Log::query()->create([
|
||||
'ip' => request()->ip(),
|
||||
'email' => $email,
|
||||
]);
|
||||
@@ -314,8 +309,8 @@ class ZEmail 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();
|
||||
}
|
||||
@@ -325,34 +320,34 @@ class ZEmail 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'))->domains ?? [];
|
||||
$domains = json_decode((string) config('app.settings.configuration_settings'))->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'))->gmailUsernames ?? [];
|
||||
$gmailusername = json_decode((string) config('app.settings.configuration_settings'))->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'))->outlookUsernames ?? [];
|
||||
$outlook_username = json_decode((string) config('app.settings.configuration_settings'))->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
|
||||
@@ -362,21 +357,21 @@ class ZEmail 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;
|
||||
|
||||
Reference in New Issue
Block a user