index.aspx.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*******************************************************************************
  2. * iNethinkCMS - 网站内容管理系统
  3. * Copyright (C) 2012-2013 inethink.com
  4. *
  5. * @author jackyang <69991000@qq.com>
  6. * @website http://cms.inethink.com
  7. * @version 1.3.6.0 (2013-08-14)
  8. *
  9. * This is licensed under the GNU LGPL, version 3.0 or later.
  10. * For details, see: http://www.gnu.org/licenses/gpl-3.0.html
  11. *******************************************************************************/
  12. using System;
  13. using System.Data;
  14. using System.Collections.Generic;
  15. using System.Web;
  16. using System.Web.UI;
  17. using System.Web.UI.WebControls;
  18. using System.Text.RegularExpressions;
  19. using iNethinkCMS.Web.UI;
  20. using iNethinkCMS.Helper;
  21. namespace iNethinkCMS.Web.plugs.search
  22. {
  23. public partial class index : BasePage
  24. {
  25. iNethinkCMS.BLL.BLL_iNethinkCMS_Content bll = new iNethinkCMS.BLL.BLL_iNethinkCMS_Content();
  26. iNethinkCMS.Model.Model_iNethinkCMS_Content model = new iNethinkCMS.Model.Model_iNethinkCMS_Content();
  27. private string vXmlPath = @"/plugs/search/setting.xml";
  28. private string vState;
  29. private string vTemplatepath;
  30. private string vKeywordlengthMin;
  31. private string vKeywordlengthMax;
  32. private string vSearchMode;
  33. protected void Page_Load(object sender, EventArgs e)
  34. {
  35. vState = XMLHelper.GetXmlAttribute(Server.MapPath(vXmlPath), "//plugs//config//key[@name=\"state\"]", "value").Value.Trim();
  36. vTemplatepath = XMLHelper.GetXmlAttribute(Server.MapPath(vXmlPath), "//plugs//config//key[@name=\"templatepath\"]", "value").Value.Trim();
  37. vKeywordlengthMin = XMLHelper.GetXmlAttribute(Server.MapPath(vXmlPath), "//plugs//config//key[@name=\"keywordlengthmin\"]", "value").Value.Trim();
  38. vKeywordlengthMax = XMLHelper.GetXmlAttribute(Server.MapPath(vXmlPath), "//plugs//config//key[@name=\"keywordlengthmax\"]", "value").Value.Trim();
  39. vSearchMode = XMLHelper.GetXmlAttribute(Server.MapPath(vXmlPath), "//plugs//config//key[@name=\"searchmode\"]", "value").Value.Trim();
  40. //判断是否开启
  41. if (vState == "0")
  42. {
  43. Response.Write("<H3>搜索功能尚未开启!</H3>");
  44. Response.End();
  45. }
  46. string vKeyWord;
  47. vKeyWord = Request.QueryString["skeyword"];
  48. if (vKeyWord == null) { vKeyWord = ""; }
  49. vKeyWord = Command.Command_Validate.SqlTextClear(vKeyWord);
  50. string rPage;
  51. rPage = Request.QueryString["page"];
  52. int vPage = 1;
  53. if (rPage != string.Empty && rPage != null && Command.Command_Validate.IsNumber(rPage))
  54. {
  55. vPage = Convert.ToInt32(rPage);
  56. }
  57. string vHtml = Fun_GetSearchContent(vPage, vKeyWord);
  58. Response.Write(vHtml);
  59. }
  60. private string Fun_GetSearchContent(int byPage, string byKeyWord)
  61. {
  62. string vTemplateUrl = vTemplatepath;
  63. //是否调用移动端模板
  64. if (base.Request.QueryString["m"] != null && base.Request.QueryString["m"].ToString() == "1")
  65. {
  66. vTemplateUrl = Global.MobilePath + vTemplateUrl;
  67. }
  68. WebUI_Template wt = new WebUI_Template();
  69. wt.Load_Template(vTemplateUrl);
  70. wt.vPage = byPage;
  71. wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:title}"), siteConfig.WebName, RegexOptions.IgnoreCase);
  72. wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:seotitle}"), seoConfig.SeoTitle, RegexOptions.IgnoreCase);
  73. wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:keywords}"), seoConfig.IndexKeywords, RegexOptions.IgnoreCase);
  74. wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:description}"), seoConfig.IndexDescription, RegexOptions.IgnoreCase);
  75. wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{sys:sitepath}"), "<a href=\"/\">首页</a>&nbsp;>&nbsp;网站搜索", RegexOptions.IgnoreCase);
  76. //搜索专用-开始
  77. string vSqlWhere = "[Display]=1";
  78. int vCountNum = 0;
  79. string vErrorInfo = "";
  80. if (byKeyWord.Length < int.Parse(vKeywordlengthMin) || byKeyWord.Length > int.Parse(vKeywordlengthMax))
  81. {
  82. vErrorInfo = "关键字长度请控制在" + vKeywordlengthMin + "至" + vKeywordlengthMax + "位之间!";
  83. wt.vContent = Fun_GetShowContent(wt.vContent, "yesinfo");
  84. wt.vContent = Fun_GetShowContent(wt.vContent, "noinfo");
  85. }
  86. else
  87. {
  88. int vSearchModeInt = int.Parse(vSearchMode);
  89. if (vSearchModeInt >= 1)
  90. {
  91. vSqlWhere += " And [Title] Like '%" + byKeyWord + "%'";
  92. }
  93. if (vSearchModeInt >= 2)
  94. {
  95. vSqlWhere += " Or [SubTitle] Like '%" + byKeyWord + "%'";
  96. }
  97. if (vSearchModeInt >= 3)
  98. {
  99. vSqlWhere += " Or [Keywords] Like '%" + byKeyWord + "%'";
  100. }
  101. if (vSearchModeInt >= 4)
  102. {
  103. vSqlWhere += " Or [Description] Like '%" + byKeyWord + "%'";
  104. }
  105. if (vSearchModeInt >= 5)
  106. {
  107. vSqlWhere += " Or [Contents] Like '%" + byKeyWord + "%'";
  108. }
  109. DataSet ds = bll.GetList(vSqlWhere);
  110. vCountNum = ds.Tables[0].Rows.Count;
  111. //过滤掉相应的内容
  112. if (vCountNum == 0)
  113. {
  114. wt.vContent = Fun_GetShowContent(wt.vContent, "yesinfo");
  115. wt.vContent = Fun_GetShowContent(wt.vContent, "errorinfo");
  116. }
  117. else
  118. {
  119. wt.vContent = Fun_GetShowContent(wt.vContent, "noinfo");
  120. wt.vContent = Fun_GetShowContent(wt.vContent, "errorinfo");
  121. }
  122. }
  123. wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{search:keyword}"), byKeyWord, RegexOptions.IgnoreCase);
  124. wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{search:countnum}"), vCountNum.ToString(), RegexOptions.IgnoreCase);
  125. wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{search:error}"), vErrorInfo, RegexOptions.IgnoreCase);
  126. wt.vContent = Regex.Replace(wt.vContent, Regex.Escape("{search:sqlwhere}"), vSqlWhere, RegexOptions.IgnoreCase);
  127. //搜索专用-结束
  128. wt.Parser_MyTag();
  129. wt.Parser_List();
  130. wt.Parser_Page();
  131. wt.Parser_IF();
  132. return wt.vContent;
  133. }
  134. private string Fun_GetShowContent(string byContent, string byTagsstr)
  135. {
  136. Regex regex = new Regex(@"<!--(search:" + byTagsstr + @")-->([\s\S]*?)<!--search:" + byTagsstr + "-->", RegexOptions.IgnoreCase);
  137. MatchCollection matchCollection = regex.Matches(byContent);
  138. foreach (Match m in matchCollection)
  139. {
  140. byContent = byContent.Replace(m.Value, "");
  141. }
  142. return byContent;
  143. }
  144. }
  145. }