1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Spiral\Validation\Checkers; |
10
|
|
|
|
11
|
|
|
use Interop\Container\ContainerInterface; |
12
|
|
|
use Psr\Http\Message\UploadedFileInterface; |
13
|
|
|
use Spiral\Core\Container\SingletonInterface; |
14
|
|
|
use Spiral\Files\FilesInterface; |
15
|
|
|
use Spiral\Validation\Checkers\Traits\FileTrait; |
16
|
|
|
use Spiral\Validation\Prototypes\AbstractChecker; |
17
|
|
|
|
18
|
|
|
class FileChecker extends AbstractChecker implements SingletonInterface |
19
|
|
|
{ |
20
|
|
|
use FileTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
protected $messages = [ |
26
|
|
|
'exists' => '[[File does not exists.]]', |
27
|
|
|
'uploaded' => '[[File not received, please try again.]]', |
28
|
|
|
'size' => '[[File exceeds the maximum file size of {1}KB.]]', |
29
|
|
|
'extension' => '[[File has an invalid file format.]]', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param FilesInterface $files |
34
|
|
|
* @param ContainerInterface $container |
35
|
|
|
*/ |
36
|
|
|
public function __construct(FilesInterface $files, ContainerInterface $container = null) |
37
|
|
|
{ |
38
|
|
|
$this->files = $files; |
39
|
|
|
|
40
|
|
|
parent::__construct($container); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Check if file exist. |
45
|
|
|
* |
46
|
|
|
* @param mixed $filename |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function exists($filename): bool |
51
|
|
|
{ |
52
|
|
|
return (bool)$this->filename($filename, false); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Will check if local file exists or just uploaded. |
57
|
|
|
* |
58
|
|
|
* @param mixed $file Local file or uploaded file array. |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function uploaded($file): bool |
63
|
|
|
{ |
64
|
|
|
return (bool)$this->filename($file, true); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check if file size less that specified value in KB. |
69
|
|
|
* |
70
|
|
|
* @param mixed $filename Local file or uploaded file array. |
71
|
|
|
* @param int $size Size in KBytes. |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function size($filename, int $size): bool |
76
|
|
|
{ |
77
|
|
|
$filename = $this->filename($filename, false); |
78
|
|
|
if (empty($filename) || !is_string($filename)) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->files->size($filename) < $size * 1024; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Check if file extension in whitelist. Client name of uploaded file will be used! |
87
|
|
|
* |
88
|
|
|
* @param mixed $filename |
89
|
|
|
* @param array|string $extensions |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function extension($filename, $extensions): bool |
94
|
|
|
{ |
95
|
|
|
if (!is_array($extensions)) { |
96
|
|
|
$extensions = array_slice(func_get_args(), 1); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($filename instanceof UploadedFileInterface) { |
100
|
|
|
return in_array( |
101
|
|
|
$this->files->extension($filename->getClientFilename()), |
102
|
|
|
$extensions |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return in_array($this->files->extension($filename), $extensions); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|