--- lib/VFS/ftp.php.0	2005-08-30 12:07:59.000000000 +0200
+++ lib/VFS/ftp.php	2005-09-02 09:05:50.000000000 +0200
@@ -13,7 +13,10 @@
  *                       than 21.
  *      'ssl'            If true, and PHP had been compiled with OpenSSL
  *                       support, TLS transport-level encryption will be
- *                       negotiated with the server.</pre>
+ *                       negotiated with the server.
+ *      'charset'        The character set used on FTP server to store 
+ *                       filenames. If unspecified, no conversion is
+ *                       done.</pre>
  *
  * $Horde: framework/VFS/VFS/ftp.php,v 1.75.4.8 2005/07/03 04:52:42 selsky Exp $
  *
@@ -54,6 +57,32 @@
      */
     var $_stream = false;
 
+    
+    /** 
+     * Converts strings to/from FTP server character set.
+     * To be used on filenames and pathnames.
+     *
+     * @param string $data  The string data to convert.
+     * @param string $to  The conversion wanted :
+     *          'horde' -> convert from FTP server charset to Horde's current charset
+     *          'ftp'   -> convert from Horde's current charset to FTP server charset
+     *
+     * @return string  the converted string data.
+     */ 
+    function charconv($data,$to="horde")
+    {
+        
+        $ftpcharset = $this->_params['charset'];
+        $hordecharset = NLS::getCharset();
+        if ( empty($ftpcharset) || $ftpcharset == $hordecharset ) { return $data; }
+        if ( $to == "ftp" ) { 
+            return String::convertCharset($data, $hordecharset, $ftpcharset);
+        } else {
+            return String::convertCharset($data, $ftpcharset, $hordecharset);
+        }
+    }
+
+
     /**
      * Retrieves a file from the VFS.
      *
@@ -71,7 +100,7 @@
 
         $tmpFile = $this->_getTempFile();
         $fetch = @ftp_get($this->_stream, $tmpFile,
-                          $this->_getPath($path, $name), FTP_BINARY);
+                          $this->charconv($this->_getPath($path, $name),'ftp'), FTP_BINARY);
         if ($fetch === false) {
             return PEAR::raiseError(sprintf(_("Unable to open VFS file \"%s\"."), $this->_getPath($path, $name)));
         }
@@ -112,13 +141,13 @@
             return $conn;
         }
 
-        if (!@ftp_put($this->_stream, $this->_getPath($path, $name), $tmpFile, FTP_BINARY)) {
+        if (!@ftp_put($this->_stream, $this->charconv($this->_getPath($path, $name),'ftp'), $tmpFile, FTP_BINARY)) {
             if ($autocreate) {
                 $result = $this->autocreatePath($path);
                 if (is_a($result, 'PEAR_Error')) {
                     return $result;
                 }
-                if (!@ftp_put($this->_stream, $this->_getPath($path, $name), $tmpFile, FTP_BINARY)) {
+                if (!@ftp_put($this->_stream, $this->charconv($this->_getPath($path, $name),'ftp'), $tmpFile, FTP_BINARY)) {
                     return PEAR::raiseError(sprintf(_("Unable to write VFS file \"%s\"."), $this->_getPath($path, $name)));
                 }
             } else {
@@ -166,7 +195,7 @@
             return $conn;
         }
 
-        if (!@ftp_delete($this->_stream, $this->_getPath($path, $name))) {
+        if (!@ftp_delete($this->_stream, $this->charconv($this->_getPath($path, $name),'ftp'))) {
             return PEAR::raiseError(sprintf(_("Unable to delete VFS file \"%s\"."), $this->_getPath($path, $name)));
         }
 
@@ -192,7 +221,7 @@
         $olddir = $this->getCurrentDirectory();
 
         /* See if we can change to the given path. */
-        if (@ftp_chdir($this->_stream, $this->_getPath($path, $name))) {
+        if (@ftp_chdir($this->_stream, $this->charconv($this->_getPath($path, $name),'ftp'))) {
             $result = true;
         }
 
@@ -248,11 +277,11 @@
                 }
             }
 
-            if (!@ftp_rmdir($this->_stream, $this->_getPath($path, $name))) {
+            if (!@ftp_rmdir($this->_stream, $this->charconv($this->_getPath($path, $name),'ftp'))) {
                 return PEAR::raiseError(sprintf(_("Cannot remove directory \"%s\"."), $this->_getPath($path, $name)));
             }
         } else {
-            if (!@ftp_delete($this->_stream, $this->_getPath($path, $name))) {
+            if (!@ftp_delete($this->_stream, $this->charconv($this->_getPath($path, $name),'ftp'))) {
                 return PEAR::raiseError(sprintf(_("Cannot delete file \"%s\"."), $this->_getPath($path, $name)));
             }
         }
@@ -277,7 +306,7 @@
             return $conn;
         }
 
-        if (!@ftp_rename($this->_stream, $this->_getPath($oldpath, $oldname), $this->_getPath($newpath, $newname))) {
+        if (!@ftp_rename($this->_stream, $this->charconv($this->_getPath($oldpath, $oldname),'ftp'), $this->charconv($this->_getPath($newpath, $newname),'ftp'))) {
             return PEAR::raiseError(sprintf(_("Unable to rename VFS file \"%s\"."), $this->_getPath($oldpath, $oldname)));
         }
 
@@ -299,7 +328,7 @@
             return $conn;
         }
 
-        if (!@ftp_mkdir($this->_stream, $this->_getPath($path, $name))) {
+        if (!@ftp_mkdir($this->_stream, $this->charconv($this->_getPath($path, $name),'ftp'))) {
             return PEAR::raiseError(sprintf(_("Unable to create VFS directory \"%s\"."), $this->_getPath($path, $name)));
         }
 
@@ -322,7 +351,7 @@
             return $conn;
         }
 
-        if (!@ftp_site($this->_stream, 'CHMOD ' . $permission . ' ' . $this->_getPath($path, $name))) {
+        if (!@ftp_site($this->_stream, 'CHMOD ' . $permission . ' ' . $this->charconv($this->_getPath($path, $name),'ftp'))) {
             return PEAR::raiseError(sprintf(_("Unable to change permission for VFS file \"%s\"."), $this->_getPath($path, $name)));
         }
 
@@ -369,13 +398,13 @@
             // doing an ls -a and then not doing the check later (by
             // setting $dotfiles to true, the if is short-circuited).
             if ($dotfiles) {
-                $list = ftp_rawlist($this->_stream, '-al');
+                $list = $this->charconv(ftp_rawlist($this->_stream, '-al'),'horde');
                 $dotfiles = true;
             } else {
-                $list = ftp_rawlist($this->_stream, '-l');
+                $list = $this->charconv(ftp_rawlist($this->_stream, '-l'),'horde');
             }
         } else {
-           $list = ftp_rawlist($this->_stream, '');
+           $list = $this->charconv(ftp_rawlist($this->_stream, ''),'horde');
         }
 
         if (!is_array($list)) {
@@ -597,13 +626,13 @@
             }
         } else {
             $tmpFile = $this->_getTempFile();
-            $fetch = @ftp_get($this->_stream, $tmpFile, $this->_getPath($path, $name), FTP_BINARY);
+            $fetch = @ftp_get($this->_stream, $tmpFile, $this->charconv($this->_getPath($path, $name),'ftp'), FTP_BINARY);
             if (!$fetch) {
                 unlink($tmpFile);
                 return PEAR::raiseError(sprintf(_("Failed to copy from \"%s\"."), $this->_getPath($path, $name)));
             }
 
-            if (!@ftp_put($this->_stream, $this->_getPath($dest, $name), $tmpFile, FTP_BINARY)) {
+            if (!@ftp_put($this->_stream, $this->charconv($this->_getPath($dest, $name),'ftp'), $tmpFile, FTP_BINARY)) {
                 unlink($tmpFile);
                 return PEAR::raiseError(sprintf(_("Failed to copy to \"%s\"."), $this->_getPath($dest, $name)));
             }
@@ -637,7 +666,7 @@
             }
         }
 
-        if (!@ftp_rename($this->_stream, $this->_getPath($path, $name), $this->_getPath($dest, $name))) {
+        if (!@ftp_rename($this->_stream, $this->charconv($this->_getPath($path, $name),'ftp'), $this->charconv($this->_getPath($dest, $name),'ftp'))) {
             return PEAR::raiseError(sprintf(_("Failed to move to \"%s\"."), $this->_getPath($dest, $name)));
         }
 
@@ -654,7 +683,7 @@
         if (is_a($connected = $this->_connect(), 'PEAR_Error')) {
             return $connected;
         }
-        return ftp_pwd($this->_stream);
+        return $this->charconv(ftp_pwd($this->_stream),'horde');
     }
 
     /**
@@ -668,7 +697,7 @@
      */
     function _setPath($path)
     {
-        if (!@ftp_chdir($this->_stream, $path)) {
+        if (!@ftp_chdir($this->_stream, $this->charconv($path,'ftp'))) {
             return PEAR::raiseError(sprintf(_("Unable to change to %s."), $path));
         }
         return true;
