Skip to content

Commit

Permalink
new fnencode option FS#1649
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
splitbrain committed Apr 4, 2010
1 parent 70e083c commit f03fd95
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 40 deletions.
1 change: 1 addition & 0 deletions conf/dokuwiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
$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
Expand Down
1 change: 1 addition & 0 deletions inc/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
50 changes: 50 additions & 0 deletions inc/pageutils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

39 changes: 0 additions & 39 deletions inc/utf8.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,6 @@

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
Expand Down
1 change: 1 addition & 0 deletions lib/plugins/config/lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
$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)';
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/config/settings/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ class 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) {
Expand Down
1 change: 1 addition & 0 deletions lib/plugins/config/settings/config.metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
$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');
Expand Down

0 comments on commit f03fd95

Please sign in to comment.