return true;
}
+/**
+ * Zip an array of files/dirs to a destination zip file
+ * Both parameters must be FULL paths to the files/dirs
+ */
+function zip_files ($originalfiles, $destination) {
+ global $CFG;
+
+ //Extract everything from destination
+ $path_parts = pathinfo(cleardoubleslashes($destination));
+ $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($destfilename)) {
+ return false;
+ }
+
+ //If no extension, add it
+ if (empty($extension)) {
+ $extension = 'zip';
+ $destfilename = $destfilename.'.'.$extension;
+ }
+
+ //Check destination path exists
+ if (!is_dir($destpath)) {
+ return false;
+ }
+
+ //Check destination path is writable. TODO!!
+
+ //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;
+ }
+
+ $zipfiles = array();
+ $start = strlen($origpath)+1;
+ foreach($files as $file) {
+ $zipfiles[substr($file, $start)] = $file;
+ }
+
+ $packer = get_file_packer();
+
+ return $packer->zip_files_to_pathname($zipfiles, $destfilename);
+}
+
/////////////////////////////////////////////////////////////
/// Old functions not used anymore - candidates for removal
/////////////////////////////////////////////////////////////
return preg_replace('/(\/|\\\){1,}/','/',$path);
}
-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));
- $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($destfilename)) {
- return false;
- }
-
- //If no extension, add it
- if (empty($extension)) {
- $extension = 'zip';
- $destfilename = $destfilename.'.'.$extension;
- }
-
- //Check destination path exists
- if (!is_dir($destpath)) {
- return false;
- }
-
- //Check destination path is writable. TODO!!
-
- //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");
- //rewrite filenames because the old method with PCLZIP_OPT_REMOVE_PATH does not work under win32
- $zipfiles = array();
- $start = strlen($origpath)+1;
- foreach($files as $file) {
- $tf = array();
- $tf[PCLZIP_ATT_FILE_NAME] = $file;
- $tf[PCLZIP_ATT_FILE_NEW_FULL_NAME] = substr($file, $start);
- $zipfiles[] = $tf;
- }
- //create the archive
- $archive = new PclZip(cleardoubleslashes("$destpath/$destfilename"));
- if (($list = $archive->create($zipfiles) == 0)) {
- notice($archive->errorInfo(true));
- return false;
- }
-
- } else { // Use external zip program
-
- $filestozip = "";
- foreach ($files as $filetozip) {
- $filestozip .= escapeshellarg(basename($filetozip));
- $filestozip .= " ";
- }
- //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;
- //All converted to backslashes in WIN
- if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
- $command = str_replace('/','\\',$command);
- }
- Exec($command);
- }
- return true;
-}
-
-/**
- * This function is used as callback in unzip_file() function
- * to clean illegal characters for given platform and to prevent directory traversal.
- * Produces the same result as info-zip unzip.
- */
-function unzip_cleanfilename ($p_event, &$p_header) {
- $p_header['filename'] = ereg_replace('[[:cntrl:]]', '', $p_header['filename']); //strip control chars first!
- $p_header['filename'] = ereg_replace('\.\.+', '', $p_header['filename']); //directory traversal protection
- 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 {
- //Add filtering for other systems here
- // BSD: none (tested)
- // Linux: ??
- // MacosX: ??
- }
- $p_header['filename'] = cleardoubleslashes($p_header['filename']); //normalize the slashes/backslashes
- return 1;
-}
-
-/**
- * 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.
- */
-function unzip_show_status ($list,$removepath) {
- global $CFG;
-
- if (empty($CFG->unzip)) { // Use built-in php-based zip function
- $strname = get_string("name");
- $strsize = get_string("size");
- $strmodified = get_string("modified");
- $strstatus = get_string("status");
- echo "<table width=\"640\">";
- echo "<tr><th class=\"header\" scope=\"col\">$strname</th>";
- echo "<th class=\"header\" align=\"right\" scope=\"col\">$strsize</th>";
- echo "<th class=\"header\" align=\"right\" scope=\"col\">$strmodified</th>";
- echo "<th class=\"header\" align=\"right\" scope=\"col\">$strstatus</th></tr>";
- foreach ($list as $item) {
- echo "<tr>";
- $item['filename'] = str_replace(cleardoubleslashes($removepath).'/', "", $item['filename']);
- print_cell("left", s($item['filename']));
- if (! $item['folder']) {
- print_cell("right", display_size($item['size']));
- } else {
- echo "<td> </td>";
- }
- $filedate = userdate($item['mtime'], get_string("strftimedatetime"));
- print_cell("right", $filedate);
- print_cell("right", $item['status']);
- echo "</tr>";
- }
- echo "</table>";
-
- } else { // Use external zip program
- print_simple_box_start("center");
- echo "<pre>";
- foreach ($list as $item) {
- echo s(str_replace(cleardoubleslashes($removepath.'/'), '', $item)).'<br />';
- }
- echo "</pre>";
- print_simple_box_end();
- }
-}
-
/**
* Is current ip in give list?
* @param string $list