Fix bugs with international filenames
Fix bugs with international filenames

PHP's basename function has buggy handling of non-ASCII filenames,
requiring a custom implementation. Based on Drupal's custom basename
function.

Bug #100

--- a/include/Util.class.php
+++ b/include/Util.class.php
@@ -95,5 +95,39 @@
 		return str_replace($from, $to, $str);
 	}
 
+	/**
+	 * BaseName
+	 *
+	 * Get the filename of a given path
+	 *
+	 * based on Drupal's basename
+	 *
+	 * @access public
+	 * @param string $path path
+	 * @param string $suffix optionally trim this suffix
+	 * @static
+	 * @return string filename
+	 */
+	public static function BaseName($path, $suffix = null)
+	{
+		$sep = '/';
+		if (GitPHP_Util::IsWindows()) {
+			$sep .= '\\';
+		}
+
+		$path = rtrim($path, $sep);
+
+		if (!preg_match('@[^' . preg_quote($sep) . ']+$@', $path, $matches)) {
+			return '';
+		}
+
+		$filename = $matches[0];
+
+		if ($suffix) {
+			$filename = preg_replace('@' . preg_quote($suffix, '@') . '$@', '', $filename);
+		}
+		return $filename;
+	}
+
 }
 

--- a/include/git/FilesystemObject.class.php
+++ b/include/git/FilesystemObject.class.php
@@ -93,7 +93,7 @@
 	public function GetName()
 	{
 		if (!empty($this->path))
-			return basename($this->path);
+			return GitPHP_Util::BaseName($this->path);
 
 		return '';
 	}

comments