|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2015 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
namespace fkooman\VPN\Server; |
|
18
|
|
|
|
|
19
|
|
|
use fkooman\Http\Exception\BadRequestException; |
|
20
|
|
|
|
|
21
|
|
|
class InputValidation |
|
22
|
|
|
{ |
|
23
|
|
|
// XXX dot matches any char? |
|
24
|
|
|
const COMMON_NAME_PATTERN = '/^[a-zA-Z0-9-_.@]+$/'; |
|
25
|
|
|
const USER_ID_PATTERN = '/^[a-zA-Z0-9-_.@]+$/'; |
|
26
|
|
|
const DATE_PATTERN = '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/'; |
|
27
|
|
|
|
|
28
|
|
View Code Duplication |
public static function commonName($commonName, $isRequired) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
$commonName = !empty($commonName) ? $commonName : null; |
|
31
|
|
|
if (!$isRequired && is_null($commonName)) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
if (0 === preg_match(self::COMMON_NAME_PATTERN, $commonName)) { |
|
35
|
|
|
throw new BadRequestException('invalid value for "common_name"'); |
|
36
|
|
|
} |
|
37
|
|
|
if ('..' === $commonName) { |
|
38
|
|
|
throw new BadRequestException('"common_name" cannot be ".."'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $commonName; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
View Code Duplication |
public static function userId($userId, $isRequired) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
$userId = !empty($userId) ? $userId : null; |
|
47
|
|
|
if (!$isRequired && is_null($userId)) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
if (0 === preg_match(self::USER_ID_PATTERN, $userId)) { |
|
51
|
|
|
throw new BadRequestException('invalid value for "user_id"'); |
|
52
|
|
|
} |
|
53
|
|
|
if ('..' === $userId) { |
|
54
|
|
|
throw new BadRequestException('"user_id" cannot be ".."'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $userId; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public static function date($date, $isRequired) |
|
61
|
|
|
{ |
|
62
|
|
|
$date = !empty($date) ? $date : null; |
|
63
|
|
|
if (!$isRequired && is_null($date)) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
if (0 === preg_match(self::DATE_PATTERN, $date)) { |
|
67
|
|
|
throw new BadRequestException('invalid value for "date"'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $date; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public static function v4($v4, $isRequired) |
|
74
|
|
|
{ |
|
75
|
|
|
$v4 = !empty($v4) ? $v4 : null; |
|
76
|
|
|
if (!$isRequired && is_null($v4)) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
if (false === filter_var($v4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
80
|
|
|
throw new BadRequestException('"v4" is invalid'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $v4; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
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.