|
|
using System;
|
using System.Security.Cryptography;
|
using System.Text;
|
using System.Text.RegularExpressions;
|
using System.Web;
|
|
namespace Common
|
{
|
public class StringHelper
|
{
|
public static void AppendString(StringBuilder sb, string append)
|
{
|
StringHelper.AppendString(sb, append, ",");
|
}
|
|
public static void AppendString(StringBuilder sb, string append, string split)
|
{
|
if (sb.Length == 0)
|
{
|
sb.Append(append);
|
}
|
else
|
{
|
sb.Append(split);
|
sb.Append(append);
|
}
|
}
|
|
public static string Base64StringDecode(string input)
|
{
|
return Encoding.UTF8.GetString(Convert.FromBase64String(input));
|
}
|
|
public static string Base64StringEncode(string input)
|
{
|
return Convert.ToBase64String(Encoding.UTF8.GetBytes(input));
|
}
|
|
public static bool CheckNodePurview(string arrstr1, string arrstr2)
|
{
|
if (!string.IsNullOrEmpty(arrstr1) && !string.IsNullOrEmpty(arrstr2))
|
{
|
string[] strArray1 = arrstr1.Split(Convert.ToChar(","));
|
string[] strArray2 = arrstr2.Split(Convert.ToChar(","));
|
foreach (string str1 in strArray1)
|
{
|
foreach (string str2 in strArray2)
|
{
|
if (!string.IsNullOrEmpty(str2.Trim()) && str2.Trim() == str1.Trim())
|
return true;
|
}
|
}
|
}
|
return false;
|
}
|
|
public static string CollectionFilter(string conStr, string tagName, int fType)
|
{
|
string input = conStr;
|
switch (fType)
|
{
|
case 1:
|
return Regex.Replace(input, "<" + tagName + "([^>])*>", "", RegexOptions.IgnoreCase);
|
case 2:
|
return Regex.Replace(input, "<" + tagName + "([^>])*>.*?</" + tagName + "([^>])*>", "", RegexOptions.IgnoreCase);
|
case 3:
|
return Regex.Replace(Regex.Replace(input, "<" + tagName + "([^>])*>", "", RegexOptions.IgnoreCase), "</" + tagName + "([^>])*>", "", RegexOptions.IgnoreCase);
|
default:
|
return input;
|
}
|
}
|
|
public static string DecodeIP(long ip)
|
{
|
return (ip >> 24 & (long) byte.MaxValue).ToString() + "." + (ip >> 16 & (long) byte.MaxValue).ToString() + "." + (ip >> 8 & (long) byte.MaxValue).ToString() + "." + (ip & (long) byte.MaxValue).ToString();
|
}
|
|
public static string DecodeLockIP(string lockIP)
|
{
|
StringBuilder stringBuilder = new StringBuilder(256);
|
if (string.IsNullOrEmpty(lockIP))
|
return stringBuilder.ToString();
|
try
|
{
|
string str1 = lockIP;
|
string[] separator1 = new string[1]
|
{
|
"$$$"
|
};
|
int num1 = 1;
|
foreach (string str2 in str1.Split(separator1, (StringSplitOptions) num1))
|
{
|
string[] separator2 = new string[1]
|
{
|
"----"
|
};
|
int num2 = 1;
|
string[] strArray = str2.Split(separator2, (StringSplitOptions) num2);
|
stringBuilder.Append(StringHelper.DecodeIP(Convert.ToInt64(strArray[0])) + "----" + StringHelper.DecodeIP(Convert.ToInt64(strArray[1])) + "\n");
|
}
|
return stringBuilder.ToString().TrimEnd('\n');
|
}
|
catch (IndexOutOfRangeException)
|
{
|
return stringBuilder.ToString();
|
}
|
}
|
|
public static double EncodeIP(string sip)
|
{
|
if (string.IsNullOrEmpty(sip))
|
return 0.0;
|
string[] strArray = sip.Split('.');
|
long num = 0L;
|
foreach (string s in strArray)
|
{
|
byte result;
|
if (!byte.TryParse(s, out result))
|
return 0.0;
|
num = num << 8 | (long) result;
|
}
|
return (double) num;
|
}
|
|
public static string EncodeLockIP(string ipList)
|
{
|
StringBuilder stringBuilder = new StringBuilder(256);
|
if (!string.IsNullOrEmpty(ipList.Trim()))
|
{
|
string[] strArray1 = ipList.Split('\n');
|
for (int index = 0; index < strArray1.Length; ++index)
|
{
|
if (!string.IsNullOrEmpty(strArray1[index]) && strArray1[index].Contains("----"))
|
{
|
string[] strArray2 = strArray1[index].Split(new string[1]
|
{
|
"----"
|
}, StringSplitOptions.RemoveEmptyEntries);
|
if (strArray2.Length < 2)
|
throw new ArgumentException("请填写正确网站黑白名单中的IP地址!");
|
if (!DataValidate.IsIP(strArray2[0]) || !DataValidate.IsIP(strArray2[1]))
|
throw new ArgumentException("请填写正确网站黑白名单中的IP地址!");
|
if (index == 0)
|
stringBuilder.Append( StringHelper.EncodeIP(strArray2[0]) + "----" + StringHelper.EncodeIP(strArray2[1]));
|
else
|
stringBuilder.Append(string.Concat(new object[4]
|
{
|
(object) "$$$",
|
(object) StringHelper.EncodeIP(strArray2[0]),
|
(object) "----",
|
(object) StringHelper.EncodeIP(strArray2[1])
|
}));
|
}
|
}
|
}
|
return stringBuilder.ToString();
|
}
|
|
public static string FilterScript(string conStr, string filterItem)
|
{
|
string str1 = conStr.Replace("\r", "{$Chr13}").Replace("\n", "{$Chr10}");
|
string str2 = filterItem;
|
char[] separator = new char[1]
|
{
|
','
|
};
|
int num = 1;
|
foreach (string tagName in str2.Split(separator, (StringSplitOptions) num))
|
{
|
switch (tagName)
|
{
|
case "Iframe":
|
str1 = StringHelper.CollectionFilter(str1, tagName, 2);
|
break;
|
case "Object":
|
str1 = StringHelper.CollectionFilter(str1, tagName, 2);
|
break;
|
case "Script":
|
str1 = StringHelper.CollectionFilter(str1, tagName, 2);
|
break;
|
case "Style":
|
str1 = StringHelper.CollectionFilter(str1, tagName, 2);
|
break;
|
case "Div":
|
str1 = StringHelper.CollectionFilter(str1, tagName, 3);
|
break;
|
case "Span":
|
str1 = StringHelper.CollectionFilter(str1, tagName, 3);
|
break;
|
case "Table":
|
str1 = StringHelper.CollectionFilter(StringHelper.CollectionFilter(StringHelper.CollectionFilter(StringHelper.CollectionFilter(StringHelper.CollectionFilter(str1, tagName, 3), "Tbody", 3), "Tr", 3), "Td", 3), "Th", 3);
|
break;
|
case "Img":
|
str1 = StringHelper.CollectionFilter(str1, tagName, 1);
|
break;
|
case "Font":
|
str1 = StringHelper.CollectionFilter(str1, tagName, 3);
|
break;
|
case "A":
|
str1 = StringHelper.CollectionFilter(str1, tagName, 3);
|
break;
|
case "Html":
|
str1 = StringHelper.StripTags(str1);
|
break;
|
}
|
}
|
return str1.Replace("{$Chr13}", "\r").Replace("{$Chr10}", "\n");
|
}
|
|
public static bool FoundCharInArr(string checkStr, string findStr)
|
{
|
return StringHelper.FoundCharInArr(checkStr, findStr, ",");
|
}
|
|
public static bool FoundCharInArr(string checkStr, string findStr, string split)
|
{
|
bool flag = false;
|
if (string.IsNullOrEmpty(split))
|
split = ",";
|
if (string.IsNullOrEmpty(checkStr))
|
return false;
|
if (checkStr.IndexOf(split) != -1)
|
{
|
if (findStr.IndexOf(split) != -1)
|
{
|
string[] strArray1 = checkStr.Split(Convert.ToChar(split));
|
string[] strArray2 = findStr.Split(Convert.ToChar(split));
|
foreach (string strA in strArray1)
|
{
|
foreach (string strB in strArray2)
|
{
|
if (string.Compare(strA, strB) == 0)
|
{
|
flag = true;
|
break;
|
}
|
}
|
if (flag)
|
return flag;
|
}
|
return flag;
|
}
|
string str = checkStr;
|
char[] chArray = new char[1]
|
{
|
Convert.ToChar(split)
|
};
|
foreach (string strA in str.Split(chArray))
|
{
|
if (string.Compare(strA, findStr) == 0)
|
return true;
|
}
|
return flag;
|
}
|
if (string.Compare(checkStr, findStr) == 0)
|
flag = true;
|
return flag;
|
}
|
|
public static bool FoundStringInArr(string arr, string toFind, char separator)
|
{
|
if (arr.IndexOf(separator) >= 0)
|
{
|
string[] strArray = arr.Split('|');
|
for (int index = 0; index < strArray.Length; ++index)
|
{
|
if (toFind.ToLower().IndexOf(strArray[index].ToLower()) >= 0 && strArray[index].ToLower() != "")
|
return true;
|
}
|
}
|
else if (toFind.ToLower().IndexOf(arr.ToLower()) >= 0 && arr.ToLower() != "")
|
return true;
|
return false;
|
}
|
|
public static string[] SplitString(string strContent, string strSplit)
|
{
|
strContent.IndexOf(strSplit);
|
if (strContent.IndexOf(strSplit) >= 0)
|
return Regex.Split(strContent, strSplit.Replace(".", "\\."));
|
return new string[1]
|
{
|
strContent
|
};
|
}
|
|
public static string NullToSting(string returnStr)
|
{
|
if (string.IsNullOrEmpty(returnStr))
|
return "";
|
return returnStr;
|
}
|
|
private static string GetGbkX(string testTxt)
|
{
|
if (testTxt.CompareTo("吖") >= 0)
|
{
|
if (testTxt.CompareTo("八") < 0)
|
return "A";
|
if (testTxt.CompareTo("嚓") < 0)
|
return "B";
|
if (testTxt.CompareTo("咑") < 0)
|
return "C";
|
if (testTxt.CompareTo("妸") < 0)
|
return "D";
|
if (testTxt.CompareTo("发") < 0)
|
return "E";
|
if (testTxt.CompareTo("旮") < 0)
|
return "F";
|
if (testTxt.CompareTo("铪") < 0)
|
return "G";
|
if (testTxt.CompareTo("讥") < 0)
|
return "H";
|
if (testTxt.CompareTo("咔") < 0)
|
return "J";
|
if (testTxt.CompareTo("垃") < 0)
|
return "K";
|
if (testTxt.CompareTo("嘸") < 0)
|
return "L";
|
if (testTxt.CompareTo("拏") < 0)
|
return "M";
|
if (testTxt.CompareTo("噢") < 0)
|
return "N";
|
if (testTxt.CompareTo("妑") < 0)
|
return "O";
|
if (testTxt.CompareTo("七") < 0)
|
return "P";
|
if (testTxt.CompareTo("亽") < 0)
|
return "Q";
|
if (testTxt.CompareTo("仨") < 0)
|
return "R";
|
if (testTxt.CompareTo("他") < 0)
|
return "S";
|
if (testTxt.CompareTo("哇") < 0)
|
return "T";
|
if (testTxt.CompareTo("夕") < 0)
|
return "W";
|
if (testTxt.CompareTo("丫") < 0)
|
return "X";
|
if (testTxt.CompareTo("帀") < 0)
|
return "Y";
|
if (testTxt.CompareTo("咗") < 0)
|
return "Z";
|
}
|
return testTxt;
|
}
|
|
public static string GetInitial(string str)
|
{
|
StringBuilder stringBuilder = new StringBuilder();
|
for (int startIndex = 0; startIndex < str.Length; ++startIndex)
|
stringBuilder.Append(StringHelper.GetOneIndex(str.Substring(startIndex, 1)));
|
return stringBuilder.ToString();
|
}
|
|
private static string GetOneIndex(string testTxt)
|
{
|
if ((int) Convert.ToChar(testTxt) >= 0 && (int) Convert.ToChar(testTxt) < 256)
|
return testTxt;
|
return StringHelper.GetGbkX(testTxt);
|
}
|
|
public static string MD5(string str)
|
{
|
byte[] hash = new MD5CryptoServiceProvider().ComputeHash(Encoding.Default.GetBytes(str));
|
string str1 = "";
|
for (int index = 0; index < hash.Length; ++index)
|
str1 += hash[index].ToString("x").PadLeft(2, '0');
|
return str1;
|
}
|
|
public static string MD5gb2312(string input)
|
{
|
using (MD5CryptoServiceProvider cryptoServiceProvider = new MD5CryptoServiceProvider())
|
return BitConverter.ToString(cryptoServiceProvider.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(input))).Replace("-", "").ToLower();
|
}
|
|
public static string RemoveXss(string input)
|
{
|
string str1;
|
do
|
{
|
str1 = input;
|
input = Regex.Replace(input, "(&#*\\w+)[\\x00-\\x20]+;", "$1;");
|
input = Regex.Replace(input, "(&#x*[0-9A-F]+);*", "$1;", RegexOptions.IgnoreCase);
|
input = Regex.Replace(input, "&(amp|lt|gt|nbsp|quot);", "&$1;");
|
input = HttpUtility.HtmlDecode(input);
|
}
|
while (str1 != input);
|
string str2;
|
do
|
{
|
str2 = input;
|
input = Regex.Replace(input, "(<[^>]+style[\\x00-\\x20]*=[\\x00-\\x20]*[^>]*?)\\\\([^>]*>)", "$1/$2", RegexOptions.IgnoreCase);
|
}
|
while (str2 != input);
|
input = Regex.Replace(input, "[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x19]", "");
|
input = Regex.Replace(input, "(<[^>]+[\\x00-\\x20\"'/])(on|xmlns)[^>]*>", "$1>", RegexOptions.IgnoreCase);
|
input = Regex.Replace(input, "([a-z]*)[\\x00-\\x20]*=[\\x00-\\x20]*([`'\"]*)[\\x00-\\x20]*j[\\x00-\\x20]*a[\\x00-\\x20]*v[\\x00-\\x20]*a[\\x00-\\x20]*s[\\x00-\\x20]*c[\\x00-\\x20]*r[\\x00-\\x20]*i[\\x00-\\x20]*p[\\x00-\\x20]*t[\\x00-\\x20]*:", "$1=$2nojavascript...", RegexOptions.IgnoreCase);
|
input = Regex.Replace(input, "([a-z]*)[\\x00-\\x20]*=[\\x00-\\x20]*([`'\"]*)[\\x00-\\x20]*v[\\x00-\\x20]*b[\\x00-\\x20]*s[\\x00-\\x20]*c[\\x00-\\x20]*r[\\x00-\\x20]*i[\\x00-\\x20]*p[\\x00-\\x20]*t[\\x00-\\x20]*:", "$1=$2novbscript...", RegexOptions.IgnoreCase);
|
input = Regex.Replace(input, "(<[^>]+style[\\x00-\\x20]*=[\\x00-\\x20]*[^>]*?)/\\*[^>]*\\*/([^>]*>)", "$1$2", RegexOptions.IgnoreCase);
|
input = Regex.Replace(input, "(<[^>]+)style[\\x00-\\x20]*=[\\x00-\\x20]*([`'\"]*).*expression[\\x00-\\x20]*\\([^>]*>", "$1>", RegexOptions.IgnoreCase);
|
input = Regex.Replace(input, "(<[^>]+)style[\\x00-\\x20]*=[\\x00-\\x20]*([`'\"]*).*behaviour[^>]*>", "$1>", RegexOptions.IgnoreCase);
|
input = Regex.Replace(input, "(<[^>]+)style[\\x00-\\x20]*=[\\x00-\\x20]*([`'\"]*).*behavior[^>]*>", "$1>", RegexOptions.IgnoreCase);
|
input = Regex.Replace(input, "(<[^>]+)style[\\x00-\\x20]*=[\\x00-\\x20]*([`'\"]*).*s[\\x00-\\x20]*c[\\x00-\\x20]*r[\\x00-\\x20]*i[\\x00-\\x20]*p[\\x00-\\x20]*t[\\x00-\\x20]*:*[^>]*>", "$1>", RegexOptions.IgnoreCase);
|
input = Regex.Replace(input, "</*\\w+:\\w[^>]*>", "noxss");
|
string str3;
|
do
|
{
|
str3 = input;
|
input = Regex.Replace(input, "</*(applet|meta|xml|blink|link|style|script|embed|object|iframe|frame|frameset|ilayer|layer|bgsound|title|base)[^>]*>?", "no$1", RegexOptions.IgnoreCase);
|
}
|
while (str3 != input);
|
return input;
|
}
|
|
public static string ReplaceString(string SourceString, string SearchString, string ReplaceString, bool IsCaseInsensetive)
|
{
|
return Regex.Replace(SourceString, Regex.Escape(SearchString), ReplaceString, IsCaseInsensetive ? RegexOptions.IgnoreCase : RegexOptions.None);
|
}
|
|
public static string SHA1(string input)
|
{
|
using (SHA1CryptoServiceProvider cryptoServiceProvider = new SHA1CryptoServiceProvider())
|
return BitConverter.ToString(cryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(input))).Replace("-", "").ToLower();
|
}
|
|
public static string StripTags(string input)
|
{
|
return new Regex("<([^<]|\n)+?>").Replace(input, "");
|
}
|
|
public static string SubString(string demand, int length, string substitute)
|
{
|
if (Encoding.Default.GetBytes(demand).Length <= length)
|
return demand;
|
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
|
length -= Encoding.Default.GetBytes(substitute).Length;
|
int num = 0;
|
StringBuilder stringBuilder = new StringBuilder();
|
byte[] bytes = asciiEncoding.GetBytes(demand);
|
for (int startIndex = 0; startIndex < bytes.Length; ++startIndex)
|
{
|
if ((int) bytes[startIndex] == 63)
|
num += 2;
|
else
|
++num;
|
if (num <= length)
|
stringBuilder.Append(demand.Substring(startIndex, 1));
|
else
|
break;
|
}
|
stringBuilder.Append(substitute);
|
return stringBuilder.ToString();
|
}
|
|
public static int GetStringLength(string str)
|
{
|
return Encoding.Default.GetBytes(str).Length;
|
}
|
|
public static string Trim(string returnStr)
|
{
|
if (!string.IsNullOrEmpty(returnStr))
|
return returnStr.Trim();
|
return string.Empty;
|
}
|
|
public static bool ValidateMD5(string password, string md5Value)
|
{
|
if (string.Compare(password, md5Value) != 0)
|
return string.Compare(password, md5Value.Substring(8, 16)) == 0;
|
return true;
|
}
|
|
public static string GetDate()
|
{
|
return DateTime.Now.ToString("yyyy-MM-dd");
|
}
|
|
public static string GetDate(string datetimestr, string replacestr)
|
{
|
if (datetimestr == null)
|
return replacestr;
|
if (datetimestr.Equals(""))
|
return replacestr;
|
try
|
{
|
datetimestr = Convert.ToDateTime(datetimestr).ToString("yyyy-MM-dd").Replace("1900-01-01", replacestr);
|
}
|
catch
|
{
|
return replacestr;
|
}
|
return datetimestr;
|
}
|
|
public static string GetTime()
|
{
|
return DateTime.Now.ToString("HH:mm:ss");
|
}
|
|
public static string GetDateTime()
|
{
|
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
}
|
|
public static string GetDateTime(int relativeday)
|
{
|
DateTime dateTime = DateTime.Now;
|
dateTime = dateTime.AddDays((double) relativeday);
|
return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
|
}
|
|
public static string GetDateTimeF()
|
{
|
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff");
|
}
|
|
public static string GetStandardDateTime(string fDateTime, string formatStr)
|
{
|
return Convert.ToDateTime(fDateTime).ToString(formatStr);
|
}
|
|
public static string GetStandardDateTime(string fDateTime)
|
{
|
return StringHelper.GetStandardDateTime(fDateTime, "yyyy-MM-dd HH:mm:ss");
|
}
|
|
public static int StrDateDiffSeconds(string Time, int Sec)
|
{
|
TimeSpan timeSpan = DateTime.Now - DateTime.Parse(Time).AddSeconds((double) Sec);
|
if (timeSpan.TotalSeconds > (double) int.MaxValue)
|
return int.MaxValue;
|
if (timeSpan.TotalSeconds < (double) int.MinValue)
|
return int.MinValue;
|
return (int) timeSpan.TotalSeconds;
|
}
|
|
public static int StrDateDiffMinutes(string time, int minutes)
|
{
|
if (time == "" || time == null)
|
return 1;
|
TimeSpan timeSpan = DateTime.Now - DateTime.Parse(time).AddMinutes((double) minutes);
|
if (timeSpan.TotalMinutes > (double) int.MaxValue)
|
return int.MaxValue;
|
if (timeSpan.TotalMinutes < (double) int.MinValue)
|
return int.MinValue;
|
return (int) timeSpan.TotalMinutes;
|
}
|
|
public DateTime DateAdd(string datepart, double number, DateTime date)
|
{
|
switch (datepart)
|
{
|
case "yy":
|
return date.AddYears((int) number);
|
case "mm":
|
return date.AddMonths((int) number);
|
case "dd":
|
return date.AddDays(number);
|
case "hh":
|
return date.AddHours(number);
|
case "mi":
|
return date.AddMinutes(number);
|
case "ss":
|
return date.AddSeconds(number);
|
default:
|
return date;
|
}
|
}
|
|
public static int StrDateDiffHours(string time, int hours)
|
{
|
if (time == "" || time == null)
|
return 1;
|
TimeSpan timeSpan = DateTime.Now - DateTime.Parse(time).AddHours((double) hours);
|
if (timeSpan.TotalHours > (double) int.MaxValue)
|
return int.MaxValue;
|
if (timeSpan.TotalHours < (double) int.MinValue)
|
return int.MinValue;
|
return (int) timeSpan.TotalHours;
|
}
|
|
public static string StrFormat(string str)
|
{
|
string str1;
|
if (str == null)
|
{
|
str1 = "";
|
}
|
else
|
{
|
str = str.Replace("\r\n", "<br />");
|
str = str.Replace("\n", "<br />");
|
str1 = str;
|
}
|
return str1;
|
}
|
|
public static string TrimEnd(string src, string end)
|
{
|
int length1 = src.Length;
|
int length2 = end.Length;
|
if (length1 < length2)
|
return src;
|
int num = 0;
|
while (num < length2 && (int) src[length1 - num - 1] == (int) end[length2 - num - 1])
|
++num;
|
return src.Substring(0, length1 - num);
|
}
|
}
|
}
|