|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the PHPMongo package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Dmytro Sokil <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sokil\Mongo\Validator; |
|
13
|
|
|
|
|
14
|
|
|
use Sokil\Mongo\Structure; |
|
15
|
|
|
|
|
16
|
|
|
class UrlValidator extends \Sokil\Mongo\Validator |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
public function validateField(Structure $document, $fieldName, array $params) |
|
20
|
|
|
{ |
|
21
|
|
|
$value = $document->get($fieldName); |
|
22
|
|
|
|
|
23
|
|
|
// check only if set |
|
24
|
|
|
if (!$value) { |
|
25
|
|
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
// check if url valid |
|
29
|
|
|
$isValidUrl = (bool) filter_var($value, FILTER_VALIDATE_URL); |
|
30
|
|
|
if (!$isValidUrl) { |
|
31
|
|
|
if (!isset($params['message'])) { |
|
32
|
|
|
$params['message'] = 'Value of field "' |
|
33
|
|
|
. $fieldName |
|
34
|
|
|
. '" is not valid url in model ' |
|
35
|
|
|
. get_called_class(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$document->addError($fieldName, $this->getName(), $params['message']); |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// ping not required - so url is valid |
|
43
|
|
|
if (empty($params['ping'])) { |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// ping required |
|
48
|
|
|
$dnsRecordExists = dns_get_record(parse_url($value, PHP_URL_HOST)); |
|
49
|
|
|
|
|
50
|
|
|
// network not allowed |
|
51
|
|
|
if ($dnsRecordExists === false) { |
|
52
|
|
|
throw new \RuntimeException('Error getting DNS record to validated url'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// empty array - host not found |
|
56
|
|
|
if (is_array($dnsRecordExists) && empty($dnsRecordExists)) { |
|
57
|
|
|
if (!isset($params['message'])) { |
|
58
|
|
|
$params['message'] = 'Value of field "' |
|
59
|
|
|
. $fieldName |
|
60
|
|
|
. '" is valid url but host is unreachable in model ' |
|
61
|
|
|
. get_called_class(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$document->addError($fieldName, $this->getName(), $params['message']); |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($this->isUrlAccessible($value)) { |
|
69
|
|
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if (!isset($params['message'])) { |
|
73
|
|
|
$params['message'] = 'Value of field "' |
|
74
|
|
|
. $fieldName |
|
75
|
|
|
. '" is valid url but page not found ' |
|
76
|
|
|
. get_called_class(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$document->addError($fieldName, $this->getName(), $params['message']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function isUrlAccessible($url) |
|
83
|
|
|
{ |
|
84
|
|
|
$headers = get_headers($url, true); |
|
85
|
|
|
|
|
86
|
|
|
$isAccessible = false; |
|
87
|
|
|
$i = 0; |
|
88
|
|
|
while (true) { |
|
89
|
|
|
if (false !== strpos($headers[$i], '200')) { |
|
90
|
|
|
$isAccessible = true; |
|
91
|
|
|
break; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$i++; |
|
95
|
|
|
if (!isset($headers[$i])) { |
|
96
|
|
|
break; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// page is accessible |
|
101
|
|
|
return $isAccessible; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|