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