|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Simple S3 package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Matecat\SimpleS3\Components\Validators; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This class check if a string is a valid S3 Bucket Name |
|
16
|
|
|
* |
|
17
|
|
|
* Amazon S3 Bucket Naming Requirements |
|
18
|
|
|
* ------------------------------------------------------------------------- |
|
19
|
|
|
* The Amazon S3 bucket that you use to store CloudTrail log files must have a name that conforms with naming requirements for non-US Standard regions. Amazon S3 defines a bucket name as a series of one or more labels, separated by periods, that adhere to the following rules: |
|
20
|
|
|
* |
|
21
|
|
|
* - The bucket name can be between 3 and 63 characters long, and can contain only lower-case characters, numbers, periods, and dashes. |
|
22
|
|
|
* - Each label in the bucket name must start with a lowercase letter or number. |
|
23
|
|
|
* - The bucket name cannot contain underscores, end with a dash, have consecutive periods, or use dashes adjacent to periods. |
|
24
|
|
|
* - The bucket name cannot be formatted as an IP address (198.51.100.24). |
|
25
|
|
|
* |
|
26
|
|
|
* Complete reference: |
|
27
|
|
|
* |
|
28
|
|
|
* https://docs.aws.amazon.com/en_us/awscloudtrail/latest/userguide/cloudtrail-s3-bucket-naming-requirements.html |
|
29
|
|
|
* |
|
30
|
|
|
* @package SimpleS3 |
|
31
|
|
|
*/ |
|
32
|
|
|
final class S3BucketNameValidator extends S3NameValidator |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $string |
|
36
|
|
|
* |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
18 |
|
public static function validate(string $string): array |
|
40
|
|
|
{ |
|
41
|
18 |
|
$errors = []; |
|
42
|
|
|
|
|
43
|
18 |
|
if (strlen($string) < 3) { |
|
44
|
1 |
|
$errors[] = 'The string is too short'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
18 |
|
if (strlen($string) > 64) { |
|
48
|
1 |
|
$errors[] = 'The string is too long'; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
18 |
|
if (filter_var($string, FILTER_VALIDATE_IP)) { |
|
52
|
2 |
|
$errors[] = 'The string is an IP address'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
18 |
|
if (strtolower($string) !== $string) { |
|
56
|
1 |
|
$errors[] = 'The string contains capital letters'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
18 |
|
if (preg_match('/[^a-z.\-0-9]/i', $string)) { |
|
60
|
1 |
|
$errors[] = 'The string contains a not allowed character'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
18 |
|
if (str_ends_with($string, '-')) { |
|
64
|
1 |
|
$errors[] = 'The string ends with a -'; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
18 |
|
$notAllowedCombos = [ |
|
68
|
18 |
|
'..', |
|
69
|
18 |
|
'-.', |
|
70
|
18 |
|
'.-' |
|
71
|
18 |
|
]; |
|
72
|
|
|
|
|
73
|
18 |
|
foreach ($notAllowedCombos as $notAllowedCombo) { |
|
74
|
18 |
|
if (strpos($string, $notAllowedCombo)) { |
|
75
|
1 |
|
$errors[] = 'The string contains a not allowed character combination'; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
18 |
|
return $errors; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|