summaryrefslogtreecommitdiff
path: root/inc/pageutils.php
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2010-04-04 20:28:39 +0200
committerAndreas Gohr <andi@splitbrain.org>2010-04-04 20:28:39 +0200
commitf03fd957525a714da1cde7e2957939046bd51bd5 (patch)
treead10bc8fee061ff42efa36c18b5902952e079493 /inc/pageutils.php
parent70e083cec45ef4a738ae0b3f20af8e4f288a7dfa (diff)
downloadrpg-f03fd957525a714da1cde7e2957939046bd51bd5.tar.gz
rpg-f03fd957525a714da1cde7e2957939046bd51bd5.tar.bz2
new fnencode option FS#1649
This patch adds an option to choose how filenames are encoded when saved to the file system. You can choose between urlencoding (url), the new SafeFn method (safe) and storing real UTF-8 (utf-8).
Diffstat (limited to 'inc/pageutils.php')
-rw-r--r--inc/pageutils.php50
1 files changed, 50 insertions, 0 deletions
diff --git a/inc/pageutils.php b/inc/pageutils.php
index cd3cf1fce..43c84038f 100644
--- a/inc/pageutils.php
+++ b/inc/pageutils.php
@@ -543,3 +543,53 @@ function prettyprint_id($id) {
}
return hsc($id);
}
+
+/**
+ * Encode a UTF-8 filename to use on any filesystem
+ *
+ * Uses the 'fnencode' option to determine encoding
+ *
+ * When the second parameter is true the string will
+ * be encoded only if non ASCII characters are detected -
+ * This makes it safe to run it multiple times on the
+ * same string (default is true)
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ * @see urlencode
+ */
+function utf8_encodeFN($file,$safe=true){
+ global $conf;
+ if($conf['fnencode'] == 'utf-8') return $file;
+
+ if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){
+ return $file;
+ }
+
+ if($conf['fnencode'] == 'safe'){
+ return SafeFN::encode($file);
+ }
+
+ $file = urlencode($file);
+ $file = str_replace('%2F','/',$file);
+ return $file;
+}
+
+/**
+ * Decode a filename back to UTF-8
+ *
+ * Uses the 'fnencode' option to determine encoding
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ * @see urldecode
+ */
+function utf8_decodeFN($file){
+ global $conf;
+ if($conf['fnencode'] == 'utf-8') return $file;
+
+ if($conf['fnencode'] == 'safe'){
+ return SafeFN::decode($file);
+ }
+
+ return urldecode($file);
+}
+