|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Validator\HasValidationErrorMessage; |
|
8
|
|
|
use Yiisoft\Validator\Result; |
|
9
|
|
|
use Yiisoft\Validator\Rule; |
|
10
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
11
|
|
|
|
|
12
|
|
|
use function is_string; |
|
13
|
|
|
use function strlen; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* UrlValidator validates that the attribute value is a valid HTTP or HTTPS URL. |
|
17
|
|
|
* |
|
18
|
|
|
* Note that this validator only checks if the URL scheme and host part are correct. |
|
19
|
|
|
* It does not check the remaining parts of a URL. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Url extends Rule |
|
22
|
|
|
{ |
|
23
|
|
|
use HasValidationErrorMessage; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string the regular expression used to validateValue the attribute value. |
|
27
|
|
|
* The pattern may contain a `{schemes}` token that will be replaced |
|
28
|
|
|
* by a regular expression which represents the {@see schemes()}. |
|
29
|
|
|
*/ |
|
30
|
|
|
private string $pattern = '/^{schemes}:\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(?::\d{1,5})?(?:$|[?\/#])/i'; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var array list of URI schemes which should be considered valid. By default, http and https |
|
33
|
|
|
* are considered to be valid schemes. |
|
34
|
|
|
*/ |
|
35
|
|
|
private array $validSchemes = ['http', 'https']; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var bool whether validation process should take into account IDN (internationalized |
|
38
|
|
|
* domain names). Defaults to false meaning that validation of URLs containing IDN will always |
|
39
|
|
|
* fail. Note that in order to use IDN validation you have to install and enable `intl` PHP |
|
40
|
|
|
* extension, otherwise an exception would be thrown. |
|
41
|
|
|
*/ |
|
42
|
|
|
private bool $enableIDN = false; |
|
43
|
|
|
|
|
44
|
|
|
private string $message = 'This value is not a valid URL.'; |
|
45
|
|
|
|
|
46
|
9 |
|
protected function validateValue($value, ValidationContext $context = null): Result |
|
47
|
|
|
{ |
|
48
|
9 |
|
$result = new Result(); |
|
49
|
|
|
|
|
50
|
|
|
// make sure the length is limited to avoid DOS attacks |
|
51
|
9 |
|
if (is_string($value) && strlen($value) < 2000) { |
|
52
|
8 |
|
if ($this->enableIDN) { |
|
53
|
5 |
|
$value = $this->convertIdn($value); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
8 |
|
if (preg_match($this->getPattern(), $value)) { |
|
57
|
6 |
|
return $result; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
5 |
|
$result->addError($this->formatMessage($this->message)); |
|
62
|
|
|
|
|
63
|
5 |
|
return $result; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
5 |
|
private function idnToAscii($idn) |
|
67
|
|
|
{ |
|
68
|
5 |
|
return idn_to_ascii($idn, 0, INTL_IDNA_VARIANT_UTS46); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
5 |
|
private function convertIdn($value): string |
|
72
|
|
|
{ |
|
73
|
5 |
|
if (strpos($value, '://') === false) { |
|
74
|
3 |
|
return $this->idnToAscii($value); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
return preg_replace_callback( |
|
78
|
2 |
|
'/:\/\/([^\/]+)/', |
|
79
|
2 |
|
fn ($matches) => '://' . $this->idnToAscii($matches[1]), |
|
80
|
|
|
$value |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
8 |
|
private function getPattern(): string |
|
85
|
|
|
{ |
|
86
|
8 |
|
if (strpos($this->pattern, '{schemes}') !== false) { |
|
87
|
6 |
|
return str_replace('{schemes}', '(' . implode('|', $this->validSchemes) . ')', $this->pattern); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
return $this->pattern; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
public function pattern(string $pattern): self |
|
94
|
|
|
{ |
|
95
|
2 |
|
$new = clone $this; |
|
96
|
2 |
|
$new->pattern = $pattern; |
|
97
|
2 |
|
return $new; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
8 |
|
public function enableIDN(): self |
|
101
|
|
|
{ |
|
102
|
8 |
|
if (!function_exists('idn_to_ascii')) { |
|
103
|
1 |
|
throw new \RuntimeException('In order to use IDN validation intl extension must be installed and enabled.'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
7 |
|
$new = clone $this; |
|
107
|
7 |
|
$new->enableIDN = true; |
|
108
|
7 |
|
return $new; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
public function schemes(array $schemes): self |
|
112
|
|
|
{ |
|
113
|
1 |
|
$new = clone $this; |
|
114
|
1 |
|
$new->validSchemes = $schemes; |
|
115
|
1 |
|
return $new; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
6 |
|
public function getOptions(): array |
|
119
|
|
|
{ |
|
120
|
6 |
|
return array_merge( |
|
121
|
6 |
|
parent::getOptions(), |
|
122
|
|
|
[ |
|
123
|
6 |
|
'message' => $this->formatMessage($this->message), |
|
124
|
6 |
|
'enableIDN' => $this->enableIDN, |
|
125
|
6 |
|
'validSchemes' => $this->validSchemes, |
|
126
|
6 |
|
'pattern' => $this->pattern, |
|
127
|
|
|
], |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|