pacKman/inc/class.filezip.php

152 lines
4.6 KiB
PHP
Raw Normal View History

2021-08-16 20:48:26 +00:00
<?php
2021-09-02 20:14:32 +00:00
/**
* @brief pacKman, a plugin for Dotclear 2
2021-11-01 10:39:02 +00:00
*
2021-09-02 20:14:32 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-01 10:39:02 +00:00
*
2021-09-02 20:14:32 +00:00
* @author Jean-Christian Denis
2021-11-01 10:39:02 +00:00
*
2021-09-02 20:14:32 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2022-12-17 21:12:10 +00:00
declare(strict_types=1);
namespace plugins\pacKman;
if (!defined('DC_CONTEXT_ADMIN')) {
return null;
}
/* clearbricks ns */
use fileZip;
class FileZip extends fileZip
2021-08-16 20:48:26 +00:00
{
2021-11-08 21:19:07 +00:00
/** @var boolean Remove comments from files content */
public static $remove_comment = false;
2021-11-08 21:19:07 +00:00
/** @var boolean Fix newline from files content */
public static $fix_newline = false;
/**
* Replace clearbricks fileZip::writeFile
*
* @param string $name Name
* @param string $file File
* @param int $size Size
* @param int $mtime Mtime
*
* @return void
*/
2021-08-23 13:07:29 +00:00
protected function writeFile($name, $file, $size, $mtime)
{
if (!isset($this->entries[$name])) {
return;
}
$size = filesize($file);
$this->memoryAllocate($size * 3);
2021-11-08 21:19:07 +00:00
$content = (string) file_get_contents($file);
2021-08-23 13:07:29 +00:00
//cleanup file contents
// at this time only php files
2021-11-01 10:39:02 +00:00
if (self::$remove_comment && substr($file, -4) == '.php') {
2021-08-23 13:07:29 +00:00
$content = self::removePHPComment($content);
}
2021-11-01 10:39:02 +00:00
if (self::$fix_newline && substr($file, -4) == '.php') {
$content = self::fixNewline($content);
}
2021-08-23 13:07:29 +00:00
$unc_len = strlen($content);
2021-11-01 10:39:02 +00:00
$crc = crc32($content);
2021-11-08 21:19:07 +00:00
$zdata = (string) gzdeflate($content);
2021-11-01 10:39:02 +00:00
$c_len = strlen($zdata);
2021-08-23 13:07:29 +00:00
unset($content);
$mdate = $this->makeDate($mtime);
$mtime = $this->makeTime($mtime);
# Data descriptor
2021-11-01 10:39:02 +00:00
$data_desc = "\x50\x4b\x03\x04" .
2021-08-23 13:07:29 +00:00
"\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
2021-11-01 10:39:02 +00:00
pack('v', 0) . # extra field length
2021-08-23 13:07:29 +00:00
$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
2021-11-01 10:39:02 +00:00
$cdrec = "\x50\x4b\x01\x02" .
2021-08-23 13:07:29 +00:00
"\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
2021-11-01 10:39:02 +00:00
pack('V', $this->old_offset) . # relative offset of local header
2021-08-23 13:07:29 +00:00
$name;
$this->old_offset = $new_offset;
$this->ctrl_dir[] = $cdrec;
}
2021-11-08 21:19:07 +00:00
protected static function removePHPComment(string $content): string
2021-08-23 13:07:29 +00:00
{
$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;
}
}
2021-11-01 10:39:02 +00:00
2021-08-23 13:07:29 +00:00
return $newStr;
}
2021-11-08 21:19:07 +00:00
protected static function fixNewline(string $content): string
{
return str_replace("\r\n", "\n", $content);
}
2021-11-06 13:56:29 +00:00
}