using System;
using CuteEditor.Dialogs;
using CuteEditor;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using CuteEditor.Impl;
using System.Text.RegularExpressions;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using DBOperator;

/****************************************************************************
 * Copyright (c)   六智网络技术有限公司版权所有
 * 功能: insertgallery.frame.aspx页面的功能实现类
 * 作者: 王佳楠
 * 时间: 2006/10/26
 * 备注:
 ****************************************************************************/
namespace CuteEditor.Dialogs
{
    public class InsertGalleryFrame2 : System.Web.UI.Page
    {
        private string a;
        private string b;
        private string c;
        protected Label ImageSize;
        protected DropDownList cbColumns;
        protected DropDownList cbRows;
        protected DropDownList cbThumbSize;
        protected DropDownList cbTypes;
        private int d;
        private int e;
        private int f;
        protected Label FolderDescription;
        protected Table FoldersAndFiles;
        protected FileStorage fs;
        private int g;
        private int h;
        private int i;
        private int j;
        private int k;
        private int l;
        protected Label Label_PictureUpload;
        private bool m;
        protected HtmlInputFile myuploadFile;
        private bool n;
        private bool o;
        private EditorProvider provider;
        //private EditorSetting eSetting;
        private SecuritySetting sSetting;
        protected string Sr_Cancel;
        protected Label StatusMessage;
        protected string Str_Columns;
        protected string Str_imageTitle;
        protected string Str_PopupColor;
        protected string Str_Rows;
        protected string Str_Size;
        protected string Str_Types;

        protected ThumbList2 ThumbList1;
        //protected Button uploadButton;
        protected Panel uploadpanel;
        protected Label uploadResult;

        public string strSettingHash = "";

        //this.p
        //this.provider

        //this.q
        //this.eSetting

        //this.r
        //this.sSetting


        public InsertGalleryFrame2()
        {
            this.a = string.Empty;
            this.b = string.Empty;
            this.c = string.Empty;
            this.f = 5;
        }

        private void a2()
        {
            //this.uploadButton.Click += new EventHandler(this.a2);

        }

        private void b2(object A_0, EventArgs A_1)
        {

            try
            {
                //update by heyang 2010-03-31
                if (Request.QueryString["settinghash"] != null)
                { strSettingHash = Request.QueryString["settinghash"].ToString(); }


                if (Request.QueryString["op"] != null && Request.QueryString["op"].ToString() != "")
                {
                    if (Request.QueryString["op"].ToString().ToLower() == "deletefile")
                    {
                        ProcessDeleteFile();
                        return;
                    }
                }

                //this.provider = EditorProvider.CreateInstance(this.Context, EditorSetting.GetFromContext(this.Context, true));
                //this.eSetting = this.provider.Setting;
                //this.sSetting = this.provider.Security;
                //this.fs = FileStorage.CreateInstance(this.Context);
                //this.fs.VirtualRoot = EditorUtility.ProcessWebPath(this.Context, null, this.sSetting.ImageGalleryPath).TrimEnd(new char[] { '/' }) + "/";
                this.Str_PopupColor = "";
                //this.b = this.fs.VirtualRoot;
                this.b = "";
                //string strCG = MapPath(this.b.ToString());
                //System.IO.DirectoryInfo DI = new DirectoryInfo(strCG);
                long Size = 0;
                //FileInfo[] fis = DI.GetFiles();
                //foreach (FileInfo fi in fis)
                //{
                //    Size += fi.Length;
                //}
                //string strAllLength = Size.ToString();
                //计算filesize
                //if (fis.Length > 0)
                //{
                //    double bb = Convert.ToDouble(strAllLength);
                //    bb = bb / 1000;
                //    if (bb / 1000 >= 1)
                //    {
                //        bb = bb / 1000;
                //        strAllLength = bb.ToString().Substring(0, bb.ToString().IndexOf(".") + 3) + "M";
                //    }
                //    else
                //    {
                //        //不到1M，用0.0几表示
                //        bb = bb / 1000;
                //        strAllLength = bb.ToString().Substring(0, bb.ToString().IndexOf(".") + 3) + "M";
                //    }
                //    //updated by chengeng 201003021200，更新界面信息
                //    double DRestSpace = 20 - Convert.ToDouble(strAllLength.Replace("M", ""));
                //    this.ImageSize.Text = "已有" + fis.Length.ToString() + "张图片，共占用" + strAllLength + "空间，还有" + DRestSpace.ToString() + "M空间可以使用";
                //}
                //else
                //{
                //    this.ImageSize.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;你还没有上传过图片，还有20M空间可以使用";
                //}                
                int imgFileCont = 0;
                double imgUsedSize = 0.0;
                double imgCanUseSize = 0.0;
                string strUserID = commonClass.getCookies360doc1();
                GetUserUploadedImagInfo(strUserID, out imgFileCont, out imgUsedSize, out imgCanUseSize);



                if (imgFileCont > 0)
                {
                    //this.ImageSize.Text = "已有" + imgFileCont + "张图片，共占用" + String.Format("{0:N1}", imgUsedSize) + "M空间，还有" + String.Format("{0:N1}", imgCanUseSize) + "M空间可以使用";
                    this.ImageSize.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;已有" + imgFileCont + "张图片";
                }
                else
                {
                    //this.ImageSize.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;你还没有上传过图片，还有" + String.Format("{0:N1}", imgCanUseSize) + "M空间可以使用";
                    this.ImageSize.Text = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;你还没有上传过图片";
                }

                this.a = base.Request.QueryString["C_GP"];
                if ((this.a == null) || (this.a == ""))
                {
                    this.a = "";
                }
                else if (this.a.IndexOf("..") != -1)
                {
                    throw new Exception("!");
                }
                if (this.a.ToLower().IndexOf(this.b.ToLower()) < 0)
                {
                    this.a = "";
                }

                //this.FolderDescription.Text = this.fs.GetDirectoryText(this.a);//update by tang 2008.04.21
                this.g = 80;//this.sSetting.ThumbnailWidth;//80
                this.h = 80;//this.sSetting.ThumbnailHeight;//80
                this.i = 5;//this.sSetting.ThumbnailColumns;//5
                this.j = 3;//this.sSetting.ThumbnailRows;//3
                this.m = true;//this.sSetting.AllowUpload;//true
                this.k = 640;//this.sSetting.MaxImageWidth;//640
                this.l = 480;//this.sSetting.MaxImageHeight;//480
                this.o = true;//this.sSetting.RestrictUploadedImageDimension;//true
                this.n = true;//this.sSetting.AutoResizeUploadedImages;//true
                this.f = 1024;//this.sSetting.MaxImageSize;//1024
                //this.uploadButton.Attributes["onclick"] = "ShowUploading()";
                this.Label_PictureUpload.Text = "上传：";//base.GetString("Upload");//上传：
                //this.uploadButton.Text = "上传";//base.GetString("Send");//上传
                //this.uploadResult.Text = "上传";//base.GetString("uploadResult");//上传
                if (!base.IsPostBack)
                {
                    if (this.m)
                    {
                        this.uploadpanel.Visible = true;
                        this.uploadResult.Visible = false;
                    }

                    this.cbThumbSize.Items.Add(new ListItem(string.Format("{0}x{1}", this.g, this.h), string.Format("{0}x{1}", this.g, this.h)));
                    for (int num1 = 40; num1 <= 200; num1 += 20)
                    {
                        this.cbThumbSize.Items.Add(new ListItem(string.Format("{0}x{1}", num1, num1), string.Format("{0}x{1}", num1, num1)));
                    }
                    this.cbThumbSize.Attributes.Add("style", "margin-left:5; width:100px; font:8pt Verdana;vertical-align:middle;");
                    this.cbColumns.Items.Add(new ListItem(this.i.ToString(), this.i.ToString()));
                    for (int num2 = 1; num2 <= 10; num2++)
                    {
                        this.cbColumns.Items.Add(new ListItem(num2.ToString(), num2.ToString()));
                    }
                    // this.cbColumns.Attributes.Add("style", "margin-left:5; width:40px;font:8pt Verdana;vertical-align:middle;");
                    this.cbColumns.Attributes.Add("style", "margin-left:5; width:20px;font:8pt Verdana;vertical-align:top;");
                    this.cbRows.Items.Add(new ListItem(this.j.ToString(), this.j.ToString()));
                    for (int num3 = 1; num3 <= 10; num3++)
                    {
                        this.cbRows.Items.Add(new ListItem(num3.ToString(), num3.ToString()));
                    }
                    this.cbRows.Attributes.Add("style", "margin-left:5; width:20px;font:8pt Verdana;vertical-align:top;");
                    //this.cbTypes.Items.Add(new ListItem(base.GetString("All"), "*.*"));//All
                    this.cbTypes.Items.Add(new ListItem("All", "*.*"));
                    this.cbTypes.Items.Add(new ListItem("JPG", "*.jpg"));
                    this.cbTypes.Items.Add(new ListItem("GIF", "*.gif"));
                    this.cbTypes.Items.Add(new ListItem("BMP", "*.bmp"));
                    this.cbTypes.Items.Add(new ListItem("PNG", "*.png"));
                    this.cbTypes.Items.Add(new ListItem("JPEG", "*.jpeg"));

                    //update by heyang 2010-06-10 代替sSetting.ImageFilters;
                    string[] ImageFilters = new string[] { ".jpg", ".jpeg", ".gif", ".png", ".bmp", "." };

                    foreach (string text1 in ImageFilters)
                    {
                        if ((text1 != null) && (text1.Length != 0))
                        {
                            bool flag1 = false;
                            foreach (ListItem item1 in this.cbTypes.Items)
                            {
                                if (string.Compare("*" + text1, item1.Value, true) == 0)
                                {
                                    flag1 = true;
                                    break;
                                }
                            }
                            if (!flag1)
                            {
                                this.cbTypes.Items.Add(new ListItem("*" + text1));
                            }
                        }
                    }
                    this.cbTypes.Attributes.Add("style", "margin-left:5; width:40px;font:8pt Verdana;vertical-align:top;");
                    this.cbTypes.SelectedIndex = 0;
                }
                //settinghash,{DNN_Arg}
                //this.c = base.Request.FilePath + "?settinghash=" + base.GetString("_setting_") + "&" + base.GetString("DNN_Arg");

                this.c = base.Request.FilePath + "?settinghash=&{DNN_Arg}";
                string text2 = this.cbThumbSize.SelectedItem.Value;
                int num4 = text2.IndexOf('x');
                this.d = int.Parse(text2.Substring(0, num4));
                this.e = int.Parse(text2.Substring(num4 + 1));
                //this.ThumbList1.VRoot = this.fs.VirtualRoot;
                this.ThumbList1.VRoot = "";
                this.ThumbList1.VPath = this.a;
                this.ThumbList1.Type = this.cbTypes.SelectedItem.Value;
                this.ThumbList1.Width = this.d;
                this.ThumbList1.Height = this.e;
                this.ThumbList1.Columns = int.Parse(this.cbColumns.SelectedItem.Value);
                this.ThumbList1.Rows = int.Parse(this.cbRows.SelectedItem.Value);

            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());

            }
        }

        private void ProcessDeleteFile()
        {
            Stream ss = Request.InputStream;
            StreamReader sr = new StreamReader(ss);
            string strXml = sr.ReadToEnd();

            if (Request.QueryString["opType"] != null && Request.QueryString["opType"].ToString() != "")
            {
                if (Request.QueryString["opType"].ToString() == "direct")
                {
                    DeleteImage(strXml);
                    return;
                }
            }


            int DeleteCheck = CheckDeleteImage(strXml);

            if (DeleteCheck == 0)
            {
                DeleteImage(strXml);
                Response.Flush();
                Response.Close();
            }
            else if (DeleteCheck == 1)
            {
                //Response.Write("AlertImageInArt");
                Response.Flush();
                Response.Close();
            }
            else if (DeleteCheck == 2)
            {
                Response.Write("AlertUserPhoto");
                Response.Flush();
                Response.Close();
            }
        }

        private int CheckDeleteImage(string strXml)
        {
            int numResult = 0;
            OLEDBOperator oo_Image = null;
            //http://userimage2.360doc.com/10/0610/14/11991_201006101410050984.jpg
            //http://userimage.360doc.com/11991/20100610154946755.jpg
            string[] strArrayUrl = null;
            string strUserID = "";
            string strFileName = "";

            if (strXml.IndexOf("userimage2.360doc.com") > -1)
            {
                strArrayUrl = strXml.Split('/');
                strUserID = strArrayUrl[strArrayUrl.Length - 1].Split('_')[0];
                strFileName = strArrayUrl[strArrayUrl.Length - 1].Split('_')[1];
            }
            else if (strXml.IndexOf("userimage.360doc.com") > -1)
            {
                strArrayUrl = strXml.Split('/');
                strUserID = strArrayUrl[strArrayUrl.Length - 2];
                strFileName = strArrayUrl[strArrayUrl.Length - 1];
            }

            if (CheckImageIsUserPhoto(strUserID, strFileName))
            {
                return 2;
            }





            ImageTableSettingPaser iTableSettingPaser = new ImageTableSettingPaser(new DocDataConfig.DataConfig()["UserUploadImageInfoTableSetting"].ToString(),
            new DocDataConfig.DataConfig()["UserUploadImageInfoTableDefaultCONN"].ToString());

            ImageTableSetting its = iTableSettingPaser.GetImageSettingItem(strUserID);

            oo_Image = new OLEDBOperator(its.ConnString);

            string strSql = "Select ArticleIDList from " + its.TableName + " where ImageFileName='" + strFileName + "'";

            strSql = oo_Image.ExceSqlforScalar(strSql, ref numResult);

            if (strSql != null && strSql != "")
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        private bool CheckImageIsUserPhoto(string strDeleteUserID, string strDeleteFileName)
        {
            int numResult = 0;
            OLEDBOperator oo_Site = new OLEDBOperator(new DocDataConfig.DataConfig()["data"].ToString());
            string strUserTableName = GetUsersTableName(strDeleteUserID);
            string strSql = "Select UserPhoto from " + strUserTableName + " where UserID=" + strDeleteUserID;
            string strUserPhoto = "";


            string[] strArrayUrl = null;
            string strUserID = "";
            string strFileName = "";


            try
            {
                strUserPhoto = oo_Site.ExceSqlforScalar(strSql, ref numResult);

                if (strUserPhoto != null && strUserPhoto != "")
                {
                    if (strUserPhoto.IndexOf("userimage2.360doc.com") > -1)
                    {
                        strArrayUrl = strUserPhoto.Split('/');
                        strUserID = strArrayUrl[strArrayUrl.Length - 1].Split('_')[0];
                        strFileName = strArrayUrl[strArrayUrl.Length - 1].Split('_')[1];
                    }
                    else if (strUserPhoto.IndexOf("userimage.360doc.com") > -1)
                    {
                        strArrayUrl = strUserPhoto.Split('/');
                        strUserID = strArrayUrl[strArrayUrl.Length - 2];
                        strFileName = strArrayUrl[strArrayUrl.Length - 1];
                    }

                    if (strDeleteFileName == strFileName)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        //update by heyang 2010-06-11
        #region OLD Function


        //private void a2(string A_0)
        //{
        //    this.Label_PictureUpload.Controls.Add(new LiteralControl(A_0));
        //}

        //private bool a2(HttpPostedFile A_0)
        //{
        //    if (((A_0 != null) && Regex.IsMatch(A_0.ContentType, @"image/\S+")) && (A_0.ContentLength > 0))
        //    {
        //        string text1 = A_0.FileName.ToLower();

        //        //update by heyang 2010-06-10 代替sSetting.ImageFilters;
        //        string[] ImageFilters = new string[] { ".jpg", ".jpeg", ".gif", ".png", ".bmp", "." };


        //        //foreach (string text2 in this.sSetting.ImageFilters)
        //        foreach (string text2 in ImageFilters)
        //        {
        //            if (((text2 != null) && (text2.Length != 0)) && text1.EndsWith(text2.ToLower()))
        //            {
        //                return true;
        //            }
        //        }
        //    }
        //    return false;
        //}


        //private void b2()//update by tang 2008.04.21
        //{
        //    //TableRow row1 = new TableRow();
        //    //TableCell cell1 = new TableCell();
        //    //TableCell cell2 = new TableCell();
        //    //HyperLink link1 = new HyperLink();
        //    //HyperLink link2 = new HyperLink();
        //    //cell1.Width = (Unit)20;
        //    //cell2.Width = (Unit)460;
        //    //link1.NavigateUrl = this.c + "&C_GP=" + this.fs.GetParentDirectory(this.a);
        //    //link1.Text = " ... ";
        //    //link2.Text = "<img src='../images/ParentFolder.gif' alt='Go up one level' valign=middle border=0>";
        //    //link2.NavigateUrl = link1.NavigateUrl;
        //    //cell1.Controls.Add(link2);
        //    //cell1.HorizontalAlign = HorizontalAlign.Right;
        //    //cell2.Controls.Add(link1);
        //    //cell2.HorizontalAlign = HorizontalAlign.Left;
        //    //row1.Cells.Add(new TableCell());
        //    //row1.Cells.Add(cell1);
        //    //row1.Cells.Add(cell2);
        //    //this.FoldersAndFiles.Rows.Add(row1);
        //    //foreach (DirectoryItem item1 in this.fs.GetDirectoryItems(this.a, false))
        //    //{
        //    //    row1 = new TableRow();
        //    //    cell2 = new TableCell();
        //    //    link1 = new HyperLink();
        //    //    cell1 = new TableCell();
        //    //    link2 = new HyperLink();
        //    //    cell1.Width = (Unit)20;
        //    //    cell2.Width = (Unit)460;
        //    //    link1.NavigateUrl = this.c + "&C_GP=" + item1.Path;
        //    //    link1.Text = item1.Name;
        //    //    cell2.Controls.Add(link1);
        //    //    link2.Text = "<img src='../images/closedfolder.gif' alt='Open this folder' valign=middle border=0>";
        //    //    link2.NavigateUrl = link1.NavigateUrl;
        //    //    cell1.Controls.Add(link2);
        //    //    cell1.HorizontalAlign = HorizontalAlign.Right;
        //    //    cell2.HorizontalAlign = HorizontalAlign.Left;
        //    //    row1.Cells.Add(new TableCell());
        //    //    row1.Cells.Add(cell1);
        //    //    row1.Cells.Add(cell2);
        //    //    this.FoldersAndFiles.Rows.Add(row1);
        //    //}
        //}





        //protected string CheckUploadData(ref byte[] data)
        //{
        //    int imgFileCont = 0;
        //    double imgUsedSize = 0.0;
        //    double imgCanUseSize = 0.0;
        //    string strUserID = commonClass.getCookies360doc1();
        //    GetUserUploadedImagInfo(strUserID, out imgFileCont, out imgUsedSize, out imgCanUseSize);


        //    if (imgCanUseSize < 0)
        //    {
        //        return ("超过最大容量: " + FileStorage.FormatSize((double)(this.sSetting.MaxImageFolderSize * 0x400)));
        //    }
        //    try
        //    {
        //        System.Drawing.Image.FromStream(new MemoryStream(data));
        //    }
        //    catch
        //    {
        //        return "上传的图片有错误";
        //    }
        //    return null;
        //}



        //protected bool AllowFileName(string filename)
        //{
        //    filename = filename.ToLower();
        //    foreach (string text1 in this.sSetting.ImageFilters)
        //    {
        //        if (((text1 != null) && (text1.Length != 0)) && filename.EndsWith(text1.ToLower()))
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}






        //private void a2(object A_0, EventArgs A_1)
        //{
        //    #region OLD Upload
        //    //    if (!this.sSetting.AllowUpload)
        //    //    {
        //    //        this.a2("<font color=red><b>[[NoPermission]]</b></font>");
        //    //        return;
        //    //    }
        //    //    HttpPostedFile file1 = this.myuploadFile.PostedFile;
        //    //    if (file1 == null)
        //    //    {
        //    //        return;
        //    //    }
        //    //    if ((file1.FileName == null) || (file1.FileName == ""))
        //    //    {
        //    //        return;
        //    //    }
        //    //    string text1 = Path.GetFileName(file1.FileName);
        //    //    if (this.sSetting.FileNamePrefix != "")
        //    //    {
        //    //        text1 = this.sSetting.FileNamePrefix + text1;
        //    //    }
        //    //    string text2 = "";
        //    //    if (this.sSetting.UseTimeStampRenameUploadedFiles)
        //    //    {
        //    //        text2 = EditorUtility.GetTimeStampfilename(text1);
        //    //        if (this.sSetting.FileNamePrefix != "")
        //    //        {
        //    //            text2 = this.sSetting.FileNamePrefix + text2;
        //    //        }
        //    //    }
        //    //    else
        //    //    {
        //    //        if (!Regex.IsMatch(text1, this.sSetting.filenamePattern))
        //    //        {
        //    //            this.a2("<font color=red><b>[[FilenameNotsupported]]</b></font>");
        //    //            return;
        //    //        }
        //    //        try
        //    //        {
        //    //            if (this.sSetting.OverWriteExistingUploadedFile || !File.Exists(this.Context.Server.MapPath(this.fs.GetDirectoryText(this.a).TrimEnd(new char[] { '/' }) + "/" + text1)))
        //    //            {
        //    //                goto Label_014A;
        //    //            }
        //    //            this.a2("<font color=red><b>[[FileWithNameExist]]</b></font>");
        //    //        }
        //    //        catch
        //    //        {
        //    //        }
        //    //        return;
        //    //    }
        //    //Label_014A:
        //    //    if (!this.AllowFileName(text1))
        //    //    {
        //    //        this.a2("<font color=red><b>[[FileformatNotsupported]]</b></font>");
        //    //    }
        //    //    else
        //    //    {
        //    //        byte[] buffer1 = new byte[file1.InputStream.Length];
        //    //        file1.InputStream.Read(buffer1, 0, buffer1.Length);
        //    //        string text3 = this.CheckUploadData(ref buffer1);
        //    //        if (text3 != null)
        //    //        {
        //    //            this.a2("<font color=red><b>" + text3 + "</b></font>");
        //    //        }
        //    //        else
        //    //        {
        //    //            if (!EditorSetting.AllowFileSystem)
        //    //            {
        //    //                throw new ArgumentException("CuteEditor needs read/write permission to the system temp folder. If your host doesn't allow such access, you can specify a different path by adding an entry to your Web.config file. <add key='CuteEditorTempPath' value='/mydirectory' /> ");
        //    //            }
        //    //            string text4 = "";
        //    //            int num5 = Guid.NewGuid().GetHashCode();
        //    //            text4 = Path.Combine(EditorSetting.GetTempDir(), "CuteEditor_Upload_" + num5.ToString("x") + text1);
        //    //            Bitmap bitmap1 = null;
        //    //            try
        //    //            {
        //    //                using (FileStream stream1 = new FileStream(text4, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite))
        //    //                {
        //    //                    stream1.Write(buffer1, 0, buffer1.Length);
        //    //                }
        //    //                try
        //    //                {
        //    //                    bitmap1 = new Bitmap(text4);
        //    //                }
        //    //                catch
        //    //                {
        //    //                }
        //    //                if (bitmap1 != null)
        //    //                {
        //    //                    if ((bitmap1.Width > this.sSetting.MaxImageWidth) || (bitmap1.Height > this.sSetting.MaxImageHeight))
        //    //                    {
        //    //                        if (!this.sSetting.AutoResizeUploadedImages)
        //    //                        {
        //    //                            this.a2("<font color=red><b>" + string.Format("Image dimension ({0} x {1} pixels) exceeding limits.", bitmap1.Width, bitmap1.Height) + "</b></font>");
        //    //                            return;
        //    //                        }
        //    //                        int num1 = this.sSetting.MaxImageWidth;
        //    //                        int num2 = this.sSetting.MaxImageHeight;
        //    //                        double num3 = Math.Min((double)(((double)num1) / ((double)bitmap1.Width)), (double)(((double)num2) / ((double)bitmap1.Height)));
        //    //                        if (num3 < 1)
        //    //                        {
        //    //                            num1 = (int)(bitmap1.Width * num3);
        //    //                            num2 = (int)(bitmap1.Height * num3);
        //    //                        }
        //    //                        using (MemoryStream stream2 = new MemoryStream())
        //    //                        {
        //    //                            Bitmap bitmap2 = new Bitmap(num1, num2);
        //    //                            Graphics graphics1 = Graphics.FromImage(bitmap2);
        //    //                            graphics1.SmoothingMode = SmoothingMode.HighQuality;
        //    //                            graphics1.InterpolationMode = InterpolationMode.HighQualityBicubic;
        //    //                            graphics1.PixelOffsetMode = PixelOffsetMode.HighQuality;
        //    //                            int[] numArray1 = bitmap1.PropertyIdList;
        //    //                            PropertyItem[] itemArray1 = new PropertyItem[numArray1.Length];
        //    //                            foreach (int num4 in numArray1)
        //    //                            {
        //    //                                try
        //    //                                {
        //    //                                    bitmap2.SetPropertyItem(bitmap1.GetPropertyItem(num4));
        //    //                                }
        //    //                                catch
        //    //                                {
        //    //                                }
        //    //                            }
        //    //                            graphics1.DrawImage(bitmap1, 0, 0, num1, num2);
        //    //                            bitmap2.Save(stream2, bitmap1.RawFormat);
        //    //                            bitmap2.Dispose();
        //    //                            buffer1 = stream2.ToArray();
        //    //                        }
        //    //                    }
        //    //                    if (this.sSetting.RestrictUploadedImageDimension && (buffer1.Length > (this.sSetting.MaxImageSize * 0x400)))
        //    //                    {
        //    //                        this.a2("<font color=red><b>[[ImageFileIsTooLarge]]</b></font>");
        //    //                        return;
        //    //                    }
        //    //                }
        //    //            }
        //    //            finally
        //    //            {
        //    //                if (bitmap1 != null)
        //    //                {
        //    //                    bitmap1.Dispose();
        //    //                }
        //    //                File.Delete(text4);
        //    //            }
        //    //            //try
        //    //            //{
        //    //            //    if (text2 != "")
        //    //            //    {
        //    //            //        this.fs.CreateFile(this.fs.GetDirectoryText(this.a), text2, buffer1);
        //    //            //    }
        //    //            //    else
        //    //            //    {
        //    //            //        this.fs.CreateFile(this.fs.GetDirectoryText(this.a), text1, buffer1);
        //    //            //    }
        //    //            //}
        //    //            //catch
        //    //            //{
        //    //            //    this.a2("<font color=red><b>[[UnableUploadfile]]</b></font>");
        //    //            //    return;
        //    //            //}

        //    //            //*********************updated by chengeng
        //    //            try
        //    //            {

        //    //                string strPathTemp = this.fs.GetDirectoryText(this.a);

        //    //                if (text2 != "")
        //    //                {

        //    //                    if (strPathTemp.StartsWith("/UserImage/", StringComparison.OrdinalIgnoreCase))
        //    //                    {
        //    //                        if (text2.IndexOf("chr(0)", StringComparison.OrdinalIgnoreCase) != -1 || text2.IndexOf("../", StringComparison.OrdinalIgnoreCase) != -1 || text2.IndexOf(".asa", StringComparison.OrdinalIgnoreCase) != -1 || text2.IndexOf(".inc", StringComparison.OrdinalIgnoreCase) != -1 || text2.IndexOf(".asp", StringComparison.OrdinalIgnoreCase) != -1)
        //    //                        {
        //    //                            this.a2("<font color=red><b>[[UnableUploadfile]]</b></font>");
        //    //                            return;
        //    //                        }
        //    //                        else
        //    //                        {
        //    //                            this.fs.CreateFile(strPathTemp.Replace("../", ""), text2, buffer1);
        //    //                        }
        //    //                    }
        //    //                }
        //    //                else
        //    //                {

        //    //                    if (strPathTemp.StartsWith("/UserImage/", StringComparison.OrdinalIgnoreCase))
        //    //                    {
        //    //                        if (text1.IndexOf("chr(0)", StringComparison.OrdinalIgnoreCase) != -1 || text1.IndexOf("../", StringComparison.OrdinalIgnoreCase) != -1 || text1.IndexOf(".asa", StringComparison.OrdinalIgnoreCase) != -1 || text1.IndexOf(".inc", StringComparison.OrdinalIgnoreCase) != -1 || text1.IndexOf(".asp", StringComparison.OrdinalIgnoreCase) != -1)
        //    //                        {
        //    //                            this.a2("<font color=red><b>[[UnableUploadfile]]</b></font>");
        //    //                            return;
        //    //                        }
        //    //                        else
        //    //                        {
        //    //                            this.fs.CreateFile(strPathTemp.Replace("../", ""), text1, buffer1);
        //    //                        }
        //    //                    }
        //    //                }
        //    //            }
        //    //            catch
        //    //            {
        //    //                this.a2("<font color=red><b>[[UnableUploadfile]]</b></font>");
        //    //                return;
        //    //            }
        //    //            //*********************************

        //    //            this.a2("<font color=red><b>[[uploadSuccessfully]]</b></font>");
        //    //            base.Response.Redirect(this.c + "&C_GP=" + this.a);
        //    //            ///CuteSoft_Client/CuteEditor/Dialogs/InsertGallery.Frame.Aspx?settinghash=ce710c71&{DNN_Arg}
        //    //        }
        //    //    }
        //    #endregion
        //}
        #endregion

        protected override void OnInit(EventArgs e)
        {
            //this.a2();
            //base.Load += new EventHandler(this.b2);

            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            try
            {
                //base.OnLoad(e);
                this.b2(null, e);
            }
            catch (Exception ex)
            {
                Response.Write("错误信息：" + ex);
            }
        }



        public void DeleteImage(string strPath)
        {
            //http://localhost:99/10/0324/15/361043_201003241524060484.jpg
            int numResult = 0;
            string strdUserID = "";
            string strdImageName = "";
            string strImageFileSize = "";
            string strImageFilePath = "";

            string strImageLocalFileBaseDic = "";
            DataTable dtImageInfo = null;


            FilePoster fp = new FilePoster();

            string[] arrPathInfo = null;
            OLEDBOperator oo_Image = null;
            OLEDBOperator oo_Site = null;



            bool IsDeleteRemoteFile = false;

            if (strPath.IndexOf("_") > 0)
            {
                IsDeleteRemoteFile = true;

                strdImageName = strPath.Substring(strPath.LastIndexOf('_') + 1, strPath.Length - (strPath.LastIndexOf('_') + 1));
                strdUserID = strPath.Substring(strPath.LastIndexOf('/') + 1, strPath.Length - (strPath.LastIndexOf('/') + 1)).Replace("_" + strdImageName, "");

                strdImageName = strdUserID + "_" + strdImageName;
            }
            else
            {
                IsDeleteRemoteFile = false;

                arrPathInfo = strPath.Split('/');
                strdImageName = arrPathInfo[arrPathInfo.Length - 1];
                //strPath.Substring(strPath.LastIndexOf('/') + 1, strPath.Length - (strPath.LastIndexOf('/') + 1));
                strdUserID = arrPathInfo[arrPathInfo.Length - 2];
                //strPath.Substring(strPath.LastIndexOf('/') + 1, strPath.Length - (strPath.LastIndexOf('/') + 1)).Replace("_" + strdImageName, "");
            }


            //if (strXml.IndexOf("userimage2.360doc.com") > -1)
            //{
            //    strArrayUrl = strXml.Split('/');
            //    strUserID = strArrayUrl[strArrayUrl.Length - 1].Split('_')[0];
            //    strFileName = strArrayUrl[strArrayUrl.Length - 1].Split('_')[1];
            //}
            //else if (strXml.IndexOf("userimage.360doc.com") > -1)
            //{
            //    strArrayUrl = strXml.Split('/');
            //    strUserID = strArrayUrl[strArrayUrl.Length - 2];
            //    strFileName = strArrayUrl[strArrayUrl.Length - 1];
            //}





            ImageTableSettingPaser iTableSettingPaser = new ImageTableSettingPaser(new DocDataConfig.DataConfig()["UserUploadImageInfoTableSetting"].ToString(), new DocDataConfig.DataConfig()["UserUploadImageInfoTableDefaultCONN"].ToString());
            ImageTableSetting its = iTableSettingPaser.GetImageSettingItem(strdUserID);
            oo_Image = new OLEDBOperator(its.ConnString);
            //oo_Site = new OLEDBOperator(new DocDataConfig.DataConfig()["data"].ToString());
            string strSql = "";

            //strSql = "Select ImageSize,ImageFilePath from " + its.TableName + " where UserID='" + strdUserID + "' and ImageFileName='" + strdImageName + "'";

            //dtImageInfo = oo_Image.ExecSqlforDataTable(strSql, ref numResult);
            //strImageFileSize = dtImageInfo.Rows[0]["ImageSize"].ToString();
            //strImageFilePath = dtImageInfo.Rows[0]["ImageFilePath"].ToString();

            //bool IsDeleteOK = false;

            //if (IsDeleteRemoteFile)
            //{
            //    fp.AddString("<SFileFullPath>" + strImageFilePath + "<EFileFullPath>");
            //    fp.PostData(new DocDataConfig.DataConfig()["UserUploadImagePHPServerDelete"].ToString());
            //    IsDeleteOK = true;
            //}
            //else
            //{
            //    IsDeleteOK=DeleteLocalImageFile(strImageFilePath);
            //}

            //if (IsDeleteOK)
            //{

            //strSql = "Update " + its.TableName + " set IsDeleted=1,ImageFilePath=ImageFilePath+'_deleted',ImageFileName=ImageFileName+'_deleted' where UserID='" + strdUserID + "' and ImageFileName='" + strdImageName + "'";

            try
            {
                int.Parse(strdUserID);
            }
            catch
            {
                return;
            }

            strdImageName = strdImageName.Replace("'", "");

            strSql = "Update " + its.TableName + " set IsDeleted=1 where UserID='" + strdUserID + "' and ImageFileName='" + strdImageName + "'";
            oo_Image.ExecSqlNonQuery(strSql);

            //if (strImageFileSize != null && strImageFileSize != "")
            //{
            //    strSql = "Update " + GetUsersTableName(strdUserID) + " set ImageUsedSize=ImageUsedSize-" + strImageFileSize + " where UserID=" + strdUserID;
            //    oo_Site.ExecSqlNonQuery(strSql);
            //}
            //}
        }

        private bool DeleteLocalImageFile(string strImageFilePath)
        {
            try
            {
                FileInfo fi = new FileInfo(strImageFilePath);

                if (File.Exists(strImageFilePath))
                {
                    File.Copy(strImageFilePath, strImageFilePath + "_deleted");
                    File.Delete(strImageFilePath);
                    return true;
                }
                else
                {
                    Response.Write("NoFileAlert");
                    return false;
                }
            }
            catch (Exception ex)
            {
                Response.Write("AclAlert");
                return false;
            }
        }

        protected void GetUserUploadedImagInfo(string strUserID, out int numImageCount, out double numImageUsedMB, out double numImgCanUseSpaceMB)
        {
            int numResult = 0;
            OLEDBOperator oo_Image = null;
            OLEDBOperator oo_Site = null;
            ImageTableSettingPaser iTableSettingPaser = new ImageTableSettingPaser(new DocDataConfig.DataConfig()["UserUploadImageInfoTableSetting"].ToString(), new DocDataConfig.DataConfig()["UserUploadImageInfoTableDefaultCONN"].ToString());

            ImageTableSetting its = iTableSettingPaser.GetImageSettingItem(strUserID);

            oo_Site = new OLEDBOperator(new DocDataConfig.DataConfig()["data"].ToString());
            oo_Image = new OLEDBOperator(its.ConnString);

            DataTable dtImgInfo = oo_Site.ExecSqlforDataTable("select ImageUsedSize,ImageMaxSize from " + GetUsersTableName(strUserID) + " where UserID=" + strUserID, ref numResult);

            double d_ImgMaxSize = Convert.ToDouble(dtImgInfo.Rows[0]["ImageMaxSize"].ToString());

            numImageUsedMB = Convert.ToDouble(dtImgInfo.Rows[0]["ImageUsedSize"].ToString());
            numImageCount = Convert.ToInt32(oo_Image.ExceSqlforScalar("Select Count(ID) from " + its.TableName + " where UserID=" + strUserID + " and IsDeleted=0", ref numResult));


            numImageUsedMB = numImageUsedMB / 1024;
            d_ImgMaxSize = d_ImgMaxSize / 1024;

            numImgCanUseSpaceMB = d_ImgMaxSize - numImageUsedMB;
        }

        private string GetUsersTableName(string userID)
        {
            return ("users" + (Convert.ToInt32(userID) / 3000000 + 1));
        }



    }
}
