Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Now the zip_files() function is working better:

- More checks are done
- Non existing files are out
- Both internal an external are supported and protected
- All Petr's patchs applied
- Included support to Win32 external zip.

TODO: Create a paralel unzip_file() function
      Use this functions in all the FileManager scripts

THOUGHT: External zip seems to work always adding files to
      zip file if it exists, where PclZip resets the file.
      Perhaps a rm (or delete) should be included in the
      command chain. Analyse it!!
  • Loading branch information...
commit 2b26825cb2671eb1c798d4dbc686aee234264385 1 parent d5640b8
stronk7 authored
Showing with 60 additions and 23 deletions.
  1. +60 −23 lib/moodlelib.php
View
83 lib/moodlelib.php
@@ -3122,54 +3122,91 @@ function cleardoubleslashes ($path) {
return preg_replace('/(\/|\\\){1,}/','/',$path);
}
-//Zip an array of files/dirs to a destination file
function zip_files ($originalfiles, $destination) {
+//Zip an array of files/dirs to a destination zip file
+//Both parameters must be FULL paths to the files/dirs
global $CFG;
//Extract everything from destination
$path_parts = pathinfo(cleardoubleslashes($destination));
- $path = $path_parts["dirname"];
- $filename = $path_parts["basename"];
- $extension = $path_parts["extension"];
+ $destpath = $path_parts["dirname"]; //The path of the zip file
+ $destfilename = $path_parts["basename"]; //The name of the zip file
+ $extension = $path_parts["extension"]; //The extension of the file
//If no file, error
- if (empty($filename)) {
+ if (empty($destfilename)) {
return false;
}
//If no extension, add it
if (empty($extension)) {
$extension = 'zip';
- $file = $file.'.'.$extension;
+ $destfilename = $destfilename.'.'.$extension;
}
- //Check path exists
- if (!is_dir($path)) {
+ //Check destination path exists
+ if (!is_dir($destpath)) {
return false;
}
- //Clean filename
- $filename = clean_filename($filename);
+ //Check destination path is writable. TODO!!
- if (empty($CFG->zip)) { // Use built-in php-based zip function
- $files = array();
- foreach ($originalfiles as $file) {
- $files[] = cleardoubleslashes($file); // no doubleslashes!
+ //Clean destination filename
+ $destfilename = clean_filename($destfilename);
+
+ //Now check and prepare every file
+ $files = array();
+ $origpath = NULL;
+
+ foreach ($originalfiles as $file) { //Iterate over each file
+ //Check for every file
+ $tempfile = cleardoubleslashes($file); // no doubleslashes!
+ //Calculate the base path for all files if it isn't set
+ if ($origpath === NULL) {
+ $origpath = rtrim(cleardoubleslashes(dirname($tempfile)), "/");
+ }
+ //See if the file is readable
+ if (!is_readable($tempfile)) { //Is readable
+ continue;
+ }
+ //See if the file/dir is in the same directory than the rest
+ if (rtrim(cleardoubleslashes(dirname($tempfile)), "/") != $origpath) {
+ continue;
}
+ //Add the file to the array
+ $files[] = $tempfile;
+ }
+
+ //Everything is ready:
+ // -$origpath is the path where ALL the files to be compressed reside (dir).
+ // -$destpath is the destination path where the zip file will go (dir).
+ // -$files is an array of files/dirs to compress (fullpath)
+ // -$destfilename is the name of the zip file (without path)
+
+ //print_object($files); //Debug
+
+ if (empty($CFG->zip)) { // Use built-in php-based zip function
+
include_once("$CFG->libdir/pclzip/pclzip.lib.php");
- $archive = new PclZip(cleardoubleslashes("$path/$filename"));
- if (($list = $archive->create($files, PCLZIP_OPT_REMOVE_PATH,
- rtrim(cleardoubleslashes($path), "/"))) == 0) { // no double slashes and trailing slash!
- error($archive->errorInfo(true));
+ $archive = new PclZip(cleardoubleslashes("$destpath/$destfilename"));
+ if (($list = $archive->create($files, PCLZIP_OPT_REMOVE_PATH,$origpath) == 0)) {
+ notice($archive->errorInfo(true));
+ return false;
}
+
} else { // Use external zip program
- $files = "";
- foreach ($originalfiles as $file) {
- $files .= basename($file);
- $files .= " ";
+
+ $filestozip = "";
+ foreach ($files as $filetozip) {
+ $filestozip .= escapeshellarg(basename($filetozip));
+ $filestozip .= " ";
}
- $command = "cd $path ; $CFG->zip -r $filename $files";
+ //Construct the command
+ $separator = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? ' &' : ' ;';
+ $command = 'cd '.escapeshellarg($origpath).$separator.
+ escapeshellarg($CFG->zip).' -r '.
+ escapeshellarg(cleardoubleslashes("$destpath/$destfilename")).' '.$filestozip;
Exec($command);
}
return true;
Please sign in to comment.
Something went wrong with that request. Please try again.