using System;
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.Data.OleDb;
using System.Configuration;
using System.Text.RegularExpressions; 

namespace baoge.Admin
{
	/// <summary>
	/// other 的摘要说明。
	/// </summary>
	public class other : System.Web.UI.Page
	{
		public string get_ip;
		public bool checksid;
		static protected OleDbConnection Con=other.CreateConnection();
		protected OleDbCommand cmd;
		static protected OleDbDataAdapter Adp;
		static protected DataSet ds=new DataSet();
		protected OleDbDataReader dr;
		protected string sql;
		public other()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}
		public static OleDbConnection CreateConnection()
		{
			String ConnStr;
			ConnStr = ConfigurationSettings.AppSettings["dbPath"];
			return new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+System.Web.HttpContext.Current.Server.MapPath(ConnStr));
		}
		public void online(DateTime now,string ipaddr)
		{
			
			sql="select ip_address from online where ip_address='"+ipaddr+"'";
			cmd=new OleDbCommand(sql,Con);
			Con.Open();
			dr=cmd.ExecuteReader();
			if(dr.Read())
			{
				dr.Close();
				sql="update online set in_time='"+now+"' where ip_address='"+ipaddr+"'";
				cmd=new OleDbCommand(sql,Con);
				cmd.ExecuteNonQuery();
			}
			else
			{
				dr.Close();
				sql="insert into online(ip_address,in_time) values('"+ipaddr+"','"+now+"')";
				cmd=new OleDbCommand(sql,Con);
				cmd.ExecuteNonQuery();
			}
			sql="delete from online where DateDiff(n,in_time,'"+now+"')>5";
			cmd=new OleDbCommand(sql,Con);
			cmd.ExecuteNonQuery();
			Con.Close();
		}
		
		public bool check_ip(string ip)
		{
			//取得数据库路径
			string dbPath=System.Configuration.ConfigurationSettings.AppSettings["dbPath"];
			//定义数据库连接字符串
			string ACconnStr="Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath(dbPath);
			//创建数据库连接对像
			OleDbConnection Con=new OleDbConnection(ACconnStr);
			
			OleDbCommand cmd;
			OleDbDataReader dr;
			string sql;
			sql="select id from lockip where ip='"+ip+"'";
			cmd=new OleDbCommand(sql,Con);
			Con.Open();
			dr=cmd.ExecuteReader();
			if(dr.Read())
			{
				Con.Close();
				return true;
			}
			else
			{
				Con.Close();
				return false;
			}
		}
		public string getip()          //取真实IP地址
		{
			string httpx;
			httpx=System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
			if(httpx==null)
			{
				get_ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
			}
			else
			{
				httpx=httpx.ToLower();
				if(httpx.IndexOf(",")>0)
				{
					get_ip=httpx.Remove(httpx.IndexOf(","),httpx.Length-httpx.IndexOf(";"));
				}
				else
				{
					if(httpx.IndexOf(";")>0)
					{
						get_ip=httpx.Remove(httpx.IndexOf(";"),httpx.Length-httpx.IndexOf(";"));
					}
					else
					{
						if(httpx.IndexOf("unknown")>0)
						{
							get_ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
						}
						else
						{
							get_ip=System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
						}
					}
				}
			}
			get_ip=get_ip.Trim();
			return get_ip;
		}
		
		public string HTMLEncode(string str)
		{
			str = str.Replace(">", "&gt;"); 
			str = str.Replace("<", "&lt;");
			char ch;
			ch=(char)32;
			str = str.Replace(ch.ToString(), "&nbsp;");
			ch=(char)34;
			str = str.Replace(ch.ToString(), "&quot;");
			ch=(char)39;
			str = str.Replace(ch.ToString(), "&#39;");
			ch=(char)13;
			str = str.Replace(ch.ToString(), " ");
			ch=(char)10;
			str = str.Replace(ch.ToString(), "<BR>");
			return str;
		}
		public string Unhtmlcode(string str)
		{
			str = str.Replace( "&gt;",">"); 
			str = str.Replace("&lt;","<");
			char ch;
			ch=(char)34;
			str = str.Replace("&quot;",ch.ToString());
			ch=(char)39;
			str = str.Replace("&#39;",ch.ToString());
			str = str.Replace("&nbsp;"," ");
			ch=(char)10;
			str = str.Replace("<BR>",ch.ToString());
			return str;
		}
		public string Ubbcode(string chr)
		{
			if(chr==null)
				return "";
			chr=chr.Replace("\n","<br>");
			chr = Regex.Replace(chr,@"<script(?<x>[^\>]*)>(?<y>[^\>]*)            \</script\>",@"&lt;script$1&gt;$2&lt;/script&gt;",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[url=(?<x>[^\]]*)\](?<y>[^\]]*)\[/url\]",@"<a href=$1  target=_blank>$2</a>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[url\](?<x>[^\]]*)\[/url\]",@"<a href=$1 target=_blank>$1</a>",RegexOptions.IgnoreCase);

			chr = Regex.Replace(chr,@"\[email=(?<x>[^\]]*)\](?<y>[^\]]*)\[/email\]",@"<a href=mailto:$1>$2</a>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[email\](?<x>[^\]]*)\[/email\]",@"<a href=$1>$1</a>",RegexOptions.IgnoreCase);

			chr = Regex.Replace(chr,@"\[flash](?<x>[^\]]*)\[/flash]",@"<OBJECT codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 width=500 height=400><PARAM NAME=movie VALUE=""$1""><PARAM NAME=quality VALUE=high><embed src=""$1"" quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=500 height=400>$1</embed></OBJECT>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[img](?<x>[^\]]*)\[/img]",@"<IMG SRC=""$1"" border=0>",RegexOptions.IgnoreCase);

			chr = Regex.Replace(chr,@"\[color=(?<x>[^\]]*)\](?<y>[^\]]*)\[/color\]",@"<font color=$1>$2</font>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[face=(?<x>[^\]]*)\](?<y>[^\]]*)\[/face\]",@"<font face=$1>$2</font>",RegexOptions.IgnoreCase);

			chr = Regex.Replace(chr,@"\[size=1\](?<x>[^\]]*)\[/size\]",@"<font size=1>$1</font>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[size=2\](?<x>[^\]]*)\[/size\]",@"<font size=2>$1</font>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[size=3\](?<x>[^\]]*)\[/size\]",@"<font size=3>$1</font>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[size=4\](?<x>[^\]]*)\[/size\]",@"<font size=4>$1</font>",RegexOptions.IgnoreCase);

			chr = Regex.Replace(chr,@"\[align=(?<x>[^\]]*)\](?<y>[^\]]*)\[/align\]",@"<align=$1>$2</align>",RegexOptions.IgnoreCase);

			chr = Regex.Replace(chr,@"\[fly](?<x>[^\]]*)\[/fly]",@"<marquee width=90% behavior=alternate scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[move](?<x>[^\]]*)\[/move]",@"<marquee scrollamount=3>$1</marquee>",RegexOptions.IgnoreCase);

			chr = Regex.Replace(chr,@"\[glow=(?<x>[^\]]*),(?<y>[^\]]*),(?<z>[^\]]*)\](?<w>[^\]]*)\[/glow\]",@"<table width=$1 style='filter:glow(color=$2, strength=$3)'>$4</table>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[shadow=(?<x>[^\]]*),(?<y>[^\]]*),(?<z>[^\]]*)\](?<w>[^\]]*)\[/shadow\]",@"<table width=$1 style='filter:shadow(color=$2, strength=$3)'>$4</table>",RegexOptions.IgnoreCase);

			chr = Regex.Replace(chr,@"\[b\](?<x>[^\]]*)\[/b\]",@"<b>$1</b>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[i\](?<x>[^\]]*)\[/i\]",@"<i>$1</i>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[u\](?<x>[^\]]*)\[/u\]",@"<u>$1</u>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[code\](?<x>[^\]]*)\[/code\]",@"<pre id=code><font size=1 face='Verdana, Arial' id=code>$1</font id=code></pre id=code>",RegexOptions.IgnoreCase);

			chr = Regex.Replace(chr,@"\[list\](?<x>[^\]]*)\[/list\]",@"<ul>$1</ul>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[list=1\](?<x>[^\]]*)\[/list\]",@"<ol type=1>$1</ol id=1>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[list=a\](?<x>[^\]]*)\[/list\]",@"<ol type=a>$1</ol id=a>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[\*\](?<x>[^\]]*)\[/\*\]",@"<li>$1</li>",RegexOptions.IgnoreCase);
			chr = Regex.Replace(chr,@"\[quote](?<x>.*)\[/quote]",@"<center>—— 以下是引用 ——<table border='1' width='80%' cellpadding='10' cellspacing='0' ><tr><td>$1</td></tr></table></center>",RegexOptions.IgnoreCase);

			return(chr);
		}
		static public DataSet Getdata(string sql)
		{
			Adp=new OleDbDataAdapter(sql,Con);
			Adp.Fill(ds);
			return ds;
		}
	}
}