From a56b548c063dcdf00eb1f05f7a0db311c8dd6eae Mon Sep 17 00:00:00 2001 From: Vadimon Date: Sun, 31 Jan 2010 15:36:00 +0700 Subject: [PATCH] Encoding selection when unzipping and zip-listing Signed-off-by: Vadimon --- files/index.php | 135 +++++++++++++++++++++++++++++++++++++++++----- lang/en_utf8/moodle.php | 1 + lib/moodlelib.php | 28 +++++++--- 3 files changed, 142 insertions(+), 22 deletions(-) diff --git a/files/index.php b/files/index.php index dff961d..0aa4db4 100644 --- a/files/index.php +++ b/files/index.php @@ -22,6 +22,7 @@ $save = optional_param('save', 0, PARAM_BOOL); $text = optional_param('text', '', PARAM_RAW); $confirm = optional_param('confirm', 0, PARAM_BOOL); + $encoding = optional_param('encoding', 0, PARAM_TEXT); if ($choose) { if (count(explode('.', $choose)) > 2) { @@ -534,24 +535,106 @@ $strok = get_string("ok"); $strunpacking = get_string("unpacking", "", $file); - echo "

$strunpacking:

"; - $file = basename($file); - - if (!unzip_file("$basedir$wdir/$file")) { - print_error("unzipfileserror","error"); + include_once("$CFG->libdir/pclzip/pclzip.lib.php"); + $archive = new PclZip(cleardoubleslashes("$basedir$wdir/$file")); + if (!$list = $archive->listContent(cleardoubleslashes("$basedir$wdir"))) { + notify($archive->errorInfo(true)); + html_footer(); + break; + } + // checking if files containt characters with codes > 127 + $textlib = textlib_get_instance(); + $decodingneeded = false; + foreach ($list as $item) { + if ($item['filename'] != $textlib->utf8_to_entities($item['filename'])) { + $decodingneeded = true; + break; + } } + if ($decodingneeded) { //listing file end selecting encoding + if (!empty($encoding) && $confirm) { //unziping with encoding change + echo "

$strunpacking:

"; - echo "
"; - echo "
"; - echo ' '; - echo " "; - echo " "; - echo " "; - echo " "; - echo "
"; - echo "
"; - echo "
"; + $file = basename($file); + + if (!unzip_file("$basedir$wdir/$file", '', true, $encoding)) { + print_error("unzipfileserror","error"); + } + + echo "
"; + echo "
"; + echo ' '; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + echo "
"; + } else { //listing problematic files + echo "

".get_string("filesunknownencoding")."

"; + if (empty($encoding)) { + $encoding = 'utf-8'; + } + $encodings = $textlib->get_encodings(); + $encodings['866'] = 'CP866'; + echo '
'; + popup_form("index.php?id=$id&wdir=$wdir&file=$file&action=unzip&sesskey=$USER->sesskey&choose=$choose&encoding=", $encodings, 'selectencoding', $encoding, '', '', '', false, 'self', get_string('encoding', 'admin')); + echo '
'; + echo ""; + echo ""; + foreach ($list as $item) { + if ($item['filename'] != $textlib->utf8_to_entities($item['filename'])) { + $item['filename'] = $textlib->convert($item['filename'],$encoding); + echo ""; + print_cell("left", s($item['filename']), 'name'); + if (! $item['folder']) { + print_cell("right", display_size($item['size']), 'size'); + } else { + echo ""; + } + $filedate = userdate($item['mtime'], get_string("strftimedatetime")); + print_cell("right", $filedate, 'date'); + echo ""; + } + } + echo "
$strname$strsize$strmodified
 
"; + echo "
"; + echo "
"; + echo " "; + echo " "; + echo " "; + echo " "; + echo " "; + echo " "; + echo " "; + echo " sesskey\" />"; + echo " "; + echo "
"; + echo "
"; + echo "
"; + } + } else { //normal unzipping + echo "

$strunpacking:

"; + + $file = basename($file); + + if (!unzip_file("$basedir$wdir/$file")) { + print_error("unzipfileserror","error"); + } + + echo "
"; + echo "
"; + echo ' '; + echo " "; + echo " "; + echo " "; + echo " "; + echo "
"; + echo "
"; + echo "
"; + } } else { displaydir($wdir); } @@ -576,9 +659,31 @@ notify($archive->errorInfo(true)); } else { + // checking if files containt characters with codes > 127 + $textlib = textlib_get_instance(); + $decodingneeded = false; + foreach ($list as $item) { + if ($item['filename'] != $textlib->utf8_to_entities($item['filename'])) { + $decodingneeded = true; + break; + } + } + if ($decodingneeded){ + if (empty($encoding)) { + $encoding = 'utf-8'; + } + $encodings = $textlib->get_encodings(); + $encodings['866'] = 'CP866'; + echo '
'; + popup_form("index.php?id=$id&wdir=$wdir&file=$file&action=listzip&sesskey=$USER->sesskey&choose=$choose&encoding=", $encodings, 'selectencoding', $encoding, '', '', '', false, 'self', get_string('encoding', 'admin')); + echo '
'; + } echo ""; echo ""; foreach ($list as $item) { + if ($decodingneeded){ + $item['filename'] = $textlib->convert($item['filename'],$encoding); + } echo ""; print_cell("left", s($item['filename']), 'name'); if (! $item['folder']) { diff --git a/lang/en_utf8/moodle.php b/lang/en_utf8/moodle.php index db5747f..e80e584 100644 --- a/lang/en_utf8/moodle.php +++ b/lang/en_utf8/moodle.php @@ -660,6 +660,7 @@ $string['file'] = 'File'; $string['filemissing'] = '$a is missing'; $string['files'] = 'Files'; $string['filesfolders'] = 'Files/folders'; +$string['filesunknownencoding'] = 'Cannot determine encoding for listed files! Select suitable encoding'; $string['filloutallfields'] = 'Please fill out all fields in this form'; $string['findmorecourses'] = 'Find more courses...'; $string['firstaccess'] = 'First access'; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 936c4bb..22a344c 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -7585,7 +7585,7 @@ function zip_files ($originalfiles, $destination) { return true; } -function unzip_file ($zipfile, $destination = '', $showstatus = true) { +function unzip_file ($zipfile, $destination = '', $showstatus = true, $encoding = '') { //Unzip one zip file to a destination dir //Both parameters must be FULL paths //If destination isn't specified, it will be the @@ -7677,12 +7677,12 @@ function unzip_file ($zipfile, $destination = '', $showstatus = true) { Exec($command,$list); } - unzip_process_temp_dir($temppath, $destpath); + unzip_process_temp_dir($temppath, $destpath, $encoding); fulldelete($temppath); //Display some info about the unzip execution if ($showstatus) { - unzip_show_status($list, $temppath, $destpath); + unzip_show_status($list, $temppath, $destpath, $encoding); } return true; @@ -7694,11 +7694,13 @@ function unzip_file ($zipfile, $destination = '', $showstatus = true) { * @param string $destpath destination path * @return void */ -function unzip_process_temp_dir($temppath, $destpath) { +function unzip_process_temp_dir($temppath, $destpath, $encoding = '') { global $CFG; $filepermissions = ($CFG->directorypermissions & 0666); // strip execute flags - + if (!empty($encoding)) { + $textlib = textlib_get_instance(); + } if (check_dir_exists($destpath, true, true)) { $currdir = opendir($temppath); while (false !== ($file = readdir($currdir))) { @@ -7713,6 +7715,9 @@ function unzip_process_temp_dir($temppath, $destpath) { // invalid file name continue; } + if (!empty($encoding)) { + $cleanfile = $textlib->convert($cleanfile, $encoding); + } if ($cleanfile !== $file and file_exists("$temppath/$cleanfile")) { // eh, weird chars collision detected continue; @@ -7720,7 +7725,7 @@ function unzip_process_temp_dir($temppath, $destpath) { $descfile = "$destpath/$cleanfile"; if (is_dir($fullfile)) { // recurse into subdirs - unzip_process_temp_dir($fullfile, $descfile); + unzip_process_temp_dir($fullfile, $descfile, $encoding); } if (is_file($fullfile)) { // rename and move the file @@ -7756,13 +7761,16 @@ function unzip_cleanfilename ($p_event, &$p_header) { return 1; } -function unzip_show_status($list, $removepath, $removepath2) { +function unzip_show_status($list, $removepath, $removepath2, $encoding = '') { //This function shows the results of the unzip execution //depending of the value of the $CFG->zip, results will be //text or an array of files. global $CFG; + if (!empty($encoding)) { + $textlib = textlib_get_instance(); + } if (empty($CFG->unzip)) { // Use built-in php-based zip function $strname = get_string("name"); $strsize = get_string("size"); @@ -7777,6 +7785,9 @@ function unzip_show_status($list, $removepath, $removepath2) { echo ""; $item['filename'] = str_replace(cleardoubleslashes($removepath).'/', "", $item['filename']); $item['filename'] = str_replace(cleardoubleslashes($removepath2).'/', "", $item['filename']); + if (!empty($encoding)) { + $item['filename'] = $textlib->convert($item['filename'],$encoding); + } print_cell("left", s(clean_param($item['filename'], PARAM_PATH))); if (! $item['folder']) { print_cell("right", display_size($item['size'])); @@ -7796,6 +7807,9 @@ function unzip_show_status($list, $removepath, $removepath2) { foreach ($list as $item) { $item = str_replace(cleardoubleslashes($removepath.'/'), '', $item); $item = str_replace(cleardoubleslashes($removepath2.'/'), '', $item); + if (!empty($encoding)) { + $item = $textlib->convert($item,$encoding); + } echo s($item).'
'; } echo ""; -- 1.6.5.1.1367.gcd48
$strname$strsize$strmodified