| 1 |
<?php |
| 2 |
namespace BitCode\BitForm\Core\Util; |
| 3 |
|
| 4 |
final class FileHandler |
| 5 |
{ |
| 6 |
public function rmrf($dir) |
| 7 |
{ |
| 8 |
if (is_dir($dir)) { |
| 9 |
$objects = scandir($dir); |
| 10 |
foreach ($objects as $object) { |
| 11 |
if ($object != "." && $object != "..") { |
| 12 |
if (is_dir($dir. DIRECTORY_SEPARATOR .$object) && !is_link($dir.DIRECTORY_SEPARATOR.$object)) { |
| 13 |
$this->rmrf($dir. DIRECTORY_SEPARATOR .$object); |
| 14 |
} else { |
| 15 |
unlink($dir. DIRECTORY_SEPARATOR .$object); |
| 16 |
} |
| 17 |
} |
| 18 |
} |
| 19 |
rmdir($dir); |
| 20 |
} else { |
| 21 |
unlink($dir); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
public function cpyr($source, $destination) |
| 26 |
{ |
| 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 |
{ |
| 50 |
$file_upoalded = array(); |
| 51 |
$_upload_dir = BITFORMS_UPLOAD_DIR.DIRECTORY_SEPARATOR.$form_id.DIRECTORY_SEPARATOR.$entry_id; |
| 52 |
wp_mkdir_p($_upload_dir); |
| 53 |
if (is_array($file_details['name'])) { |
| 54 |
foreach ($file_details['name'] as $key => $value) { |
| 55 |
//check accepted filetype in_array($file_details['name'][$key], $supported_files) else \ |
| 56 |
if (!empty($value)) { |
| 57 |
$fileNameCount = 1; |
| 58 |
// $file_upoalded[$key] = time()."_$value"; |
| 59 |
$file_upoalded[$key] = sanitize_file_name($value); |
| 60 |
while (file_exists($_upload_dir.DIRECTORY_SEPARATOR.$file_upoalded[$key])) { |
| 61 |
$file_upoalded[$key] = sanitize_file_name(preg_replace("/(.[a-z A-Z 0-9]+)$/", "__{$fileNameCount}$1", $value)); |
| 62 |
$fileNameCount = $fileNameCount + 1; |
| 63 |
if ($fileNameCount === 11) { |
| 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 |
$file_upoalded[0] = sanitize_file_name(preg_replace("/(.[a-z A-Z 0-9]+)$/", "__{$fileNameCount}$1", $file_details['name'])); |
| 79 |
$fileNameCount = $fileNameCount + 1; |
| 80 |
if ($fileNameCount === 11) { |
| 81 |
break; |
| 82 |
} |
| 83 |
} |
| 84 |
$move_status = \move_uploaded_file($file_details['tmp_name'], $_upload_dir.DIRECTORY_SEPARATOR.$file_upoalded[0]); |
| 85 |
if (!$move_status) { |
| 86 |
unset($file_upoalded[0]); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
return $file_upoalded; |
| 91 |
} |
| 92 |
|
| 93 |
public function deleteFiles($form_id, $entry_id, $files) |
| 94 |
{ |
| 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 |
|