|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Swaggest\JsonSchema\Constraint\Format; |
|
4
|
|
|
|
|
5
|
|
|
class Uri |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @see http://stackoverflow.com/a/1420225 |
|
9
|
|
|
*/ |
|
10
|
|
|
const HOSTNAME_REGEX = '/^ |
|
11
|
|
|
(?=.{1,255}$) |
|
12
|
|
|
[0-9a-z] |
|
13
|
|
|
(([0-9a-z]|-){0,61}[0-9a-z])? |
|
14
|
|
|
(\.[0-9a-z](?:(?:[0-9a-z]|-){0,61}[0-9a-z])?)* |
|
15
|
|
|
\.? |
|
16
|
|
|
$/ix'; |
|
17
|
|
|
|
|
18
|
|
|
const IS_URI_REFERENCE = 1; |
|
19
|
|
|
const IS_URI_TEMPLATE = 2; |
|
20
|
|
|
const IS_SCHEME_REQUIRED = 8; |
|
21
|
|
|
|
|
22
|
207 |
|
public static function validationError($data, $options = 0) |
|
23
|
|
|
{ |
|
24
|
207 |
|
if ($options === Uri::IS_URI_TEMPLATE) { |
|
25
|
10 |
|
$opened = false; |
|
26
|
10 |
|
for ($i = 0; $i < strlen($data); ++$i) { |
|
27
|
10 |
|
if ($data[$i] === '{') { |
|
28
|
8 |
|
if ($opened) { |
|
29
|
|
|
return 'Invalid uri-template: unexpected "{"'; |
|
30
|
|
|
} else { |
|
31
|
8 |
|
$opened = true; |
|
32
|
|
|
} |
|
33
|
10 |
|
} elseif ($data[$i] === '}') { |
|
34
|
8 |
|
if ($opened) { |
|
35
|
8 |
|
$opened = false; |
|
36
|
|
|
} else { |
|
37
|
|
|
return 'Invalid uri-template: unexpected "}"'; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
10 |
|
if ($opened) { |
|
42
|
3 |
|
return 'Invalid uri-template: unexpected end of string'; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
204 |
|
$uri = parse_url($data); |
|
47
|
204 |
|
if (!$uri) { |
|
|
|
|
|
|
48
|
|
|
return 'Malformed URI'; |
|
49
|
|
|
} |
|
50
|
204 |
|
if (($options & self::IS_SCHEME_REQUIRED) && (!isset($uri['scheme']) || $uri['scheme'] === '')) { |
|
51
|
38 |
|
return 'Missing scheme in URI'; |
|
52
|
|
|
} |
|
53
|
185 |
|
if (isset($uri['host'])) { |
|
54
|
155 |
|
$host = $uri['host']; |
|
55
|
155 |
|
if (!preg_match(self::HOSTNAME_REGEX, $host)) { |
|
56
|
|
|
// stripping [ ] |
|
57
|
8 |
|
if ($host[0] === '[' && $host[strlen($host) - 1] === ']') { |
|
58
|
4 |
|
$host = substr($host, 1, -1); |
|
59
|
|
|
} |
|
60
|
8 |
|
if (!filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
61
|
4 |
|
return 'Malformed host in URI: ' . $host; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
181 |
|
if (isset($uri['path'])) { |
|
67
|
166 |
|
if (strpos($uri['path'], '\\') !== false) { |
|
68
|
3 |
|
return 'Invalid path: unescaped backslash'; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
178 |
|
if (isset($uri['fragment'])) { |
|
73
|
21 |
|
if (strpos($uri['fragment'], '\\') !== false) { |
|
74
|
3 |
|
return 'Invalid fragment: unescaped backslash'; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
175 |
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.