DomainService.php 848 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\Cache;
  4. class DomainService
  5. {
  6. const CACHE_KEY = 'pf:services:domains:';
  7. public static function hasValidDns($domain)
  8. {
  9. if (! $domain || ! strlen($domain) || strpos($domain, '.') == -1) {
  10. return false;
  11. }
  12. if (config('security.url.trusted_domains')) {
  13. if (in_array($domain, explode(',', config('security.url.trusted_domains')))) {
  14. return true;
  15. }
  16. }
  17. $valid = filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
  18. if (! $valid) {
  19. return false;
  20. }
  21. return Cache::remember(self::CACHE_KEY.'valid-dns:'.$domain, 1800, function () use ($domain) {
  22. return count(dns_get_record($domain, DNS_A | DNS_AAAA)) > 0;
  23. });
  24. }
  25. }