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).
This commit is contained in:
Andreas Gohr 2010-04-04 20:28:39 +02:00
parent 70e083cec4
commit f03fd95752
7 changed files with 55 additions and 40 deletions

View File

@ -89,6 +89,7 @@ $conf['usedraft'] = 1; //automatically save a draft while edit
$conf['sepchar'] = '_'; //word separator character in page names; may be a
// letter, a digit, '_', '-', or '.'.
$conf['canonical'] = 0; //Should all URLs use full canonical http://... style?
$conf['fnencode'] = 'url'; //encode filenames (url|safe|utf-8)
$conf['autoplural'] = 0; //try (non)plural form of nonexisting files?
$conf['compression'] = 'gz'; //compress old revisions: (0: off) ('gz': gnuzip) ('bz2': bzip)
// bz2 generates smaller files, but needs more cpu-power

View File

@ -73,6 +73,7 @@ function load_autoload($name){
'ZipLib' => DOKU_INC.'inc/ZipLib.class.php',
'DokuWikiFeedCreator' => DOKU_INC.'inc/feedcreator.class.php',
'Doku_Parser_Mode' => DOKU_INC.'inc/parser/parser.php',
'SafeFN' => DOKU_INC.'inc/SafeFN.class.php',
'DokuWiki_Action_Plugin' => DOKU_PLUGIN.'action.php',
'DokuWiki_Admin_Plugin' => DOKU_PLUGIN.'admin.php',

View File

@ -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);
}

View File

@ -19,45 +19,6 @@ if(!defined('UTF8_MBSTRING')){
if(UTF8_MBSTRING){ mb_internal_encoding('UTF-8'); }
if(!function_exists('utf8_encodeFN')){
/**
* URL-Encode a filename to allow unicodecharacters
*
* Slashes are not encoded
*
* 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){
if($safe && preg_match('#^[a-zA-Z0-9/_\-.%]+$#',$file)){
return $file;
}
$file = urlencode($file);
$file = str_replace('%2F','/',$file);
return $file;
}
}
if(!function_exists('utf8_decodeFN')){
/**
* URL-Decode a filename
*
* This is just a wrapper around urldecode
*
* @author Andreas Gohr <andi@splitbrain.org>
* @see urldecode
*/
function utf8_decodeFN($file){
$file = urldecode($file);
return $file;
}
}
if(!function_exists('utf8_isASCII')){
/**
* Checks if a string contains 7bit ASCII only

View File

@ -116,6 +116,7 @@ $lang['useslash'] = 'Use slash as namespace separator in URLs';
$lang['usedraft'] = 'Automatically save a draft while editing';
$lang['sepchar'] = 'Page name word separator';
$lang['canonical'] = 'Use fully canonical URLs';
$lang['fnencode'] = 'Method for encoding non-ASCII filenames.';
$lang['autoplural'] = 'Check for plural forms in links';
$lang['compression'] = 'Compression method for attic files';
$lang['cachetime'] = 'Maximum age for cache (sec)';

View File

@ -343,7 +343,7 @@ if (!class_exists('setting')) {
var $_cautionList = array(
'basedir' => 'danger', 'baseurl' => 'danger', 'savedir' => 'danger', 'useacl' => 'danger', 'authtype' => 'danger', 'superuser' => 'danger', 'userewrite' => 'danger',
'start' => 'warning', 'camelcase' => 'warning', 'deaccent' => 'warning', 'sepchar' => 'warning', 'compression' => 'warning', 'xsendfile' => 'warning', 'renderer_xhtml' => 'warning',
'allowdebug' => 'security', 'htmlok' => 'security', 'phpok' => 'security', 'iexssprotect' => 'security', 'xmlrpc' => 'security'
'allowdebug' => 'security', 'htmlok' => 'security', 'phpok' => 'security', 'iexssprotect' => 'security', 'xmlrpc' => 'security', 'fnencode' => 'warning'
);
function setting($key, $params=NULL) {

View File

@ -171,6 +171,7 @@ $meta['userewrite'] = array('multichoice','_choices' => array(0,1,2));
$meta['useslash'] = array('onoff');
$meta['sepchar'] = array('sepchar');
$meta['canonical'] = array('onoff');
$meta['fnencode'] = array('multichoice','_choices' => array('url','safe','utf-8'));
$meta['autoplural'] = array('onoff');
$meta['mailfrom'] = array('richemail');
$meta['compress'] = array('onoff');