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

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

204 lines 7.6 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 private function getByteSizeByUnit($sizeString) {
49 // split 2MB into 2 and MB
50 $size = preg_replace('/[^0-9\.]/', '', $sizeString);
51 $unit = preg_replace('/[^a-zA-Z]/', '', $sizeString);
52 $unit = strtolower($unit);
53 if ('kb' === $unit) {
54 return $size * 1024;
55 } elseif ('mb' === $unit) {
56 return $size * 1024 * 1024;
57 } elseif ('gb' === $unit) {
58 return $size * 1024 * 1024 * 1024;
59 } else {
60 return $size;
61 }
62 }
63
64 public function validation($field_key, $file_details, $form_id) {
65 if (!function_exists('wp_check_filetype_and_ext')) {
66 require_once ABSPATH . 'wp-admin/includes/file.php';
67 }
68
69 $formManager = new FormManager($form_id);
70 $form_contents = $formManager->getFormContent();
71 $field_content_details = $form_contents->fields;
72 $fieldDetail = $field_content_details->{$field_key};
73 $fieldType = $fieldDetail->typ;
74 $maxSizeDetails = [];
75 $allowFileTypes = [];
76 if ('file-up' === $fieldType) {
77 $allowFileTypes = !empty($fieldDetail->exts) ? $fieldDetail->exts : '';
78 if (!empty($fieldDetail->mxUp)) {
79 if (!empty($fieldDetail->mxUp)) {
80 $maxSizeDetails['maxSize'] = $fieldDetail->mxUp . $fieldDetail->unit;
81 }
82 }
83 if (!empty($allowFileTypes)) {
84 $allowFileTypes = explode(',', $allowFileTypes);
85 }
86 }
87 if (!empty($maxSizeDetails['maxSize'])) {
88 $maxSize = $this->getByteSizeByUnit($maxSizeDetails['maxSize']);
89 }
90
91 $errorMessage = [
92 'message' => '',
93 'error_type'=> '',
94 ];
95
96 if (is_array($file_details['name'])) {
97 $totalSize = 0;
98 foreach ($file_details['name'] as $key => $file) {
99 if (!empty($file)) {
100 $fileInfo = [
101 'name' => $file,
102 'type' => $file_details['type'][$key],
103 'tmp_name' => $file_details['tmp_name'][$key],
104 'error' => $file_details['error'][$key],
105 'size' => $file_details['size'][$key],
106 ];
107 $totalSize += $fileInfo['size'];
108 $validateState = $this->validateSingleFile($fieldType, $fileInfo, $allowFileTypes, $maxSize);
109 if (!empty($validateState)) {
110 return $validateState;
111 }
112 }
113 }
114 if (!is_null($maxTotalFileSize) && $totalSize > $maxTotalFileSize) {
115 $errorMessage['message'] = __('Total File size is too large', 'bit-form');
116 $errorMessage['error_type'] = 'file_size_error';
117 return $errorMessage;
118 }
119 } else {
120 $validateState = $this->validateSingleFile($fieldType, $file_details, $allowFileTypes, $maxSize);
121 if (!empty($validateState)) {
122 return $validateState;
123 }
124 }
125
126 return $errorMessage;
127 }
128
129 private function validateSingleFile($fieldType, $file, $allowTypes, $maxSize) {
130 $fileName = sanitize_file_name($file['name']);
131 if (!empty($fileName)) {
132 $fileSize = $file['size'];
133 if (!empty($maxSize) && $fileSize > $maxSize) {
134 return [
135 'message' => __('File size is too large', 'bit-form'),
136 'error_type'=> 'file_size_error',
137 ];
138 }
139
140 $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
141 $fileExtAllowedByWp = wp_check_filetype_and_ext($file['tmp_name'], $fileName);
142 $isAllowedFileType = in_array('.' . $fileExtension, $allowTypes);
143 if ((!empty($allowTypes) && !$isAllowedFileType) || (empty($allowTypes) && empty($fileExtAllowedByWp['ext']))) {
144 return [
145 'message' => __(($fileExtension ? ".{$fileExtension}" : 'empty') . ' file extension is not allowed', 'bit-form'),
146 'error_type'=> 'file_type_error',
147 ];
148 }
149 }
150 }
151
152 public function moveUploadedFiles($file_details, $form_id, $entry_id) {
153 $file_upoalded = [];
154 $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $form_id . DIRECTORY_SEPARATOR . $entry_id;
155 wp_mkdir_p($_upload_dir);
156 if (is_array($file_details['name'])) {
157 foreach ($file_details['name'] as $key => $value) {
158 //check accepted filetype in_array($file_details['name'][$key], $supported_files) else \
159 if (!empty($value)) {
160 $fileNamePattern = '/\_/';
161 $file_upoalded[$key] = preg_replace($fileNamePattern, '', $value);
162 $extension = pathinfo($file_upoalded[$key], PATHINFO_EXTENSION);
163 if (in_array($extension, ['php', 'html', 'xhtml', 'js'])) {
164 $extension = 'txt';
165 }
166 $uniqueName = wp_generate_uuid4();
167 $UploadedFieldName = $uniqueName . '.' . $extension;
168 $file_upoalded[$key] = $UploadedFieldName . '_' . $file_upoalded[$key];
169 $move_status = \move_uploaded_file($file_details['tmp_name'][$key], $_upload_dir . DIRECTORY_SEPARATOR . $UploadedFieldName);
170 if (!$move_status) {
171 unset($file_upoalded[$key]);
172 }
173 }
174 }
175 } else {
176 if (!empty($file_details['name'])) {
177 $fileNamePattern = '/\_/';
178 $fileName = sanitize_file_name($file_details['name']);
179 $file_upoalded[0] = preg_replace($fileNamePattern, '', $fileName);
180 $extension = pathinfo($file_upoalded[0], PATHINFO_EXTENSION);
181 if (in_array($extension, ['php', 'html', 'xhtml', 'js'])) {
182 $extension = 'txt';
183 }
184 $uniqueName = wp_generate_uuid4();
185 $UploadedFieldName = $uniqueName . '.' . $extension;
186 $move_status = \move_uploaded_file($file_details['tmp_name'], $_upload_dir . DIRECTORY_SEPARATOR . $UploadedFieldName);
187 $file_upoalded[0] = $UploadedFieldName . '_' . $file_upoalded[0];
188
189 if (!$move_status) {
190 unset($file_upoalded[0]);
191 }
192 }
193 }
194 return $file_upoalded;
195 }
196
197 public function deleteFiles($form_id, $entry_id, $files) {
198 $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $form_id . DIRECTORY_SEPARATOR . $entry_id;
199 foreach ($files as $name) {
200 unlink($_upload_dir . DIRECTORY_SEPARATOR . $name);
201 }
202 }
203 }
204