PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
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
bit-form / includes / Core / Util / FileHandler.php

FileHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.0, at includes/Core/Util/FileHandler.php

279 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Util;
4
5 use BitCode\BitForm\Core\Form\FormManager;
6
7 final class FileHandler {
8 public function rmrf($dir) {
9 if (is_dir($dir)) {
10 $objects = scandir($dir);
11 foreach ($objects as $object) {
12 if ('.' !== $object && '..' !== $object) {
13 if (is_dir($dir . DIRECTORY_SEPARATOR . $object) && !is_link($dir . DIRECTORY_SEPARATOR . $object)) {
14 $this->rmrf($dir . DIRECTORY_SEPARATOR . $object);
15 } else {
16 unlink($dir . DIRECTORY_SEPARATOR . $object);
17 }
18 }
19 }
20 rmdir($dir);
21 } else {
22 unlink($dir);
23 }
24 }
25
26 public function cpyr($source, $destination) {
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);
45 }
46 }
47
48 public function moveUploadedFiles($file_details, $form_id, $entry_id) {
49 $file_upoalded = [];
50 $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $form_id . DIRECTORY_SEPARATOR . $entry_id;
51 wp_mkdir_p($_upload_dir);
52 if (is_array($file_details['name'])) {
53 foreach ($file_details['name'] as $key => $value) {
54 //check accepted filetype in_array($file_details['name'][$key], $supported_files) else \
55 if (!empty($value)) {
56 $fileNameCount = 1;
57 // $file_upoalded[$key] = time()."_$value";
58 $file_upoalded[$key] = sanitize_file_name($value);
59 while (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $file_upoalded[$key])) {
60 $fileNameWithSeparator = BITFORMS_BF_SEPARATOR . $fileNameCount;
61 $file_upoalded[$key] = sanitize_file_name(preg_replace('/(.[a-z A-Z 0-9]+)$/', "{$fileNameWithSeparator}$1", $value));
62 $fileNameCount = $fileNameCount + 1;
63 if (11 === $fileNameCount) {
64 break;
65 }
66 }
67 $move_status = \move_uploaded_file($file_details['tmp_name'][$key], $_upload_dir . DIRECTORY_SEPARATOR . $file_upoalded[$key]);
68 if (!$move_status) {
69 unset($file_upoalded[$key]);
70 }
71 }
72 }
73 } else {
74 if (!empty($file_details['name'])) {
75 $fileNameCount = 1;
76 $file_upoalded[0] = sanitize_file_name($file_details['name']);
77 while (file_exists($_upload_dir . DIRECTORY_SEPARATOR . $file_upoalded[0])) {
78 $fileNameWithSeparator = BITFORMS_BF_SEPARATOR . $fileNameCount;
79 $file_upoalded[0] = sanitize_file_name(preg_replace('/(.[a-z A-Z 0-9]+)$/', "{$fileNameWithSeparator}$1", $file_details['name']));
80 $fileNameCount = $fileNameCount + 1;
81 if (11 === $fileNameCount) {
82 break;
83 }
84 }
85 $move_status = \move_uploaded_file($file_details['tmp_name'], $_upload_dir . DIRECTORY_SEPARATOR . $file_upoalded[0]);
86 if (!$move_status) {
87 unset($file_upoalded[0]);
88 }
89 }
90 }
91 return $file_upoalded;
92 }
93
94 public function deleteFiles($form_id, $entry_id, $files) {
95 $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $form_id . DIRECTORY_SEPARATOR . $entry_id;
96 foreach ($files as $name) {
97 unlink($_upload_dir . DIRECTORY_SEPARATOR . $name);
98 }
99 }
100
101 public static function getFileUploadError($code) {
102 $errors = [
103 0 => __('Unknown upload error', 'bit-form'),
104 1 => __('The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'bit-form'),
105 2 => __('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', 'bit-form'),
106 3 => __('The uploaded file was only partially uploaded.', 'bit-form'),
107 4 => __('No file was uploaded.', 'bit-form'),
108 6 => __('Missing a temporary folder.', 'bit-form'),
109 7 => __('Failed to write file to disk.', 'bit-form'),
110 8 => __('A PHP extension stopped the file upload.', 'bit-form'),
111 ];
112 return $errors[$code];
113 }
114
115 public static function fileCopy($tmpdir, $destinationDir, $file) {
116 $tmpFile = $tmpdir . DIRECTORY_SEPARATOR . $file;
117 $newFile = $destinationDir . DIRECTORY_SEPARATOR . $file;
118 if (file_exists($tmpFile)) {
119 copy($tmpFile, $newFile);
120 }
121 }
122
123 public static function tempDirToUploadDir($submitted_data, $fields, $formId, $entryID) {
124 $upload_dir = wp_upload_dir();
125 $tempDir = $upload_dir['basedir'] . '/bitforms/temp';
126 $destinationDir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formId . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR;
127 if (!is_dir($destinationDir)) {
128 mkdir($destinationDir);
129 }
130
131 foreach ($submitted_data as $key => $data) {
132 if (isset($fields[$key]) && 'advanced-file-up' === $fields[$key]['type']) {
133 $files = $data;
134 $fldData = $submitted_data[$key];
135 $files = explode(',', $fldData);
136 if (is_array($files) && count($files) > 0) {
137 foreach ($files as $file) {
138 self::fileCopy($tempDir, $destinationDir, trim($file));
139 }
140 } else {
141 self::fileCopy($tempDir, $destinationDir, trim($files));
142 }
143 if (!empty($files)) {
144 $submitted_data[$key] = $files;
145 }
146 }
147 }
148 array_map('unlink', array_filter(
149 (array) array_merge(glob("$tempDir/*"))
150 ));
151
152 return $submitted_data;
153 }
154
155 private function getByteSizeByUnit($sizeString) {
156 // split 2MB into 2 and MB
157 $size = preg_replace('/[^0-9\.]/', '', $sizeString);
158 $unit = preg_replace('/[^a-zA-Z]/', '', $sizeString);
159 $unit = strtolower($unit);
160 if ('kb' === $unit) {
161 return $size * 1024;
162 } elseif ('mb' === $unit) {
163 return $size * 1024 * 1024;
164 } elseif ('gb' === $unit) {
165 return $size * 1024 * 1024 * 1024;
166 } else {
167 return $size;
168 }
169 }
170
171 public function validation($field_key, $file_details, $form_id) {
172 if (!function_exists('wp_check_filetype_and_ext')) {
173 require_once ABSPATH . 'wp-admin/includes/file.php';
174 }
175
176 $formManager = new FormManager($form_id);
177 $form_contents = $formManager->getFormContent();
178 $field_content_details = $form_contents->fields;
179 $fieldDetail = $field_content_details->{$field_key};
180 $fieldType = $fieldDetail->typ;
181 $maxSizeDetails = [];
182 $allowFileTypes = [];
183 if ('file-up' === $fieldType) {
184 $allowFileTypes = !empty($fieldDetail->config->allowedFileType) ? $fieldDetail->config->allowedFileType : [];
185 if (!empty($fieldDetail->config->allowMaxSize)) {
186 if (!empty($fieldDetail->config->maxSize)) {
187 $maxSizeDetails['maxSize'] = $fieldDetail->config->maxSize . $fieldDetail->config->sizeUnit;
188 }
189 if (!empty($fieldDetail->config->isItTotalMax)) {
190 $maxSizeDetails['maxTotalFileSize'] = $fieldDetail->config->maxSize . $fieldDetail->config->sizeUnit;
191 }
192 }
193 if (!empty($allowFileTypes)) {
194 $allowFileTypes = explode(',', $allowFileTypes);
195 }
196 } elseif ('advanced-file-up' === $fieldType) {
197 $allowFileTypes = !empty($fieldDetail->config->allowFileTypeValidation) ? $fieldDetail->config->acceptedFileTypes : [];
198 if (!empty($fieldDetail->config->allowFileSizeValidation)) {
199 if (!empty($fieldDetail->config->maxFileSize)) {
200 $maxSizeDetails['maxSize'] = $fieldDetail->config->maxFileSize;
201 }
202 if (!empty($fieldDetail->config->maxTotalFileSize)) {
203 $maxSizeDetails['maxTotalFileSize'] = $fieldDetail->config->maxTotalFileSize;
204 }
205 }
206 }
207 if (!empty($maxSizeDetails['maxSize'])) {
208 $maxSize = $this->getByteSizeByUnit($maxSizeDetails['maxSize']);
209 }
210 if (!empty($maxSizeDetails['maxTotalFileSize'])) {
211 $maxTotalFileSize = $this->getByteSizeByUnit($maxSizeDetails['maxTotalFileSize']);
212 }
213
214 $errorMessage = [
215 'message' => '',
216 'error_type'=> '',
217 ];
218
219 if (is_array($file_details['name'])) {
220 $totalSize = 0;
221 foreach ($file_details['name'] as $key => $file) {
222 if (!empty($file)) {
223 $fileInfo = [
224 'name' => $file,
225 'type' => $file_details['type'][$key],
226 'tmp_name' => $file_details['tmp_name'][$key],
227 'error' => $file_details['error'][$key],
228 'size' => $file_details['size'][$key],
229 ];
230 $totalSize += $fileInfo['size'];
231 $validateState = $this->validateSingleFile($fieldType, $fileInfo, $allowFileTypes, $maxSize);
232 if (!empty($validateState)) {
233 return $validateState;
234 }
235 }
236 }
237 if (!is_null($maxTotalFileSize) && $totalSize > $maxTotalFileSize) {
238 $errorMessage['message'] = __('Total File size is too large', 'bit-form');
239 $errorMessage['error_type'] = 'file_size_error';
240 return $errorMessage;
241 }
242 } else {
243 $validateState = $this->validateSingleFile($fieldType, $file_details, $allowFileTypes, $maxSize);
244 if (!empty($validateState)) {
245 return $validateState;
246 }
247 }
248
249 return $errorMessage;
250 }
251
252 private function validateSingleFile($fieldType, $file, $allowTypes, $maxSize) {
253 $fileName = sanitize_file_name($file['name']);
254 if (!empty($fileName)) {
255 $fileSize = $file['size'];
256 if (!empty($maxSize) && $fileSize > $maxSize) {
257 return [
258 'message' => __('File size is too large', 'bit-form'),
259 'error_type'=> 'file_size_error',
260 ];
261 }
262
263 $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
264 $fileExtAllowedByWp = wp_check_filetype_and_ext($file['tmp_name'], $fileName);
265 $isAllowedFileType = in_array('.' . $fileExtension, $allowTypes);
266 if ('advanced-file-up' === $fieldType && !empty($allowTypes)) {
267 $fileMimeType = mime_content_type($file['tmp_name']);
268 $isAllowedFileType = in_array($fileMimeType, $allowTypes);
269 }
270 if ((!empty($allowTypes) && !$isAllowedFileType) || (empty($allowTypes) && empty($fileExtAllowedByWp['ext']))) {
271 return [
272 'message' => __(($fileExtension ? ".{$fileExtension}" : 'empty') . ' file extension is not allowed', 'bit-form'),
273 'error_type'=> 'file_type_error',
274 ];
275 }
276 }
277 }
278 }
279