Included some filename transformations to work like info-zip.

Directory traversal protection added.

Working under Win32, MacOS X and BSD (and rest of Unix, I hope).

Patch file from Petr. Thanks!

Merger from MOODLE_14_STABLE
This commit is contained in:
stronk7 2004-10-17 22:28:53 +00:00
parent 4f9b2efd50
commit 1c1520c5bb

View file

@ -4363,7 +4363,7 @@ function unzip_file ($zipfile, $destination = '', $showstatus = true) {
include_once("$CFG->libdir/pclzip/pclzip.lib.php"); include_once("$CFG->libdir/pclzip/pclzip.lib.php");
$archive = new PclZip(cleardoubleslashes("$zippath/$zipfilename")); $archive = new PclZip(cleardoubleslashes("$zippath/$zipfilename"));
if (!$list = $archive->extract(PCLZIP_OPT_PATH, $destpath, if (!$list = $archive->extract(PCLZIP_OPT_PATH, $destpath,
PCLZIP_CB_PRE_EXTRACT, 'unzip_approvefile')) { PCLZIP_CB_PRE_EXTRACT, 'unzip_cleanfilename')) {
notice($archive->errorInfo(true)); notice($archive->errorInfo(true));
return false; return false;
} }
@ -4392,15 +4392,22 @@ function unzip_file ($zipfile, $destination = '', $showstatus = true) {
return true; return true;
} }
function unzip_approvefile ($p_event, &$p_header) { function unzip_cleanfilename ($p_event, &$p_header) {
//This function is used as callback in unzip_file() function //This function is used as callback in unzip_file() function
//to decide if one file in the zip file must be extracted or no //to clean illegal characters for given platform and to prevent directory traversal.
//print_object ($p_header['filename']); //Produces the same result as info-zip unzip.
if (detect_munged_arguments($p_header['filename'], 0)) { $p_header['filename'] = ereg_replace('\.\.+', '', $p_header['filename']); //directory traversal protection
return 0; // do not extract file!! if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$p_header['filename'] = ereg_replace('[:*"?<>|]', '_', $p_header['filename']); //replace illegal chars
$p_header['filename'] = ereg_replace('^([a-zA-Z])_', '\1:', $p_header['filename']); //repair drive letter
} else { } else {
return 1; //Add filtering for other systems here
// BSD: none (tested)
// Linux: ??
// MacosX: ??
} }
$p_header['filename'] = cleardoubleslashes($p_header['filename']); //normalize the slashes/backslashes
return 1;
} }
function unzip_show_status ($list,$removepath) { function unzip_show_status ($list,$removepath) {