1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Upload |
5
|
|
|
* |
6
|
|
|
* Platine Upload provides a flexible file uploads with extensible |
7
|
|
|
* validation and storage strategies. |
8
|
|
|
* |
9
|
|
|
* This content is released under the MIT License (MIT) |
10
|
|
|
* |
11
|
|
|
* Copyright (c) 2020 Platine Upload |
12
|
|
|
* |
13
|
|
|
* @author Josh Lockhart <[email protected]> |
14
|
|
|
* @copyright 2012 Josh Lockhart |
15
|
|
|
* @link http://www.joshlockhart.com |
16
|
|
|
* @version 2.0.0 |
17
|
|
|
* |
18
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
19
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
20
|
|
|
* in the Software without restriction, including without limitation the rights |
21
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
22
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
23
|
|
|
* furnished to do so, subject to the following conditions: |
24
|
|
|
* |
25
|
|
|
* The above copyright notice and this permission notice shall be included in all |
26
|
|
|
* copies or substantial portions of the Software. |
27
|
|
|
* |
28
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
29
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
30
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
31
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
32
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
33
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
34
|
|
|
* SOFTWARE. |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @file Extension.php |
39
|
|
|
* |
40
|
|
|
* The file upload extension validation rule class |
41
|
|
|
* |
42
|
|
|
* @package Platine\Upload\Validator |
43
|
|
|
* @author Platine Developers Team |
44
|
|
|
* @copyright Copyright (c) 2020 |
45
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
46
|
|
|
* @link http://www.iacademy.cf |
47
|
|
|
* @version 1.0.0 |
48
|
|
|
* @filesource |
49
|
|
|
*/ |
50
|
|
|
|
51
|
|
|
declare(strict_types=1); |
52
|
|
|
|
53
|
|
|
namespace Platine\Upload\Validator\Rule; |
54
|
|
|
|
55
|
|
|
use Platine\Upload\File\File; |
56
|
|
|
use Platine\Upload\Util\Helper; |
57
|
|
|
use Platine\Upload\Validator\RuleInterface; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Class Extension |
61
|
|
|
* @package Platine\Upload\Validator\Rule |
62
|
|
|
*/ |
63
|
|
|
class Extension implements RuleInterface |
64
|
|
|
{ |
65
|
|
|
/** |
66
|
|
|
* The list of allowed/forbidden extensions |
67
|
|
|
* @var array<int, string> |
68
|
|
|
*/ |
69
|
|
|
protected array $extensions; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Whether the extension list is to allow/forbidden |
73
|
|
|
* @var bool |
74
|
|
|
s */ |
75
|
|
|
protected bool $exclude = false; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Create new instance |
79
|
|
|
* @param array<int, string>|string $extensions |
80
|
|
|
* @param bool $exclude |
81
|
|
|
*/ |
82
|
|
|
public function __construct($extensions, bool $exclude = false) |
83
|
|
|
{ |
84
|
|
|
if (!is_array($extensions)) { |
85
|
|
|
$extensions = [$extensions]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->extensions = $extensions; |
89
|
|
|
$this->exclude = $exclude; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
* @see RuleInterface |
95
|
|
|
*/ |
96
|
|
|
public function validate(File $file): bool |
97
|
|
|
{ |
98
|
|
|
$extension = strtolower($file->getExtension()); |
99
|
|
|
$extensions = array_map('strtolower', $this->extensions); |
100
|
|
|
$result = in_array($extension, $extensions); |
101
|
|
|
|
102
|
|
|
if ($result && $this->exclude) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!$result && !$this->exclude) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
* @see RuleInterface |
116
|
|
|
*/ |
117
|
|
|
public function getErrorMessage(File $file): string |
118
|
|
|
{ |
119
|
|
|
return sprintf( |
120
|
|
|
'The uploaded file extension [%s] is not allowed/forbidden, [%s]', |
121
|
|
|
$file->getExtension(), |
122
|
|
|
implode(', ', $this->extensions) |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|