1
0
mirror of https://github.com/e107inc/e107.git synced 2025-07-27 01:40:22 +02:00

Issue #5208 - Start of PCLZip removal.

This commit is contained in:
camer0n
2024-04-17 14:17:04 -07:00
parent 15e717b66e
commit bdbda018d2
2 changed files with 2320 additions and 2278 deletions

View File

@@ -1,5 +1,5 @@
<?php <?php
/* /*
* e107 website system * e107 website system
* *
* Copyright (C) 2008-2010 e107 Inc (e107.org) * Copyright (C) 2008-2010 e107 Inc (e107.org)
@@ -10,7 +10,7 @@
* $Id$ * $Id$
*/ */
/** /**
* File/folder manipulation handler * File/folder manipulation handler
* *
* @package e107 * @package e107
@@ -19,24 +19,24 @@
* @author e107 Inc. * @author e107 Inc.
*/ */
if(!defined('e107_INIT')) if(!defined('e107_INIT'))
{ {
exit; exit;
} }
if(defined('SAFE_MODE') && SAFE_MODE === false) if(defined('SAFE_MODE') && SAFE_MODE === false)
{ {
@set_time_limit(10 * 60); // throws error in safe-mode. @set_time_limit(10 * 60); // throws error in safe-mode.
} }
//session_write_close(); //session_write_close();
@ini_set("max_execution_time", 10 * 60); @ini_set("max_execution_time", 10 * 60);
//while (@ob_end_clean()); // kill all output buffering else it eats server resources //while (@ob_end_clean()); // kill all output buffering else it eats server resources
//ob_implicit_flush(TRUE); //ob_implicit_flush(TRUE);
/* /*
Class to return a list of files, with options to specify a filename matching string and exclude specified directories. Class to return a list of files, with options to specify a filename matching string and exclude specified directories.
get_files() is the usual entry point. get_files() is the usual entry point.
$path - start directory (doesn't matter whether it has a trailing '/' or not - its stripped) $path - start directory (doesn't matter whether it has a trailing '/' or not - its stripped)
$fmask - regex expression of file names to match (empty string matches all). Omit the start and end delimiters - '#' is added here. $fmask - regex expression of file names to match (empty string matches all). Omit the start and end delimiters - '#' is added here.
If the first character is '~', this becomes a list of files to exclude (the '~' is stripped) If the first character is '~', this becomes a list of files to exclude (the '~' is stripped)
@@ -50,37 +50,37 @@
If the standard file or directory filter is unacceptable in a special application, the relevant variable can be set to an empty array (emphasis - ARRAY). If the standard file or directory filter is unacceptable in a special application, the relevant variable can be set to an empty array (emphasis - ARRAY).
setDefaults() restores the defaults - preferable to setting using a 'fixed' string. Can be called prior to using the class without knowledge of what went before. setDefaults() restores the defaults - preferable to setting using a 'fixed' string. Can be called prior to using the class without knowledge of what went before.
get_dirs() returns a list of the directories in a specified directory (no recursion) - similar critera to get_files() get_dirs() returns a list of the directories in a specified directory (no recursion) - similar critera to get_files()
rmtree() attempts to remove a complete directory tree, including the files it contains rmtree() attempts to remove a complete directory tree, including the files it contains
Note: Note:
Directory filters look for an exact match (i.e. regex not supported) Directory filters look for an exact match (i.e. regex not supported)
Behaviour is slightly different to previous version: Behaviour is slightly different to previous version:
$omit used to be applied to just files (so would recurse down a tree even if no files match) - now used for directories $omit used to be applied to just files (so would recurse down a tree even if no files match) - now used for directories
The default file and directory filters are always applied (unless modified between instantiation/set defaults and call) The default file and directory filters are always applied (unless modified between instantiation/set defaults and call)
*/ */
/** /**
* Flag used by prepareDirectory() method -- create directory if not present. * Flag used by prepareDirectory() method -- create directory if not present.
*/ */
define('FILE_CREATE_DIRECTORY', 1); define('FILE_CREATE_DIRECTORY', 1);
/** /**
* Flag used by prepareDirectory() method -- file permissions may be changed. * Flag used by prepareDirectory() method -- file permissions may be changed.
*/ */
define('FILE_MODIFY_PERMISSIONS', 2); define('FILE_MODIFY_PERMISSIONS', 2);
/** /**
* *
*/ */
class e_file class e_file
{ {
/** /**
* Array of directory names to ignore (in addition to any set by caller) * Array of directory names to ignore (in addition to any set by caller)
@@ -244,6 +244,7 @@ class e_file
*/ */
function get_files($path, $fmask = '', $omit = 'standard', $recurse_level = 0) function get_files($path, $fmask = '', $omit = 'standard', $recurse_level = 0)
{ {
$ret = array(); $ret = array();
$invert = false; $invert = false;
if(!empty($fmask) && strpos($fmask, '~') === 0) if(!empty($fmask) && strpos($fmask, '~') === 0)
@@ -441,14 +442,16 @@ class e_file
/** /**
* Return information about a file, including mime-type * Return information about a file, including mime-type
* @deprecated - use getFileInfo() instead. *
* @param string $path_to_file * @param string $path_to_file
* @param bool $imgcheck * @param bool $imgcheck
* @param bool $auto_fix_ext * @param bool $auto_fix_ext
* @return array|bool * @return array|bool
* @deprecated - use getFileInfo() instead.
*/ */
public function get_file_info($path_to_file, $imgcheck = true, $auto_fix_ext = true) public function get_file_info($path_to_file, $imgcheck = true, $auto_fix_ext = true)
{ {
return $this->getFileInfo($path_to_file, $imgcheck, $auto_fix_ext); return $this->getFileInfo($path_to_file, $imgcheck, $auto_fix_ext);
} }
@@ -560,6 +563,7 @@ class e_file
} }
error_log($msg); error_log($msg);
return false; // May not be installed return false; // May not be installed
} }
@@ -581,7 +585,7 @@ class e_file
if(curl_errno($cp)) // Fixes curl_error output - here see #1936 if(curl_errno($cp)) // Fixes curl_error output - here see #1936
{ {
error_log('cURL error: '.curl_error($cp)); error_log('cURL error: ' . curl_error($cp));
} }
curl_close($cp); curl_close($cp);
@@ -820,7 +824,7 @@ class e_file
{ {
$ret = array(); $ret = array();
$path = rtrim($path,'/'); $path = rtrim($path, '/');
if($path[strlen($path) - 1] === '/') if($path[strlen($path) - 1] === '/')
// if(substr($path, -1) == '/') // if(substr($path, -1) == '/')
{ {
@@ -1201,14 +1205,14 @@ class e_file
header('Expires: 0'); header('Expires: 0');
header("Cache-Control: max-age=30"); header("Cache-Control: max-age=30");
header('Content-Type: '.$contentType); header('Content-Type: ' . $contentType);
header('Content-Disposition: '.$contentDisp.'; filename="'.$file.'"'); header('Content-Disposition: ' . $contentDisp . '; filename="' . $file . '"');
header("Content-Length: {$data_len}"); header("Content-Length: {$data_len}");
header("Pragma: public"); header("Pragma: public");
if(!empty($opts['encoding'])) if(!empty($opts['encoding']))
{ {
header('Content-Transfer-Encoding: '.$opts['encoding']); header('Content-Transfer-Encoding: ' . $opts['encoding']);
} }
if($seek) if($seek)
@@ -1229,7 +1233,7 @@ class e_file
{ {
if(E107_DEBUG_LEVEL > 0 && ADMIN) if(E107_DEBUG_LEVEL > 0 && ADMIN)
{ {
$mes = __METHOD__." -- File failed: " . $file . "\n"; $mes = __METHOD__ . " -- File failed: " . $file . "\n";
$mes .= "Path: " . $path . "\n"; $mes .= "Path: " . $path . "\n";
$mes .= "Backtrace: "; $mes .= "Backtrace: ";
$mes .= print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), true); $mes .= print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), true);
@@ -1321,6 +1325,37 @@ class e_file
} }
private function addToZip(ZipArchive $zip, $src, $localname)
{
if(is_dir($src))
{
$dir = opendir($src);
// add empty directories
$zip->addEmptyDir($localname);
while(false !== ($file = readdir($dir)))
{
if(($file != '.') && ($file != '..'))
{
$this->addToZip($zip, $src . '/' . $file, $localname . '/' . $file);
}
}
closedir($dir);
}
elseif(is_file($src))
{
if(!$zip->addFile($src, $localname))
{
$this->error = "Could not add file: $src";
e107::getLog()->addError($this->error)->save('FILE', E_LOG_NOTICE);
}
}
}
/** /**
* Zip up folders and files * Zip up folders and files
* *
@@ -1331,7 +1366,6 @@ class e_file
*/ */
public function zip($filePaths = null, $newFile = '', $options = array()) public function zip($filePaths = null, $newFile = '', $options = array())
{ {
if(empty($newFile)) if(empty($newFile))
{ {
$newFile = e_BACKUP . eHelper::title2sef(SITENAME) . "_" . date("Y-m-d-H-i-s") . ".zip"; $newFile = e_BACKUP . eHelper::title2sef(SITENAME) . "_" . date("Y-m-d-H-i-s") . ".zip";
@@ -1342,22 +1376,27 @@ class e_file
return "No file-paths set!"; return "No file-paths set!";
} }
require_once(e_HANDLER . 'pclzip.lib.php'); $zip = new ZipArchive();
$archive = new PclZip($newFile);
$removePath = (!empty($options['remove_path'])) ? $options['remove_path'] : e_BASE; if($zip->open($newFile, ZipArchive::CREATE) !== true)
if($archive->create($filePaths, PCLZIP_OPT_REMOVE_PATH, $removePath) == 0)
{ {
$error = $archive->errorInfo(true); $this->error = "Cannot open <$newFile>\n";
e107::getLog()->addError($error)->save('FILE', E_LOG_NOTICE); e107::getLog()->addError($this->error)->save('FILE', E_LOG_NOTICE);
return false; return false;
} }
else
$removePath = (!empty($options['remove_path'])) ? $options['remove_path'] : e_BASE;
foreach($filePaths as $file)
{ {
return $newFile; $localname = str_replace($removePath, '', $file);
$this->addToZip($zip, $file, rtrim($localname, '/'));
} }
$zip->close();
return $newFile;
} }
@@ -1684,14 +1723,14 @@ class e_file
} }
else // Legacy Method. /* else // Legacy Method.
{ {
require_once(e_HANDLER . "pclzip.lib.php"); require_once(e_HANDLER . "pclzip.lib.php");
$archive = new PclZip(e_TEMP . $localfile); $archive = new PclZip(e_TEMP . $localfile);
$unarc = ($fileList = $archive->extract(PCLZIP_OPT_PATH, e_TEMP, PCLZIP_OPT_SET_CHMOD, 0755)); // Store in TEMP first. $unarc = ($fileList = $archive->extract(PCLZIP_OPT_PATH, e_TEMP, PCLZIP_OPT_SET_CHMOD, 0755)); // Store in TEMP first.
$dir = $this->getRootFolder($unarc); $dir = $this->getRootFolder($unarc);
} }*/
$destpath = ($type == 'theme') ? e_THEME : e_PLUGIN; $destpath = ($type == 'theme') ? e_THEME : e_PLUGIN;
@@ -1761,14 +1800,14 @@ class e_file
/** /**
* @deprecated Use getAllowedFileTypes()
* Get an array of permitted filetypes according to a set hierarchy.
* If a specific file name given, that's used. Otherwise the default hierarchy is used
*
* @param string|boolean $file_mask - comma-separated list of allowed file types * @param string|boolean $file_mask - comma-separated list of allowed file types
* @param string $filename - optional override file name - defaults ignored * @param string $filename - optional override file name - defaults ignored
* *
* @return array of filetypes * @return array of filetypes
* @deprecated Use getAllowedFileTypes()
* Get an array of permitted filetypes according to a set hierarchy.
* If a specific file name given, that's used. Otherwise the default hierarchy is used
*
*/ */
function getFiletypeLimits($file_mask = false, $filename = '') // Wrapper only for now. function getFiletypeLimits($file_mask = false, $filename = '') // Wrapper only for now.
{ {
@@ -1877,8 +1916,10 @@ class e_file
foreach($unarc as $k => $v) foreach($unarc as $k => $v)
{ {
if($this->matchFound($v['stored_filename'], $excludeMatch) || if(
in_array($v['stored_filename'], $excludes)) $this->matchFound($v['stored_filename'], $excludeMatch) ||
in_array($v['stored_filename'], $excludes)
)
{ {
$skipped[] = $v['stored_filename']; $skipped[] = $v['stored_filename'];
continue; continue;
@@ -2235,7 +2276,7 @@ class e_file
$remote = false; $remote = false;
if(strpos($targetFile,'http') === 0) // remote file. if(strpos($targetFile, 'http') === 0) // remote file.
{ {
$tmp = parse_url($targetFile); $tmp = parse_url($targetFile);
$targetFile = $tmp['path']; $targetFile = $tmp['path'];
@@ -2278,6 +2319,7 @@ class e_file
*/ */
private function getMimeTypes() private function getMimeTypes()
{ {
return array( return array(
'asc' => 'text/plain', 'asc' => 'text/plain',
'css' => 'text/css', 'css' => 'text/css',
@@ -2413,15 +2455,15 @@ class e_file
} }
/** /**
* Return the mime-type based on the file's extension. * Return the mime-type based on the file's extension.
*
* @param string $filename * @param string $filename
* @return string * @return string
*/ */
public function getMime($filename) public function getMime($filename)
{ {
$filename = basename($filename); $filename = basename($filename);
$tmp = explode('.', $filename); $tmp = explode('.', $filename);
@@ -2570,4 +2612,4 @@ class e_file
} }
} }

View File

@@ -436,7 +436,7 @@ class e_jsmanagerTest extends \Codeception\Test\Unit
} }
$tp->setStaticUrl(null); $tp->setStaticUrl(null);
e107::getParser()->setStaticUrl(null);
} }
/* /*