diff --git a/lib/Service/BlackListService.php b/lib/Service/BlackListService.php index 020c58fac70e3e4112cb8aab125e17070d6d3f96..f9a4a4366833578267abb2d2f92c620a3cfc87e1 100644 --- a/lib/Service/BlackListService.php +++ b/lib/Service/BlackListService.php @@ -17,6 +17,7 @@ class BlackListService { private $appName; private const BLACKLISTED_DOMAINS_FILE_NAME = 'blacklisted_domains.json'; private const BLACKLISTED_DOMAINS_URL = 'https://raw.githubusercontent.com/disposable/disposable-email-domains/master/domains.json'; + private const POPULAR_DOMAINS_FILE_NAME = 'domains.json'; public function __construct(string $appName, ILogger $logger, IFactory $l10nFactory, IAppData $appData) { @@ -43,6 +44,7 @@ class BlackListService { $emailDomain = strtolower(end($emailParts)); return in_array($emailDomain, $blacklistedDomains); } + /** * Update the blacklisted domains data by fetching it from a URL and saving it locally. * @@ -120,4 +122,64 @@ class BlackListService { } return true; } + + /** + * Check if an domain is popular domain a JSON list of popular domains. + * + * @param string $email The email address to check. + * @return bool True if the email domain is popular, false otherwise. + */ + public function isPopularDomain(string $email): bool { + if (!$this->ensureDocumentsFolder()) { + return false; + } + $popularlistedDomains = $this->getPopularlistedDomainData(); + if (empty($popularlistedDomains)) { + return false; + } + $emailParts = explode('@', $email); + $emailDomain = strtolower(end($emailParts)); + return in_array($emailDomain, $popularlistedDomains); + } + + /** + * Retrieve the Popular domain file path + * + * @return ISimpleFile + */ + private function getPopularDomainsFile(): ISimpleFile { + try { + $currentFolder = $this->appData->getFolder('/'); + } catch (NotFoundException $e) { + $currentFolder = $this->appData->newFolder('/'); + } + $filename = self::POPULAR_DOMAINS_FILE_NAME; + if ($currentFolder->fileExists($filename)) { + return $currentFolder->getFile($filename); + } + return $currentFolder->newFile($filename); + } + + /** + * Retrieve the Popular domain data. + * + * @return array The array of popular domains. + */ + public function getPopularlistedDomainData(): array { + $document = self::POPULAR_DOMAINS_FILE_NAME; + $file = $this->getPopularDomainsFile(); + try { + $popularlistedDomainsInJson = $file->getContent(); + if (empty($popularlistedDomainsInJson)) { + return []; + } + return json_decode($popularlistedDomainsInJson, true, 512, JSON_THROW_ON_ERROR); + } catch (NotFoundException $e) { + $this->logger->warning('Popular domains file ' . $document . ' not found!'); + return []; + } catch (\Throwable $e) { + $this->logger->warning('Error decoding Popular domains file ' . $document . ': ' . $e->getMessage()); + return []; + } + } } diff --git a/lib/Service/RecoveryEmailService.php b/lib/Service/RecoveryEmailService.php index 0fe18447e926ecdf79a2c14559dfea77d0ab4826..ded7ae2a9afa2a761ceb6bbf1aba6556f1b15468 100644 --- a/lib/Service/RecoveryEmailService.php +++ b/lib/Service/RecoveryEmailService.php @@ -135,6 +135,10 @@ class RecoveryEmailService { $this->logger->info("User ID $username's requested recovery email address domain is blacklisted. Please provide another recovery address."); throw new BlacklistedEmailException($l->t('The domain of this email address is blacklisted. Please provide another recovery address.')); } + if ($this->blackListService->isNotPopularDomain($recoveryEmail)) { + $this->logger->info("User ID $username's requested recovery email address domain is popular domain"); + } + echo "test"; } return true; }