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

Commit

Permalink
Another shot at making convert encoding feature work
Browse files Browse the repository at this point in the history
  • Loading branch information
NewEraCracker committed Apr 5, 2016
1 parent f3c33dc commit e81327b
Showing 1 changed file with 32 additions and 46 deletions.
78 changes: 32 additions & 46 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2274,9 +2274,10 @@ function getName()
return $this->name;
}

function getNameHtml()
function getNameHtml($translate_encoding = false)
{
return EncodeExplorer::htmlEncode($this->name);
$name = ($translate_encoding ? EncodeExplorer::translate_encoding($this->name) : $this->name);
return htmlspecialchars($name);
}

function getNameEncoded()
Expand Down Expand Up @@ -2335,9 +2336,10 @@ function getNameEncoded()
return rawurlencode($this->name);
}

function getNameHtml()
function getNameHtml($translate_encoding = false)
{
return EncodeExplorer::htmlEncode($this->name);
$name = ($translate_encoding ? EncodeExplorer::translate_encoding($this->name) : $this->name);
return htmlspecialchars($name);
}

function getSize()
Expand Down Expand Up @@ -2464,14 +2466,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 @@ -2481,12 +2484,11 @@ function getDir($prefix, $encoded, $html, $up)
return $dir;
}

function getPathLink($i, $html)
function getPathLink($i, $html, $translate_encoding = false)
{
if($html)
return EncodeExplorer::htmlEncode($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 @@ -2819,41 +2821,25 @@ function debug()
//
// Encode HTML output in correct encoding
//
public static function htmlEncode($string)
{
// Only perform charset convertion if output configured to be in UTF-8 and input isn't already UTF-8
if( EncodeExplorer::getConfig('charset') == 'UTF-8' &&
EncodeExplorer::getConfig('os_charset') != 'UTF-8' &&
// From http://w3.org/International/questions/qa-forms-utf-8.html
!preg_match('%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs', $string)) {

// Attempt using mb_convert_encoding
if(function_exists('mb_convert_encoding')) {
$tmp = @mb_convert_encoding($string, 'UTF-8', EncodeExplorer::getConfig('os_charset'));
}
public static function translate_encoding($string)
{
if(!is_string($string) || EncodeExplorer::getConfig('charset') == EncodeExplorer::getConfig('os_charset'))
return $string;

// Attempt using iconv
if(empty($tmp) && function_exists('iconv')) {
$tmp = @iconv(EncodeExplorer::getConfig('os_charset'), 'UTF-8', $string);
}
// Attempt using mb_convert_encoding
if(function_exists('mb_convert_encoding'))
$tmp = @mb_convert_encoding($string, EncodeExplorer::getConfig('charset'), EncodeExplorer::getConfig('os_charset'));

// If any of them succeeds, overwrite string with converted one
if(!empty($tmp) && is_string($tmp)) {
$string = $tmp;
}
}
// Attempt using iconv
if(empty($tmp) && function_exists('iconv'))
$tmp = @iconv(EncodeExplorer::getConfig('os_charset'), EncodeExplorer::getConfig('charset'), $string);

// If any of them succeeds, return converted one
if(!empty($tmp) && is_string($tmp))
return $tmp;

// Convert special characters to HTML entities
return htmlspecialchars($string);
// Otherwise return as is
return $string;
}

//
Expand Down Expand Up @@ -3105,7 +3091,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 @@ -3156,7 +3142,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 @@ -3191,7 +3177,7 @@ function(){
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 e81327b

Please sign in to comment.