| @@ -2,8 +2,9 @@ | ||
| 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 | 8 | final class FileHandler |
| 8 | 9 | { |
| 9 | 10 | public function rmrf($dir) |
| @@ -14,15 +15,15 @@ | ||
| 14 | 15 | if ('.' !== $object && '..' !== $object) { |
| 15 | 16 | if (is_dir($dir . DIRECTORY_SEPARATOR . $object) && !is_link($dir . DIRECTORY_SEPARATOR . $object)) { |
| 16 | 17 | $this->rmrf($dir . DIRECTORY_SEPARATOR . $object); |
| 17 | 18 | } else { |
| 18 | - unlink($dir . DIRECTORY_SEPARATOR . $object); | |
| 19 | + wp_delete_file($dir . DIRECTORY_SEPARATOR . $object); | |
| 19 | 20 | } |
| 20 | 21 | } |
| 21 | 22 | } |
| 22 | 23 | rmdir($dir); |
| 23 | 24 | } else { |
| 24 | - unlink($dir); | |
| 25 | + wp_delete_file($dir); | |
| 25 | 26 | } |
| 26 | 27 | } |
| 27 | 28 | |
| 28 | 29 | public function cpyr($source, $destination) |
| @@ -98,9 +99,9 @@ | ||
| 98 | 99 | public function deleteFiles($form_id, $entry_id, $files) |
| 99 | 100 | { |
| 100 | 101 | $_upload_dir = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $form_id . DIRECTORY_SEPARATOR . $entry_id; |
| 101 | 102 | foreach ($files as $name) { |
| 102 | - unlink($_upload_dir . DIRECTORY_SEPARATOR . $name); | |
| 103 | + wp_delete_file($_upload_dir . DIRECTORY_SEPARATOR . $name); | |
| 103 | 104 | } |
| 104 | 105 | } |
| 105 | 106 | |
| 106 | 107 | public static function getFileUploadError($code) |
| @@ -283,9 +284,9 @@ | ||
| 283 | 284 | |
| 284 | 285 | return $errorMessage; |
| 285 | 286 | } |
| 286 | 287 | |
| 287 | - private function validateSingleFile($fieldType, $file, $allowTypes, $maxSize = null) | |
| 288 | + private function validateSingleFile($fieldType, &$file, $allowTypes, $maxSize = null) | |
| 288 | 289 | { |
| 289 | 290 | $fileName = sanitize_file_name($file['name']); |
| 290 | 291 | if (!empty($fileName)) { |
| 291 | 292 | $fileSize = $file['size']; |
| @@ -312,7 +313,26 @@ | ||
| 312 | 313 | 'message' => __(($fileExtension ? ".{$fileExtension}" : 'empty') . ' file extension is not allowed', 'bit-form'), |
| 313 | 314 | 'error_type'=> 'file_type_error', |
| 314 | 315 | ]; |
| 315 | 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); | |
| 316 | 336 | } |
| 317 | 337 | } |
| 318 | 338 | } |