|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: vpoturaev |
|
5
|
|
|
* Date: 1/19/18 |
|
6
|
|
|
* Time: 15:05 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Swaggest\JsonSchema\Constraint\Format; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Swaggest\JsonSchema\Constraint\Format; |
|
13
|
|
|
|
|
14
|
|
|
class Uri |
|
15
|
|
|
{ |
|
16
|
|
|
const IS_URI_REFERENCE = 1; |
|
17
|
|
|
const IS_URI_TEMPLATE = 2; |
|
18
|
|
|
const IS_SCHEME_REQUIRED = 8; |
|
19
|
|
|
|
|
20
|
|
|
public static function validationError($data, $options = 0) |
|
21
|
|
|
{ |
|
22
|
|
|
if ($options === Uri::IS_URI_TEMPLATE) { |
|
23
|
|
|
$opened = false; |
|
24
|
|
|
for ($i = 0; $i < strlen($data); ++$i) { |
|
25
|
|
|
if ($data[$i] === '{') { |
|
26
|
|
|
if ($opened) { |
|
27
|
|
|
return 'Invalid uri-template: unexpected "{"'; |
|
28
|
|
|
} else { |
|
29
|
|
|
$opened = true; |
|
30
|
|
|
} |
|
31
|
|
|
} elseif ($data[$i] === '}') { |
|
32
|
|
|
if ($opened) { |
|
33
|
|
|
$opened = false; |
|
34
|
|
|
} else { |
|
35
|
|
|
return 'Invalid uri-template: unexpected "}"'; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
if ($opened) { |
|
40
|
|
|
return 'Invalid uri-template: unexpected end of string'; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$uri = parse_url($data); |
|
45
|
|
|
if (!$uri) { |
|
46
|
|
|
return 'Malformed URI'; |
|
47
|
|
|
} |
|
48
|
|
|
if (($options & self::IS_SCHEME_REQUIRED) && (!isset($uri['scheme']) || $uri['scheme'] === '')) { |
|
49
|
|
|
return 'Missing scheme in URI'; |
|
50
|
|
|
} |
|
51
|
|
|
if (isset($uri['host'])) { |
|
52
|
|
|
$host = $uri['host']; |
|
53
|
|
|
if (!preg_match(Format::HOSTNAME_REGEX, $host)) { |
|
54
|
|
|
// stripping [ ] |
|
55
|
|
|
if ($host[0] === '[' && $host[strlen($host) - 1] === ']') { |
|
56
|
|
|
$host = substr($host, 1, -1); |
|
57
|
|
|
} |
|
58
|
|
|
if (!filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
59
|
|
|
return 'Malformed host in URI: ' . $host; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (isset($uri['path'])) { |
|
65
|
|
|
if (strpos($uri['path'], '\\') !== false) { |
|
66
|
|
|
return 'Invalid path: unescaped backslash'; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (isset($uri['fragment'])) { |
|
71
|
|
|
if (strpos($uri['fragment'], '\\') !== false) { |
|
72
|
|
|
return 'Invalid fragment: unescaped backslash'; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
} |