1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.8 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Validation\Traits; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Trait Resource |
19
|
|
|
* @package Quantum\Libraries\Validation\Rules |
20
|
|
|
*/ |
21
|
|
|
trait Resource |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Checks for valid URL or subdomain |
26
|
|
|
* @param string $field |
27
|
|
|
* @param string $value |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
|
|
protected function url(string $field, string $value): bool |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
return filter_var($value, FILTER_VALIDATE_URL) !== false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Checks to see if the url exists |
37
|
|
|
* @param string $field |
38
|
|
|
* @param string $value |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
protected function urlExists(string $field, string $value): bool |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
if (filter_var($value, FILTER_VALIDATE_URL) === false) { |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$host = parse_url(strtolower($value), PHP_URL_HOST); |
48
|
|
|
|
49
|
|
|
if (!$host) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (function_exists('checkdnsrr') && function_exists('idn_to_ascii')) { |
54
|
|
|
return checkdnsrr(idn_to_ascii($host, 0, INTL_IDNA_VARIANT_UTS46), 'A'); |
55
|
|
|
} else { |
56
|
|
|
return gethostbyname($host) !== $host; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Checks for valid IP address |
62
|
|
|
* @param string $field |
63
|
|
|
* @param string $value |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
protected function ip(string $field, string $value): bool |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return filter_var($value, FILTER_VALIDATE_IP) !== false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Checks for valid IPv4 address |
73
|
|
|
* @param string $field |
74
|
|
|
* @param string $value |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
protected function ipv4(string $field, string $value): bool |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Checks for valid IPv6 address |
84
|
|
|
* @param string $field |
85
|
|
|
* @param string $value |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
protected function ipv6(string $field, string $value): bool |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false; |
91
|
|
|
} |
92
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.