1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
 
 
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);", "&amp;$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);
    }
  }
}