namespace Kesion.NET.WebSite
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.IO;

    using Kesion.Publics;
    using Kesion.BLL;
    using Kesion.Cache;
    public partial class Admin_Include_FileList : System.Web.UI.Page
    {
        public string ParentPath=string.Empty;
        public string _CurrPath=string.Empty, _OriginFolder=string.Empty, _PreFolder=string.Empty;
        public int ChannelID = 0;
        public Boolean CanBack = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            KSSecurity.CheckLoginAndClose();

            Context.Response.Cache.SetCacheability(HttpCacheability.NoCache);  //禁止缓存

            _CurrPath = KSCMS.S("CurrPath");
            ChannelID = Utils.StrToInt(KSCMS.S("channelid"),0);

            if (KSCMS.GetCookie("Admin", "UserType") == "1")
            {
                _OriginFolder = string.Concat(KSCMS.GetInstallDir(), MyCache.GetCacheConfig(22), "/");
            }
            else
            {
                _OriginFolder = Kesion.APPCode.Public.GetUpfilesDir();
            }


            if (string.IsNullOrEmpty(_CurrPath)) _CurrPath = _OriginFolder;


            #region 判断上传路径合法性
                //正则表达式   
                string strExp = @"^" + string.Concat(KSCMS.GetInstallDir(), MyCache.GetCacheConfig(22), @"\/");
                //创建正则表达式对象   
                Regex myRegex = new Regex(strExp);
                if (!myRegex.IsMatch(_CurrPath))
                {
                    JS.Alert("非法上传目录!");
                    KSCMS.Die();
                }
            #endregion



           if (!IsPostBack)
            {
                switch (KSCMS.S("action"))
                {
                    case "AddFolder":
                        string folderName = KSCMS.S("path");
                        if (string.IsNullOrEmpty(folderName))
                        {
                            return;
                        }

                        if (folderName.IndexOf(".") != -1||folderName.IndexOf(";")!=-1)
                        {
                            JS.Message("对不起,目录名不合法!");

                        }
                        else
                        {
                            Utils.CreateDir(KSCMS.S("path"));
                        }
                        break;
                   case "FolderReName":
                        Rename();
                        break;
                    case "DelFolder":
                        Delete();
                        break;
                }
                ParentPath += _CurrPath;
                InitialPrePath();
                InitialList(ParentPath);
            }

        }
        void InitialPrePath()
        {
            _PreFolder = string.Empty;
           
                string[] PArr = ParentPath.Split('/');
             
                for (int i = 0; i < PArr.Length - 2; i++)
                {
                    if (i.Equals(PArr.Length - 3))
                        _PreFolder += PArr[i];
                    else
                        _PreFolder += PArr[i] + "/";
                    CanBack = true;
                }
                if (_PreFolder.Equals(""))
                {
                    _PreFolder = _CurrPath;
                    CanBack = false;
                }
                if (_CurrPath.Equals(_OriginFolder))
                {
                    _PreFolder = _CurrPath;
                    CanBack = false;
                }

        }

        void InitialList(string _path)
        {
            Utils.CreateDir(_path);
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(string)));
            dt.Columns.Add(new DataColumn("Name", typeof(string)));
            dt.Columns.Add(new DataColumn("Size", typeof(string)));
            dt.Columns.Add(new DataColumn("Time", typeof(string)));
            dt.Columns.Add(new DataColumn("TypeName", typeof(string)));
            dt.Columns.Add(new DataColumn("OP1", typeof(string)));
            dt.Columns.Add(new DataColumn("OP2", typeof(string)));

            string[] files = Directory.GetFiles(Server.MapPath(_path));
            int id = 0;//自动编号
            long size = 0;//文件大小
            string TypeName;//类型

            if (CanBack.Equals(true))
            {
                dt.Rows.Add(new string[] { "", "<img src='../../SysImg/FileIcon/arrow.gif' align='absmiddle'><span class='FolderItem'  style='cursor:pointer' onClick='OpenParentFolder();' title='返回上级目录'>返回上级目录</span>", "", "", "", "" });
            }

            DirectoryInfo d = new DirectoryInfo(Server.MapPath(_path));
            foreach (DirectoryInfo _d in d.GetDirectories())
            {
                id = id + 1;
                TypeName = "目录";
                size = DirSize(_d) / 1024;
                dt.Rows.Add(new string[] { id.ToString(), @"<span class=""folder""></span><a class='FolderItem' style='cursor:pointer' href=""javascript:;"" onclick=""OpenFolder('"+ _d.Name.ToString()+@"');"" title='点击进入此目录'>" + _d.Name.ToString() + "</span>", size.ToString() + "K", _d.CreationTime.ToString(), TypeName, @"<a class=""edit"" href=javascript:RenameFolder('" + _d.Name.ToString() + @"');></a>",@"<a class=""delete"" href=javascript:DelFolderFile(1,'" + _d.Name.ToString() + "');></a>" });
            }

            foreach (string file in files)
            {
                System.IO.FileInfo fileInfo = new FileInfo(file);
                    id = id + 1;
                    size = fileInfo.Length / 1024;
                    TypeName = "文件";
                    dt.Rows.Add(new string[] { id.ToString(), "<img src='../../sysimg/FileIcon/" + KSCMS.GetFileIcon(fileInfo.Name.ToString()) + "'><span class='FolderItem' id='f" + id + "' File='" + fileInfo.Name.ToString() + "' onDblClick='SetFile(this);' onClick='SelectFile(" + id + ");' title='" + fileInfo.Name.ToString() + "'> " + Utils.CutStr(fileInfo.Name.ToString(), 50) + "</span>", size.ToString() + "K", fileInfo.CreationTime.ToString(), TypeName, @"<a href=""javascript:RenameFolder('" + fileInfo.Name.ToString() + @"');"" class=""edit""></a>",@"<a href=""javascript:DelFolderFile(2,'" + fileInfo.Name.ToString() + @"');"" class=""delete""></a>" });
            }
            dlPubInfo.DataSource = dt;
            dlPubInfo.DataBind();

        }



        public static long DirSize(DirectoryInfo d)
        {
            long Size = 0;
            //   Add   file   sizes.   
            FileInfo[] fis = d.GetFiles();
            foreach (FileInfo fi in fis)
            {
                Size += fi.Length;
            }
            //   Add   subdirectory   sizes.   
            DirectoryInfo[] dis = d.GetDirectories();
            foreach (DirectoryInfo di in dis)
            {
                Size += DirSize(di);
            }
            return (Size);
        }

        void Rename()
        {
            string OldFileName = KSCMS.S("OldPathName");
            string NewFileName = KSCMS.S("NewPathName");

            if (NewFileName.IndexOf(";") != -1)
            {
                JS.Message("对不起,文件名不合法!");
                return;
            }
            else if (NewFileName.ToLower().IndexOf(".asp") != -1 || NewFileName.ToLower().IndexOf(".php") != -1 || NewFileName.ToLower().IndexOf(".asa") != -1 || NewFileName.ToLower().IndexOf(".cer") != -1)
            {

                JS.Message("对不起,您的文件扩展名不允许!");

                return;
            }


            if (OldFileName == NewFileName) return;
            try
            {
            Directory.Move(Server.MapPath(_CurrPath + "/" + OldFileName), Server.MapPath(_CurrPath + "/" + NewFileName));
            }
            catch { }

        }
        void Delete()
        {
            try
            {
                if (KSCMS.S("flag").Equals("1"))
                {
                    Directory.Delete(Server.MapPath(_CurrPath + "/" + KSCMS.S("path")), true);
                }
                else
                {
                    File.Delete(Server.MapPath(_CurrPath + "/" + KSCMS.S("path")));
                }
            }
            catch(Exception ex)
            {
                JS.Message("对不起,删除操作失败!失败原因:"+ex.Message);
            }

        }
    }
}