using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
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.Xml;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Net.Mail;
/// <summary>
/// Function 的摘要说明
/// </summary>
public class Fun
{
    public string etw_clientid;
	public Fun()
	{
	}
    public static string JsAlerT(string str_Prompt, string reurl)
    {
        string str = "<script language=\"javascript\">alert('" + str_Prompt + "');window.location.href='" + reurl + "'</" + "script>";
        System.Web.HttpContext.Current.Response.Write(str);
        return str;
    }
     public static string JsAlerT2(string reurl)
    {
        string str = "<script language=\"javascript\">if (confirm('是否添加PDF!')){window.location.href='" + reurl + "'}</" + "script>";
        System.Web.HttpContext.Current.Response.Write(str);
        return str;
    }
    public static string JsAlerT1(string str_Prompt, string reurl)
    {
        string str = "<script language=\"javascript\">alert('" + str_Prompt + "');window.location.reload();</" + "script>";
        System.Web.HttpContext.Current.Response.Write(str);
        return str;
    }
    public static string Encode(string str)
    {
        if (str != "" && str != null)
        {
            str = str.Replace("'", "&#39;");
        }
        return str;
    }
    public static string Fancode(string str)
    {
        if (str != "" && str != null)
        {
            str = str.Replace("&#39;", "'");
        }
        return str;
    }
    public static string Encode1(string str)
    {
        if (str != "" && str != null)
        {
            str = str.Replace("\n", "<br />");
        }
        return str;
    }
    public static string GetUnixTime(string timeStamp)// 转换成日期格式
    {
        string newtime=string.Empty;
        if (timeStamp != "" && timeStamp != null)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime dtResult = dtStart.Add(toNow);
            newtime = dtResult.ToString("yyyy-MM-dd HH:mm:ss");
        }
        return newtime;
    }
    public static string GetDateTime()// 转换成时间戳
    {
        string newtime = string.Empty;
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            DateTime dtNow = DateTime.Parse(DateTime.Now.ToString());
            TimeSpan toNow = dtNow.Subtract(dtStart);
            string timeStamp = toNow.Ticks.ToString();
            newtime = timeStamp.Substring(0, timeStamp.Length - 7);   
        return newtime;
    }
    public static string Decode(string str)
    {
        if (str != "" && str != null)
        {
            str = str.Replace("&#39;", "'").Replace("&asymp;", "≈");
            str = str.Replace("&hArr;", "⇔").Replace("&rArr;", "⇒");
            str = str.Replace("&rarr;", "→").Replace("&bull;", "•");
            str = str.Replace("&hellip;", "…").Replace("&bdquo;", "„");
            str = str.Replace("&#8219;", "'").Replace("&sbquo;", ",");
            str = str.Replace("&#375;", "ŷ").Replace("&#373", "ŵ");
            str = str.Replace("&#374", "Ŷ").Replace("&#372;", "Ŵ");
            str = str.Replace("&oelig;", "œ").Replace("&OElig;", "Œ");
            str = str.Replace("&yuml;", "ÿ").Replace("&thorn;", "þ");
            str = str.Replace("&yacute;", "ý").Replace("&uuml;", "ü");
            str = str.Replace("&ucirc;", "û").Replace("&uacute;", "ú");
            str = str.Replace("&ugrave;", "ù").Replace("&oslash;", "ø");
            str = str.Replace("&divide;", "÷").Replace("&ouml;", "ö");
            str = str.Replace("&otilde;", "õ").Replace("&ocirc;", "ô");
            str = str.Replace("&oacute;", "ó").Replace("&ograve;", "ò");
            str = str.Replace("&ntilde;", "ñ").Replace("&eth;", "ð");
            str = str.Replace("&iuml;", "ï").Replace("&icirc;", "î");
            str = str.Replace("&iacute;", "í").Replace("&igrave;", "ì");
            str = str.Replace("&euml;", "ë").Replace("&ecirc;", "ê");
            str = str.Replace("&eacute;", "é").Replace("&egrave;", "è");
            str = str.Replace("&ccedil;", "ç").Replace("&aelig;", "æ");
            str = str.Replace("&aring;", "å").Replace("&auml;", "ä");
            str = str.Replace("&atilde;", "ã").Replace("&acirc;", "â");
            str = str.Replace("&aacute;", "á").Replace("&agrave;", "à");
            str = str.Replace("&szlig;", "ß").Replace("&THORN;", "Þ");
            str = str.Replace("&Yacute;", "Ý").Replace("&Uuml;", "Ü");
            str = str.Replace("&Ucirc;", "Û").Replace("&Uacute;", "Ú");
            str = str.Replace("&Ugrave;", "Ù").Replace("&Oslash;", "Ø");
            str = str.Replace("&times;", "×").Replace("&Ouml;", "Ö");
            str = str.Replace("&Otilde;", "Õ").Replace("&Ocirc;", "Ô");
            str = str.Replace("&Oacute;", "Ó").Replace("&Ograve;", "Ò");
            str = str.Replace("&Ntilde;", "Ñ").Replace("&ETH;", "Ð");
            str = str.Replace("&Iuml;", "Ï").Replace("&Icirc;", "Î");
            str = str.Replace("&Iacute;", "Í").Replace("&Igrave;", "Ì");
            str = str.Replace("&Euml;", "Ë").Replace("&Ecirc;", "Ê");
            str = str.Replace("&Eacute;", "É").Replace("&Egrave;", "È");
            str = str.Replace("&Ccedil;", "Ç").Replace("&AElig;", "Æ");
            str = str.Replace("&Aring;", "Å").Replace("&Auml;", "Ä");
            str = str.Replace("&Atilde;", "Ã").Replace("&Acirc;", "Â");
            str = str.Replace("&Aacute;", "Á").Replace("&Agrave;", "À");
            str = str.Replace("&iquest;", "¿").Replace("&frac34;", "¾");
            str = str.Replace("&frac12;", "½").Replace("&frac14;", "¼");
            str = str.Replace("&raquo;", "»").Replace("&ordm;", "º");
            str = str.Replace("&cedil;", "¸").Replace("&middot;", "·");
            str = str.Replace("&micro;", "µ").Replace("&acute;", "'");
            str = str.Replace("&plusmn;", "±").Replace("&deg;", "°");
            str = str.Replace("&macr;", "¯").Replace("&reg;", "®");
            str = str.Replace("&ordf;", "ª").Replace("&copy;", "©");
            str = str.Replace("&uml;", "¨").Replace("&sect;", "§");
            str = str.Replace("&yen;", "¥").Replace("&curren;", "¤");
            str = str.Replace("&pound;", "£").Replace("&cent;", "¢");
            str = str.Replace("&iexcl;", "¡").Replace("&rdquo;", "\"");
            str = str.Replace("&ldquo;", "\"").Replace("&rsquo;", "'");
            str = str.Replace("&lsquo;", "'").Replace("&euro;", "€");
        }
        return str;
    }
    public static string ClearHTML(string Htmlstring)
    {
        if (Htmlstring != "" && Htmlstring != null)
        {
            Htmlstring = Regex.Replace(Htmlstring, @"<p[^>]*?>", "<p>", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"style=""([^^]*?)""", "", RegexOptions.IgnoreCase);
        }
        return Htmlstring;
    }
    public static string NoHTML3(string Htmlstring)
    {
        Regex reg = new Regex(@"(?is)<span style=""font-weight:bold;"">(.*?)</span>");
        Regex reg2 = new Regex(@"(?is)<span style=""font-weight:bold"">(.*?)</span>");
        Regex reg3 = new Regex(@"(?is)<span style=""FONT-WEIGHT: bold;"">(.*?)</span>");
        Regex reg4 = new Regex(@"(?is)<span style=""FONT-WEIGHT: bold"">(.*?)</span>");
        Htmlstring = reg.Replace(Htmlstring, "<strong>$1</strong>");
        Htmlstring = reg2.Replace(Htmlstring, "<strong>$1</strong>");
        Htmlstring = reg3.Replace(Htmlstring, "<strong>$1</strong>");
        Htmlstring = reg4.Replace(Htmlstring, "<strong>$1</strong>");
        return Htmlstring;
    }
    public static string NoHTML(string Htmlstring)
    {
        if (Htmlstring != "" && Htmlstring != null)
        {
            Htmlstring = Regex.Replace(Htmlstring, @"<!--\[if gte mso (\d+)\]>([^^]*?)<!\[endif\]-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<meta [^>]+>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<link [^>]+>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<o:p>([^^]*?)</o:p>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--\[if !mso\]>([^^]*?)<!\[endif\]-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--\[if ![^>]*?\]-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--\[endif\]-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<li[^>]*?>", "<li>", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<ul[^>]*?>", "<ul>", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<o:smarttagtype[^>]*?>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"</o:smarttagtype>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<tr[^>]*?>", "<tr>", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<font[^>]*?>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"</font>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<st1:[^>]*?>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"</st1:[^>]*?>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"valign=""([^^]*?)""", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"lang=""([^^]*?)""", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"nowrap=""([^^]*?)""", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"class=""MsoNormal", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<style[^>]+>([^^]*?)</style>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<style>([^^]*?)</style>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<table[^>]*?>", "<table  cellpadding=\"1\" cellspacing=\"1\" class=\"table01\">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<p>&nbsp;</p>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<p></p>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"font:([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"font-([^^]*?):([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"line-height:([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"width:([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"border:([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"border-([^^]*?):([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"padding:([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"padding-([^^]*?):([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"margin:([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"margin-([^^]*?):([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"text-([^^]*?):([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"width=""([^^]*?)""", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"height=""([^^]*?)""", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-moz-background-([^^]*?):([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"background-position:([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"background-attachment:([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"background-repeat:([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"height:([^^]*?);", "", RegexOptions.IgnoreCase);
            Htmlstring = NoHTML3(Htmlstring);
            Htmlstring = NoHTML2(Htmlstring);
        }
       return Htmlstring;
    }
    public static string NoHTML2(string Htmlstring)
    {
        Htmlstring = Htmlstring.Replace("，", ",");
        Htmlstring = Htmlstring.Replace("：", ":");
        Htmlstring = Htmlstring.Replace("（", "(");
        Htmlstring = Htmlstring.Replace("）", ")");
        Htmlstring = Htmlstring.Replace("～", "~");
        Htmlstring = Htmlstring.Replace("“", "\"");
        Htmlstring = Htmlstring.Replace("‘", "\'");
        Htmlstring = Htmlstring.Replace("？", "?");
        Htmlstring = Htmlstring.Replace("；", ";");
        Htmlstring = Htmlstring.Replace("&ndash;", "-");
        return Htmlstring;
    }
    public static string NoHTML1(string Htmlstring)//去掉标签
    {
        Htmlstring = Regex.Replace(Htmlstring, @"<[^>]*?>", "", RegexOptions.IgnoreCase);
        Htmlstring = Regex.Replace(Htmlstring, @"<[^>]*?", "", RegexOptions.IgnoreCase);
        return Htmlstring;
    }
    public static string NoHTML5(string Htmlstring)//符号
    {
        Htmlstring = Regex.Replace(Htmlstring, @"[\u4e00-\u9fa5]*[^a-zA-Z]*[\u4e00-\u9fa5]* ", " ", RegexOptions.IgnoreCase);
        return Htmlstring;
    }
    public static int Count_cn(string Htmlstring)
    {
        Regex regex = new Regex("[\u4e00-\u9fa5]");
        MatchCollection ms = regex.Matches(Htmlstring);
        return ms.Count;
    }
    public static int Count_com(string Htmlstring)
    {
        int count = 0;
        count = count + Count_cn(Htmlstring);
        Htmlstring = Regex.Replace(Htmlstring, @"<table[^>]+>([^^]*?)</table>", "", RegexOptions.IgnoreCase);
        string s = Fun.NoHTML1(Fun.Decode(Htmlstring));
        s = s.Replace("\r", " ").Replace("\n", " ").Replace("&quot;", "").Replace("&nbsp;", "");
        //s = Regex.Replace(s, @"[^a-zA-Z]", " ", RegexOptions.IgnoreCase);
        s = Regex.Replace(s, @"[\-,\/,\|,\$,\+,\%,\&,\',\(,\),\*,\x20-\x2f,\x3a-\x40,\x5b-\x60,\x7b-\x7e,\x80-\xff,\u3000-\u3002,\u300a,\u300b,\u300e-\u3011,\u2014,\u2018,\u2019,\u201c,\u201d,\u2026,\u203b,\u25ce,\uff01-\uff5e,\uffe5]", " ", RegexOptions.IgnoreCase);
        s = Regex.Replace(s, @"[\d]", " ", RegexOptions.IgnoreCase);
        s = Regex.Replace(s, @"[\u4e00-\u9fa5]", " ", RegexOptions.IgnoreCase);
        string[] sAl = s.Split(' ');
        for (int i = 0; i < sAl.Length; i++)
        {
            if (sAl[i].ToString() != "")
            {
                count++;
            }
        }
        return count;
    }
    public static string IPAddress(string ip)//通过IP地址获取对应的物理地址
    {
        string str = "http://usa1.etwun.com/google/getip.php?ip=" + ip + "";
        WebRequest wrequest = WebRequest.Create(str);
        WebResponse wresponse = wrequest.GetResponse();
        Stream resStream = wresponse.GetResponseStream();
        StreamReader sr = new StreamReader(resStream, Encoding.UTF8);
        string ipcontent = sr.ReadToEnd();
        return ipcontent;
    }
    public static string curl(string url, string Str, string encoding)
    {
        HttpWebRequest request;
        string ResponseHtml = "";
        try
        {
            request = (HttpWebRequest)WebRequest.Create(url);
            if (encoding == null) { encoding = "utf-8"; }
            Encoding myEncoding = Encoding.GetEncoding(encoding); //指定编码

            string parmsStr = Str;
            byte[] postBytes = myEncoding.GetBytes(parmsStr);

            request.Timeout = 60000; //超时时间
            request.Method = "GET"; //采用post方法提交
            request.ContentType = "application/x-www-form-urlencoded;charset=" + myEncoding;
            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
           // StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8);
            StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("GB2312"));
            ResponseHtml = readStream.ReadToEnd().ToString();
            readStream.Close();
            return ResponseHtml; 
        }
        catch (Exception ex)
        {
            //ResponseHtml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><data><success value=\"0\" msg=\"登录失败！" + ex.Message.ToString() + "\" /></data>";
        }
        return ResponseHtml;
    }
    //读取公告
    public static string gonggao()
    {
        string content = string.Empty;
        DataSet ds = DbHelperSQL.Query("select top 1 * from etw_gonggao order by id desc");
        if (ds.Tables[0].Rows.Count > 0)
        {
            content = "<p align=\"center\">" + ds.Tables[0].Rows[0]["subject"] + "</p>" + "<p>" + ds.Tables[0].Rows[0]["content"] + "</p>" + "<p>" + ds.Tables[0].Rows[0]["edittime"] + "</p>";
        }
        return content;
    }
    public static string sendmail(string email, string title, string content)
    {
        string msg = string.Empty;
        if (email == "" || email == null)
        {
            msg = "请输入邮件地址!";
        }
        else if (title == "" || title == null)
        {
            msg = "请输入邮件主题!";
        }
        else
        {
            string FromMail = "newsletter@lclaw.cn";     //发件人
            MailMessage mailmessage = new MailMessage();
            MailAddress n = new MailAddress(FromMail);
            mailmessage.From = n;
            mailmessage.Subject = title;
            mailmessage.Body = content;
            mailmessage.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
            mailmessage.IsBodyHtml = true;//是否是HTML邮件
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            client.Host = "mail.lclaw.cn";
            client.Credentials = new System.Net.NetworkCredential("newsletter@lclaw.cn", "LC&CO070301");
            try
            {

                MailAddress to = new MailAddress(email);
                mailmessage.To.Add(to);
                client.Send(mailmessage);
                mailmessage.To.Clear();
                msg = "'邮件发送成功!";
                //Response.Write("<script language='javascript'>alert('邮件发送成功!')</script>");
            }
            catch (Exception)
            {
                msg = "'邮件发送失败!";
            }
        }
        return msg;
    }
    public static void Funrecord(string pagename,string ip,string usernameid)
    {
        etw.etw_record lead = new etw.etw_record();
        lead.pagename = pagename;
        lead.usernameid = Convert.ToInt32(usernameid);
        lead.datatime = System.DateTime.Now.ToString();
        lead.ip = ip;
        lead.Add();
    }
}
