| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Filesystem; |
| 4 |
|
| 5 |
class ZipArchive |
| 6 |
{ |
| 7 |
public function __construct() |
| 8 |
{ |
| 9 |
if (self::has_requirements_met()) { |
| 10 |
add_filter('iwp/importer/file_uploaded/file_path', [$this, 'read_file_matching_ext'], 10, 2); |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
public static function has_requirements_met() |
| 15 |
{ |
| 16 |
return class_exists('\ZipArchive'); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* @param string $input_filepath |
| 21 |
* @param \ImportWP\Common\Model\ImporterModel $importer_model |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
function read_file_matching_ext($input_filepath, $importer_model) |
| 25 |
{ |
| 26 |
$ext = $this->get_ext($input_filepath); |
| 27 |
|
| 28 |
switch ($ext) { |
| 29 |
case 'zip': |
| 30 |
|
| 31 |
$zip = new \ZipArchive(); |
| 32 |
if (true !== $zip->open($input_filepath)) { |
| 33 |
return $input_filepath; |
| 34 |
} |
| 35 |
|
| 36 |
$file_found = false; |
| 37 |
for ($i = 0; $i < $zip->numFiles; $i++) { |
| 38 |
|
| 39 |
$filename = $zip->getNameIndex($i); |
| 40 |
|
| 41 |
// TODO: we should be able to get file based on desired extension |
| 42 |
if (preg_match('/\.(xml|csv|blm)$/', $filename) === 0 && $zip->numFiles > 1) { |
| 43 |
continue; |
| 44 |
} |
| 45 |
|
| 46 |
$file_found = $filename; |
| 47 |
break; |
| 48 |
} |
| 49 |
|
| 50 |
if (!$file_found) { |
| 51 |
return $input_filepath; |
| 52 |
} |
| 53 |
|
| 54 |
$file_found_ext = $this->get_ext($file_found); |
| 55 |
$output_filepath = $input_filepath . '.' . $file_found_ext; |
| 56 |
|
| 57 |
$file_contents = $zip->getFromName($file_found); |
| 58 |
if (!$file_contents) { |
| 59 |
return $input_filepath; |
| 60 |
} |
| 61 |
|
| 62 |
$zip->close(); |
| 63 |
|
| 64 |
$output_filepath = $this->unique_filename($output_filepath); |
| 65 |
file_put_contents($output_filepath, $file_contents); |
| 66 |
|
| 67 |
update_post_meta($importer_model->getId(), '_iwp_zip', $input_filepath); |
| 68 |
|
| 69 |
return $output_filepath; |
| 70 |
case 'gz': |
| 71 |
|
| 72 |
$output_filepath = $this->get_output_filepath($input_filepath); |
| 73 |
$output_filepath = $this->unique_filename($output_filepath); |
| 74 |
$sfp = gzopen($input_filepath, "rb"); |
| 75 |
$fp = fopen($output_filepath, "w"); |
| 76 |
|
| 77 |
while (!gzeof($sfp)) { |
| 78 |
$string = gzread($sfp, 4096); |
| 79 |
fwrite($fp, $string, strlen($string)); |
| 80 |
} |
| 81 |
gzclose($sfp); |
| 82 |
fclose($fp); |
| 83 |
|
| 84 |
if ($input_filepath !== $output_filepath) { |
| 85 |
@unlink($input_filepath); |
| 86 |
} |
| 87 |
|
| 88 |
return $output_filepath; |
| 89 |
} |
| 90 |
|
| 91 |
return $input_filepath; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
function get_ext($filepath) |
| 97 |
{ |
| 98 |
$file_parts = explode('.', basename($filepath)); |
| 99 |
return $file_parts[count($file_parts) - 1]; |
| 100 |
} |
| 101 |
|
| 102 |
function get_output_filepath($filepath) |
| 103 |
{ |
| 104 |
$file_parts = explode('.', $filepath); |
| 105 |
array_pop($file_parts); |
| 106 |
|
| 107 |
$last_part = $file_parts[count($file_parts) - 1]; |
| 108 |
$matches = []; |
| 109 |
if (preg_match('/([^_]+)/', $last_part, $matches) !== false) { |
| 110 |
$file_parts[count($file_parts) - 1] = $matches[1]; |
| 111 |
} |
| 112 |
|
| 113 |
return implode('.', $file_parts); |
| 114 |
} |
| 115 |
|
| 116 |
function unique_filename($filepath) |
| 117 |
{ |
| 118 |
$filename = basename($filepath); |
| 119 |
$dir = substr($filepath, 0, -strlen($filename)); |
| 120 |
$filename = wp_unique_filename($dir, basename($filename)); |
| 121 |
|
| 122 |
return $dir . '/' . $filename; |
| 123 |
} |
| 124 |
} |
| 125 |
|