Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for #42, #40 and #37 (Encoding issues with Swedish and Russian chars) #53

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 78 additions & 21 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
***************************************************************************/

Expand Down 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 @@ -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
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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.
//
Expand Down Expand Up @@ -3071,7 +3128,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 +3179,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 +3207,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