Esempio n. 1
0
        /// <summary>
        /// IP 地址字符串形式转换成长整型
        /// </summary>
        public static long Ip2Int(string ip)
        {
            if (!RegExp.IsIp(ip))
            {
                return(-1);
            }
            string[] arr = ip.Split('.');
            long     lng = long.Parse(arr[0]) * 16777216;

            lng += int.Parse(arr[1]) * 65536;
            lng += int.Parse(arr[2]) * 256;
            lng += int.Parse(arr[3]);
            return(lng);
        }
Esempio n. 2
0
        /// <summary>
        /// 截断字符串,如果str 的长度超过 need,则提取 str 的前 need 个字符
        /// </summary>
        public static string Left(string s, int need, bool encode, string tail)
        {
            if (s == null || s == "")
            {
                return(string.Empty);
            }

            int len = s.Length;

            if (len < need / 2)
            {
                return(encode ? TextEncode(s) : s);
            }

            int i, j, bytes = 0;

            for (i = 0; i < len; i++)
            {
                bytes += RegExp.IsUnicode(s[i].ToString()) ? 2 : 1;
                if (bytes >= need)
                {
                    break;
                }
            }

            string result = s.Substring(0, i);

            if (len > i)
            {
                for (j = 0; j < 5; j++)
                {
                    bytes -= RegExp.IsUnicode(s[i - j].ToString()) ? 2 : 1;
                    if (bytes <= need)
                    {
                        break;
                    }
                }
                result = s.Substring(0, i - j) + tail;
            }
            return(encode ? TextEncode(result) : result);
        }
Esempio n. 3
0
        /// <summary>
        /// 获取页面地址的参数值并检查安全性,相当于 Request.QueryString
        /// chkType 有 CheckGetEnum.Int, CheckGetEnum.Safety两种类型,
        /// CheckGetEnum.Int 保证参数是数字型
        /// CheckGetEnum.Safety 保证提交的参数值没有操作数据库的语句
        /// </summary>
        public static string Get(string name, CheckGetEnum chkType)
        {
            string value  = Get(name);
            bool   isPass = false;

            switch (chkType)
            {
            default:
                isPass = true;
                break;

            case CheckGetEnum.Int:
            {
                try
                {
                    int.Parse(value);
                    isPass = RegExp.IsNumeric(value);
                }
                catch
                {
                    isPass = false;
                }
                break;
            }

            case CheckGetEnum.Safety:
                isPass = RegExp.IsSafety(value);
                break;
            }
            if (!isPass)
            {
                new Terminator().Throw("地址栏中参数“" + name + "”的值不符合要求或具有潜在威胁,请不要手动修改URL。");
                return(string.Empty);
            }
            return(value);
        }