|
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
|
|
|
use RuntimeException; |
|
21
|
|
|
|
|
22
|
|
|
class Utils |
|
23
|
|
|
{ |
|
24
|
|
|
public static function validateCommonName($commonName) |
|
25
|
|
|
{ |
|
26
|
|
|
if (0 === preg_match('/^[a-zA-Z0-9-_.@]+$/', $commonName)) { |
|
27
|
|
|
throw new BadRequestException('invalid characters in common name'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
// MUST NOT be '..' |
|
31
|
|
|
if ('..' === $commonName) { |
|
32
|
|
|
throw new BadRequestException('common name cannot be ".."'); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function validateUserId($userId) |
|
37
|
|
|
{ |
|
38
|
|
|
if (0 === preg_match('/^[a-zA-Z0-9-_.@]+$/', $userId)) { |
|
39
|
|
|
throw new BadRequestException('invalid characters in userId'); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function validateAddress($ipAddress) |
|
44
|
|
|
{ |
|
45
|
|
|
if (false === filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
46
|
|
|
throw new BadRequestException('invalid v4 address'); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Validate that the date is in YYYY-MM-DD format. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $dateString the date in YYYY-MM-DD format |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function validateDate($dateString) |
|
56
|
|
|
{ |
|
57
|
|
|
if (!preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $dateString)) { |
|
58
|
|
|
throw new BadRequestException('invalid date format'); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function exec($cmd) |
|
63
|
|
|
{ |
|
64
|
|
|
exec($cmd, $output, $returnValue); |
|
65
|
|
|
|
|
66
|
|
|
if (0 !== $returnValue) { |
|
67
|
|
|
throw new RuntimeException( |
|
68
|
|
|
sprintf('command "%s" did not complete successfully (%d)', $cmd, $returnValue) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|