Merge branch 'w28_MDL-33710_m24_utfzip' of git://github.com/skodak/moodle

This commit is contained in:
Sam Hemelryk
2012-07-11 11:27:48 +12:00
17 changed files with 738 additions and 8 deletions
+1 -1
View File
@@ -84,7 +84,7 @@ abstract class file_packer {
/**
* Returns array of info about all files in archive
*
* @param file_archive $archivefile
* @param string|file_archive $archivefile
* @return array of file infos
*/
public abstract function list_files($archivefile);
+1
View File
@@ -0,0 +1 @@
test
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+55
View File
@@ -0,0 +1,55 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This debug script is used during zip support development.
*
* @package core_files
* @copyright 2012 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('CLI_SCRIPT', true);
require(__DIR__.'/../../../../config.php');
require_once($CFG->libdir.'/clilib.php');
$help =
"Create sample zip file for testing
Example:
\$php zip_create_test_file.php test.zip
";
if (count($_SERVER['argv']) != 2 or file_exists($_SERVER['argv'][1])) {
echo $help;
exit(0);
}
$archive = $_SERVER['argv'][1];
$packer = get_file_packer('application/zip');
$file = __DIR__.'/test.txt';
$files = array(
'test.test' => $file,
'testíček.txt' => $file,
'Prüfung.txt' => $file,
'测试.txt' => $file,
'試験.txt' => $file,
'Žluťoučký/Koníček.txt' => $file,
);
$packer->archive_to_pathname($files, $archive);
+302
View File
@@ -0,0 +1,302 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This debug script is used during zip support development ONLY.
*
* @package core_files
* @copyright 2012 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('CLI_SCRIPT', true);
require(__DIR__.'/../../../../config.php');
require_once($CFG->libdir.'/clilib.php');
if (count($_SERVER['argv']) != 2 or !file_exists($_SERVER['argv'][1])) {
cli_error("This script expects zip file name as the only parameter");
}
$archive = $_SERVER['argv'][1];
// Note: the ZIP structure is described at http://www.pkware.com/documents/casestudies/APPNOTE.TXT
if (!$filesize = filesize($archive) or !$fp = fopen($archive, 'rb+')) {
cli_error("Can not open file file $archive");
}
fseek($fp, 0);
$info = unpack('Vsig', fread($fp, 4));
if ($info['sig'] !== 0x04034b50) {
fclose($fp);
cli_error("This is not a zip file: $archive");
}
// Find end of central directory record.
fseek($fp, $filesize - 22);
$info = unpack('Vsig', fread($fp, 4));
if ($info['sig'] === 0x06054b50) {
// There is no comment.
fseek($fp, $filesize - 22);
$data = fread($fp, 22);
} else {
// There is some comment with 0xFF max size - that is 65557.
fseek($fp, $filesize - 65557);
$data = fread($fp, 65557);
}
$pos = strpos($data, pack('V', 0x06054b50));
if ($pos === false) {
// Borked ZIP structure!
fclose($fp);
cli_error("Can not find end of central directory in $archive");
}
$centralend = unpack('Vsig/vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_length', substr($data, $pos, 22));
if ($centralend['disk'] !== 0 or $centralend['disk_start'] !== 0) {
cli_error("Multi-disk archives are not supported: $archive");
}
if ($centralend['offset'] === 0xFFFFFFFF) {
cli_error("ZIP64 archives are not supported: $archive");
}
if ($centralend['comment_length']) {
$centralend['comment'] = substr($data, 22, $centralend['comment_length']);
} else {
$centralend['comment'] = '';
}
fseek($fp, $centralend['offset']);
$data = fread($fp, $centralend['size']);
$pos = 0;
$files = array();
for($i=0; $i<$centralend['entries']; $i++) {
$file = unpack('Vsig/vversion/vversion_req/vgeneral/vmethod/Vmodified/Vcrc/Vsize_compressed/Vsize/vname_length/vextra_length/vcomment_length/vdisk/vattr/Vattrext/Vlocal_offset', substr($data, $pos, 46));
$file['error'] = null;
$file['central_offset'] = $centralend['offset'] + $pos;
$pos = $pos + 46;
if ($file['sig'] !== 0x02014b50) {
$files[] = array('error'=>'Invalid central file signature');
continue;
}
$file['name'] = substr($data, $pos, $file['name_length']);
$pos = $pos + $file['name_length'];
$file['extra'] = array();
if ($file['extra_length']) {
$extradata = substr($data, $pos, $file['extra_length']);
while (strlen($extradata) > 4) {
$extra = unpack('vid/vsize', substr($extradata, 0, 4));
$extra['data'] = substr($extradata, 4, $extra['size']);
$extradata = substr($extradata, 4+$extra['size']);
$file['extra'][] = $extra;
}
$pos = $pos + $file['extra_length'];
}
if ($file['comment_length']) {
$file['comment'] = substr($data, $pos, $file['comment_length']);
$pos = $pos + $file['comment_length'];
} else {
$file['comment'] = '';
}
// Read local file header.
fseek($fp, $file['local_offset']);
$localfile = unpack('Vsig/vversion_req/vgeneral/vmethod/Vmodified/Vcrc/Vsize_compressed/Vsize/vname_length/vextra_length', fread($fp, 30));
if ($localfile['sig'] !== 0x04034b50) {
// Borked file!
$file['error'] = 'Invalid local file signature';
$files[] = $file;
continue;
}
if ($localfile['name_length']) {
$localfile['name'] = fread($fp, $localfile['name_length']);
} else {
$localfile['name'] = '';
}
$localfile['extra'] = array();
if ($localfile['extra_length']) {
$extradata = fread($fp, $localfile['extra_length']);
while (strlen($extradata) > 4) {
$extra = unpack('vid/vsize', substr($extradata, 0, 4));
$extra['data'] = substr($extradata, 4, $extra['size']);
$extradata = substr($extradata, 4+$extra['size']);
$localfile['extra'][] = $extra;
}
}
$file['local'] = $localfile;
$files[] = $file;
}
echo "Archive: $archive\n";
echo "Number of files: {$centralend['entries']}\n";
echo "Archive comment: \"{$centralend['comment']}\" ({$centralend['comment_length']} bytes)\n";
foreach ($files as $i=>$file) {
echo "======== File ".($i+1)." ==============================================\n";
if (!empty($file['error'])) {
echo " ERROR: {$file['error']}\n";
}
echo " Name: ".zip_print_name($file['name'])."\n";
if ($file['comment'] !== '') {
echo " Comment: \"{$file['comment']}\" ({$file['comment_length']} bytes)\n";
}
echo " Version: 0x".str_pad(dechex($file['version']), 4, '0', STR_PAD_LEFT)."\n";
echo " Required: 0x".str_pad(dechex($file['version_req']), 4, '0', STR_PAD_LEFT)."\n";
echo " Method: ".zip_print_method($file['method'])."\n";
echo " General: ".zip_print_general($file['general'])."\n";
echo " Modified: ".userdate(zip_dos2unixtime($file['modified']))."\n";
echo " Size: ".zip_print_sizes($file['size'], $file['size_compressed'])."\n";
echo " CRC-32: {$file['crc']}\n";
if ($file['extra']) {
foreach($file['extra'] as $j=>$extra) {
echo " Extra ".($j+1).": ".zip_print_extra($extra)."\n";
}
}
if (!empty($file['local']['error'])) {
echo " Local ERROR: {$file['local']['error']}\n";
}
$localfile = $file['local'];
if ($localfile['name'] !== $file['name']) {
echo " Local name: ".zip_print_name($localfile['name'])."\n";
}
if ($localfile['version_req'] !== $file['version_req']) {
echo " Local required: 0x".str_pad(dechex($localfile['version_req']), 4, '0', STR_PAD_LEFT)."\n";
}
if ($localfile['method'] !== $file['method']) {
echo " Local method: ".zip_print_method($localfile['method'])."\n";
}
if ($localfile['general'] !== $file['general']) {
echo " Local general: ".zip_print_general($localfile['general'])."\n";
}
if ($localfile['modified'] !== $file['modified']) {
echo " Local modified: ".userdate(zip_dos2unixtime($localfile['modified']))."\n";
}
if ($localfile['size'] !== $file['size']) {
echo " Local size: ".zip_print_sizes($localfile['size'], $localfile['size_compressed'])."\n";
}
if ($localfile['crc'] !== $file['crc']) {
echo " Local CRC-32: {$localfile['crc']}\n";
}
if ($localfile['extra']) {
foreach($localfile['extra'] as $j=>$extra) {
echo " Local extra ".($j+1).": ".zip_print_extra($extra)."\n";
}
}
}
fclose($fp);
exit(0);
// === Some useful functions ======================================
function zip_print_name($name) {
$size = strlen($name);
$crc = crc32($name);
return "\"$name\" ($size bytes) - CRC $crc";
}
function zip_print_method($method) {
$desc = '';
switch($method) {
case 0: $desc = 'Stored'; break;
case 1: $desc = 'Shrunk'; break;
case 2: $desc = 'Reduced factor 1'; break;
case 3: $desc = 'Reduced factor 2'; break;
case 4: $desc = 'Reduced factor 3'; break;
case 5: $desc = 'Reduced factor 4'; break;
case 6: $desc = 'Imploded'; break;
case 8: $desc = 'Deflated'; break;
case 9: $desc = 'Deflate64'; break;
case 10: $desc = 'old IBM TERSE'; break;
case 12: $desc = 'BZIP2'; break;
case 14: $desc = 'LZMA'; break;
case 18: $desc = 'IBM TERSE'; break;
case 19: $desc = 'IBM LZ77'; break;
case 97: $desc = 'WavPack'; break;
case 98: $desc = 'PPMd v1'; break;
}
if ($desc) {
$desc = " ($desc)";
}
return "0x".str_pad(dechex($method), 4, '0', STR_PAD_LEFT).$desc;
}
function zip_print_general($general) {
$desc = array();
if ($general & pow(2, 0)) {
$desc[] = 'Encrypted';
}
if ($general & pow(2, 11)) {
$desc[] = 'Unicode name';
}
if ($desc) {
$desc = " (".implode(', ', $desc).")";
} else {
$desc = '';
}
return str_pad(decbin($general), 16, '0', STR_PAD_LEFT).$desc;
}
/**
* Convert MS date+time format to unix timestamp:
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms724274(v=vs.85).aspx
*
* Copied from: http://plugins.svn.wordpress.org/wp2epub/trunk/zipcreate/functions.lib.php
* author: redmonkey
* license: GPL
*/
function zip_dos2unixtime($dostime) {
$sec = 2 * ($dostime & 0x1f);
$min = ($dostime >> 5) & 0x3f;
$hrs = ($dostime >> 11) & 0x1f;
$day = ($dostime >> 16) & 0x1f;
$mon = ($dostime >> 21) & 0x0f;
$year = (($dostime >> 25) & 0x7f) + 1980;
return mktime($hrs, $min, $sec, $mon, $day, $year);
}
function zip_print_sizes($size, $compressed) {
return "$size ==> $compressed bytes";
}
function zip_print_extra($extra) {
$desc = '';
$info = "- ".bin2hex($extra['data'])." ({$extra['size']} bytes)";
switch($extra['id']) {
case 0x0009: $desc = 'OS/2'; break;
case 0x000a: $desc = 'NTFS'; break;
case 0x000d: $desc = 'UNIX'; break;
case 0x5455: $desc = 'Extended timestamp'; break;
case 0x5855: $desc = 'Infor-ZIP (original)'; break;
case 0x7075:
$desc = 'Info-ZIP Unicode path';
$data = unpack('cversion/Vcrc', substr($extra['data'], 0, 5));
$name = substr($extra['data'], 5);
$size = strlen($name);
if ($data['version'] === 1) {
$info = "- \"$name\" ($size bytes) - CRC {$data['crc']}";
}
break;
case 0x7865: $desc = 'Info-ZIP UNIX (new)'; break;
case 0x7875: $desc = 'Info-ZIP UNIX (3rd generation)'; break;
}
if ($desc) {
$desc = " ($desc)";
}
return "0x".str_pad(dechex($extra['id']), 4, '0', STR_PAD_LEFT)."$desc $info";
}
+216
View File
@@ -0,0 +1,216 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for /lib/filestorage/zip_packer.php and zip_archive.php
*
* @package core_files
* @category phpunit
* @copyright 2012 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class zip_packer_testcase extends advanced_testcase {
protected $testfile;
protected $files;
protected function setUp() {
parent::setUp();
$this->testfile = __DIR__.'/fixtures/test.txt';
$fs = get_file_storage();
$context = context_system::instance();
if (!$file = $fs->get_file($context->id, 'phpunit', 'data', 0, '/', 'test.txt')) {
$file = $fs->create_file_from_pathname(
array('contextid'=>$context->id, 'component'=>'phpunit', 'filearea'=>'data', 'itemid'=>0, 'filepath'=>'/', 'filename'=>'test.txt'),
$this->testfile);
}
$this->files = array(
'test.test' => $this->testfile,
'testíček.txt' => $this->testfile,
'Prüfung.txt' => $this->testfile,
'测试.txt' => $this->testfile,
'試験.txt' => $this->testfile,
'Žluťoučký/Koníček.txt' => $file,
);
}
public function test_get_packer() {
$this->resetAfterTest(false);
$packer = get_file_packer();
$this->assertInstanceOf('zip_packer', $packer);
$packer = get_file_packer('application/zip');
$this->assertInstanceOf('zip_packer', $packer);
}
/**
* @depends test_get_packer
*/
public function test_list_files() {
$this->resetAfterTest(false);
$moodle22 = __DIR__.'/fixtures/test_moodle_22.zip';
$moodle = __DIR__.'/fixtures/test_moodle.zip';
$packer = get_file_packer('application/zip');
$archivefiles22 = $packer->list_files($moodle22);
$this->assertTrue(is_array($archivefiles22));
$this->assertEquals(count($this->files), count($archivefiles22));
foreach($archivefiles22 as $file) {
$this->assertArrayHasKey($file->pathname, $this->files);
}
$archivefiles = $packer->list_files($moodle);
$this->assertTrue(is_array($archivefiles));
$this->assertEquals(count($this->files), count($archivefiles));
foreach($archivefiles as $file) {
$this->assertArrayHasKey($file->pathname, $this->files);
}
}
/**
* @depends test_list_files
*/
public function test_archive_to_pathname() {
global $CFG;
$this->resetAfterTest(false);
$packer = get_file_packer('application/zip');
$archive = "$CFG->tempdir/archive.zip";
$this->assertFalse(file_exists($archive));
$result = $packer->archive_to_pathname($this->files, $archive);
$this->assertTrue($result);
$this->assertTrue(file_exists($archive));
$archivefiles = $packer->list_files($archive);
$this->assertTrue(is_array($archivefiles));
$this->assertEquals(count($this->files), count($archivefiles));
foreach($archivefiles as $file) {
$this->assertArrayHasKey($file->pathname, $this->files);
}
}
/**
* @depends test_archive_to_pathname
*/
public function test_archive_to_storage() {
$this->resetAfterTest(false);
$packer = get_file_packer('application/zip');
$fs = get_file_storage();
$context = context_system::instance();
$this->assertFalse($fs->file_exists($context->id, 'phpunit', 'test', 0, '/', 'archive.zip'));
$result = $packer->archive_to_storage($this->files, $context->id, 'phpunit', 'test', 0, '/', 'archive.zip');
$this->assertInstanceOf('stored_file', $result);
$this->assertTrue($fs->file_exists($context->id, 'phpunit', 'test', 0, '/', 'archive.zip'));
$archivefiles = $result->list_files($packer);
$this->assertTrue(is_array($archivefiles));
$this->assertEquals(count($this->files), count($archivefiles));
foreach($archivefiles as $file) {
$this->assertArrayHasKey($file->pathname, $this->files);
}
}
/**
* @depends test_archive_to_storage
*/
public function test_extract_to_pathname() {
global $CFG;
$this->resetAfterTest(false);
$packer = get_file_packer('application/zip');
$fs = get_file_storage();
$context = context_system::instance();
$target = "$CFG->tempdir/test/";
$testcontent = file_get_contents($this->testfile);
@mkdir($target, $CFG->directorypermissions);
$this->assertTrue(is_dir($target));
$archive = "$CFG->tempdir/archive.zip";
$this->assertTrue(file_exists($archive));
$result = $packer->extract_to_pathname($archive, $target);
$this->assertTrue(is_array($result));
$this->assertEquals(count($this->files), count($result));
foreach($this->files as $file=>$unused) {
$this->assertTrue($result[$file]);
$this->assertTrue(file_exists($target.$file));
$this->assertSame($testcontent, file_get_contents($target.$file));
}
$archive = $fs->get_file($context->id, 'phpunit', 'test', 0, '/', 'archive.zip');
$this->assertNotEmpty($archive);
$result = $packer->extract_to_pathname($archive, $target);
$this->assertTrue(is_array($result));
$this->assertEquals(count($this->files), count($result));
foreach($this->files as $file=>$unused) {
$this->assertTrue($result[$file]);
$this->assertTrue(file_exists($target.$file));
$this->assertSame($testcontent, file_get_contents($target.$file));
}
}
/**
* @depends test_archive_to_storage
*/
public function test_extract_to_storage() {
global $CFG;
$this->resetAfterTest(true);
$packer = get_file_packer('application/zip');
$fs = get_file_storage();
$context = context_system::instance();
$testcontent = file_get_contents($this->testfile);
$archive = $fs->get_file($context->id, 'phpunit', 'test', 0, '/', 'archive.zip');
$this->assertNotEmpty($archive);
$result = $packer->extract_to_storage($archive, $context->id, 'phpunit', 'target', 0, '/');
$this->assertTrue(is_array($result));
$this->assertEquals(count($this->files), count($result));
foreach($this->files as $file=>$unused) {
$this->assertTrue($result[$file]);
$stored_file = $fs->get_file_by_hash(sha1("/$context->id/phpunit/target/0/$file"));
$this->assertInstanceOf('stored_file', $stored_file);
$this->assertSame($testcontent, $stored_file->get_content());
}
$archive = "$CFG->tempdir/archive.zip";
$this->assertTrue(file_exists($archive));
$result = $packer->extract_to_storage($archive, $context->id, 'phpunit', 'target', 0, '/');
$this->assertTrue(is_array($result));
$this->assertEquals(count($this->files), count($result));
foreach($this->files as $file=>$unused) {
$this->assertTrue($result[$file]);
$stored_file = $fs->get_file_by_hash(sha1("/$context->id/phpunit/target/0/$file"));
$this->assertInstanceOf('stored_file', $stored_file);
$this->assertSame($testcontent, $stored_file->get_content());
}
}
}
+162 -6
View File
@@ -46,9 +46,12 @@ class zip_archive extends file_archive {
/** @var int Iteration position */
protected $pos = 0;
/** @var zip_archive TipArchive instance */
/** @var ZipArchive instance */
protected $za;
/** @var bool was this archive modified? */
protected $modified = false;
/**
* Open or create archive (depending on $mode)
*
@@ -87,7 +90,7 @@ class zip_archive extends file_archive {
} else {
$this->za = null;
$this->archivepathname = null;
$this->encooding = 'utf-8';
$this->encoding = 'utf-8';
// TODO: maybe we should return some error info
return false;
}
@@ -106,6 +109,11 @@ class zip_archive extends file_archive {
$res = $this->za->close();
$this->za = null;
if ($this->modified) {
$this->fix_utf8_flags();
$this->modified = false;
}
return $res;
}
@@ -241,7 +249,11 @@ class zip_archive extends file_archive {
}
}
return $this->za->addFile($pathname, $localname);
if (!$this->za->addFile($pathname, $localname)) {
return false;
}
$this->modified = true;
return true;
}
/**
@@ -274,8 +286,11 @@ class zip_archive extends file_archive {
}
$this->usedmem += strlen($contents);
return $this->za->addFromString($localname, $contents);
if (!$this->za->addFromString($localname, $contents)) {
return false;
}
$this->modified = true;
return true;
}
/**
@@ -296,7 +311,11 @@ class zip_archive extends file_archive {
return false;
}
return $this->za->addEmptyDir($localname);
if (!$this->za->addEmptyDir($localname)) {
return false;
}
$this->modified = true;
return true;
}
/**
@@ -347,4 +366,141 @@ class zip_archive extends file_archive {
return ($this->pos < $this->count());
}
/**
* Add unicode flag to all files in archive.
*
* NOTE: single disk archives only, no ZIP64 support.
*
* @return bool success, modifies the file contents
*/
protected function fix_utf8_flags() {
if ($this->encoding !== 'utf-8') {
return true;
}
if (!file_exists($this->archivepathname)) {
return true;
}
// Note: the ZIP structure is described at http://www.pkware.com/documents/casestudies/APPNOTE.TXT
if (!$fp = fopen($this->archivepathname, 'rb+')) {
return false;
}
if (!$filesize = filesize($this->archivepathname)) {
return false;
}
// Find end of central directory record.
fseek($fp, $filesize - 22);
$info = unpack('Vsig', fread($fp, 4));
if ($info['sig'] === 0x06054b50) {
// There is no comment.
fseek($fp, $filesize - 22);
$data = fread($fp, 22);
} else {
// There is some comment with 0xFF max size - that is 65557.
fseek($fp, $filesize - 65557);
$data = fread($fp, 65557);
}
$pos = strpos($data, pack('V', 0x06054b50));
if ($pos === false) {
// Borked ZIP structure!
fclose($fp);
return false;
}
$centralend = unpack('Vsig/vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_length', substr($data, $pos, 22));
if ($centralend['disk'] !== 0 or $centralend['disk_start'] !== 0) {
// Single disk archives only, sorry.
fclose($fp);
return false;
}
if ($centralend['offset'] === 0xFFFFFFFF) {
// No support for ZIP64, sorry!
fclose($fp);
return false;
}
fseek($fp, $centralend['offset']);
$data = fread($fp, $centralend['size']);
$pos = 0;
$files = array();
for($i=0; $i<$centralend['entries']; $i++) {
$file = unpack('Vsig/vversion/vversion_req/vgeneral/vmethod/vmtime/vmdate/Vcrc/Vsize_compressed/Vsize/vname_length/vextra_length/vcomment_length/vdisk/vattr/Vattrext/Vlocal_offset', substr($data, $pos, 46));
$file['central_offset'] = $centralend['offset'] + $pos;
$pos = $pos + 46;
if ($file['sig'] !== 0x02014b50) {
// Borked file!
fclose($fp);
return false;
}
$file['name'] = substr($data, $pos, $file['name_length']);
$pos = $pos + $file['name_length'];
if ($file['extra_length']) {
$file['extra'] = substr($data, $pos, $file['extra_length']);
$pos = $pos + $file['extra_length'];
} else {
$file['extra'] = '';
}
if ($file['comment_length']) {
$file['comment'] = substr($data, $pos, $file['comment_length']);
$pos = $pos + $file['comment_length'];
} else {
$file['comment'] = '';
}
$newgeneral = $file['general'] | pow(2, 11);
if ($newgeneral === $file['general']) {
// Nothing to do with this file.
continue;
}
if (preg_match('/^[a-zA-Z0-9_\-\.]*$/', $file['name'])) {
// ASCII file names are always ok.
continue;
}
if ($file['extra'] !== '') {
// Most probably not created by php zip ext, better to skip it.
continue;
}
if (fix_utf8($file['name']) !== $file['name']) {
// Does not look like a valid utf-8 encoded file name, skip it.
continue;
}
// Read local file header.
fseek($fp, $file['local_offset']);
$localfile = unpack('Vsig/vversion_req/vgeneral/vmethod/vmtime/vmdate/Vcrc/Vsize_compressed/Vsize/vname_length/vextra_length', fread($fp, 30));
if ($localfile['sig'] !== 0x04034b50) {
// Borked file!
fclose($fp);
return false;
}
$file['local'] = $localfile;
$files[] = $file;
}
foreach ($files as $file) {
$localfile = $file['local'];
// Add the unicode flag in central file header.
fseek($fp, $file['central_offset'] + 8);
if (ftell($fp) === $file['central_offset'] + 8) {
$newgeneral = $file['general'] | pow(2, 11);
fwrite($fp, pack('v', $newgeneral));
}
// Modify local file header too.
fseek($fp, $file['local_offset'] + 6);
if (ftell($fp) === $file['local_offset'] + 6) {
$newgeneral = $localfile['general'] | pow(2, 11);
fwrite($fp, pack('v', $newgeneral));
}
}
fclose($fp);
return true;
}
}
+1 -1
View File
@@ -435,7 +435,7 @@ class zip_packer extends file_packer {
/**
* Returns array of info about all files in archive
*
* @param file_archive $archivefile
* @param string|file_archive $archivefile
* @return array of file infos
*/
public function list_files($archivefile) {