diff --git a/index.php b/index.php index ef1d808..a75e538 100644 --- a/index.php +++ b/index.php @@ -123,6 +123,22 @@ // $_CONFIG['charset'] = "UTF-8"; +// +// OS Charset. +// +// Recommended setting for Linux is UTF-8. +// +// Recommended setting for Windows is: +// CP1251 if your OS uses cyrillic encoding +// CP1252 if your OS uses latin encoding +// +// Technical details about this: +// https://github.com/marekrei/encode-explorer/issues/42 +// +// Default: $_CONFIG['os_charset'] = "UTF-8"; +// +$_CONFIG['os_charset'] = "UTF-8"; + /* * PERMISSIONS */ @@ -749,7 +765,7 @@ "upload_type_not_allowed" => "이 종류의 파일은 올릴 수 없습니다.", "del" => "삭제", "log_out" => "로그아웃" -); +); // Norwegian $_TRANSLATIONS["no"] = array( @@ -2154,7 +2170,7 @@ function uploadFile($location, $userfile) $name = stripslashes($name); $upload_dir = $location->getFullPath(); - $upload_file = $upload_dir . $name; + $upload_file = $upload_dir . EncodeExplorer::translate_encoding($name, true); if(function_exists("finfo_open") && function_exists("finfo_file")) $mime_type = File::getFileMime($userfile['tmp_name']); @@ -2280,9 +2296,10 @@ function getName() return $this->name; } - function getNameHtml() + function getNameHtml($translate_encoding = false) { - return htmlspecialchars($this->name); + $name = ($translate_encoding ? EncodeExplorer::translate_encoding($this->name) : $this->name); + return htmlspecialchars($name); } function getNameEncoded() @@ -2341,9 +2358,10 @@ function getNameEncoded() return rawurlencode($this->name); } - function getNameHtml() + function getNameHtml($translate_encoding = false) { - return htmlspecialchars($this->name); + $name = ($translate_encoding ? EncodeExplorer::translate_encoding($this->name) : $this->name); + return htmlspecialchars($name); } function getSize() @@ -2470,14 +2488,15 @@ public static function splitPath($dir) // Get the current directory. // Options: Include the prefix ("./"); URL-encode the string; HTML-encode the string; return directory n-levels up // - function getDir($prefix, $encoded, $html, $up) + function getDir($prefix, $encoded, $html, $up, $translate_encoding = false) { $dir = ""; if($prefix == true) $dir .= "./"; for($i = 0; $i < ((count($this->path) >= $up && $up > 0)?count($this->path)-$up:count($this->path)); $i++) { - $temp = $this->path[$i]; + $temp = ($translate_encoding ? EncodeExplorer::translate_encoding($this->path[$i]) : $this->path[$i]); + if($encoded) $temp = rawurlencode($temp); if($html) @@ -2487,12 +2506,11 @@ function getDir($prefix, $encoded, $html, $up) return $dir; } - function getPathLink($i, $html) + function getPathLink($i, $html, $translate_encoding = false) { - if($html) - return htmlspecialchars($this->path[$i]); - else - return $this->path[$i]; + $path = ($translate_encoding ? EncodeExplorer::translate_encoding($this->path[$i]) : $this->path[$i]); + + return ($html ? htmlspecialchars($path) : $path); } function getFullPath() @@ -2822,6 +2840,40 @@ function debug() $this->files[$i]->output(); } + // + // Encode output in correct encoding + // + public static function translate_encoding($string, $undo = false) + { + if(!is_string($string) || EncodeExplorer::getConfig('charset') == EncodeExplorer::getConfig('os_charset')) + return $string; + + if(!$undo) { + // From system encoding to output + $in = EncodeExplorer::getConfig('os_charset'); + $out = EncodeExplorer::getConfig('charset'); + } else { + // From input to system encoding + $in = EncodeExplorer::getConfig('charset'); + $out = EncodeExplorer::getConfig('os_charset'); + } + + // Attempt using mb_convert_encoding + if(function_exists('mb_convert_encoding')) + $tmp = @mb_convert_encoding($string, $out, $in); + + // Attempt using iconv + if(empty($tmp) && function_exists('iconv')) + $tmp = @iconv($in, $out, $string); + + // If any of them succeeds, return converted one + if(!empty($tmp) && is_string($tmp)) + return $tmp; + + // Otherwise return as is + return $string; + } + // // Comparison functions for sorting. // @@ -3071,7 +3123,7 @@ function(){ for($i = 0; $i < count($this->location->path); $i++) { print "> makeLink(false, false, null, null, null, $this->location->getDir(false, true, false, count($this->location->path) - $i - 1))."\">"; - print $this->location->getPathLink($i, true); + print $this->location->getPathLink($i, true, true); print "\n"; } ?> @@ -3122,7 +3174,7 @@ function(){ print "