SiteInfo.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using Castle.ActiveRecord;
  2. using NHibernate.Criterion;
  3. using System.Text;
  4. namespace BizCom
  5. {
  6. [ActiveRecord("SiteInfo")]
  7. public class SiteInfo : ComBase<SiteInfo>
  8. {
  9. private string _id = "";
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. [PrimaryKey(PrimaryKeyType.Assigned)]
  14. public string id
  15. {
  16. get { return _id; }
  17. set { _id = value; }
  18. }
  19. private string _site_title = "";
  20. /// <summary>
  21. ///
  22. /// </summary>
  23. [Property]
  24. public string site_title
  25. {
  26. get { return _site_title; }
  27. set { _site_title = value; }
  28. }
  29. private string _web_domain = "";
  30. /// <summary>
  31. ///
  32. /// </summary>
  33. [Property]
  34. public string web_domain
  35. {
  36. get { return _web_domain; }
  37. set { _web_domain = value; }
  38. }
  39. private int _open_ip_white_list = 0;
  40. /// <summary>
  41. ///
  42. /// </summary>
  43. [Property]
  44. public int open_ip_white_list
  45. {
  46. get { return _open_ip_white_list; }
  47. set { _open_ip_white_list = value; }
  48. }
  49. private string _ip_white_list = "";
  50. /// <summary>
  51. ///
  52. /// </summary>
  53. [Property]
  54. public string ip_white_list
  55. {
  56. get { return _ip_white_list; }
  57. set { _ip_white_list = value; }
  58. }
  59. private string _remark = "";
  60. /// <summary>
  61. ///
  62. /// </summary>
  63. [Property]
  64. public string remark
  65. {
  66. get { return _remark; }
  67. set { _remark = value; }
  68. }
  69. public void Update(bool isOpenIpWiteList, string ipWiteList, string remark)
  70. {
  71. SiteInfo siteInfo = new SiteInfo();
  72. siteInfo.id = "1";
  73. siteInfo.open_ip_white_list = isOpenIpWiteList ? 1 : 0;
  74. siteInfo.ip_white_list = ipWiteList;
  75. siteInfo.remark = remark;
  76. _ipWhiteList = null;
  77. Update(siteInfo);
  78. }
  79. public SiteInfo[] GetSiteInfos()
  80. {
  81. return FindAll(Expression.Eq("open_ip_white_list", 1));
  82. }
  83. public SiteInfo GetSiteInfoById(string id)
  84. {
  85. return FindFirst(Expression.Eq("id", id));
  86. }
  87. public override void Update()
  88. {
  89. base.Update();
  90. _ipWhiteList = null;
  91. }
  92. private static string[] _ipWhiteList = null;
  93. private static string[] getIpWhiteList()
  94. {
  95. if (_ipWhiteList != null)
  96. {
  97. return _ipWhiteList;
  98. }
  99. SiteInfo[] siteInfo = new SiteInfo().GetSiteInfos();
  100. for (int i = 0; i < siteInfo.Length; i++)
  101. {
  102. string ipWiteList = siteInfo[i].ip_white_list.Replace("\r\n", ",").Replace("\n", ",").Replace(" ", ",");
  103. _ipWhiteList = ipWiteList.Split(',');
  104. }
  105. return _ipWhiteList;
  106. }
  107. public static bool isPassIp(string ip)
  108. {
  109. if (string.IsNullOrWhiteSpace(ip))
  110. return false;
  111. if (ip.IndexOf("192.168.") != -1 || "127.0.0.1" == ip || "0:0:0:0:0:0:0:1" == ip)
  112. return true;
  113. //�ж��Ƿ��ڰ�������
  114. string[] ipNumList = ip.Split('.');
  115. string newIp = "";
  116. if (ipNumList.Length >= 2)
  117. {
  118. newIp = ipNumList[0] + "." + ipNumList[1] + ".*.*";
  119. }
  120. SiteInfo info = GetSiteInfoByIp(newIp);
  121. if (info != null) return true;
  122. //if (getIpWhiteList().Length == 0)
  123. // return true;
  124. //foreach (string matche in getIpWhiteList())
  125. //{
  126. // Match result = Regex.Match(ip, matche);
  127. // if (result.Length > 0)
  128. // {
  129. // return true;
  130. // }
  131. //}
  132. return false;
  133. }
  134. public static SiteInfo GetSiteInfoByIp(string ip)
  135. {
  136. return FindFirst(Expression.Sql(string.Format("ip_white_list like '%{0}%'", ip)));
  137. }
  138. public static void DelById(object id)
  139. {
  140. StringBuilder sql = new StringBuilder();
  141. sql.AppendFormat("delete from SiteInfo where id='" + id + "'");
  142. ExecuteNonQuery(sql.ToString());
  143. }
  144. }
  145. }