Helpers.cs
资源名称:FtpServer.rar [点击查看]
上传用户:xuelanruo
上传日期:2015-04-02
资源大小:163k
文件大小:10k
源码类别:
Ftp服务器
开发平台:
C#
- /*
- Ftp Dot Net Some static functions used by the FtpServer and FtpSession Classes
- Copyright (C)
- This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
- You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- Last modification : 04/20/2004 by Simon FERQUEL
- */
- using System;
- using System.Collections;
- using Microsoft.Win32;
- using System.Xml;
- using System.Xml.XPath;
- namespace FtpServerLibrary
- {
- public class Helpers
- {
- public static Hashtable VPaths(string username)
- {
- System.Collections.Hashtable vpaths=new Hashtable();
- try
- {
- XPathDocument doc = new XPathDocument(System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "VPaths.xml"));
- XPathNavigator nav=doc.CreateNavigator();
- nav.MoveToFirstChild();
- nav.MoveToFirstChild();
- if(nav.Name.ToLower()=="default")
- {
- if(nav.MoveToFirstChild())
- {
- try
- {
- vpaths.Add(nav.GetAttribute("source","").Trim(),nav.GetAttribute("target","").Trim());
- }
- catch
- {
- vpaths[nav.GetAttribute("source","").Trim()]=nav.GetAttribute("target","").Trim();
- }
- while(nav.MoveToNext())
- {
- try
- {
- vpaths.Add(nav.GetAttribute("source","").Trim(),nav.GetAttribute("target","").Trim());
- }
- catch
- {
- vpaths[nav.GetAttribute("source","").Trim()]=nav.GetAttribute("target","").Trim();
- }
- }
- nav.MoveToParent();
- }
- }
- else if (nav.GetAttribute("user","").ToLower().Trim()==username.ToLower().Trim())
- {
- if(nav.MoveToFirstChild())
- {
- try
- {
- vpaths.Add(nav.GetAttribute("source","").Trim(),nav.GetAttribute("target","").Trim());
- }
- catch
- {
- vpaths[nav.GetAttribute("source","").Trim()]=nav.GetAttribute("target","").Trim();
- }
- while(nav.MoveToNext())
- {
- try
- {
- vpaths.Add(nav.GetAttribute("source","").Trim(),nav.GetAttribute("target","").Trim());
- }
- catch
- {
- vpaths[nav.GetAttribute("source","").Trim()]=nav.GetAttribute("target","").Trim();
- }
- }
- nav.MoveToParent();
- }
- }
- while(nav.MoveToNext())
- {
- if(nav.Name.ToLower()=="default")
- {
- if(nav.MoveToFirstChild())
- {
- try
- {
- vpaths.Add(nav.GetAttribute("source","").Trim(),nav.GetAttribute("target","").Trim());
- }
- catch
- {
- vpaths[nav.GetAttribute("source","").Trim()]=nav.GetAttribute("target","").Trim();
- }
- while(nav.MoveToNext())
- {
- try
- {
- vpaths.Add(nav.GetAttribute("source","").Trim(),nav.GetAttribute("target","").Trim());
- }
- catch
- {
- vpaths[nav.GetAttribute("source","").Trim()]=nav.GetAttribute("target","").Trim();
- }
- }
- nav.MoveToParent();
- }
- }
- else if (nav.GetAttribute("user","").ToLower().Trim()==username.ToLower().Trim())
- {
- if(nav.MoveToFirstChild())
- {
- try
- {
- vpaths.Add(nav.GetAttribute("source","").Trim(),nav.GetAttribute("target","").Trim());
- }
- catch
- {
- vpaths[nav.GetAttribute("source","").Trim()]=nav.GetAttribute("target","").Trim();
- }
- while(nav.MoveToNext())
- {
- try
- {
- vpaths.Add(nav.GetAttribute("source","").Trim(),nav.GetAttribute("target","").Trim());
- }
- catch
- {
- vpaths[nav.GetAttribute("source","").Trim()]=nav.GetAttribute("target","").Trim();
- }
- }
- nav.MoveToParent();
- }
- }
- }
- if (nav.GetAttribute("user","").ToLower().Trim()==username.ToLower().Trim())
- {
- if(nav.MoveToFirstChild())
- {
- vpaths[nav.GetAttribute("source","").Trim()]=nav.GetAttribute("target","").Trim();
- while(nav.MoveToNext())
- {
- vpaths[nav.GetAttribute("source","").Trim()]=nav.GetAttribute("target","").Trim();
- }
- nav.MoveToParent();
- }
- }
- while (nav.MoveToPrevious())
- {
- if (nav.GetAttribute("user","").ToLower().Trim()==username.ToLower().Trim())
- {
- if(nav.MoveToFirstChild())
- {
- vpaths[nav.GetAttribute("source","").Trim()]=nav.GetAttribute("target","").Trim();
- while(nav.MoveToNext())
- {
- vpaths[nav.GetAttribute("source","").Trim()]=nav.GetAttribute("target","").Trim();
- }
- nav.MoveToParent();
- }
- }
- }
- }
- catch
- {
- }
- return vpaths;
- }
- /// <summary>
- /// Retourne le r閜ertoire "home" correspondant ?l'utilisateur connect?
- /// </summary>
- /// <param name="username"></param>
- /// <returns></returns>
- public static string GetFtpRoot(string username)
- {
- try
- {
- string defaultHome="";
- System.Collections.Hashtable userHomes=new Hashtable();
- XPathDocument doc = new XPathDocument(System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Homes.xml"));
- XPathNavigator nav=doc.CreateNavigator();
- nav.MoveToFirstChild();
- nav.MoveToFirstChild();
- if(nav.Name.ToLower()=="default")
- {
- defaultHome=nav.GetAttribute("home","");
- }
- else if (nav.Name.ToLower()=="home")
- {
- userHomes.Add(nav.GetAttribute("user","").ToLower(),nav.GetAttribute("path",""));
- }
- while(nav.MoveToNext())
- {
- if(nav.Name.ToLower()=="default")
- {
- defaultHome=nav.GetAttribute("home","");
- }
- else if (nav.Name.ToLower()=="home")
- {
- userHomes.Add(nav.GetAttribute("user","").ToLower(),nav.GetAttribute("path",""));
- }
- }
- if (userHomes.ContainsKey(username.ToLower()))
- {
- return (string)userHomes[username.ToLower()];
- }
- else return defaultHome;
- }
- catch
- {
- return null;
- }
- }
- public static ArrayList VPathsInDir(string curDir,Hashtable vpaths)
- {
- ArrayList vdirs=new ArrayList();
- if (!curDir.EndsWith("/"))curDir+="/";
- foreach (string virdir in vpaths.Keys)
- {
- string vdir=virdir;
- if(!vdir.EndsWith("/"))vdir+="/";
- if(!vdir.StartsWith("/"))vdir="/"+vdir;
- if((vdir.Split('/').Length==(curDir.Split('/').Length+1)&&vdir.ToLower().StartsWith(curDir.ToLower())))
- {
- string[] parts=vdir.Split('/');
- //string ndir=vdir.Split('/')[vdir.Split('/').Length-2];
- //vdirs.Add(ndir);
- vdirs.Add(parts[parts.Length-2]);
- }
- }
- return vdirs;
- }
- /// <summary>
- /// Cette m閠hode d閠ermine si l'utilisateur peut acc閐er au chemin "dirToList" (si le chemin se
- /// trouve dans l'arborescence du ftp). Elle donne aussi le chemin d'acc鑣 physique au chemin ftp donn?
- /// Note : Elle ne v閞ifie pas la pr閟ence du fichier (pour pouvoir 阾re utilis閑 par les fonctions d'uploads etc...)
- /// ni les droits d'acc鑣 sur le fichier.
- /// </summary>
- /// <param name="ftpRoot">Racine du Ftp</param>
- /// <param name="curDir">R閜ertoire courrant par rapport ?la racine</param>
- /// <param name="path">Chemin ?v閞ifier</param>
- /// <param name="physicalPath">Param鑤re en sortie : Chemin physique correspondant au "path"</param>
- /// <returns></returns>
- public static bool IsValidDir(string ftpRoot,string curDir,string path,Hashtable vpaths,out string physicalPath, out string newCurdir)
- {
- if(path.Length>0)
- {
- if(path.StartsWith("/"))
- {
- string[]parts=path.Split('/');
- int depth=0;
- foreach(string p in parts)
- {
- if(p!="")
- {
- if (p=="..") depth--;
- else depth++;
- if (depth<0)
- {
- newCurdir=ResolveComplexUrl(curDir);
- physicalPath=String.Empty;
- return false;
- }
- }
- }
- }
- else
- {
- if (!curDir.EndsWith("/"))curDir+="/";
- path=curDir+path;
- string[]parts=path.Split('/');
- int depth=0;
- foreach(string p in parts)
- {
- if(p!="")
- {
- if (p=="..") depth--;
- else depth++;
- if (depth<0)
- {
- newCurdir=ResolveComplexUrl(curDir);
- physicalPath=String.Empty;
- return false;
- }
- }
- }
- }
- if (ftpRoot.EndsWith("\"))
- {
- ftpRoot.Remove(ftpRoot.Length-1,1);
- }
- newCurdir=ResolveComplexUrl(path);
- physicalPath=GetPhysicalDir(path,ftpRoot,vpaths);
- return true;
- }
- else
- {
- if (ftpRoot.EndsWith("\"))
- {
- ftpRoot.Remove(ftpRoot.Length-1,1);
- }
- newCurdir=path=ResolveComplexUrl(curDir);
- physicalPath=GetPhysicalDir(path,ftpRoot,vpaths);
- return true;
- }
- }
- private static string ResolveComplexUrl(string url)
- {
- string[] parts=url.Split('/');
- string rslv="/";
- for (int i=0;i<parts.Length-1;i++)
- {
- if (parts[i]!=".."&&parts[i+1]!=".."&&parts[i]!="")
- {
- rslv+=parts[i]+"/";
- }
- }
- if(parts[parts.Length-1]!=".."&&parts[parts.Length-1]!="")
- rslv+=parts[parts.Length-1];
- return rslv;
- }
- private static string GetPhysicalDir(string path,string ftproot,Hashtable vpaths)
- {
- if(!path.EndsWith("/"))path+="/";
- foreach( string vpath in vpaths.Keys)
- {
- string v=vpath;
- if (!v.EndsWith("/"))v+="/";
- if(path.ToLower().StartsWith(v.ToLower()))
- {
- string rval=(string)vpaths[vpath]+path.Substring(vpath.Length).Replace("/","\");
- if (rval.EndsWith("\"))rval=rval.Remove(rval.Length-1,1);
- return rval;
- }
- }
- return ftproot+path.Replace("/","\");
- }
- }
- }