PluginProbe
WPIDE – File Manager & Code Editor / 3.1
WPIDE – File Manager & Code Editor v3.1
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
wpide / vendor / rakit / validation / src / Rules / UploadedFile.php

UploadedFile.php in WPIDE – File Manager & Code Editor 3.1, at vendor/rakit/validation/src/Rules/UploadedFile.php

185 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Rakit\Validation\Rules;
4
5 use Rakit\Validation\Helper;
6 use Rakit\Validation\MimeTypeGuesser;
7 use Rakit\Validation\Rule;
8 use Rakit\Validation\Rules\Interfaces\BeforeValidate;
9
10 class UploadedFile extends Rule implements BeforeValidate
11 {
12 use Traits\FileTrait, Traits\SizeTrait;
13
14 /** @var string */
15 protected $message = "The :attribute is not valid uploaded file";
16
17 /** @var string|int */
18 protected $maxSize = null;
19
20 /** @var string|int */
21 protected $minSize = null;
22
23 /** @var array */
24 protected $allowedTypes = [];
25
26 /**
27 * Given $params and assign $this->params
28 *
29 * @param array $params
30 * @return self
31 */
32 public function fillParameters(array $params): Rule
33 {
34 $this->minSize(array_shift($params));
35 $this->maxSize(array_shift($params));
36 $this->fileTypes($params);
37
38 return $this;
39 }
40
41 /**
42 * Given $size and set the max size
43 *
44 * @param string|int $size
45 * @return self
46 */
47 public function maxSize($size): Rule
48 {
49 $this->params['max_size'] = $size;
50 return $this;
51 }
52
53 /**
54 * Given $size and set the min size
55 *
56 * @param string|int $size
57 * @return self
58 */
59 public function minSize($size): Rule
60 {
61 $this->params['min_size'] = $size;
62 return $this;
63 }
64
65 /**
66 * Given $min and $max then set the range size
67 *
68 * @param string|int $min
69 * @param string|int $max
70 * @return self
71 */
72 public function sizeBetween($min, $max): Rule
73 {
74 $this->minSize($min);
75 $this->maxSize($max);
76
77 return $this;
78 }
79
80 /**
81 * Given $types and assign $this->params
82 *
83 * @param mixed $types
84 * @return self
85 */
86 public function fileTypes($types): Rule
87 {
88 if (is_string($types)) {
89 $types = explode('|', $types);
90 }
91
92 $this->params['allowed_types'] = $types;
93
94 return $this;
95 }
96
97 /**
98 * {@inheritDoc}
99 */
100 public function beforeValidate()
101 {
102 $attribute = $this->getAttribute();
103
104 // We only resolve uploaded file value
105 // from complex attribute such as 'files.photo', 'images.*', 'images.foo.bar', etc.
106 if (!$attribute->isUsingDotNotation()) {
107 return;
108 }
109
110 $keys = explode(".", $attribute->getKey());
111 $firstKey = array_shift($keys);
112 $firstKeyValue = $this->validation->getValue($firstKey);
113
114 $resolvedValue = $this->resolveUploadedFileValue($firstKeyValue);
115
116 // Return original value if $value can't be resolved as uploaded file value
117 if (!$resolvedValue) {
118 return;
119 }
120
121 $this->validation->setValue($firstKey, $resolvedValue);
122 }
123
124 /**
125 * Check the $value is valid
126 *
127 * @param mixed $value
128 * @return bool
129 */
130 public function check($value): bool
131 {
132 $minSize = $this->parameter('min_size');
133 $maxSize = $this->parameter('max_size');
134 $allowedTypes = $this->parameter('allowed_types');
135
136 if ($allowedTypes) {
137 $or = $this->validation ? $this->validation->getTranslation('or') : 'or';
138 $this->setParameterText('allowed_types', Helper::join(Helper::wraps($allowedTypes, "'"), ', ', ", {$or} "));
139 }
140
141 // below is Required rule job
142 if (!$this->isValueFromUploadedFiles($value) or $value['error'] == UPLOAD_ERR_NO_FILE) {
143 return true;
144 }
145
146 if (!$this->isUploadedFile($value)) {
147 return false;
148 }
149
150 // just make sure there is no error
151 if ($value['error']) {
152 return false;
153 }
154
155 if ($minSize) {
156 $bytesMinSize = $this->getBytesSize($minSize);
157 if ($value['size'] < $bytesMinSize) {
158 $this->setMessage('The :attribute file is too small, minimum size is :min_size');
159 return false;
160 }
161 }
162
163 if ($maxSize) {
164 $bytesMaxSize = $this->getBytesSize($maxSize);
165 if ($value['size'] > $bytesMaxSize) {
166 $this->setMessage('The :attribute file is too large, maximum size is :max_size');
167 return false;
168 }
169 }
170
171 if (!empty($allowedTypes)) {
172 $guesser = new MimeTypeGuesser;
173 $ext = $guesser->getExtension($value['type']);
174 unset($guesser);
175
176 if (!in_array($ext, $allowedTypes)) {
177 $this->setMessage('The :attribute file type must be :allowed_types');
178 return false;
179 }
180 }
181
182 return true;
183 }
184 }
185