namespace SK.MemberWeb
{
	using System;
	using System.Collections;
	using System.IO;
	using System.Net;
	using System.Text.RegularExpressions;
	using System.Web;

	public class ImageToLocal
	{
		// Methods
		public ImageToLocal()
		{
			this.text = "";
			string[] textArray1 = new string[0];
			this.localDomains = textArray1;
			this.folder = "upload";
			this.ht = new Hashtable();
			this.alFiles = new ArrayList();
		}

		public void Get()
		{
			Regex regex1 = new Regex("<img", RegexOptions.Singleline | (RegexOptions.Compiled | RegexOptions.IgnoreCase));
			if (!regex1.IsMatch(this.text))
			{
				return;
			}
			string text1 = this.GetRndName();
			ArrayList list1 = new ArrayList();
			ArrayList list2 = new ArrayList();
			regex1 = new Regex("<img[^>]+?src=(?<quot>['\"]?)(?<url>.+?)\\k<quot>", RegexOptions.Singleline | (RegexOptions.Compiled | RegexOptions.IgnoreCase));
			MatchCollection collection1 = regex1.Matches(this.text);
			for (int num1 = 0; num1 < collection1.Count; num1++)
			{
				list2.Add(collection1[num1].Result("${url}"));
			}
			list2.Sort();
			for (int num2 = 0; num2 < list2.Count; num2++)
			{
				if (list1.Count == 0)
				{
					list1.Add(list2[num2]);
				}
				else if (list1[list1.Count - 1].ToString().ToLower() != list2[num2].ToString().ToLower())
				{
					list1.Add(list2[num2]);
				}
			}
			list2.Clear();
			for (int num3 = 0; num3 < list1.Count; num3++)
			{
				object[] objArray1 = new object[4] { this.folder, "/", text1, num3 } ;
				string text2 = this.GetImage(list1[num3].ToString(), string.Concat(objArray1));
				if ((text2 != null) && (text2 != ""))
				{
					this.ht.Add(list1[num3].ToString().ToLower(), text2);
					//this.text = Regex.Replace(this.text, list1[num3].ToString(), text2, RegexOptions.Singleline | RegexOptions.IgnoreCase);
					this.text = this.text.Replace(list1[num3].ToString(),text2);
				}
			}
			list1.Clear();
		}

		public string GetImage(string url)
		{
			return this.GetImage(url, this.folder + "/" + this.GetRndName());
		}

		private string GetImage(string url, string fileName)
		{
			string text2;
			/*if (this.alFiles.Count > 0)
			{
				return null;
			}*/
			if (this.ht[url.ToLower()] != null)
			{
				return this.ht[url.ToLower()].ToString();
			}
			if (!this.IsGet(url))
			{
				return null;
			}
			HttpWebResponse response1 = null;
			try
			{
				string text3;
				if ((url.IndexOf("http://") != 0) && (url.IndexOf("https://") != 0))
				{
					return null;
				}
				if (url.Substring(url.Length - 3, 3).ToLower() == "swf")
				{
					return null;
				}
				HttpWebRequest request1 = (HttpWebRequest) WebRequest.Create(url);
				request1.Referer = url.Substring(0, url.LastIndexOf("/") + 1);
				response1 = (HttpWebResponse) request1.GetResponse();
				Stream stream1 = response1.GetResponseStream();
				BinaryReader reader1 = new BinaryReader(stream1);
				byte[] buffer1 = new byte[0x100];
				int num1 = reader1.Read(buffer1, 0, 0x100);
				if ((text3 = response1.ContentType) != null)
				{
					text3 = string.IsInterned(text3);
					if (text3 != "image/jpeg")
					{
						if (text3 == "image/gif")
						{
							goto Label_0166;
						}
						if (text3 == "image/png")
						{
							goto Label_016F;
						}
						if (text3 == "image/bmp")
						{
							goto Label_0178;
						}
						if (text3 == "image/tiff")
						{
							goto Label_0181;
						}
					}
				}
				string text1 = "jpg";
				goto Label_018C;
			Label_0166:
				text1 = "gif";
				goto Label_018C;
			Label_016F:
				text1 = "png";
				goto Label_018C;
			Label_0178:
				text1 = "bmp";
				goto Label_018C;
			Label_0181:
				text1 = "tif";
			Label_018C:
				fileName = fileName + "." + text1;
				FileStream stream2 = new FileStream(HttpContext.Current.Server.MapPath(fileName), FileMode.Create);
				BinaryWriter writer1 = new BinaryWriter(stream2);
				while (num1 > 0)
				{
					writer1.Write(buffer1, 0, num1);
					num1 = reader1.Read(buffer1, 0, 0x100);
				}
				response1.Close();
				stream2.Close();
				this.alFiles.Add(fileName);
				text2 = fileName;
			}
			catch
			{
				text2 = null;
				//throw new Exception(ex.Message);
			}
			return text2;
		}

		private string GetRndName()
		{
			string text1 = DateTime.Now.ToString("yyMMddHHmm");
			Random random1 = new Random();
			int num1 = random1.Next(1000, 0x270f);
			return (text1 + num1.ToString());
		}

		private bool IsGet(string url)
		{
			for (int num1 = 0; num1 < this.localDomains.Length; num1++)
			{
				if ((this.localDomains[num1] != "") && (url.IndexOf(this.localDomains[num1]) > -1))
				{
					return false;
				}
			}
			return true;
		}


		// Properties
		public string[] Files
		{
			get
			{
				return (string[]) this.alFiles.ToArray(Type.GetType("System.String"));
			}
		}

		public string Folder
		{
			get
			{
				return this.folder;
			}
			set
			{
				this.folder = value;
			}
		}

		public string[] LocalDomains
		{
			get
			{
				return this.localDomains;
			}
			set
			{
				this.localDomains = value;
			}
		}

		public string Text
		{
			get
			{
				return this.text;
			}
			set
			{
				this.text = value;
			}
		}


		// Fields
		private ArrayList alFiles;
		private string folder;
		private Hashtable ht;
		private string[] localDomains;
		private string text;

		public string FirstImage()
		{
			Regex regex1 = new Regex("<img", RegexOptions.Singleline | (RegexOptions.Compiled | RegexOptions.IgnoreCase));
			if (!regex1.IsMatch(this.text))
			{
				return null;
			}
			string text1 = this.GetRndName();
			ArrayList list1 = new ArrayList();
			ArrayList list2 = new ArrayList();
			regex1 = new Regex("<img[^>]+?src=(?<quot>['\"]?)(?<url>.+?)\\k<quot>", RegexOptions.Singleline | (RegexOptions.Compiled | RegexOptions.IgnoreCase));
			MatchCollection collection1 = regex1.Matches(this.text);
			if(collection1.Count>0)
				return collection1[0].Result("${url}");
			else
				return null;
		}
		public string[] AllImage()
		{
			Regex regex1 = new Regex("<img", RegexOptions.Singleline | (RegexOptions.Compiled | RegexOptions.IgnoreCase));
			if (!regex1.IsMatch(this.text))
			{
				return null;
			}
			string text1 = this.GetRndName();
			ArrayList list1 = new ArrayList();
			ArrayList list2 = new ArrayList();
			regex1 = new Regex("<img[^>]+?src=(?<quot>['\"]?)(?<url>.+?)\\k<quot>", RegexOptions.Singleline | (RegexOptions.Compiled | RegexOptions.IgnoreCase));
			MatchCollection collection1 = regex1.Matches(this.text);
			string[] allimage = new string[collection1.Count];
			for(int i=0;i<collection1.Count;i++)
			{
				allimage[i] = collection1[i].Result("${url}");
			}
			return allimage;
		}
	}
}

