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 +78 -18 2.02.16.2 View file →
@@ -2,11 +2,14 @@
2 2
3 3 namespace BitCode\BitForm\Core\Util;
4 4
5 5 use BitCode\BitForm\Core\Form\FormManager;
6 +use BitCode\BitForm\enshrined\svgSanitize\Sanitizer;
6 7
7 -final class FileHandler {
8 - public function rmrf($dir) {
8 +final class FileHandler
9 +{
10 + public function rmrf($dir)
11 + {
9 12 if (is_dir($dir)) {
10 13 $objects = scandir($dir);
11 14 foreach ($objects as $object) {
12 15 if ('.' !== $object && '..' !== $object) {
@@ -12,19 +15,20 @@
12 15 if ('.' !== $object && '..' !== $object) {
13 16 if (is_dir($dir . DIRECTORY_SEPARATOR . $object) && !is_link($dir . DIRECTORY_SEPARATOR . $object)) {
14 17 $this->rmrf($dir . DIRECTORY_SEPARATOR . $object);
15 18 } else {
16 - unlink($dir . DIRECTORY_SEPARATOR . $object);
19 + wp_delete_file($dir . DIRECTORY_SEPARATOR . $object);
17 20 }
18 21 }
19 22 }
20 23 rmdir($dir);
21 24 } else {
22 - unlink($dir);
25 + wp_delete_file($dir);
23 26 }
24 27 }
25 28
26 - public function cpyr($source, $destination) {
29 + public function cpyr($source, $destination)
30 + {
27 31 if (is_dir($source)) {
28 32 mkdir($destination);
29 33 // chmod($destination, 0744);
30 34 $objects = scandir($source);
@@ -33,9 +37,9 @@
33 37 if (is_dir($source . DIRECTORY_SEPARATOR . $object) && !is_link($source . DIRECTORY_SEPARATOR . $object)) {
34 38 cpyr($source . DIRECTORY_SEPARATOR . $object, $destination . DIRECTORY_SEPARATOR . $object);
35 39 } elseif (is_file($source . DIRECTORY_SEPARATOR . $object)) {
36 40 copy($source . DIRECTORY_SEPARATOR . $object, $destination . DIRECTORY_SEPARATOR . $object);
37 - // chmod($destination. DIRECTORY_SEPARATOR .$object, 0644);
41 + // chmod($destination. DIRECTORY_SEPARATOR .$object, 0644);
38 42 } else {
39 43 symlink($source . DIRECTORY_SEPARATOR . $object, $destination . DIRECTORY_SEPARATOR . $object);
40 44 }
41 45 }
@@ -44,9 +48,10 @@
44 48 copy($source, $destination);
45 49 }
46 50 }
47 51
48 - public function moveUploadedFiles($file_details, $form_id, $entry_id) {
52 + public function moveUploadedFiles($file_details, $form_id, $entry_id)
53 + {
49 54 $file_upoalded = [];
50 55 $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $form_id . DIRECTORY_SEPARATOR . $entry_id;
51 56 wp_mkdir_p($_upload_dir);
52 57 if (is_array($file_details['name'])) {
@@ -90,16 +95,18 @@
90 95 }
91 96 return $file_upoalded;
92 97 }
93 98
94 - public function deleteFiles($form_id, $entry_id, $files) {
99 + public function deleteFiles($form_id, $entry_id, $files)
100 + {
95 101 $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $form_id . DIRECTORY_SEPARATOR . $entry_id;
96 102 foreach ($files as $name) {
97 - unlink($_upload_dir . DIRECTORY_SEPARATOR . $name);
103 + wp_delete_file($_upload_dir . DIRECTORY_SEPARATOR . $name);
98 104 }
99 105 }
100 106
101 - public static function getFileUploadError($code) {
107 + public static function getFileUploadError($code)
108 + {
102 109 $errors = [
103 110 0 => __('Unknown upload error', 'bit-form'),
104 111 1 => __('The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'bit-form'),
105 112 2 => __('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', 'bit-form'),
@@ -111,9 +118,10 @@
111 118 ];
112 119 return $errors[$code];
113 120 }
114 121
115 - public static function fileCopy($tmpdir, $destinationDir, $file) {
122 + public static function fileCopy($tmpdir, $destinationDir, $file)
123 + {
116 124 $tmpFile = $tmpdir . DIRECTORY_SEPARATOR . $file;
117 125 $newFile = $destinationDir . DIRECTORY_SEPARATOR . $file;
118 126 if (file_exists($tmpFile)) {
119 127 copy($tmpFile, $newFile);
@@ -119,9 +127,10 @@
119 127 copy($tmpFile, $newFile);
120 128 }
121 129 }
122 130
123 - public static function tempDirToUploadDir($submitted_data, $fields, $formId, $entryID) {
131 + public static function tempDirToUploadDir($submitted_data, $fields, $formId, $entryID)
132 + {
124 133 $upload_dir = wp_upload_dir();
125 134 $tempDir = $upload_dir['basedir'] . '/bitforms/temp';
126 135 $destinationDir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formId . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR;
127 136 if (!is_dir($destinationDir)) {
@@ -151,9 +160,10 @@
151 160
152 161 return $submitted_data;
153 162 }
154 163
155 - private function getByteSizeByUnit($sizeString) {
164 + private function getByteSizeByUnit($sizeString)
165 + {
156 166 // split 2MB into 2 and MB
157 167 $size = preg_replace('/[^0-9\.]/', '', $sizeString);
158 168 $unit = preg_replace('/[^a-zA-Z]/', '', $sizeString);
159 169 $unit = strtolower($unit);
@@ -167,9 +177,10 @@
167 177 return $size;
168 178 }
169 179 }
170 180
171 - public function validation($field_key, $file_details, $form_id) {
181 + public function validation($field_key, $file_details, $form_id)
182 + {
172 183 if (!function_exists('wp_check_filetype_and_ext')) {
173 184 require_once ABSPATH . 'wp-admin/includes/file.php';
174 185 }
175 186
@@ -179,8 +190,9 @@
179 190 $fieldDetail = $field_content_details->{$field_key};
180 191 $fieldType = $fieldDetail->typ;
181 192 $maxSizeDetails = [];
182 193 $allowFileTypes = [];
194 + $maxSize = null;
183 195 if ('file-up' === $fieldType) {
184 196 $allowFileTypes = !empty($fieldDetail->config->allowedFileType) ? $fieldDetail->config->allowedFileType : [];
185 197 if (!empty($fieldDetail->config->allowMaxSize)) {
186 198 if (!empty($fieldDetail->config->maxSize)) {
@@ -206,17 +218,41 @@
206 218 }
207 219 if (!empty($maxSizeDetails['maxSize'])) {
208 220 $maxSize = $this->getByteSizeByUnit($maxSizeDetails['maxSize']);
209 221 }
222 + $maxTotalFileSize = null;
210 223 if (!empty($maxSizeDetails['maxTotalFileSize'])) {
211 224 $maxTotalFileSize = $this->getByteSizeByUnit($maxSizeDetails['maxTotalFileSize']);
212 225 }
213 226
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 + {
214 251 $errorMessage = [
215 252 'message' => '',
216 253 'error_type'=> '',
217 254 ];
218 -
219 255 if (is_array($file_details['name'])) {
220 256 $totalSize = 0;
221 257 foreach ($file_details['name'] as $key => $file) {
222 258 if (!empty($file)) {
@@ -233,9 +269,9 @@
233 269 return $validateState;
234 270 }
235 271 }
236 272 }
237 - if (!is_null($maxTotalFileSize) && $totalSize > $maxTotalFileSize) {
273 + if (isset($maxTotalFileSize) && !is_null($maxTotalFileSize) && $totalSize > $maxTotalFileSize) {
238 274 $errorMessage['message'] = __('Total File size is too large', 'bit-form');
239 275 $errorMessage['error_type'] = 'file_size_error';
240 276 return $errorMessage;
241 277 }
@@ -248,9 +284,10 @@
248 284
249 285 return $errorMessage;
250 286 }
251 287
252 - private function validateSingleFile($fieldType, $file, $allowTypes, $maxSize) {
288 + private function validateSingleFile($fieldType, &$file, $allowTypes, $maxSize = null)
289 + {
253 290 $fileName = sanitize_file_name($file['name']);
254 291 if (!empty($fileName)) {
255 292 $fileSize = $file['size'];
256 293 if (!empty($maxSize) && $fileSize > $maxSize) {
@@ -263,9 +300,13 @@
263 300 $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
264 301 $fileExtAllowedByWp = wp_check_filetype_and_ext($file['tmp_name'], $fileName);
265 302 $isAllowedFileType = in_array('.' . $fileExtension, $allowTypes);
266 303 if ('advanced-file-up' === $fieldType && !empty($allowTypes)) {
267 - $fileMimeType = mime_content_type($file['tmp_name']);
304 + if (function_exists('mime_content_type')) {
305 + $fileMimeType = mime_content_type($file['tmp_name']);
306 + } else {
307 + $fileMimeType = $fileExtAllowedByWp['type'];
308 + }
268 309 $isAllowedFileType = in_array($fileMimeType, $allowTypes);
269 310 }
270 311 if ((!empty($allowTypes) && !$isAllowedFileType) || (empty($allowTypes) && empty($fileExtAllowedByWp['ext']))) {
271 312 return [
@@ -272,7 +313,26 @@
272 313 'message' => __(($fileExtension ? ".{$fileExtension}" : 'empty') . ' file extension is not allowed', 'bit-form'),
273 314 'error_type'=> 'file_type_error',
274 315 ];
275 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 + }
329 + }
330 + }
331 +
332 + public static function deleteIsFileExists($path)
333 + {
334 + if (file_exists($path)) {
335 + wp_delete_file($path);
276 336 }
277 337 }
278 338 }