| @@ -3,10 +3,12 @@ | ||
| 3 | 3 | namespace BitCode\BitForm\Core\Util; |
| 4 | 4 | |
| 5 | 5 | use BitCode\BitForm\Core\Form\FormManager; |
| 6 | 6 | |
| 7 | -final class FileHandler { | |
| 8 | - public function rmrf($dir) { | |
| 7 | +final class FileHandler | |
| 8 | +{ | |
| 9 | + public function rmrf($dir) | |
| 10 | + { | |
| 9 | 11 | if (is_dir($dir)) { |
| 10 | 12 | $objects = scandir($dir); |
| 11 | 13 | foreach ($objects as $object) { |
| 12 | 14 | if ('.' !== $object && '..' !== $object) { |
| @@ -22,9 +24,10 @@ | ||
| 22 | 24 | unlink($dir); |
| 23 | 25 | } |
| 24 | 26 | } |
| 25 | 27 | |
| 26 | - public function cpyr($source, $destination) { | |
| 28 | + public function cpyr($source, $destination) | |
| 29 | + { | |
| 27 | 30 | if (is_dir($source)) { |
| 28 | 31 | mkdir($destination); |
| 29 | 32 | // chmod($destination, 0744); |
| 30 | 33 | $objects = scandir($source); |
| @@ -33,9 +36,9 @@ | ||
| 33 | 36 | if (is_dir($source . DIRECTORY_SEPARATOR . $object) && !is_link($source . DIRECTORY_SEPARATOR . $object)) { |
| 34 | 37 | cpyr($source . DIRECTORY_SEPARATOR . $object, $destination . DIRECTORY_SEPARATOR . $object); |
| 35 | 38 | } elseif (is_file($source . DIRECTORY_SEPARATOR . $object)) { |
| 36 | 39 | copy($source . DIRECTORY_SEPARATOR . $object, $destination . DIRECTORY_SEPARATOR . $object); |
| 37 | - // chmod($destination. DIRECTORY_SEPARATOR .$object, 0644); | |
| 40 | + // chmod($destination. DIRECTORY_SEPARATOR .$object, 0644); | |
| 38 | 41 | } else { |
| 39 | 42 | symlink($source . DIRECTORY_SEPARATOR . $object, $destination . DIRECTORY_SEPARATOR . $object); |
| 40 | 43 | } |
| 41 | 44 | } |
| @@ -44,9 +47,10 @@ | ||
| 44 | 47 | copy($source, $destination); |
| 45 | 48 | } |
| 46 | 49 | } |
| 47 | 50 | |
| 48 | - public function moveUploadedFiles($file_details, $form_id, $entry_id) { | |
| 51 | + public function moveUploadedFiles($file_details, $form_id, $entry_id) | |
| 52 | + { | |
| 49 | 53 | $file_upoalded = []; |
| 50 | 54 | $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $form_id . DIRECTORY_SEPARATOR . $entry_id; |
| 51 | 55 | wp_mkdir_p($_upload_dir); |
| 52 | 56 | if (is_array($file_details['name'])) { |
| @@ -90,9 +94,10 @@ | ||
| 90 | 94 | } |
| 91 | 95 | return $file_upoalded; |
| 92 | 96 | } |
| 93 | 97 | |
| 94 | - public function deleteFiles($form_id, $entry_id, $files) { | |
| 98 | + public function deleteFiles($form_id, $entry_id, $files) | |
| 99 | + { | |
| 95 | 100 | $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $form_id . DIRECTORY_SEPARATOR . $entry_id; |
| 96 | 101 | foreach ($files as $name) { |
| 97 | 102 | unlink($_upload_dir . DIRECTORY_SEPARATOR . $name); |
| 98 | 103 | } |
| @@ -97,9 +102,10 @@ | ||
| 97 | 102 | unlink($_upload_dir . DIRECTORY_SEPARATOR . $name); |
| 98 | 103 | } |
| 99 | 104 | } |
| 100 | 105 | |
| 101 | - public static function getFileUploadError($code) { | |
| 106 | + public static function getFileUploadError($code) | |
| 107 | + { | |
| 102 | 108 | $errors = [ |
| 103 | 109 | 0 => __('Unknown upload error', 'bit-form'), |
| 104 | 110 | 1 => __('The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'bit-form'), |
| 105 | 111 | 2 => __('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', 'bit-form'), |
| @@ -111,9 +117,10 @@ | ||
| 111 | 117 | ]; |
| 112 | 118 | return $errors[$code]; |
| 113 | 119 | } |
| 114 | 120 | |
| 115 | - public static function fileCopy($tmpdir, $destinationDir, $file) { | |
| 121 | + public static function fileCopy($tmpdir, $destinationDir, $file) | |
| 122 | + { | |
| 116 | 123 | $tmpFile = $tmpdir . DIRECTORY_SEPARATOR . $file; |
| 117 | 124 | $newFile = $destinationDir . DIRECTORY_SEPARATOR . $file; |
| 118 | 125 | if (file_exists($tmpFile)) { |
| 119 | 126 | copy($tmpFile, $newFile); |
| @@ -119,9 +126,10 @@ | ||
| 119 | 126 | copy($tmpFile, $newFile); |
| 120 | 127 | } |
| 121 | 128 | } |
| 122 | 129 | |
| 123 | - public static function tempDirToUploadDir($submitted_data, $fields, $formId, $entryID) { | |
| 130 | + public static function tempDirToUploadDir($submitted_data, $fields, $formId, $entryID) | |
| 131 | + { | |
| 124 | 132 | $upload_dir = wp_upload_dir(); |
| 125 | 133 | $tempDir = $upload_dir['basedir'] . '/bitforms/temp'; |
| 126 | 134 | $destinationDir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formId . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; |
| 127 | 135 | if (!is_dir($destinationDir)) { |
| @@ -151,9 +159,10 @@ | ||
| 151 | 159 | |
| 152 | 160 | return $submitted_data; |
| 153 | 161 | } |
| 154 | 162 | |
| 155 | - private function getByteSizeByUnit($sizeString) { | |
| 163 | + private function getByteSizeByUnit($sizeString) | |
| 164 | + { | |
| 156 | 165 | // split 2MB into 2 and MB |
| 157 | 166 | $size = preg_replace('/[^0-9\.]/', '', $sizeString); |
| 158 | 167 | $unit = preg_replace('/[^a-zA-Z]/', '', $sizeString); |
| 159 | 168 | $unit = strtolower($unit); |
| @@ -167,9 +176,10 @@ | ||
| 167 | 176 | return $size; |
| 168 | 177 | } |
| 169 | 178 | } |
| 170 | 179 | |
| 171 | - public function validation($field_key, $file_details, $form_id) { | |
| 180 | + public function validation($field_key, $file_details, $form_id) | |
| 181 | + { | |
| 172 | 182 | if (!function_exists('wp_check_filetype_and_ext')) { |
| 173 | 183 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 174 | 184 | } |
| 175 | 185 | |
| @@ -206,17 +216,41 @@ | ||
| 206 | 216 | } |
| 207 | 217 | if (!empty($maxSizeDetails['maxSize'])) { |
| 208 | 218 | $maxSize = $this->getByteSizeByUnit($maxSizeDetails['maxSize']); |
| 209 | 219 | } |
| 220 | + $maxTotalFileSize = null; | |
| 210 | 221 | if (!empty($maxSizeDetails['maxTotalFileSize'])) { |
| 211 | 222 | $maxTotalFileSize = $this->getByteSizeByUnit($maxSizeDetails['maxTotalFileSize']); |
| 212 | 223 | } |
| 213 | 224 | |
| 225 | + if ($formManager->isRepeatedField($field_key)) { | |
| 226 | + foreach ($file_details['name'] as $rowIndex => $file) { | |
| 227 | + if (!empty($file)) { | |
| 228 | + $fileDetails = [ | |
| 229 | + 'name' => $file, | |
| 230 | + 'type' => $file_details['type'][$rowIndex], | |
| 231 | + 'tmp_name' => $file_details['tmp_name'][$rowIndex], | |
| 232 | + 'error' => $file_details['error'][$rowIndex], | |
| 233 | + 'size' => $file_details['size'][$rowIndex], | |
| 234 | + ]; | |
| 235 | + $validateState = $this->validateFileInfo($fieldType, $fileDetails, $allowFileTypes, $maxSize, $maxTotalFileSize); | |
| 236 | + if (!empty($validateState) && !empty($validateState['message'])) { | |
| 237 | + return $validateState; | |
| 238 | + } | |
| 239 | + } | |
| 240 | + } | |
| 241 | + } else { | |
| 242 | + return $this->validateFileInfo($fieldType, $file_details, $allowFileTypes, $maxSize, $maxTotalFileSize); | |
| 243 | + } | |
| 244 | + return []; | |
| 245 | + } | |
| 246 | + | |
| 247 | + private function validateFileInfo($fieldType, $file_details, $allowFileTypes, $maxSize, $maxTotalFileSize) | |
| 248 | + { | |
| 214 | 249 | $errorMessage = [ |
| 215 | 250 | 'message' => '', |
| 216 | 251 | 'error_type'=> '', |
| 217 | 252 | ]; |
| 218 | - | |
| 219 | 253 | if (is_array($file_details['name'])) { |
| 220 | 254 | $totalSize = 0; |
| 221 | 255 | foreach ($file_details['name'] as $key => $file) { |
| 222 | 256 | if (!empty($file)) { |
| @@ -233,9 +267,9 @@ | ||
| 233 | 267 | return $validateState; |
| 234 | 268 | } |
| 235 | 269 | } |
| 236 | 270 | } |
| 237 | - if (!is_null($maxTotalFileSize) && $totalSize > $maxTotalFileSize) { | |
| 271 | + if (isset($maxTotalFileSize) && !is_null($maxTotalFileSize) && $totalSize > $maxTotalFileSize) { | |
| 238 | 272 | $errorMessage['message'] = __('Total File size is too large', 'bit-form'); |
| 239 | 273 | $errorMessage['error_type'] = 'file_size_error'; |
| 240 | 274 | return $errorMessage; |
| 241 | 275 | } |
| @@ -248,9 +282,10 @@ | ||
| 248 | 282 | |
| 249 | 283 | return $errorMessage; |
| 250 | 284 | } |
| 251 | 285 | |
| 252 | - private function validateSingleFile($fieldType, $file, $allowTypes, $maxSize) { | |
| 286 | + private function validateSingleFile($fieldType, $file, $allowTypes, $maxSize) | |
| 287 | + { | |
| 253 | 288 | $fileName = sanitize_file_name($file['name']); |
| 254 | 289 | if (!empty($fileName)) { |
| 255 | 290 | $fileSize = $file['size']; |
| 256 | 291 | if (!empty($maxSize) && $fileSize > $maxSize) { |