From 68f8fd17695f68847991fc5a058291cb708090b9 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Fri, 20 Jan 2006 09:03:16 +0000 Subject: [PATCH] * (bug 4679) Work around buggy basename() function in PHP5, which breaks uploads of files starting with multibyte characters on Linux. wfBaseName() doesn't suffer this bug, and understands backslash on both Unix and Windows. --- RELEASE-NOTES | 4 ++++ includes/GlobalFunctions.php | 18 ++++++++++++++++++ includes/SpecialUpload.php | 4 ++-- tests/GlobalTest.php | 26 ++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 30f1cda1b2..0269e164d3 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -544,6 +544,10 @@ fully support the editing toolbar, but was found to be too confusing. problem under PHP 5 with APC as opcode cache. See details: http://mail.wikipedia.org/pipermail/wikitech-l/2006-January/033660.html * Small changes to tabs in Monobook skin c/o Chris Ware +* (bug 4679) Work around buggy basename() function in PHP5, which breaks + uploads of files starting with multibyte characters on Linux. + wfBaseName() doesn't suffer this bug, and understands backslash on + both Unix and Windows. === Caveats === diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 8a74427ea4..5b4d8bc869 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -1791,5 +1791,23 @@ function wfRegexReplacement( $string ) { return $string; } +/** + * Return the final portion of a pathname. + * Reimplemented because PHP5's basename() is buggy with multibyte text. + * http://bugs.php.net/bug.php?id=33898 + * + * PHP's basename() only considers '\' a pathchar on Windows and Netware. + * We'll consider it so always, as we don't want \s in our Unix paths either. + * + * @param string $path + * @return string + */ +function wfBaseName( $path ) { + if( preg_match( '#([^/\\\\]*)[/\\\\]*$#', $path, $matches ) ) { + return $matches[1]; + } else { + return ''; + } +} ?> diff --git a/includes/SpecialUpload.php b/includes/SpecialUpload.php index ff3256471b..8ca76571d9 100644 --- a/includes/SpecialUpload.php +++ b/includes/SpecialUpload.php @@ -171,9 +171,9 @@ class UploadForm { # Chop off any directories in the given filename if ( $this->mDestFile ) { - $basename = basename( $this->mDestFile ); + $basename = wfBaseName( $this->mDestFile ); } else { - $basename = basename( $this->mOname ); + $basename = wfBaseName( $this->mOname ); } /** diff --git a/tests/GlobalTest.php b/tests/GlobalTest.php index a3aed8b075..1567a18908 100644 --- a/tests/GlobalTest.php +++ b/tests/GlobalTest.php @@ -178,6 +178,32 @@ class GlobalTest extends PHPUnit_TestCase { wfTimestamp( TS_DB, '2001-01-15 12:34:56' ), 'TS_DB to TS_DB' ); } + + function testBasename() { + $sets = array( + '' => '', + '/' => '', + '\\' => '', + '//' => '', + '\\\\' => '', + 'a' => 'a', + 'aaaa' => 'aaaa', + '/a' => 'a', + '\\a' => 'a', + '/aaaa' => 'aaaa', + '\\aaaa' => 'aaaa', + '/aaaa/' => 'aaaa', + '\\aaaa\\' => 'aaaa', + '\\aaaa\\' => 'aaaa', + '/mnt/upload3/wikipedia/en/thumb/8/8b/Zork_Grand_Inquisitor_box_cover.jpg/93px-Zork_Grand_Inquisitor_box_cover.jpg' => '93px-Zork_Grand_Inquisitor_box_cover.jpg', + 'C:\\Progra~1\\Wikime~1\\Wikipe~1\\VIEWER.EXE' => 'VIEWER.EXE', + 'Östergötland_coat_of_arms.png' => 'Östergötland_coat_of_arms.png', + ); + foreach( $sets as $from => $to ) { + $this->assertEquals( $to, wfBaseName( $from ), + "wfBaseName('$from') => '$to'"); + } + } /* TODO: many more! */ } -- 2.20.1