From da86c692f034e6071e9ea43abec42a2859431e3f Mon Sep 17 00:00:00 2001 From: NewEraCracker Date: Tue, 5 Apr 2016 10:31:11 +0100 Subject: [PATCH 1/4] Fixes for marekrei#42, marekrei#40 and marekrei#37 Encoding translation is now more transparent and handled in a logical, albeit hackish, way. Thanks to @havet and @kofbox for their feedback. Will be followed by a commit that handles the basename() issue. Tested and working when encode-explorer is running on Windows platform. --- index.php | 86 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 17 deletions(-) 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 "\"dir\"\n"; print "mobile == true ? 1:2)."\">\n"; print "makeLink(false, false, null, null, null, $this->location->getDir(false, true, false, 0).$dir->getNameEncoded())."\" class=\"item dir\">"; - print $dir->getNameHtml(); + print $dir->getNameHtml(true); print "\n"; print "\n"; if($this->mobile != true) @@ -3150,14 +3202,14 @@ function(){ print "files)?" last":"")."\">\n"; print "\"".$file-getType()."\" src=\"".$this->makeIcon($file->getType())."\" />\n"; print "\n"; - print "\t\tlocation->getDir(false, true, false, 0).$file->getNameEncoded()."\""; + print "\t\tlocation->getDir(false, true, false, 0, true).$file->getNameEncoded(true)."\""; if(EncodeExplorer::getConfig('open_in_new_window') == true) print "target=\"_blank\""; print " class=\"item file"; if($file->isValidForThumb()) print " thumb"; print "\">"; - print $file->getNameHtml(); + print $file->getNameHtml(true); if($this->mobile == true) { print "".$this->formatSize($file->getSize()).""; From 3b385e2a25a40f9d344a31efcd2bf0512e7dbbd6 Mon Sep 17 00:00:00 2001 From: NewEraCracker Date: Wed, 6 Apr 2016 15:58:04 +0100 Subject: [PATCH 2/4] Workaround for basename() issues regarding encoding Normalize slashes, add spaces, basename() and then trim back to normal. --- index.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.php b/index.php index a75e538..73dfc91 100644 --- a/index.php +++ b/index.php @@ -2165,7 +2165,12 @@ function newFolder($location, $dirname) function uploadFile($location, $userfile) { global $encodeExplorer; - $name = basename($userfile['name']); + + // XXX: basename alone is buggy under certain situations + // Workaround by normalizing slashes and adding leading spaces + $name = str_replace('/', ' / ', str_replace('\\', '/', $userfile['name'])); + $name = trim(basename(' '.$name)); + if(get_magic_quotes_gpc()) $name = stripslashes($name); From 0c9636ac27ab3667ddf6bd669f55ca5ba103b780 Mon Sep 17 00:00:00 2001 From: NewEraCracker Date: Tue, 5 Apr 2016 20:33:29 +0100 Subject: [PATCH 3/4] BOM sucks. period. https://en.wikipedia.org/wiki/Byte_order_mark --- index.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index 73dfc91..59c6dd8 100644 --- a/index.php +++ b/index.php @@ -8,8 +8,8 @@ * Homepage : encode-explorer.siineiolekala.net * * - * NB!:If you change anything, save with UTF-8! Otherwise you may - * encounter problems, especially when displaying images. + * NB!:If you change anything, save with UTF-8 without BOM! Otherwise you + * may encounter problems, especially when displaying images. * ***************************************************************************/ From 9a4bc8f2c16a6b4a94c90fdaf90f047d4f765472 Mon Sep 17 00:00:00 2001 From: NewEraCracker Date: Sat, 9 Apr 2016 18:28:19 +0100 Subject: [PATCH 4/4] Fix marekrei#45 Spotted by @TJuTZu --- index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.php b/index.php index 59c6dd8..e528a17 100644 --- a/index.php +++ b/index.php @@ -1918,7 +1918,7 @@ public static function log($message) { if(Location::isFileWritable(EncodeExplorer::getConfig('log_file'))) { - $message = "[" . date("Y-m-d h:i:s", mktime()) . "] ".$message." (".$_SERVER["HTTP_USER_AGENT"].")\n"; + $message = "[" . date("Y-m-d H:i:s", mktime()) . "] ".$message." (".$_SERVER["HTTP_USER_AGENT"].")\n"; error_log($message, 3, EncodeExplorer::getConfig('log_file')); } else