From 8c0ee39e2a4cf2a8a60d4b65d23920979f390825 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sat, 18 Mar 2023 20:05:11 +0100 Subject: [PATCH] use new Zip helper --- src/module/zip.php | 125 ++-------------------------------------- src/module/zip/Zip.php | 128 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 121 deletions(-) create mode 100644 src/module/zip/Zip.php diff --git a/src/module/zip.php b/src/module/zip.php index f836b0c..40c3b88 100644 --- a/src/module/zip.php +++ b/src/module/zip.php @@ -24,7 +24,6 @@ use Dotclear\Plugin\improve\Action; use form; use path; use files; -use fileZip; /** * Improve action module zip @@ -65,6 +64,8 @@ class zip extends action protected function init(): bool { + require_once implode(DIRECTORY_SEPARATOR, [__DIR__, 'zip', 'Zip.php']); + $this->setProperties([ 'id' => 'zip', 'name' => __('Zip module'), @@ -162,7 +163,7 @@ class zip extends action ); $this->setSuccess(sprintf(__('Prepare excluded files "%s"'), implode(', ', $exclude))); if (!empty($this->getSetting('pack_nocomment'))) { - zipFileZip::$remove_comment = true; + zip\Zip::$remove_comment = true; $this->setSuccess(__('Prepare comment removal')); } if (!empty($this->getSetting('pack_filename'))) { @@ -204,8 +205,7 @@ class zip extends action return; } @set_time_limit(300); - $fp = fopen($path, 'wb'); - $zip = new zipFileZip($fp); + $zip = new zip\Zip($path); foreach ($exclude as $e) { $e = '#(^|/)(' . str_replace( ['.', '*'], @@ -219,126 +219,9 @@ class zip extends action $this->module['id'], true ); - $zip->write(); $zip->close(); unset($zip); $this->setSuccess(sprintf(__('Zip module into "%s"'), $path)); } } - -class zipFileZip extends fileZip -{ - /** @var boolean Should remove comments from files */ - public static $remove_comment = false; - - /** - * Replace clearbrick fileZip::writeFile - * - * @param string $name Name - * @param string $file File - * @param int $size Size - * @param int $mtime Mtime - * - * @return void - */ - protected function writeFile($name, $file, $size, $mtime) - { - if (!isset($this->entries[$name])) { - return; - } - - $size = filesize($file); - $this->memoryAllocate($size * 3); - - $content = (string) file_get_contents($file); - - if (self::$remove_comment && substr($file, -4) == '.php') { - $content = self::removePHPComment($content); - } - - $unc_len = strlen($content); - $crc = crc32($content); - $zdata = (string) gzdeflate($content); - $c_len = strlen($zdata); - - unset($content); - - $mdate = $this->makeDate($mtime); - $mtime = $this->makeTime($mtime); - - # Data descriptor - $data_desc = "\x50\x4b\x03\x04" . - "\x14\x00" . # ver needed to extract - "\x00\x00" . # gen purpose bit flag - "\x08\x00" . # compression method - pack('v', $mtime) . # last mod time - pack('v', $mdate) . # last mod date - pack('V', $crc) . # crc32 - pack('V', $c_len) . # compressed filesize - pack('V', $unc_len) . # uncompressed filesize - pack('v', strlen($name)) . # length of filename - pack('v', 0) . # extra field length - $name . # end of "local file header" segment - $zdata . # "file data" segment - pack('V', $crc) . # crc32 - pack('V', $c_len) . # compressed filesize - pack('V', $unc_len); # uncompressed filesize - - fwrite($this->fp, $data_desc); - unset($zdata); - - $new_offset = $this->old_offset + strlen($data_desc); - - # Add to central directory record - $cdrec = "\x50\x4b\x01\x02" . - "\x00\x00" . # version made by - "\x14\x00" . # version needed to extract - "\x00\x00" . # gen purpose bit flag - "\x08\x00" . # compression method - pack('v', $mtime) . # last mod time - pack('v', $mdate) . # last mod date - pack('V', $crc) . # crc32 - pack('V', $c_len) . # compressed filesize - pack('V', $unc_len) . # uncompressed filesize - pack('v', strlen($name)) . # length of filename - pack('v', 0) . # extra field length - pack('v', 0) . # file comment length - pack('v', 0) . # disk number start - pack('v', 0) . # internal file attributes - pack('V', 32) . # external file attributes - 'archive' bit set - pack('V', $this->old_offset) . # relative offset of local header - $name; - - $this->old_offset = $new_offset; - $this->ctrl_dir[] = $cdrec; - } - - protected static function removePHPComment(string $content): string - { - $comment = [T_COMMENT]; - if (defined('T_DOC_COMMENT')) { - $comment[] = T_DOC_COMMENT; // PHP 5 - } - if (defined('T_ML_COMMENT')) { - $comment[] = T_ML_COMMENT; // PHP 4 - } - - $newStr = ''; - $tokens = token_get_all($content); - - foreach ($tokens as $token) { - if (is_array($token)) { - if (in_array($token[0], $comment)) { - //$newStr .= "\n"; - } else { - $newStr .= $token[1]; - } - } else { - $newStr .= $token; - } - } - - return $newStr; - } -} diff --git a/src/module/zip/Zip.php b/src/module/zip/Zip.php new file mode 100644 index 0000000..c533d42 --- /dev/null +++ b/src/module/zip/Zip.php @@ -0,0 +1,128 @@ +entries[$name])) { + return; + } + + $size = filesize($file); + $this->memoryAllocate($size * 3); + + $content = (string) file_get_contents($file); + + if (self::$remove_comment && substr($file, -4) == '.php') { + $content = self::removePHPComment($content); + } + + $unc_len = strlen($content); + $crc = crc32($content); + $zdata = (string) gzdeflate($content); + $c_len = strlen($zdata); + + unset($content); + + $mdate = $this->makeDate($mtime); + $mtime = $this->makeTime($mtime); + + # Data descriptor + $data_desc = "\x50\x4b\x03\x04" . + "\x14\x00" . # ver needed to extract + "\x00\x00" . # gen purpose bit flag + "\x08\x00" . # compression method + pack('v', $mtime) . # last mod time + pack('v', $mdate) . # last mod date + pack('V', $crc) . # crc32 + pack('V', $c_len) . # compressed filesize + pack('V', $unc_len) . # uncompressed filesize + pack('v', strlen($name)) . # length of filename + pack('v', 0) . # extra field length + $name . # end of "local file header" segment + $zdata . # "file data" segment + pack('V', $crc) . # crc32 + pack('V', $c_len) . # compressed filesize + pack('V', $unc_len); # uncompressed filesize + + fwrite($this->fp, $data_desc); + unset($zdata); + + $new_offset = $this->old_offset + strlen($data_desc); + + # Add to central directory record + $cdrec = "\x50\x4b\x01\x02" . + "\x00\x00" . # version made by + "\x14\x00" . # version needed to extract + "\x00\x00" . # gen purpose bit flag + "\x08\x00" . # compression method + pack('v', $mtime) . # last mod time + pack('v', $mdate) . # last mod date + pack('V', $crc) . # crc32 + pack('V', $c_len) . # compressed filesize + pack('V', $unc_len) . # uncompressed filesize + pack('v', strlen($name)) . # length of filename + pack('v', 0) . # extra field length + pack('v', 0) . # file comment length + pack('v', 0) . # disk number start + pack('v', 0) . # internal file attributes + pack('V', 32) . # external file attributes - 'archive' bit set + pack('V', $this->old_offset) . # relative offset of local header + $name; + + $this->old_offset = $new_offset; + $this->ctrl_dir[] = $cdrec; + } + + protected static function removePHPComment(string $content): string + { + $comment = [T_COMMENT]; + if (defined('T_DOC_COMMENT')) { + $comment[] = T_DOC_COMMENT; // PHP 5 + } + if (defined('T_ML_COMMENT')) { + $comment[] = T_ML_COMMENT; // PHP 4 + } + + $newStr = ''; + $tokens = token_get_all($content); + + foreach ($tokens as $token) { + if (is_array($token)) { + if (in_array($token[0], $comment)) { + //$newStr .= "\n"; + } else { + $newStr .= $token[1]; + } + } else { + $newStr .= $token; + } + } + + return $newStr; + } +}