PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.16.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.16.2
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
← All changes | includes/Core/Util/FileHandler.php +319 -75 1.32.16.2 View file →
@@ -1,94 +1,338 @@
1 1 <?php
2 +
2 3 namespace BitCode\BitForm\Core\Util;
3 4
5 +use BitCode\BitForm\Core\Form\FormManager;
6 +use BitCode\BitForm\enshrined\svgSanitize\Sanitizer;
7 +
4 8 final class FileHandler
5 9 {
6 - public function rmrf($dir)
7 - {
8 - if (is_dir($dir)) {
9 - $objects = scandir($dir);
10 - foreach ($objects as $object) {
11 - if ($object != "." && $object != "..") {
12 - if (is_dir($dir. DIRECTORY_SEPARATOR .$object) && !is_link($dir.DIRECTORY_SEPARATOR.$object)) {
13 - $this->rmrf($dir. DIRECTORY_SEPARATOR .$object);
14 - } else {
15 - unlink($dir. DIRECTORY_SEPARATOR .$object);
16 - }
17 - }
10 + public function rmrf($dir)
11 + {
12 + if (is_dir($dir)) {
13 + $objects = scandir($dir);
14 + foreach ($objects as $object) {
15 + if ('.' !== $object && '..' !== $object) {
16 + if (is_dir($dir . DIRECTORY_SEPARATOR . $object) && !is_link($dir . DIRECTORY_SEPARATOR . $object)) {
17 + $this->rmrf($dir . DIRECTORY_SEPARATOR . $object);
18 + } else {
19 + wp_delete_file($dir . DIRECTORY_SEPARATOR . $object);
20 + }
21 + }
22 + }
23 + rmdir($dir);
24 + } else {
25 + wp_delete_file($dir);
26 + }
27 + }
28 +
29 + public function cpyr($source, $destination)
30 + {
31 + if (is_dir($source)) {
32 + mkdir($destination);
33 + // chmod($destination, 0744);
34 + $objects = scandir($source);
35 + foreach ($objects as $object) {
36 + if ('.' !== $object && '..' !== $object) {
37 + if (is_dir($source . DIRECTORY_SEPARATOR . $object) && !is_link($source . DIRECTORY_SEPARATOR . $object)) {
38 + cpyr($source . DIRECTORY_SEPARATOR . $object, $destination . DIRECTORY_SEPARATOR . $object);
39 + } elseif (is_file($source . DIRECTORY_SEPARATOR . $object)) {
40 + copy($source . DIRECTORY_SEPARATOR . $object, $destination . DIRECTORY_SEPARATOR . $object);
41 + // chmod($destination. DIRECTORY_SEPARATOR .$object, 0644);
42 + } else {
43 + symlink($source . DIRECTORY_SEPARATOR . $object, $destination . DIRECTORY_SEPARATOR . $object);
44 + }
45 + }
46 + }
47 + } else {
48 + copy($source, $destination);
49 + }
50 + }
51 +
52 + public function moveUploadedFiles($file_details, $form_id, $entry_id)
53 + {
54 + $file_upoalded = [];
55 + $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $form_id . DIRECTORY_SEPARATOR . $entry_id;
56 + wp_mkdir_p($_upload_dir);
57 + if (is_array($file_details['name'])) {
58 + foreach ($file_details['name'] as $key => $value) {
59 + //check accepted filetype in_array($file_details['name'][$key], $supported_files) else \
60 + if (!empty($value)) {
61 + $fileNameCount = 1;
62 + // $file_upoalded[$key] = time()."_$value";
63 + $file_upoalded[$key] = sanitize_file_name($value);
64 + while (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $file_upoalded[$key])) {
65 + $fileNameWithSeparator = BITFORMS_BF_SEPARATOR . $fileNameCount;
66 + $file_upoalded[$key] = sanitize_file_name(preg_replace('/(.[a-z A-Z 0-9]+)$/', "{$fileNameWithSeparator}$1", $value));
67 + $fileNameCount = $fileNameCount + 1;
68 + if (11 === $fileNameCount) {
69 + break;
18 70 }
19 - rmdir($dir);
71 + }
72 + $move_status = \move_uploaded_file($file_details['tmp_name'][$key], $_upload_dir . DIRECTORY_SEPARATOR . $file_upoalded[$key]);
73 + if (!$move_status) {
74 + unset($file_upoalded[$key]);
75 + }
76 + }
77 + }
78 + } else {
79 + if (!empty($file_details['name'])) {
80 + $fileNameCount = 1;
81 + $file_upoalded[0] = sanitize_file_name($file_details['name']);
82 + while (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $file_upoalded[0])) {
83 + $fileNameWithSeparator = BITFORMS_BF_SEPARATOR . $fileNameCount;
84 + $file_upoalded[0] = sanitize_file_name(preg_replace('/(.[a-z A-Z 0-9]+)$/', "{$fileNameWithSeparator}$1", $file_details['name']));
85 + $fileNameCount = $fileNameCount + 1;
86 + if (11 === $fileNameCount) {
87 + break;
88 + }
89 + }
90 + $move_status = \move_uploaded_file($file_details['tmp_name'], $_upload_dir . DIRECTORY_SEPARATOR . $file_upoalded[0]);
91 + if (!$move_status) {
92 + unset($file_upoalded[0]);
93 + }
94 + }
95 + }
96 + return $file_upoalded;
97 + }
98 +
99 + public function deleteFiles($form_id, $entry_id, $files)
100 + {
101 + $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $form_id . DIRECTORY_SEPARATOR . $entry_id;
102 + foreach ($files as $name) {
103 + wp_delete_file($_upload_dir . DIRECTORY_SEPARATOR . $name);
104 + }
105 + }
106 +
107 + public static function getFileUploadError($code)
108 + {
109 + $errors = [
110 + 0 => __('Unknown upload error', 'bit-form'),
111 + 1 => __('The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'bit-form'),
112 + 2 => __('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', 'bit-form'),
113 + 3 => __('The uploaded file was only partially uploaded.', 'bit-form'),
114 + 4 => __('No file was uploaded.', 'bit-form'),
115 + 6 => __('Missing a temporary folder.', 'bit-form'),
116 + 7 => __('Failed to write file to disk.', 'bit-form'),
117 + 8 => __('A PHP extension stopped the file upload.', 'bit-form'),
118 + ];
119 + return $errors[$code];
120 + }
121 +
122 + public static function fileCopy($tmpdir, $destinationDir, $file)
123 + {
124 + $tmpFile = $tmpdir . DIRECTORY_SEPARATOR . $file;
125 + $newFile = $destinationDir . DIRECTORY_SEPARATOR . $file;
126 + if (file_exists($tmpFile)) {
127 + copy($tmpFile, $newFile);
128 + }
129 + }
130 +
131 + public static function tempDirToUploadDir($submitted_data, $fields, $formId, $entryID)
132 + {
133 + $upload_dir = wp_upload_dir();
134 + $tempDir = $upload_dir['basedir'] . '/bitforms/temp';
135 + $destinationDir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formId . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR;
136 + if (!is_dir($destinationDir)) {
137 + mkdir($destinationDir);
138 + }
139 +
140 + foreach ($submitted_data as $key => $data) {
141 + if (isset($fields[$key]) && 'advanced-file-up' === $fields[$key]['type']) {
142 + $files = $data;
143 + $fldData = $submitted_data[$key];
144 + $files = explode(',', $fldData);
145 + if (is_array($files) && count($files) > 0) {
146 + foreach ($files as $file) {
147 + self::fileCopy($tempDir, $destinationDir, trim($file));
148 + }
20 149 } else {
21 - unlink($dir);
150 + self::fileCopy($tempDir, $destinationDir, trim($files));
22 151 }
152 + if (!empty($files)) {
153 + $submitted_data[$key] = $files;
154 + }
155 + }
23 156 }
157 + array_map('unlink', array_filter(
158 + (array) array_merge(glob("$tempDir/*"))
159 + ));
24 160
25 - public function cpyr($source, $destination)
26 - {
27 - if (is_dir($source)) {
28 - mkdir($destination);
29 - // chmod($destination, 0744);
30 - $objects = scandir($source);
31 - foreach ($objects as $object) {
32 - if ($object != "." && $object != "..") {
33 - if (is_dir($source. DIRECTORY_SEPARATOR .$object) && !is_link($source.DIRECTORY_SEPARATOR.$object)) {
34 - cpyr($source. DIRECTORY_SEPARATOR .$object, $destination. DIRECTORY_SEPARATOR .$object);
35 - } elseif (is_file($source. DIRECTORY_SEPARATOR .$object)) {
36 - copy($source. DIRECTORY_SEPARATOR .$object, $destination. DIRECTORY_SEPARATOR .$object);
37 - // chmod($destination. DIRECTORY_SEPARATOR .$object, 0644);
38 - } else {
39 - symlink($source. DIRECTORY_SEPARATOR .$object, $destination. DIRECTORY_SEPARATOR .$object);
40 - }
41 - }
42 - }
43 - } else {
44 - copy($source, $destination);
161 + return $submitted_data;
162 + }
163 +
164 + private function getByteSizeByUnit($sizeString)
165 + {
166 + // split 2MB into 2 and MB
167 + $size = preg_replace('/[^0-9\.]/', '', $sizeString);
168 + $unit = preg_replace('/[^a-zA-Z]/', '', $sizeString);
169 + $unit = strtolower($unit);
170 + if ('kb' === $unit) {
171 + return $size * 1024;
172 + } elseif ('mb' === $unit) {
173 + return $size * 1024 * 1024;
174 + } elseif ('gb' === $unit) {
175 + return $size * 1024 * 1024 * 1024;
176 + } else {
177 + return $size;
178 + }
179 + }
180 +
181 + public function validation($field_key, $file_details, $form_id)
182 + {
183 + if (!function_exists('wp_check_filetype_and_ext')) {
184 + require_once ABSPATH . 'wp-admin/includes/file.php';
185 + }
186 +
187 + $formManager = new FormManager($form_id);
188 + $form_contents = $formManager->getFormContent();
189 + $field_content_details = $form_contents->fields;
190 + $fieldDetail = $field_content_details->{$field_key};
191 + $fieldType = $fieldDetail->typ;
192 + $maxSizeDetails = [];
193 + $allowFileTypes = [];
194 + $maxSize = null;
195 + if ('file-up' === $fieldType) {
196 + $allowFileTypes = !empty($fieldDetail->config->allowedFileType) ? $fieldDetail->config->allowedFileType : [];
197 + if (!empty($fieldDetail->config->allowMaxSize)) {
198 + if (!empty($fieldDetail->config->maxSize)) {
199 + $maxSizeDetails['maxSize'] = $fieldDetail->config->maxSize . $fieldDetail->config->sizeUnit;
45 200 }
201 + if (!empty($fieldDetail->config->isItTotalMax)) {
202 + $maxSizeDetails['maxTotalFileSize'] = $fieldDetail->config->maxSize . $fieldDetail->config->sizeUnit;
203 + }
204 + }
205 + if (!empty($allowFileTypes)) {
206 + $allowFileTypes = explode(',', $allowFileTypes);
207 + }
208 + } elseif ('advanced-file-up' === $fieldType) {
209 + $allowFileTypes = !empty($fieldDetail->config->allowFileTypeValidation) ? $fieldDetail->config->acceptedFileTypes : [];
210 + if (!empty($fieldDetail->config->allowFileSizeValidation)) {
211 + if (!empty($fieldDetail->config->maxFileSize)) {
212 + $maxSizeDetails['maxSize'] = $fieldDetail->config->maxFileSize;
213 + }
214 + if (!empty($fieldDetail->config->maxTotalFileSize)) {
215 + $maxSizeDetails['maxTotalFileSize'] = $fieldDetail->config->maxTotalFileSize;
216 + }
217 + }
46 218 }
219 + if (!empty($maxSizeDetails['maxSize'])) {
220 + $maxSize = $this->getByteSizeByUnit($maxSizeDetails['maxSize']);
221 + }
222 + $maxTotalFileSize = null;
223 + if (!empty($maxSizeDetails['maxTotalFileSize'])) {
224 + $maxTotalFileSize = $this->getByteSizeByUnit($maxSizeDetails['maxTotalFileSize']);
225 + }
47 226
48 - public function moveUploadedFiles($file_details, $form_id, $entry_id)
49 - {
50 - $file_upoalded = array();
51 - $_upload_dir = BITFORMS_UPLOAD_DIR.DIRECTORY_SEPARATOR.$form_id.DIRECTORY_SEPARATOR.$entry_id;
52 - wp_mkdir_p($_upload_dir);
53 - if (is_array($file_details['name'])) {
54 - foreach ($file_details['name'] as $key => $value) {
55 - //check accepted filetype in_array($file_details['name'][$key], $supported_files) else \
56 - if (!empty($value)) {
57 - $fileNameCount = 1;
58 - // $file_upoalded[$key] = time()."_$value";
59 - $file_upoalded[$key] = sanitize_file_name($value);
60 - while (file_exists($_upload_dir.DIRECTORY_SEPARATOR.$file_upoalded[$key])) {
61 - $file_upoalded[$key] = sanitize_file_name(preg_replace("/(.[a-z A-Z 0-9]+)$/", "__{$fileNameCount}$1", $value));
62 - $fileNameCount = $fileNameCount + 1;
63 - if ($fileNameCount === 11) {
64 - break;
65 - }
66 - }
67 - \move_uploaded_file($file_details['tmp_name'][$key], $_upload_dir.DIRECTORY_SEPARATOR.$file_upoalded[$key]);
68 - }
69 - }
227 + if ($formManager->isRepeatedField($field_key)) {
228 + foreach ($file_details['name'] as $rowIndex => $file) {
229 + if (!empty($file)) {
230 + $fileDetails = [
231 + 'name' => $file,
232 + 'type' => $file_details['type'][$rowIndex],
233 + 'tmp_name' => $file_details['tmp_name'][$rowIndex],
234 + 'error' => $file_details['error'][$rowIndex],
235 + 'size' => $file_details['size'][$rowIndex],
236 + ];
237 + $validateState = $this->validateFileInfo($fieldType, $fileDetails, $allowFileTypes, $maxSize, $maxTotalFileSize);
238 + if (!empty($validateState) && !empty($validateState['message'])) {
239 + return $validateState;
240 + }
241 + }
242 + }
243 + } else {
244 + return $this->validateFileInfo($fieldType, $file_details, $allowFileTypes, $maxSize, $maxTotalFileSize);
245 + }
246 + return [];
247 + }
248 +
249 + private function validateFileInfo($fieldType, $file_details, $allowFileTypes, $maxSize, $maxTotalFileSize)
250 + {
251 + $errorMessage = [
252 + 'message' => '',
253 + 'error_type'=> '',
254 + ];
255 + if (is_array($file_details['name'])) {
256 + $totalSize = 0;
257 + foreach ($file_details['name'] as $key => $file) {
258 + if (!empty($file)) {
259 + $fileInfo = [
260 + 'name' => $file,
261 + 'type' => $file_details['type'][$key],
262 + 'tmp_name' => $file_details['tmp_name'][$key],
263 + 'error' => $file_details['error'][$key],
264 + 'size' => $file_details['size'][$key],
265 + ];
266 + $totalSize += $fileInfo['size'];
267 + $validateState = $this->validateSingleFile($fieldType, $fileInfo, $allowFileTypes, $maxSize);
268 + if (!empty($validateState)) {
269 + return $validateState;
270 + }
271 + }
272 + }
273 + if (isset($maxTotalFileSize) && !is_null($maxTotalFileSize) && $totalSize > $maxTotalFileSize) {
274 + $errorMessage['message'] = __('Total File size is too large', 'bit-form');
275 + $errorMessage['error_type'] = 'file_size_error';
276 + return $errorMessage;
277 + }
278 + } else {
279 + $validateState = $this->validateSingleFile($fieldType, $file_details, $allowFileTypes, $maxSize);
280 + if (!empty($validateState)) {
281 + return $validateState;
282 + }
283 + }
284 +
285 + return $errorMessage;
286 + }
287 +
288 + private function validateSingleFile($fieldType, &$file, $allowTypes, $maxSize = null)
289 + {
290 + $fileName = sanitize_file_name($file['name']);
291 + if (!empty($fileName)) {
292 + $fileSize = $file['size'];
293 + if (!empty($maxSize) && $fileSize > $maxSize) {
294 + return [
295 + 'message' => __('File size is too large', 'bit-form'),
296 + 'error_type'=> 'file_size_error',
297 + ];
298 + }
299 +
300 + $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
301 + $fileExtAllowedByWp = wp_check_filetype_and_ext($file['tmp_name'], $fileName);
302 + $isAllowedFileType = in_array('.' . $fileExtension, $allowTypes);
303 + if ('advanced-file-up' === $fieldType && !empty($allowTypes)) {
304 + if (function_exists('mime_content_type')) {
305 + $fileMimeType = mime_content_type($file['tmp_name']);
70 306 } else {
71 - if (!empty($file_details['name'])) {
72 - $fileNameCount = 1;
73 - $file_upoalded[0] = sanitize_file_name($file_details['name']);
74 - while (file_exists($_upload_dir.DIRECTORY_SEPARATOR.$file_upoalded[0])) {
75 - $file_upoalded[0] = sanitize_file_name(preg_replace("/(.[a-z A-Z 0-9]+)$/", "__{$fileNameCount}$1", $file_details['name']));
76 - $fileNameCount = $fileNameCount + 1;
77 - if ($fileNameCount === 11) {
78 - break;
79 - }
80 - }
81 - \move_uploaded_file($file_details['tmp_name'], $_upload_dir.DIRECTORY_SEPARATOR.$file_upoalded[0]);
82 - }
307 + $fileMimeType = $fileExtAllowedByWp['type'];
83 308 }
84 - return $file_upoalded;
309 + $isAllowedFileType = in_array($fileMimeType, $allowTypes);
310 + }
311 + if ((!empty($allowTypes) && !$isAllowedFileType) || (empty($allowTypes) && empty($fileExtAllowedByWp['ext']))) {
312 + return [
313 + 'message' => __(($fileExtension ? ".{$fileExtension}" : 'empty') . ' file extension is not allowed', 'bit-form'),
314 + 'error_type'=> 'file_type_error',
315 + ];
316 + }
317 + if ('svg' === $fileExtension) {
318 + $svg_sanitizer = new Sanitizer();
319 + $dirty_svg = file_get_contents($file['tmp_name']);
320 + $clean_svg = $svg_sanitizer->sanitize($dirty_svg);
321 + if (false === $clean_svg) {
322 + return [
323 + 'message' => __('SVG file is not valid', 'bit-form'),
324 + 'error_type'=> 'file_type_error',
325 + ];
326 + }
327 + file_put_contents($file['tmp_name'], $clean_svg);
328 + }
85 329 }
330 + }
86 331
87 - public function deleteFiles($form_id, $entry_id, $files)
88 - {
89 - $_upload_dir = BITFORMS_UPLOAD_DIR.DIRECTORY_SEPARATOR.$form_id.DIRECTORY_SEPARATOR.$entry_id;
90 - foreach ($files as $name) {
91 - unlink($_upload_dir.DIRECTORY_SEPARATOR.$name);
92 - }
332 + public static function deleteIsFileExists($path)
333 + {
334 + if (file_exists($path)) {
335 + wp_delete_file($path);
93 336 }
337 + }
94 338 }