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

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
NewEraCracker committed Jul 31, 2016
1 parent 9d7b4d4 commit da86c69
Showing 1 changed file with 69 additions and 17 deletions.
86 changes: 69 additions & 17 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -749,7 +765,7 @@
"upload_type_not_allowed" => "이 종류의 파일은 올릴 수 없습니다.",
"del" => "삭제",
"log_out" => "로그아웃"
);
);

// Norwegian
$_TRANSLATIONS["no"] = array(
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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.
//
Expand Down Expand Up @@ -3071,7 +3123,7 @@ function(){
for($i = 0; $i < count($this->location->path); $i++)
{
print "&gt; <a href=\"".$this->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 "</a>\n";
}
?>
Expand Down Expand Up @@ -3122,7 +3174,7 @@ function(){
print "<td class=\"icon\"><img alt=\"dir\" src=\"?img=directory\" /></td>\n";
print "<td class=\"name\" colspan=\"".($this->mobile == true ? 1:2)."\">\n";
print "<a href=\"".$this->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 "</a>\n";
print "</td>\n";
if($this->mobile != true)
Expand Down Expand Up @@ -3150,14 +3202,14 @@ function(){
print "<tr class=\"row ".$row_style.(++$count == count($this->files)?" last":"")."\">\n";
print "<td class=\"icon\"><img alt=\"".$file->getType()."\" src=\"".$this->makeIcon($file->getType())."\" /></td>\n";
print "<td class=\"name\" colspan=\"1\">\n";
print "\t\t<a href=\"".$this->location->getDir(false, true, false, 0).$file->getNameEncoded()."\"";
print "\t\t<a href=\"".$this->location->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 "<span class =\"size\">".$this->formatSize($file->getSize())."</span>";
Expand Down

0 comments on commit da86c69

Please sign in to comment.