-
Notifications
You must be signed in to change notification settings - Fork 650
/
test.php
196 lines (166 loc) · 5.69 KB
/
test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
$num_files = 0;
$num_entries = 0;
$expected_keys = array(
'#' => 1,
'#/provider_name' => 1,
'#/provider_url' => 1,
'#/endpoints' => 1,
'#/endpoints/#' => 1,
'#/endpoints/#/schemes' => 1,
'#/endpoints/#/schemes/#' => 1,
'#/endpoints/#/url' => 1,
'#/endpoints/#/example_urls' => 1,
'#/endpoints/#/example_urls/#' => 1,
'#/endpoints/#/discovery' => 1,
'#/endpoints/#/formats' => 1,
'#/endpoints/#/formats/#' => 1,
'#/endpoints/#/notes' => 1,
'#/endpoints/#/notes/#' => 1,
'#/endpoints/#/docs_url' => 1,
);
#$p1 = yaml_parse_file("providers/vidmount.yml", -1);
#$p2 = yaml_parse_file("providers/vidyard.yml", -1);
#echo var_export($p1);
#echo var_export($p2);
#exit;
$dh = opendir('providers');
while (($file = readdir($dh)) !== false){
if (preg_match('!\.yml$!', $file)){
$partial = yaml_parse_file("providers/$file");
if (!$partial || !is_array($partial)){
echo "Unable to parse provider file providers/$file\n";
exit(1);
}
# check if there is more than one document in the file
$full = yaml_parse_file("providers/$file", -1);
if (count($full) != 1){
echo "More than one YAML document specified in providers/$file\n";
echo "(The file should end with \"...\", not \"---\")\n";
exit(1);
}
$num_files++;
$num_entries += count($partial);
#
# check for any unexpected keys
#
$keys = check_keys($partial);
foreach ($keys as $k){
if (!isset($expected_keys[$k])){
echo "Unexpected key {$k} in provider file providers/$file\n";
exit(1);
}
}
#
# check that we define at least one endpoint and that each endpoint has a scheme and a url
#
foreach ($partial as $def){
if (!isset($def['endpoints']) || !count($def['endpoints'])){
echo "No endpoints defined in provider file providers/$file\n";
exit(1);
}
foreach ($def['endpoints'] as $endpoint){
# this test is currently disabled because three providers fail it.
# i _believe_ they should be deleted, as they are of no use to consumers.
if (!isset($endpoint['discovery']) || $endpoint['discovery'] === 0){
if (!isset($endpoint['schemes']) || !count($endpoint['schemes'])){
echo "Endpoint without schemes or discovery found in provider file providers/$file\n";
print_r($endpoint);
exit(1);
}
}
if (!isset($endpoint['url'])){
echo "Endpoint without URL found in provider file providers/$file\n";
print_r($endpoint);
exit(1);
}
if (preg_match('!\*!', $endpoint['url'])){
echo "Endpoint URL contains a wildcard in provider file providers/$file\n";
print_r($endpoint);
exit(1);
}
if (isset($endpoint['example_urls']))
foreach ($endpoint['example_urls'] as $example){
if (!preg_match('!^https?://!', $example)){
echo "Endpoint example URL does not start with http:// or https:// in provider file providers/$file\n";
print_r($endpoint);
exit(1);
}
if (!preg_match('!url=!', $example)){
echo "Endpoint example URL does not contain url= param in provider file providers/$file\n";
print_r($endpoint);
exit(1);
}
}
if (isset($endpoint['schemes'])){
foreach ($endpoint['schemes'] as $scheme){
# check for people trying to put regexes in the schemes (e.g. "(foo|bar)")
if (strpos($scheme, '(') !== false){
echo "Scheme contains illegal character '(' in provider file providers/$file\n";
print_r($endpoint['schemes']);
exit(1);
}
if (strpos($scheme, ')') !== false){
echo "Scheme contains illegal character ')' in provider file providers/$file\n";
print_r($endpoint['schemes']);
exit(1);
}
# check for wildcards in schemes (and that a scheme exists)
if (!preg_match('!^([a-z]+):!', $scheme)){
echo "Scheme URL must contain a scheme which itself may not contain wildcards in provider file providers/$file\n";
print_r($endpoint['schemes']);
exit(1);
}
# for HTTP(S) URLs, check for domain wildcards
if (preg_match('!^https?://([^/]+)!', $scheme, $m)){
$domain = $m[1];
$parts = array_reverse(explode('.', $domain));
# allow 'foo.com' but no 'com'
if (count($parts) < 2){
echo "Scheme domain must be fully qualified in provider file providers/$file\n";
print_r($endpoint['schemes']);
exit(1);
}
# '*.foo.com' is ok, but '*.com' is not
if ($parts[0] == '*' || $parts[1] == '*'){
echo "Scheme domain may not contain a wildcard as the TLD in provider file providers/$file\n";
print_r($endpoint['schemes']);
exit(1);
}
# domain atoms may either be a wildcard, or a literal match,
# so '*.foo.bar.com' is ok, but '*foo.bar.com' is not
foreach ($parts as $part){
if (strpos($part, '*') !== false && $part !== '*'){
echo "Scheme domain wildcards must be for a whole atom TLD in provider file providers/$file\n";
print_r($endpoint['schemes']);
exit(1);
}
}
}
}
}
}
}
}else if (in_array($file, ['README.md', '.', '..'])){
# these are expected to be here
}else{
echo "Unexpected file {$file} in providers directory - your file must end in .yml\n";
exit(1);
}
}
echo "Loaded $num_files provider files, containing $num_entries entries\n";
exit(0);
function check_keys($a){
$out = array();
check_keys_inner('', $a, $out);
return array_keys($out);
}
function check_keys_inner($prefix, $a, &$out){
foreach ($a as $k => $v){
$key = is_int($k) ? '#' : $k;
$out[$prefix.$key] = 1;
if (is_array($v)){
check_keys_inner($prefix.$key.'/', $v, $out);
}
}
}