using System;
using System.Text.RegularExpressions;
using System.Web;

namespace Stat.Security
{
    class Common
    {
        /// <summary>
        /// 是否通过 特殊 字符 检查 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool ChkForm(string obj)
        {
            if (obj.IndexOf('>') > -1 || obj.IndexOf('<') > -1 || obj.IndexOf('\'') > -1 || obj.IndexOf("\"\"") > -1)
                return false;
            else
                return true;
        }

        /// <summary>
        /// 是否是数字
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static bool IsNumeric(string str)
        {
            Regex r = new Regex(@"^\d+(\.)?\d*$");
            if (r.IsMatch(str))
                return true;
            else
                return false;
        }
        /// <summary>
        /// 弹出 提示
        /// </summary>
        /// <param name="tep"></param>
        public static void Alert(string tep)
        {
            HttpContext.Current.Response.Write("<script>alert('" + tep + "');</script>");
        }
        /// <summary>
        /// 获取用户 IP
        /// </summary>
        /// <returns></returns>
        public static string GetUserIp()
        {
            string ip;
            string[] temp;
            bool isErr = false;
            if (HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"] == null)
                ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            else
                ip = HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"].ToString();
            if (ip.Length > 15)
                isErr = true;
            else
            {
                temp = ip.Split('.');
                if (temp.Length == 4)
                {
                    for (int i = 0; i < temp.Length; i++)
                    {
                        if (temp[i].Length > 3) isErr = true;
                    }
                }
                else
                    isErr = true;
            }

            if (isErr)
                return "1.1.1.1";
            else
                return ip;
        }
        /// <summary>
        /// 格式化 用户 IP
        /// </summary>
        /// <param name="ipStr"></param>
        /// <returns></returns>
        public static string FormatIp(string ipStr)
        {
            //拆解 分段 补零 足三
            string[] temp = ipStr.Split('.');
            string format = "";
            for (int i = 0; i < temp.Length; i++)
            {
                if (temp[i].Length < 3) temp[i] = Convert.ToString("000" + temp[i]).Substring(Convert.ToString("000" + temp[i]).Length - 3, 3);
                format += temp[i].ToString();
            }
            return format;
        }
        /// <summary>
        /// 历史记录跳转
        /// </summary>
        /// <param name="i"></param>
        public static void HistoryGo(int i)
        {
            HttpContext.Current.Response.Write("<script>history.go(" + i + ");</script>");
        }
        /// <summary>
        /// 弹出 提示
        /// </summary>
        /// <param name="tep"></param>
        public static void Alert(string msg, string path)
        {
            HttpContext.Current.Response.Write("<script>alert(\"" + msg + "\");location.href=\"" + path + "\";</script>");
        }
    }
}
