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