1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelMixins\Rules; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Validation\Rule; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Facades\Http; |
8
|
|
|
use Pdp\Rules; |
9
|
|
|
|
10
|
|
|
class HostOrSubdomain implements Rule |
11
|
|
|
{ |
12
|
|
|
private array $hosts; |
13
|
|
|
|
14
|
|
|
private $publicSuffixList; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Create a new rule instance. |
18
|
|
|
* |
19
|
|
|
* @param array|string $host |
20
|
|
|
*/ |
21
|
|
|
public function __construct($host, Rules $publicSuffixList = null) |
22
|
|
|
{ |
23
|
|
|
$this->hosts = Arr::wrap($host); |
24
|
|
|
|
25
|
|
|
$this->publicSuffixList = $publicSuffixList ?: Rules::fromString( |
26
|
|
|
Http::get('https://publicsuffix.org/list/public_suffix_list.dat') |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function make($host): self |
31
|
|
|
{ |
32
|
|
|
return new static($host); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Determine if the validation rule passes. |
37
|
|
|
* |
38
|
|
|
* @param string $attribute |
39
|
|
|
* @param mixed $value |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function passes($attribute, $value) |
43
|
|
|
{ |
44
|
|
|
if (!parse_url($value, PHP_URL_SCHEME)) { |
45
|
|
|
$value = "https://{$value}"; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$host = parse_url($value, PHP_URL_HOST); |
49
|
|
|
|
50
|
|
|
if (!$host) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$resolvedDomain = $this->publicSuffixList->resolve($host); |
55
|
|
|
|
56
|
|
|
if ($host !== $resolvedDomain->domain()->toString()) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return in_array($resolvedDomain->registrableDomain()->toString(), $this->hosts); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the validation error message. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function message() |
69
|
|
|
{ |
70
|
|
|
return __('validation.host'); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|