comparison Demo/Asp.Net/Web/App_Code/WebUtility.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Text.RegularExpressions;
3 using System.Web;
4 using System.Web.Caching;
5 using System.Configuration;
6
7 using PetShop.BusinessLogic;
8
9 namespace PetShop.Web
10 {
11 /// <summary>
12 /// Collection of utility methods for web tier
13 /// </summary>
14 public static class WebUtility
15 {
16 /// <summary>
17 /// Method to make sure that user's inputs are not malicious
18 /// </summary>
19 /// <param name="text">User's Input</param>
20 /// <param name="maxLength">Maximum length of input</param>
21 /// <returns>The cleaned up version of the input</returns>
22 public static string InputText(string text, int maxLength)
23 {
24 text = text.Trim();
25
26 if (string.IsNullOrEmpty(text))
27 return string.Empty;
28
29 text = Regex.Replace(text, "[\\s]{2,}", " "); // two or more spaces
30 text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); // <br>
31 text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); // &nbsp;
32 text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); // any other tags
33 text = text.Replace("'", "''");
34
35 if (text.Length > maxLength)
36 text = text.Substring(0, maxLength);
37
38 return text;
39 }
40
41 /// <summary>
42 /// Method to check whether input has other characters than numbers
43 /// </summary>
44 public static string CleanNonWord(string text)
45 {
46 return Regex.Replace(text, "\\W", "");
47 }
48
49 /// <summary>
50 /// Method to redirect user to search page
51 /// </summary>
52 /// <param name="key">Search keyword</param>
53 public static void SearchRedirect(string key)
54 {
55 HttpContext.Current.Response.Redirect(
56 string.Format("~/Search.aspx?keywords={0}", InputText(key, 255)));
57 }
58 }
59 }