Skip to content
This repository was archived by the owner on Mar 20, 2021. It is now read-only.

Commit da86c69

Browse files
committed
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.
1 parent 9d7b4d4 commit da86c69

1 file changed

Lines changed: 69 additions & 17 deletions

File tree

index.php

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@
123123
//
124124
$_CONFIG['charset'] = "UTF-8";
125125

126+
//
127+
// OS Charset.
128+
//
129+
// Recommended setting for Linux is UTF-8.
130+
//
131+
// Recommended setting for Windows is:
132+
// CP1251 if your OS uses cyrillic encoding
133+
// CP1252 if your OS uses latin encoding
134+
//
135+
// Technical details about this:
136+
// https://github.com/marekrei/encode-explorer/issues/42
137+
//
138+
// Default: $_CONFIG['os_charset'] = "UTF-8";
139+
//
140+
$_CONFIG['os_charset'] = "UTF-8";
141+
126142
/*
127143
* PERMISSIONS
128144
*/
@@ -749,7 +765,7 @@
749765
"upload_type_not_allowed" => "이 종류의 파일은 올릴 수 없습니다.",
750766
"del" => "삭제",
751767
"log_out" => "로그아웃"
752-
);
768+
);
753769

754770
// Norwegian
755771
$_TRANSLATIONS["no"] = array(
@@ -2154,7 +2170,7 @@ function uploadFile($location, $userfile)
21542170
$name = stripslashes($name);
21552171

21562172
$upload_dir = $location->getFullPath();
2157-
$upload_file = $upload_dir . $name;
2173+
$upload_file = $upload_dir . EncodeExplorer::translate_encoding($name, true);
21582174

21592175
if(function_exists("finfo_open") && function_exists("finfo_file"))
21602176
$mime_type = File::getFileMime($userfile['tmp_name']);
@@ -2280,9 +2296,10 @@ function getName()
22802296
return $this->name;
22812297
}
22822298

2283-
function getNameHtml()
2299+
function getNameHtml($translate_encoding = false)
22842300
{
2285-
return htmlspecialchars($this->name);
2301+
$name = ($translate_encoding ? EncodeExplorer::translate_encoding($this->name) : $this->name);
2302+
return htmlspecialchars($name);
22862303
}
22872304

22882305
function getNameEncoded()
@@ -2341,9 +2358,10 @@ function getNameEncoded()
23412358
return rawurlencode($this->name);
23422359
}
23432360

2344-
function getNameHtml()
2361+
function getNameHtml($translate_encoding = false)
23452362
{
2346-
return htmlspecialchars($this->name);
2363+
$name = ($translate_encoding ? EncodeExplorer::translate_encoding($this->name) : $this->name);
2364+
return htmlspecialchars($name);
23472365
}
23482366

23492367
function getSize()
@@ -2470,14 +2488,15 @@ public static function splitPath($dir)
24702488
// Get the current directory.
24712489
// Options: Include the prefix ("./"); URL-encode the string; HTML-encode the string; return directory n-levels up
24722490
//
2473-
function getDir($prefix, $encoded, $html, $up)
2491+
function getDir($prefix, $encoded, $html, $up, $translate_encoding = false)
24742492
{
24752493
$dir = "";
24762494
if($prefix == true)
24772495
$dir .= "./";
24782496
for($i = 0; $i < ((count($this->path) >= $up && $up > 0)?count($this->path)-$up:count($this->path)); $i++)
24792497
{
2480-
$temp = $this->path[$i];
2498+
$temp = ($translate_encoding ? EncodeExplorer::translate_encoding($this->path[$i]) : $this->path[$i]);
2499+
24812500
if($encoded)
24822501
$temp = rawurlencode($temp);
24832502
if($html)
@@ -2487,12 +2506,11 @@ function getDir($prefix, $encoded, $html, $up)
24872506
return $dir;
24882507
}
24892508

2490-
function getPathLink($i, $html)
2509+
function getPathLink($i, $html, $translate_encoding = false)
24912510
{
2492-
if($html)
2493-
return htmlspecialchars($this->path[$i]);
2494-
else
2495-
return $this->path[$i];
2511+
$path = ($translate_encoding ? EncodeExplorer::translate_encoding($this->path[$i]) : $this->path[$i]);
2512+
2513+
return ($html ? htmlspecialchars($path) : $path);
24962514
}
24972515

24982516
function getFullPath()
@@ -2822,6 +2840,40 @@ function debug()
28222840
$this->files[$i]->output();
28232841
}
28242842

2843+
//
2844+
// Encode output in correct encoding
2845+
//
2846+
public static function translate_encoding($string, $undo = false)
2847+
{
2848+
if(!is_string($string) || EncodeExplorer::getConfig('charset') == EncodeExplorer::getConfig('os_charset'))
2849+
return $string;
2850+
2851+
if(!$undo) {
2852+
// From system encoding to output
2853+
$in = EncodeExplorer::getConfig('os_charset');
2854+
$out = EncodeExplorer::getConfig('charset');
2855+
} else {
2856+
// From input to system encoding
2857+
$in = EncodeExplorer::getConfig('charset');
2858+
$out = EncodeExplorer::getConfig('os_charset');
2859+
}
2860+
2861+
// Attempt using mb_convert_encoding
2862+
if(function_exists('mb_convert_encoding'))
2863+
$tmp = @mb_convert_encoding($string, $out, $in);
2864+
2865+
// Attempt using iconv
2866+
if(empty($tmp) && function_exists('iconv'))
2867+
$tmp = @iconv($in, $out, $string);
2868+
2869+
// If any of them succeeds, return converted one
2870+
if(!empty($tmp) && is_string($tmp))
2871+
return $tmp;
2872+
2873+
// Otherwise return as is
2874+
return $string;
2875+
}
2876+
28252877
//
28262878
// Comparison functions for sorting.
28272879
//
@@ -3071,7 +3123,7 @@ function(){
30713123
for($i = 0; $i < count($this->location->path); $i++)
30723124
{
30733125
print "&gt; <a href=\"".$this->makeLink(false, false, null, null, null, $this->location->getDir(false, true, false, count($this->location->path) - $i - 1))."\">";
3074-
print $this->location->getPathLink($i, true);
3126+
print $this->location->getPathLink($i, true, true);
30753127
print "</a>\n";
30763128
}
30773129
?>
@@ -3122,7 +3174,7 @@ function(){
31223174
print "<td class=\"icon\"><img alt=\"dir\" src=\"?img=directory\" /></td>\n";
31233175
print "<td class=\"name\" colspan=\"".($this->mobile == true ? 1:2)."\">\n";
31243176
print "<a href=\"".$this->makeLink(false, false, null, null, null, $this->location->getDir(false, true, false, 0).$dir->getNameEncoded())."\" class=\"item dir\">";
3125-
print $dir->getNameHtml();
3177+
print $dir->getNameHtml(true);
31263178
print "</a>\n";
31273179
print "</td>\n";
31283180
if($this->mobile != true)
@@ -3150,14 +3202,14 @@ function(){
31503202
print "<tr class=\"row ".$row_style.(++$count == count($this->files)?" last":"")."\">\n";
31513203
print "<td class=\"icon\"><img alt=\"".$file->getType()."\" src=\"".$this->makeIcon($file->getType())."\" /></td>\n";
31523204
print "<td class=\"name\" colspan=\"1\">\n";
3153-
print "\t\t<a href=\"".$this->location->getDir(false, true, false, 0).$file->getNameEncoded()."\"";
3205+
print "\t\t<a href=\"".$this->location->getDir(false, true, false, 0, true).$file->getNameEncoded(true)."\"";
31543206
if(EncodeExplorer::getConfig('open_in_new_window') == true)
31553207
print "target=\"_blank\"";
31563208
print " class=\"item file";
31573209
if($file->isValidForThumb())
31583210
print " thumb";
31593211
print "\">";
3160-
print $file->getNameHtml();
3212+
print $file->getNameHtml(true);
31613213
if($this->mobile == true)
31623214
{
31633215
print "<span class =\"size\">".$this->formatSize($file->getSize())."</span>";

0 commit comments

Comments
 (0)