From fa446926c63aef4f4f394967b84d20e2da0dad6d Mon Sep 17 00:00:00 2001
From: Andreas Gohr <andi@splitbrain.org>
Date: Sun, 29 Jul 2012 12:09:42 +0200
Subject: [PATCH] more utf8_basename fixes

---
 _test/tests/inc/utf8_basename.test.php | 17 ++++++++++++++++-
 inc/utf8.php                           |  7 +++----
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/_test/tests/inc/utf8_basename.test.php b/_test/tests/inc/utf8_basename.test.php
index 475b7ada8..1544e9915 100644
--- a/_test/tests/inc/utf8_basename.test.php
+++ b/_test/tests/inc/utf8_basename.test.php
@@ -64,8 +64,23 @@ class utf8_basename_test extends DokuWikiTest {
             array('bar.test.png', '.png', 'bar.test'),
 
             array('/bar.test.png', '', 'bar.test.png'),
-            array('\\bar.test.png', '', 'bar.test.png'),
             array('/bar.test.png', '.png', 'bar.test'),
+            array('\\bar.test.png', '', 'bar.test.png'),
+            array('\\bar.test.png', '.png', 'bar.test'),
+            array('\\/bar.test.png', '', 'bar.test.png'),
+            array('\\/bar.test.png', '.png', 'bar.test'),
+            array('/\\bar.test.png', '', 'bar.test.png'),
+            array('/\\bar.test.png', '.png', 'bar.test'),
+
+            // PHP's basename does this too:
+            array('foo/',   '', 'foo'),
+            array('foo\\',  '', 'foo'),
+            array('foo\\/', '', 'foo'),
+            array('foo/\\', '', 'foo'),
+            array('foo.png/',   '.png', 'foo'),
+            array('foo.png\\',  '.png', 'foo'),
+            array('foo.png\\/', '.png', 'foo'),
+            array('foo.png/\\', '.png', 'foo'),
         );
 
         foreach($data as $test){
diff --git a/inc/utf8.php b/inc/utf8.php
index e3e7e8c1a..6fab8502c 100644
--- a/inc/utf8.php
+++ b/inc/utf8.php
@@ -91,10 +91,9 @@ if(!function_exists('utf8_basename')){
      * @return string
      */
     function utf8_basename($path, $suffix=''){
-        $slashrpos = strrpos($path, '/');
-        $bslashrpos = strrpos($path, '\\');
-        $rpos = max($slashrpos === false ? -1 : $slashrpos, $bslashrpos === false ? -1 : $bslashrpos);
-        $path = substr($path, $rpos+1);
+        $path = trim($path,'\\/');
+        $rpos = max(strrpos($path, '/'), strrpos($path, '\\'));
+        if($rpos) $path = substr($path, $rpos+1);
 
         $suflen = strlen($suffix);
         if($suflen && (substr($path, -$suflen) == $suffix)){
-- 
GitLab