From 70e083cec45ef4a738ae0b3f20af8e4f288a7dfa Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 4 Apr 2010 20:24:01 +0200 Subject: class for safely encoding filenames This class tries to satisfy the following requirements: * all ASCII alphanumeric chars in the input should stay the same ASCII alphanumeric chars in the output * the resulting string should be as short as possible * the operation needs to be reversable without any data loss * the resulting ASCII string should be case insensitive * there should be no restriction on the input length * the whole UTF-8 range should be allowed using it creates a way to store UTF-8 in filenames even if the underlying filesystem does not support UTF-8. It is also pretty robust when files are moved between various filesystems and it creates shorter filenames than the currently used urlencoding. --- inc/SafeFN.class.php | 210 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 inc/SafeFN.class.php (limited to 'inc') diff --git a/inc/SafeFN.class.php b/inc/SafeFN.class.php new file mode 100644 index 000000000..4ce5c94a2 --- /dev/null +++ b/inc/SafeFN.class.php @@ -0,0 +1,210 @@ + ord($adjustment[count($adjustment)-1])) { + $adjusted = $codepoint - count($adjustment); + } else if ($codepoint > ord($adjustment[0])) { + for ($i=1; $i < count($adjustment); $i++) { + if ($codepoint < ord($adjustment[$i])) { + break; + } + } + $adjusted = $codepoint - $i; + } else { + $adjusted = $codepoint; + } + + // substract number of non-printable characters and return + return $adjusted - ord(' '); + } + + private function reverseForPlain($adjusted) { + $adjustment = self::getAdjustments(); + + // reverse adjustment for non-printable characters + $adjusted += ord(' '); + + if ($adjusted + count($adjustment) > ord($adjustment[count($adjustment)-1])) { + $adjusted += count($adjustment); + } else if ($adjusted > ord($adjustment[0])) { + for ($i=1; $i < count($adjustment); $i++) { + if ($adjusted + $i < ord($adjustment[$i])) { + break; + } + } + $adjusted += $i; + } + + return $adjusted; + } + + private function getAdjustments() { + if (empty(self::$adjustments)) { + self::$adjustments = str_split(self::$plain.self::$pre_indicator.self::$post_indicator); + sort(self::$adjustments); + } + + return self::$adjustments; + } +} -- cgit v1.2.3