|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace SURFnet\VPN\Common\Http; |
|
20
|
|
|
|
|
21
|
|
|
use SURFnet\VPN\Common\Http\Exception\HttpException; |
|
22
|
|
|
|
|
23
|
|
|
class InputValidation |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function displayName($displayName) |
|
29
|
|
|
{ |
|
30
|
|
|
$displayName = filter_var($displayName, FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW); |
|
31
|
|
|
|
|
32
|
|
|
if (0 === mb_strlen($displayName)) { |
|
33
|
|
|
throw new HttpException('invalid "display_name"', 400); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $displayName; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function commonName($commonName) |
|
43
|
|
|
{ |
|
44
|
|
|
if (1 !== preg_match('/^[a-fA-F0-9]{32}$/', $commonName)) { |
|
45
|
|
|
throw new HttpException('invalid "common_name"', 400); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $commonName; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function serverCommonName($serverCommonName) |
|
55
|
|
|
{ |
|
56
|
|
|
if (1 !== preg_match('/^[a-zA-Z0-9-.]+$/', $serverCommonName)) { |
|
57
|
|
|
throw new HttpException('invalid "server_common_name"', 400); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $serverCommonName; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function profileId($profileId) |
|
67
|
|
|
{ |
|
68
|
|
|
if (1 !== preg_match('/^[a-zA-Z0-9]+$/', $profileId)) { |
|
69
|
|
|
throw new HttpException('invalid "profile_id"', 400); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $profileId; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function languageCode($languageCode) |
|
79
|
|
|
{ |
|
80
|
|
|
$supportedLanguages = ['en_US', 'nl_NL', 'de_DE', 'fr_FR']; |
|
81
|
|
|
if (!in_array($languageCode, $supportedLanguages)) { |
|
82
|
|
|
throw new HttpException('invalid "language_code"', 400); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $languageCode; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function totpSecret($totpSecret) |
|
92
|
|
|
{ |
|
93
|
|
|
if (1 !== preg_match('/^[A-Z0-9]{16}$/', $totpSecret)) { |
|
94
|
|
|
throw new HttpException('invalid "totp_secret"', 400); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $totpSecret; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function totpKey($totpKey) |
|
104
|
|
|
{ |
|
105
|
|
|
if (1 !== preg_match('/^[0-9]{6}$/', $totpKey)) { |
|
106
|
|
|
throw new HttpException('invalid "totp_key"', 400); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $totpKey; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public static function clientId($clientId) |
|
116
|
|
|
{ |
|
117
|
|
|
if (1 !== preg_match('/^(?:[\x20-\x7E])+$/', $clientId)) { |
|
118
|
|
|
throw new HttpException('invalid "client_id"', 400); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $clientId; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function userId($userId) |
|
128
|
|
|
{ |
|
129
|
|
|
if (1 !== preg_match('/^[a-zA-Z0-9-.@]+$/', $userId)) { |
|
130
|
|
|
throw new HttpException('invalid "user_id"', 400); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $userId; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
|
public static function motdMessage($motdMessage) |
|
140
|
|
|
{ |
|
141
|
|
|
// we accept everything... |
|
142
|
|
|
return $motdMessage; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return int |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function dateTime($dateTime) |
|
149
|
|
|
{ |
|
150
|
|
|
// try to parse first |
|
151
|
|
|
if (false === $unixTime = strtotime($dateTime)) { |
|
152
|
|
|
// if that fails, check if it is already unixTime |
|
153
|
|
|
$unixTime = intval($dateTime); |
|
154
|
|
|
if (0 <= $unixTime) { |
|
155
|
|
|
return $unixTime; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
throw new HttpException('invalid "date_time"', 400); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $unixTime; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
View Code Duplication |
public static function ipAddress($ipAddress) |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
if (false === filter_var($ipAddress, FILTER_VALIDATE_IP)) { |
|
170
|
|
|
throw new HttpException('invalid "ip_address"', 400); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
// normalize the IP address (only makes a difference for IPv6) |
|
174
|
|
|
return inet_ntop(inet_pton($ipAddress)); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
|
|
public static function vootToken($vootToken) |
|
181
|
|
|
{ |
|
182
|
|
|
if (1 !== preg_match('/^[a-zA-Z0-9-]+$/', $vootToken)) { |
|
183
|
|
|
throw new HttpException('invalid "voot_token"', 400); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $vootToken; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
|
|
public static function ip4($ip4) |
|
193
|
|
|
{ |
|
194
|
|
|
if (false === filter_var($ip4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
195
|
|
|
throw new HttpException('invalid "ip4"', 400); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return $ip4; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return string |
|
203
|
|
|
*/ |
|
204
|
|
View Code Duplication |
public static function ip6($ip6) |
|
|
|
|
|
|
205
|
|
|
{ |
|
206
|
|
|
if (false === filter_var($ip6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
207
|
|
|
throw new HttpException('invalid "ip6"', 400); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
// normalize the IPv6 address |
|
211
|
|
|
return inet_ntop(inet_pton($ip6)); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @return int |
|
216
|
|
|
*/ |
|
217
|
|
|
public static function connectedAt($connectedAt) |
|
218
|
|
|
{ |
|
219
|
|
|
if (!is_numeric($connectedAt) || 0 > intval($connectedAt)) { |
|
220
|
|
|
throw new HttpException('invalid "connected_at"', 400); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
return intval($connectedAt); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @return int |
|
228
|
|
|
*/ |
|
229
|
|
|
public static function disconnectedAt($disconnectedAt) |
|
230
|
|
|
{ |
|
231
|
|
|
if (!is_numeric($disconnectedAt) || 0 > intval($disconnectedAt)) { |
|
232
|
|
|
throw new HttpException('invalid "disconnected_at"', 400); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
return intval($disconnectedAt); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @return int |
|
240
|
|
|
*/ |
|
241
|
|
|
public static function bytesTransferred($bytesTransferred) |
|
242
|
|
|
{ |
|
243
|
|
|
if (!is_numeric($bytesTransferred) || 0 > intval($bytesTransferred)) { |
|
244
|
|
|
throw new HttpException('invalid "bytes_transferred"', 400); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
return intval($bytesTransferred); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.