diff --git a/config/disposable-email.php b/config/disposable-email.php index e28aabe..c40e483 100644 --- a/config/disposable-email.php +++ b/config/disposable-email.php @@ -63,6 +63,19 @@ 'whitelist' => [], + /* + |-------------------------------------------------------------------------- + | Include Subdomains + |-------------------------------------------------------------------------- + | + | Determines whether subdomains should be validated based on the disposability + | status of their parent domains. Enabling this will treat any subdomain of + | a disposable domain as disposable too (e.g., 'temp.abc.com' if 'abc.com' is + | disposable). + | + */ + 'include_subdomains' => false, + /* |-------------------------------------------------------------------------- | Cache Configuration diff --git a/src/DisposableDomains.php b/src/DisposableDomains.php index fd4c9a0..1399521 100644 --- a/src/DisposableDomains.php +++ b/src/DisposableDomains.php @@ -43,6 +43,13 @@ class DisposableDomains */ protected $cacheKey; + /** + * Whether to include subdomains. + * + * @var bool + */ + protected $includeSubdomains = false; + /** * Disposable constructor. */ @@ -164,6 +171,12 @@ public function flushStorage() public function isDisposable($email) { if ($domain = Str::lower(Arr::get(explode('@', $email, 2), 1))) { + if($this->includeSubdomains) { + $domain_parts = explode('.', $domain); + if(count($domain_parts) > 2) { + $domain = $domain_parts[count($domain_parts) - 2] . '.' . $domain_parts[count($domain_parts) - 1]; + } + } return in_array($domain, $this->domains); } @@ -225,6 +238,28 @@ public function setWhitelist(array $whitelist) return $this; } + /** + * Get whether to include subdomains. + * + * @return bool + */ + public function getIncludeSubdomains() + { + return $this->includeSubdomains; + } + + /** + * Set whether to include subdomains. + * + * @return $this + */ + public function setIncludeSubdomains(bool $includeSubdomains) + { + $this->includeSubdomains = $includeSubdomains; + + return $this; + } + /** * Get the storage path. * diff --git a/src/DisposableEmailServiceProvider.php b/src/DisposableEmailServiceProvider.php index 6ddd96c..395f09a 100644 --- a/src/DisposableEmailServiceProvider.php +++ b/src/DisposableEmailServiceProvider.php @@ -54,6 +54,7 @@ public function register() $instance = new DisposableDomains($cache ?? null); + $instance->setIncludeSubdomains($app['config']['disposable-email.include_subdomains']); $instance->setStoragePath($app['config']['disposable-email.storage']); $instance->setCacheKey($app['config']['disposable-email.cache.key']); $instance->setWhitelist($app['config']['disposable-email.whitelist']); diff --git a/tests/DisposableDomainsTest.php b/tests/DisposableDomainsTest.php index defa3ef..5fab878 100644 --- a/tests/DisposableDomainsTest.php +++ b/tests/DisposableDomainsTest.php @@ -36,6 +36,23 @@ public function it_can_set_storage_path() $this->assertEquals('foo', $this->disposable()->getStoragePath()); } + #[Test] + public function it_can_get_include_subdomains() + { + $this->assertEquals( + $this->app['config']['disposable-email.include_subdomains'], + $this->disposable()->getIncludeSubdomains() + ); + } + + #[Test] + public function it_can_set_include_subdomains() + { + $this->disposable()->setIncludeSubdomains(true); + + $this->assertEquals(true, $this->disposable()->getIncludeSubdomains()); + } + #[Test] public function it_can_get_cache_key() { @@ -162,6 +179,14 @@ public function it_checks_the_full_email_domain() $this->assertTrue($this->disposable()->isNotDisposable('example@isnotdisposable.mailinator.com')); } + #[Test] + public function it_can_check_subdomains() + { + $this->disposable()->setIncludeSubdomains(true); + + $this->assertTrue($this->disposable()->isDisposable('example@isnotdisposable.mailinator.com')); + } + #[Test] public function it_can_exclude_whitelisted_domains() {