diff --git a/index.php b/index.php index ef1d808..e528a17 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. * ***************************************************************************/ @@ -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( @@ -1902,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 @@ -2149,12 +2165,17 @@ 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); $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 +2301,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 +2363,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 +2493,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 +2511,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 +2845,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 +3128,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 +3179,7 @@ function(){ print "