From 70d9c02aae2ae357deb5ab415469fbbf965d0177 Mon Sep 17 00:00:00 2001 From: Fyorl Date: Sat, 4 Aug 2012 16:50:03 +0100 Subject: [PATCH 1/4] [ticket/11044] Compress class now deals with file conflicts PHPBB3-11044 --- phpBB/includes/functions_compress.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/phpBB/includes/functions_compress.php b/phpBB/includes/functions_compress.php index 72d8eabe76..2d8ad84657 100644 --- a/phpBB/includes/functions_compress.php +++ b/phpBB/includes/functions_compress.php @@ -23,6 +23,11 @@ class compress { var $fp = 0; + /** + * @var array + */ + private $filelist = array(); + /** * Add file to archive */ @@ -122,6 +127,24 @@ class compress return true; } + /** + * Checks if a file by that name as already been added and, if it has, + * returns a new, unique name. + * + * @param string $name The filename + * @return string A unique string + */ + private function check_name($name) + { + if (isset($this->filelist[$name])) { + $this->filelist[$name]++; + return $name . '.' . $this->filelist[$name]; + } + + $this->filelist[$name] = 0; + return $name; + } + /** * Return available methods */ @@ -361,6 +384,7 @@ class compress_zip extends compress function data($name, $data, $is_dir = false, $stat) { $name = str_replace('\\', '/', $name); + $name = $this->check_name($name); $hexdtime = pack('V', $this->unix_to_dos_time($stat[9])); @@ -633,6 +657,7 @@ class compress_tar extends compress */ function data($name, $data, $is_dir = false, $stat) { + $name = $this->check_name($name); $this->wrote = true; $fzwrite = ($this->isbz && function_exists('bzwrite')) ? 'bzwrite' : (($this->isgz && @extension_loaded('zlib')) ? 'gzwrite' : 'fwrite'); From 3390712ed7097fb11d564fb6bd82d74c6b8e3731 Mon Sep 17 00:00:00 2001 From: Fyorl Date: Sun, 5 Aug 2012 11:29:02 +0100 Subject: [PATCH 2/4] [ticket/11044] Minor adjustments as per PR comments Changed private to protected, renamed check_name to unique_filename. PHPBB3-11044 --- phpBB/includes/functions_compress.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/phpBB/includes/functions_compress.php b/phpBB/includes/functions_compress.php index 2d8ad84657..869ed21cea 100644 --- a/phpBB/includes/functions_compress.php +++ b/phpBB/includes/functions_compress.php @@ -26,7 +26,7 @@ class compress /** * @var array */ - private $filelist = array(); + protected $filelist = array(); /** * Add file to archive @@ -132,11 +132,12 @@ class compress * returns a new, unique name. * * @param string $name The filename - * @return string A unique string + * @return string A unique filename */ - private function check_name($name) + protected function unique_filename($name) { - if (isset($this->filelist[$name])) { + if (isset($this->filelist[$name])) + { $this->filelist[$name]++; return $name . '.' . $this->filelist[$name]; } @@ -384,7 +385,7 @@ class compress_zip extends compress function data($name, $data, $is_dir = false, $stat) { $name = str_replace('\\', '/', $name); - $name = $this->check_name($name); + $name = $this->unique_filename($name); $hexdtime = pack('V', $this->unix_to_dos_time($stat[9])); @@ -657,7 +658,7 @@ class compress_tar extends compress */ function data($name, $data, $is_dir = false, $stat) { - $name = $this->check_name($name); + $name = $this->unique_filename($name); $this->wrote = true; $fzwrite = ($this->isbz && function_exists('bzwrite')) ? 'bzwrite' : (($this->isgz && @extension_loaded('zlib')) ? 'gzwrite' : 'fwrite'); From 011b494bc55224e59872621ea09c0f69baed2744 Mon Sep 17 00:00:00 2001 From: Fyorl Date: Sun, 5 Aug 2012 22:11:14 +0100 Subject: [PATCH 3/4] [ticket/11044] Preserve the file extension in unique filenames PHPBB3-11044 --- phpBB/includes/functions_compress.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/phpBB/includes/functions_compress.php b/phpBB/includes/functions_compress.php index 869ed21cea..80c0242517 100644 --- a/phpBB/includes/functions_compress.php +++ b/phpBB/includes/functions_compress.php @@ -138,8 +138,16 @@ class compress { if (isset($this->filelist[$name])) { + $start = $name; + $ext = ''; $this->filelist[$name]++; - return $name . '.' . $this->filelist[$name]; + + if (($pos = strrpos($name, '.')) !== false) { + $start = substr($name, 0, $pos); + $ext = substr($name, $pos); + } + + return $start . '_' . $this->filelist[$name] . $ext; } $this->filelist[$name] = 0; From ecb310c6f7a2f8aa5c1f2217d7de6cb878aa85f4 Mon Sep 17 00:00:00 2001 From: Fyorl Date: Mon, 6 Aug 2012 08:32:12 +0800 Subject: [PATCH 4/4] [ticket/11044] Added comment explaining filename splitting PHPBB3-11044 --- phpBB/includes/functions_compress.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/phpBB/includes/functions_compress.php b/phpBB/includes/functions_compress.php index 80c0242517..8e07e6d1b8 100644 --- a/phpBB/includes/functions_compress.php +++ b/phpBB/includes/functions_compress.php @@ -142,7 +142,10 @@ class compress $ext = ''; $this->filelist[$name]++; - if (($pos = strrpos($name, '.')) !== false) { + // Separate the extension off the end of the filename to preserve it + $pos = strrpos($name, '.'); + if ($pos !== false) + { $start = substr($name, 0, $pos); $ext = substr($name, $pos); }