You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

694 lines
23 KiB

  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Data;
  5. using System.Configuration;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. using System.Text.RegularExpressions;
  13. namespace basic
  14. {
  15. public class Utils
  16. {
  17. /// <summary>
  18. /// ��ȡAppSeting�ַ�������
  19. /// </summary>
  20. /// <param name="Str">��Ҫ��ȡ���ַ���</param>
  21. /// <returns></returns>
  22. public static string GetAppSeting(string key)
  23. {
  24. if (key == null || key == "")
  25. return "";
  26. string outstr = "";
  27. outstr = System.Configuration.ConfigurationManager.AppSettings[key];
  28. if (outstr == null || outstr == "")
  29. return "";
  30. return outstr;
  31. }
  32. /// <summary>
  33. /// ����ת��UnicodeToGB���磬\u548c�������ĺ���
  34. /// </summary>
  35. /// <param name="content">��������</param>
  36. /// <returns>��������</returns>
  37. private string UnicodeToGB(string content)
  38. {
  39. Regex objRegex = new Regex("&#(?<UnicodeCode>[\\d]{5});", RegexOptions.IgnoreCase);
  40. Match objMatch = objRegex.Match(content);
  41. System.Text.StringBuilder sb = new System.Text.StringBuilder(content);
  42. while (objMatch.Success)
  43. {
  44. string code = Convert.ToString(Convert.ToInt32(objMatch.Result("${UnicodeCode}")), 16);
  45. byte[] array = new byte[2];
  46. array[0] = (byte)Convert.ToInt32(code.Substring(2), 16);
  47. array[1] = (byte)Convert.ToInt32(code.Substring(0, 2), 16);
  48. sb.Replace(objMatch.Value, System.Text.Encoding.Unicode.GetString(array));
  49. objMatch = objMatch.NextMatch();
  50. }
  51. return sb.ToString();
  52. }
  53. /// <summary>
  54. /// ��ȡ�ַ�����λ��,λ����������1
  55. /// </summary>
  56. public static string getstr(string str1, int leng)
  57. {
  58. string tmp = str1;
  59. if (leng > 0)
  60. {
  61. if (tmp.Length > leng)
  62. {
  63. tmp.Remove(leng);
  64. tmp = tmp + "...";
  65. }
  66. }
  67. return tmp;
  68. }
  69. /// <summary>
  70. /// ��ȡ�ַ�������
  71. /// </summary>
  72. /// <param name="Str">��Ҫ��ȡ���ַ���</param>
  73. /// <param name="Num">��ȡ�ַ����ij���</param>
  74. /// <returns></returns>
  75. public static string GetSubString(string Str, int Num)
  76. {
  77. if (Str == null || Str == "")
  78. return "";
  79. string outstr = "";
  80. int n = 0;
  81. foreach (char ch in Str)
  82. {
  83. n += System.Text.Encoding.Default.GetByteCount(ch.ToString());
  84. if (n > Num)
  85. break;
  86. else
  87. outstr += ch;
  88. }
  89. return outstr;
  90. }
  91. /// <summary>
  92. /// ��ȡ�ַ�������
  93. /// </summary>
  94. /// <param name="Str">��Ҫ��ȡ���ַ���</param>
  95. /// <param name="Num">��ȡ�ַ����ij���</param>
  96. /// <param name="Num">��ȡ�ַ�����ʡ�Բ��ֵ��ַ���</param>
  97. /// <returns></returns>
  98. public static string GetSubString(string Str, int Num, string LastStr)
  99. {
  100. return (Str.Length > Num) ? Str.Substring(0, Num) + LastStr : Str;
  101. }
  102. /// <summary>
  103. /// MD5�����ַ�������
  104. /// </summary>
  105. /// <param name="Half">������16λ����32λ������ΪtrueΪ16λ</param>
  106. /// <param name="Input">���������ַ���</param>
  107. /// <returns></returns>
  108. public static string MD5(string Input, bool Half)
  109. {
  110. string output = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Input, "MD5").ToLower();
  111. if (Half)//16λMD5���ܣ�ȡ32λ���ܵ�9~25�ַ���
  112. output = output.Substring(8, 16);
  113. return output;
  114. }
  115. public static string MD5(string Input)
  116. {
  117. return MD5(Input, true);
  118. }
  119. /// <summary>
  120. /// ����Sql
  121. /// </summary>
  122. /// <param name="html"></param>
  123. /// <returns></returns>
  124. public static string FilterSql(string sql)
  125. {
  126. sql = sql.Replace("'", "''");
  127. return sql;
  128. }
  129. /// <summary>
  130. /// �����û������Ƿ�����
  131. /// </summary>
  132. /// <param name="Str">�����û��ύ���� </param>
  133. /// <returns>�����Ƿ�����SQLע��ʽ�������� </returns>
  134. private bool ProcessSqlStr(string Str)
  135. {
  136. bool ReturnValue = true;
  137. try
  138. {
  139. if (Str.Trim() != "")
  140. {
  141. string SqlStr = "and |exec |insert |select |delete |update |count |* |chr |mid |master |truncate |char |declare";
  142. string[] anySqlStr = SqlStr.Split('|');
  143. foreach (string ss in anySqlStr)
  144. {
  145. if (Str.ToLower().IndexOf(ss) >= 0)
  146. {
  147. ReturnValue = false;
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. catch
  154. {
  155. ReturnValue = false;
  156. }
  157. return ReturnValue;
  158. }
  159. /// <summary>
  160. /// ִ��һ��JS����
  161. /// </summary>
  162. /// <param name="sentence">Ҫִ�е�����</param>
  163. public static void ExecuteJs(string sentence)
  164. {
  165. HttpContext.Current.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
  166. HttpContext.Current.Response.Write(sentence);
  167. HttpContext.Current.Response.Write("</script>");
  168. }
  169. /// <summary>
  170. /// �ж϶����Ƿ�ΪInt32���͵�����
  171. /// </summary>
  172. /// <param name="Expression"></param>
  173. /// <returns></returns>
  174. public static bool IsNumeric(object expression)
  175. {
  176. if (expression != null)
  177. {
  178. return IsNumeric(expression.ToString());
  179. }
  180. return false;
  181. }
  182. /// <summary>
  183. /// �ж϶����Ƿ�ΪInt32���͵�����
  184. /// </summary>
  185. /// <param name="Expression"></param>
  186. /// <returns></returns>
  187. public static bool IsNumeric(string expression)
  188. {
  189. if (expression != null)
  190. {
  191. string str = expression;
  192. if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$"))
  193. {
  194. if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
  195. {
  196. return true;
  197. }
  198. }
  199. }
  200. return false;
  201. }
  202. /// <summary>
  203. /// �Ƿ�ΪDouble����
  204. /// </summary>
  205. /// <param name="expression"></param>
  206. /// <returns></returns>
  207. public static bool IsDouble(object expression)
  208. {
  209. if (expression != null)
  210. {
  211. return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
  212. }
  213. return false;
  214. }
  215. /// <summary>
  216. /// string��ת��Ϊbool��
  217. /// </summary>
  218. /// <param name="strValue">Ҫת�����ַ���</param>
  219. /// <param name="defValue">ȱʡֵ</param>
  220. /// <returns>ת������bool���ͽ���</returns>
  221. public static bool StrToBool(object expression, bool defValue)
  222. {
  223. if (expression != null)
  224. {
  225. return StrToBool(expression, defValue);
  226. }
  227. return defValue;
  228. }
  229. /// <summary>
  230. /// string��ת��Ϊbool��
  231. /// </summary>
  232. /// <param name="strValue">Ҫת�����ַ���</param>
  233. /// <param name="defValue">ȱʡֵ</param>
  234. /// <returns>ת������bool���ͽ���</returns>
  235. public static bool StrToBool(string expression, bool defValue)
  236. {
  237. if (expression != null)
  238. {
  239. if (string.Compare(expression, "true", true) == 0)
  240. {
  241. return true;
  242. }
  243. else if (string.Compare(expression, "false", true) == 0)
  244. {
  245. return false;
  246. }
  247. }
  248. return defValue;
  249. }
  250. /// <summary>
  251. /// ������ת��ΪInt32����
  252. /// </summary>
  253. /// <param name="strValue">Ҫת�����ַ���</param>
  254. /// <param name="defValue">ȱʡֵ</param>
  255. /// <returns>ת������int���ͽ���</returns>
  256. public static int StrToInt(object expression, int defValue)
  257. {
  258. if (expression != null)
  259. {
  260. return StrToInt(expression.ToString(), defValue);
  261. }
  262. return defValue;
  263. }
  264. /// <summary>
  265. /// ������ת��ΪInt32����
  266. /// </summary>
  267. /// <param name="str">Ҫת�����ַ���</param>
  268. /// <param name="defValue">ȱʡֵ</param>
  269. /// <returns>ת������int���ͽ���</returns>
  270. public static int StrToInt(string str, int defValue)
  271. {
  272. if (str == null)
  273. return defValue;
  274. if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*$"))
  275. {
  276. if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1'))
  277. {
  278. return Convert.ToInt32(str);
  279. }
  280. }
  281. return defValue;
  282. }
  283. /// <summary>
  284. /// string��ת��Ϊfloat��
  285. /// </summary>
  286. /// <param name="strValue">Ҫת�����ַ���</param>
  287. /// <param name="defValue">ȱʡֵ</param>
  288. /// <returns>ת������int���ͽ���</returns>
  289. public static float StrToFloat(object strValue, float defValue)
  290. {
  291. if ((strValue == null))
  292. {
  293. return defValue;
  294. }
  295. return StrToFloat(strValue.ToString(), defValue);
  296. }
  297. /// <summary>
  298. /// string��ת��Ϊfloat��
  299. /// </summary>
  300. /// <param name="strValue">Ҫת�����ַ���</param>
  301. /// <param name="defValue">ȱʡֵ</param>
  302. /// <returns>ת������int���ͽ���</returns>
  303. public static float StrToFloat(string strValue, float defValue)
  304. {
  305. if ((strValue == null) || (strValue.Length > 10))
  306. {
  307. return defValue;
  308. }
  309. float intValue = defValue;
  310. if (strValue != null)
  311. {
  312. bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
  313. if (IsFloat)
  314. {
  315. intValue = Convert.ToSingle(strValue);
  316. }
  317. }
  318. return intValue;
  319. }
  320. /// <summary>
  321. /// �жϸ������ַ�������(strNumber)�е������Dz��Ƕ�Ϊ��ֵ��
  322. /// </summary>
  323. /// <param name="strNumber">Ҫȷ�ϵ��ַ�������</param>
  324. /// <returns>���򷵼�true �����򷵻� false</returns>
  325. public static bool IsNumericArray(string[] strNumber)
  326. {
  327. if (strNumber == null)
  328. {
  329. return false;
  330. }
  331. if (strNumber.Length < 1)
  332. {
  333. return false;
  334. }
  335. foreach (string id in strNumber)
  336. {
  337. if (!IsNumeric(id))
  338. {
  339. return false;
  340. }
  341. }
  342. return true;
  343. }
  344. /// <summary>
  345. /// дcookieֵ
  346. /// </summary>
  347. /// <param name="strName">����</param>
  348. /// <param name="strValue">ֵ</param>
  349. public static void WriteCookie(string strName, string strValue)
  350. {
  351. HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
  352. if (cookie == null)
  353. {
  354. cookie = new HttpCookie(strName);
  355. }
  356. cookie.Value = strValue;
  357. HttpContext.Current.Response.AppendCookie(cookie);
  358. }
  359. /// <summary>
  360. /// дcookieֵ
  361. /// </summary>
  362. /// <param name="strName">����</param>
  363. /// <param name="strValue">ֵ</param>
  364. public static void WriteCookie(string strName, string key, string strValue)
  365. {
  366. HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
  367. if (cookie == null)
  368. {
  369. cookie = new HttpCookie(strName);
  370. }
  371. cookie[key] = strValue;
  372. HttpContext.Current.Response.AppendCookie(cookie);
  373. }
  374. /// <summary>
  375. /// дcookieֵ
  376. /// </summary>
  377. /// <param name="strName">����</param>
  378. /// <param name="strValue">ֵ</param>
  379. /// <param name="strValue">����ʱ��(����)</param>
  380. public static void WriteCookie(string strName, string strValue, int expires)
  381. {
  382. HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
  383. if (cookie == null)
  384. {
  385. cookie = new HttpCookie(strName);
  386. }
  387. cookie.Value = strValue;
  388. cookie.Expires = DateTime.Now.AddMinutes(expires);
  389. HttpContext.Current.Response.AppendCookie(cookie);
  390. }
  391. /// <summary>
  392. /// ��cookieֵ
  393. /// </summary>
  394. /// <param name="strName">����</param>
  395. /// <returns>cookieֵ</returns>
  396. public static string GetCookie(string strName)
  397. {
  398. if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
  399. {
  400. return HttpContext.Current.Request.Cookies[strName].Value.ToString();
  401. }
  402. return "";
  403. }
  404. /// <summary>
  405. /// ��cookieֵ
  406. /// </summary>
  407. /// <param name="strName">����</param>
  408. /// <returns>cookieֵ</returns>
  409. public static string GetCookie(string strName, string key)
  410. {
  411. if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
  412. {
  413. return HttpContext.Current.Request.Cookies[strName][key].ToString();
  414. }
  415. return "";
  416. }
  417. /// <summary>
  418. /// ���õ�ǰҳ���ͻ��˵�IP
  419. /// </summary>
  420. /// <returns>��ǰҳ���ͻ��˵�IP</returns>
  421. public static string GetIP()
  422. {
  423. string result = String.Empty;
  424. result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  425. if (string.IsNullOrEmpty(result))
  426. {
  427. result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  428. }
  429. if (string.IsNullOrEmpty(result))
  430. {
  431. result = HttpContext.Current.Request.UserHostAddress;
  432. }
  433. if (result == "127.0.0.1")
  434. {
  435. result = HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"];
  436. if (result == "")
  437. { result = "127.0.0.1"; }
  438. }
  439. return result;
  440. }
  441. /// <summary>
  442. /// �Ƿ�Ϊip
  443. /// </summary>
  444. /// <param name="ip"></param>
  445. /// <returns></returns>
  446. public static bool IsIP(string ip)
  447. {
  448. return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
  449. }
  450. /// <summary>
  451. /// �ж��Ƿ�ʱ����ʽ
  452. /// </summary>
  453. /// <param name="str"></param>
  454. /// <returns></returns>
  455. public static bool IsTime(string str)
  456. {
  457. bool bol = false;
  458. DateTime Dt = new DateTime();
  459. if (DateTime.TryParse(str, out Dt))
  460. {
  461. bol = true;
  462. }
  463. else
  464. {
  465. bol = false;
  466. }
  467. return bol;
  468. }
  469. /// <summary>
  470. /// �жϵ�ǰҳ���Ƿ����յ���Post����
  471. /// </summary>
  472. /// <returns>�Ƿ����յ���Post����</returns>
  473. public static bool IsPost()
  474. {
  475. return HttpContext.Current.Request.HttpMethod.Equals("POST");
  476. }
  477. /// <summary>
  478. /// �жϵ�ǰҳ���Ƿ����յ���Get����
  479. /// </summary>
  480. /// <returns>�Ƿ����յ���Get����</returns>
  481. public static bool IsGet()
  482. {
  483. return HttpContext.Current.Request.HttpMethod.Equals("GET");
  484. }
  485. /// <summary>
  486. /// ����ָ������������ֵ
  487. /// </summary>
  488. /// <param name="strName">��������</param>
  489. /// <returns>����������ֵ</returns>
  490. public static string GetFileFullPath(string strName)
  491. {
  492. if (HttpContext.Current.Server.MapPath(strName) == null)
  493. {
  494. return "";
  495. }
  496. return HttpContext.Current.Server.MapPath(strName);
  497. }
  498. /// <summary>
  499. /// �滻�ַ����еĿո�
  500. /// </summary>
  501. /// <param name="strName"></param>
  502. /// <returns></returns>
  503. public static string ReplaceSpace(string strName)
  504. {
  505. return strName.Replace(" ", "-").Replace("&", "-");
  506. }
  507. /// <summary>
  508. /// ����ָ������������ֵ
  509. /// </summary>
  510. /// <param name="strName">��������</param>
  511. /// <returns>����������ֵ</returns>
  512. public static string GetFormString(string strName)
  513. {
  514. if (HttpContext.Current.Request.Form[strName] == null)
  515. {
  516. return "";
  517. }
  518. return HttpContext.Current.Request.Form[strName];
  519. }
  520. /// <summary>
  521. /// ����ָ��Url������ֵ
  522. /// </summary>
  523. /// <param name="strName">Url����</param>
  524. /// <returns>Url������ֵ</returns>
  525. public static string GetQueryString(string strName)
  526. {
  527. if (HttpContext.Current.Request.QueryString[strName] == null)
  528. {
  529. return "";
  530. }
  531. return HttpContext.Current.Request.QueryString[strName];
  532. }
  533. /// <summary>
  534. /// ���õ�ǰ����Url��ַ
  535. /// </summary>
  536. /// <returns>��ǰ����Url��ַ</returns>
  537. public static string GetUrl()
  538. {
  539. return HttpContext.Current.Request.Url.ToString();
  540. }
  541. /// <summary>
  542. /// ������һ��ҳ���ĵ�ַ
  543. /// </summary>
  544. /// <returns>��һ��ҳ���ĵ�ַ</returns>
  545. public static string GetUrlReferrer()
  546. {
  547. string retVal = null;
  548. try
  549. {
  550. retVal = HttpContext.Current.Request.UrlReferrer.ToString();
  551. }
  552. catch { }
  553. if (retVal == null)
  554. return "";
  555. return retVal;
  556. }
  557. /// <summary>
  558. /// ����html��ǩ
  559. /// </summary>
  560. /// <param name="html"></param>
  561. /// <returns></returns>
  562. public static string FilterHtmlStr(string html)
  563. {
  564. System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  565. System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  566. System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  567. System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  568. System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  569. System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  570. System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  571. System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  572. System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  573. html = regex1.Replace(html, ""); //����<script></script>����
  574. html = regex2.Replace(html, ""); //����href=javascript: (<A>) ����
  575. html = regex3.Replace(html, " _disibledevent="); //���������ؼ���on...�¼�
  576. html = regex4.Replace(html, ""); //����iframe
  577. html = regex5.Replace(html, ""); //����frameset
  578. html = regex6.Replace(html, ""); //����frameset
  579. html = regex7.Replace(html, ""); //����frameset
  580. html = regex8.Replace(html, ""); //����frameset
  581. html = regex9.Replace(html, "");
  582. //html = html.Replace(" ", "");
  583. html = html.Replace("</strong>", "");
  584. html = html.Replace("<strong>", "");
  585. return html;
  586. }
  587. /// <summary>
  588. /// URL·������
  589. /// </summary>
  590. /// <returns></returns>
  591. public static string UrlEncode(string strHtml)
  592. {
  593. return HttpUtility.UrlEncode(strHtml, Encoding.Default);
  594. }
  595. /// <summary>
  596. /// URL·������
  597. /// </summary>
  598. /// <returns></returns>
  599. public static string UrlDecode(string strHtml)
  600. {
  601. return HttpUtility.UrlDecode(strHtml, Encoding.Default);
  602. }
  603. ///// <summary>
  604. ///// URL·������
  605. ///// </summary>
  606. ///// <returns></returns>
  607. //public static string UrlPathEncode(string strHtml)
  608. //{
  609. // return HttpUtility.UrlPathEncode(strHtml);
  610. //}
  611. }
  612. }