From 49c713a33bc794629ec41fbddf2c29ebd124e926 Mon Sep 17 00:00:00 2001
From: andi <andi@splitbrain.org>
Date: Sun, 23 Jan 2005 11:29:05 +0100
Subject: [PATCH] Unicode filenames with URL encoding

darcs-hash:20050123102905-9977f-1065a1112bfd47caed0f198b94e5226c81351b64.gz
---
 inc/common.php | 26 ++++++++++++++++----------
 inc/html.php   |  3 ++-
 inc/search.php |  3 ++-
 inc/utf8.php   | 24 ++++++++++++++++++++++++
 4 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/inc/common.php b/inc/common.php
index ac5332a3e..7fa9c564b 100644
--- a/inc/common.php
+++ b/inc/common.php
@@ -206,9 +206,11 @@ function breadcrumbs(){
  * currently used to replace the colon with something else
  * on Windows systems and to have proper URL encoding
  *
+ * Urlencoding is ommitted when the second parameter is false
+ *
  * @author Andreas Gohr <andi@splitbrain.org>
  */
-function idfilter($id){
+function idfilter($id,$ue=true){
   global $conf;
   if ($conf['useslash'] && $conf['userewrite']){
     $id = strtr($id,':','/');
@@ -216,9 +218,11 @@ function idfilter($id){
       $conf['userewrite']) {
     $id = strtr($id,':',';');
   }
-  $id = urlencode($id);
-  $id = str_replace('%3A',':',$id); //keep as colon
-  $id = str_replace('%2F','/',$id); //keep as slash
+  if($ue){
+    $id = urlencode($id);
+    $id = str_replace('%3A',':',$id); //keep as colon
+    $id = str_replace('%2F','/',$id); //keep as slash
+  }
   return $id;
 }
 
@@ -440,6 +444,8 @@ function cleanID($id){
  * returns the full path to the datafile specified by ID and
  * optional revision
  *
+ * The filename is URL encoded to protect Unicode chars
+ *
  * @author Andreas Gohr <andi@splitbrain.org>
  */
 function wikiFN($id,$rev=''){
@@ -447,16 +453,16 @@ function wikiFN($id,$rev=''){
   $id = cleanID($id);
   $id = str_replace(':','/',$id);
   if(empty($rev)){
-    return $conf['datadir'].'/'.$id.'.txt';
+    $fn = $conf['datadir'].'/'.$id.'.txt';
   }else{
     $fn = $conf['olddir'].'/'.$id.'.'.$rev.'.txt';
-    if(!$conf['usegzip'] || @file_exists($fn)){
-      //return plaintext if exists or gzip is disabled
-      return $fn;
-    }else{
-      return $fn.'.gz';
+    if($conf['usegzip'] && !@file_exists($fn)){
+      //return gzip if enabled and plaintext doesn't exist
+      $fn .= '.gz';
     }
   }
+  $fn = utf8_encodeFN($fn);
+  return $fn;
 }
 
 /**
diff --git a/inc/html.php b/inc/html.php
index 97a0c091b..16269d0a8 100644
--- a/inc/html.php
+++ b/inc/html.php
@@ -242,7 +242,8 @@ function html_btn($name,$id,$akey,$params,$method='get'){
   
   $ret = '';
 
-  $id = idfilter($id);
+  //filter id (without urlencoding)
+  $id = idfilter($id,false);
 
   //make nice URLs even for buttons  
   $link = getBaseURL().'/';
diff --git a/inc/search.php b/inc/search.php
index 46b36f816..ebc7eda9d 100644
--- a/inc/search.php
+++ b/inc/search.php
@@ -324,7 +324,8 @@ function sort_search_fulltext($a,$b){
  * @author  Andreas Gohr <andi@splitbrain.org>
  */
 function pathID($path){
-  $id = str_replace('/',':',$path);
+  $id = utf8_decodeFN($path);
+  $id = str_replace('/',':',$id);
   $id = preg_replace('#\.txt$#','',$id);
   $id = preg_replace('#^:+#','',$id);
   $id = preg_replace('#:+$#','',$id);
diff --git a/inc/utf8.php b/inc/utf8.php
index 5e5dfe8bb..d06cfc58c 100644
--- a/inc/utf8.php
+++ b/inc/utf8.php
@@ -6,6 +6,30 @@
  * @author     Andreas Gohr <andi@splitbrain.org>
  */
 
+/**
+ * URL-Encode a filename to allow unicodecharacters
+ *
+ * Slashes are not encoded
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function utf8_encodeFN($file){
+  $file = rawurlencode($file);
+  $file = str_replace('%2F','/',$file);
+  return $file;
+}
+
+/**
+ * URL-Decode a filename
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function utf8_decodeFN($file){
+  $file = rawurldecode($file);
+  return $file;
+}
+
+
 /**
  * This is a unicode aware replacement for strtolower()
  *
-- 
GitLab