Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Rules/Fqdn.php
7432 views
1
<?php
2
3
namespace Pterodactyl\Rules;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Contracts\Validation\Rule;
7
use Illuminate\Contracts\Validation\DataAwareRule;
8
9
class Fqdn implements Rule, DataAwareRule
10
{
11
protected array $data = [];
12
protected string $message = '';
13
protected ?string $schemeField = null;
14
15
/**
16
* @param array $data
17
*/
18
public function setData($data): self
19
{
20
$this->data = $data;
21
22
return $this;
23
}
24
25
/**
26
* Validates that the value provided resolves to an IP address. If a scheme is
27
* specified when this rule is created additional checks will be applied.
28
*
29
* @param string $attribute
30
*/
31
public function passes($attribute, $value): bool
32
{
33
if (filter_var($value, FILTER_VALIDATE_IP)) {
34
// Check if the scheme is set to HTTPS.
35
//
36
// Unless someone owns their IP blocks and decides to pay who knows how much for a
37
// custom SSL cert, IPs will not be able to use HTTPS. This should prevent most
38
// home users from making this mistake and wondering why their node is not working.
39
if ($this->schemeField && Arr::get($this->data, $this->schemeField) === 'https') {
40
$this->message = 'The :attribute must not be an IP address when HTTPS is enabled.';
41
42
return false;
43
}
44
45
return true;
46
}
47
48
// Lookup A and AAAA DNS records for the FQDN. Note, this function will also resolve CNAMEs
49
// for us automatically, there is no need to manually resolve them here.
50
//
51
// The error suppression is intentional, see https://bugs.php.net/bug.php?id=73149
52
$records = @dns_get_record($value, DNS_A + DNS_AAAA);
53
// If no records were returned fall back to trying to resolve the value using the hosts DNS
54
// resolution. This will not work for IPv6 which is why we prefer to use `dns_get_record`
55
// first.
56
if (!empty($records) || filter_var(gethostbyname($value), FILTER_VALIDATE_IP)) {
57
return true;
58
}
59
60
$this->message = 'The :attribute could not be resolved to a valid IP address.';
61
62
return false;
63
}
64
65
public function message(): string
66
{
67
return $this->message;
68
}
69
70
/**
71
* Returns a new instance of the rule with a defined scheme set.
72
*/
73
public static function make(?string $schemeField = null): self
74
{
75
return tap(new static(), function ($fqdn) use ($schemeField) {
76
$fqdn->schemeField = $schemeField;
77
});
78
}
79
}
80
81