/******************************************************************************* * iNethinkCMS - 网站内容管理系统 * Copyright (C) 2012-2013 inethink.com * * @author jackyang <69991000@qq.com> * @website http://cms.inethink.com * @version 1.3.6.0 (2013-08-14) * * This is licensed under the GNU LGPL, version 3.0 or later. * For details, see: http://www.gnu.org/licenses/gpl-3.0.html *******************************************************************************/ using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text.RegularExpressions; using System.Web.Security; using System.Data; using iNethinkCMS.Command; using iNethinkCMS.Web.UI; namespace iNethinkCMS.Web.admin.custom { public partial class custom_pages : Admin_BasePage { iNethinkCMS.BLL.BLL_iNethinkCMS_Custom_Pages bll = new iNethinkCMS.BLL.BLL_iNethinkCMS_Custom_Pages(); iNethinkCMS.Model.Model_iNethinkCMS_Custom_Pages model = new iNethinkCMS.Model.Model_iNethinkCMS_Custom_Pages(); iNethinkCMS.BLL.BLL_iNethinkCMS_Upload bll_upload = new iNethinkCMS.BLL.BLL_iNethinkCMS_Upload(); private string vNavInfo = "当前位置:"; private string vAct = ""; private int vPage = 1; private int vID = 0; private string vSQL = ""; private string vKeyType = ""; private string vKey = ""; private string vTitle; private string vTemplatePath; private string vDir; private string vKeywords; private string vDescription; private string vHtml; protected void Page_Load(object sender, EventArgs e) { CheckUserPower("d"); #region vAct = Request.QueryString["Act"] != null ? Request.QueryString["Act"] : ""; if (Request.QueryString["Page"] != null && Request.QueryString["Page"].Trim() != "") { if (!int.TryParse(Request.QueryString["Page"], out vPage)) { vPage = 1; } } vKeyType = Request.QueryString["sKeyType"] != null ? Request.QueryString["sKeyType"] : ""; vKey = Request.QueryString["sKey"] != null ? Request.QueryString["sKey"] : ""; vKey = vKey.Replace("'", ""); if (Request.QueryString["ID"] != null && Request.QueryString["ID"].Trim() != "") { if (!int.TryParse(Request.QueryString["ID"], out vID)) { Response.Write("ID Error"); Response.End(); } } if (vKey.Length > 0) { if (vKeyType == "ID") { if (iNethinkCMS.Command.Command_Validate.IsNumber(vKey) == true) { vSQL += vKeyType + " = " + vKey + ""; } else { vKey = ""; } } else { vSQL += vKeyType + " Like '%" + vKey + "%'"; } } #endregion this.mainID.Visible = false; this.guideID.Visible = false; this.MakeAllPageID.Visible = false; switch (vAct) { case "guide": this.guideID.Visible = true; if (vID == 0) { this.navInfoID.InnerText = vNavInfo + "自定页面添加"; this.Button_Submit.Click += new EventHandler(Button_Submit_Click_Add); } else { this.navInfoID.InnerText = vNavInfo + "自定页面修改"; this.Button_Submit.Click += new EventHandler(Button_Submit_Click_Edit); } if (!IsPostBack) { if (Request.UrlReferrer != null) { ViewState["reJumpUrl"] = Request.UrlReferrer.AbsoluteUri; } Fun_SetValue(vID); //获取frm基本信息 } break; case "delete": Fun_Delete(vID); break; case "makepage": this.searchInfoID.Visible = false; string vMsgInfo = CreateFile(vID); MessageBox.ShowAndRedirect(this, vMsgInfo, Request.UrlReferrer.AbsoluteUri); break; case "makeallpage": this.searchInfoID.Visible = false; this.navInfoID.InnerText = vNavInfo + "生成全部自定页面"; this.MakeAllPageID.Visible = true; break; case "makeallpage_do": //读取系统内所有自定义模板 int tmpCountNum = 0; DataTable dt = bll.GetList("").Tables[0]; for (int i = 0; i < dt.Rows.Count; i++) { int tmpID = (int)dt.Rows[i]["ID"]; string tmpInfo = CreateFile(tmpID); if (string.IsNullOrEmpty(tmpInfo)) { tmpCountNum++; } } Response.Write(dt.Rows.Count.ToString() + "|&&|" + tmpCountNum.ToString()); Response.End(); break; default: this.navInfoID.InnerText = vNavInfo + "自定页面管理"; this.mainID.Visible = true; if (!IsPostBack) { this.sKeyType.SelectedValue = vKeyType; this.sKey.Text = vKey; } PageListInfo(); break; } } #region mainID 列表 protected void PageListInfo() { int vPageSize = int.Parse(siteConfig.PageListNum); int vRecordCount = bll.GetRecordCount(vSQL); Repeater.DataSource = bll.GetListByPage(vSQL, "ID Desc", (vPage - 1) * vPageSize, vPage * vPageSize); Repeater.DataBind(); string pTemp = ""; int vPageCount = 1; if (vRecordCount > 0) { vPageCount = (int)Math.Ceiling((double)vRecordCount / (double)vPageSize); pTemp = WebUI_PageList.GetPagingInfo_Manage(vPageCount, vRecordCount, vPage, vPageSize); } this.pagelist.InnerHtml = pTemp; this.iNoInfo.Visible = vRecordCount == 0 ? true : false; } protected void Button_Search_Click(object sender, EventArgs e) { Response.Redirect(Request.Path + "?skeytype=" + this.sKeyType.SelectedValue + "&skey=" + this.sKey.Text); } #endregion #region guideID 信息修改/添加 private void Fun_SetValue(int byID) { if (byID == 0) { this.txtTitle.Text = ""; this.txtTemplatePath.Text = ""; this.txtDir.Text = ""; this.txtKeywords.Text = ""; this.txtDescription.Text = ""; this.txtHtml.Text = ""; } else { this.txtDir.ReadOnly = true; model = bll.GetModel(byID); this.txtTitle.Text = model.Title; this.txtTemplatePath.Text = model.TemplatePath; this.txtDir.Text = model.Dir; this.txtKeywords.Text = model.Keywords; this.txtDescription.Text = model.Description; this.txtHtml.Text = model.Html; } } private bool Fun_GetValue() { if (this.txtTitle.Text.Trim().Length == 0) { MessageBox.Show(this, "请输入页面标题!"); return false; } if (this.txtDir.Text.Trim().Length == 0) { MessageBox.Show(this, "请输入保存路径!"); return false; } if (Command.Command_StringPlus.Left(this.txtDir.Text.Trim(), 1) != "/") { MessageBox.Show(this, "保存路径输入错误,必须以“/”开头!"); return false; } vTitle = this.txtTitle.Text.Trim(); vTemplatePath = this.txtTemplatePath.Text.Trim(); vDir = this.txtDir.Text.Trim(); vKeywords = Command.Command_StringPlus.LostHTML(this.txtKeywords.Text.Trim()); vDescription = Command.Command_StringPlus.LostHTML(this.txtDescription.Text.Trim()); vHtml = this.txtHtml.Text; return true; } protected void Button_Submit_Click_Add(object sender, EventArgs e) { if (Fun_GetValue() == true) { if (bll.GetRecordCount("Dir = '" + vDir + "'") > 0) { MessageBox.Show(this, "保存路径 [" + vDir + "] 已经存在!"); return; } model.Title = vTitle; model.TemplatePath = vTemplatePath; model.Dir = vDir; model.Keywords = vKeywords; model.Description = vDescription; model.Html = vHtml; bll.Add(model); int vAid = bll.GetMaxID(); bll_upload.UpdateUploadFile(vHtml, 3, vAid, 0); string vMsgInfo = CreateFile(vAid); //创建文件 MessageBox.ShowAndRedirect(this, vMsgInfo, Request.Path); } } protected void Button_Submit_Click_Edit(object sender, EventArgs e) { if (Fun_GetValue() == true) { if (bll.GetRecordCount("ID = " + vID) == 0) //判断该ID是否被删除 { MessageBox.ShowAndRedirect(this, "你所需要更新的记录 [" + vID + "] 不存在!", ViewState["reJumpUrl"].ToString()); return; } if (bll.GetRecordCount("ID <> " + vID + " And Dir = '" + vDir + "'") > 0) { MessageBox.Show(this, "保存路径 [" + vDir + "] 已经存在!"); return; } model.ID = vID; model.Title = vTitle; model.TemplatePath = vTemplatePath; model.Dir = vDir; model.Keywords = vKeywords; model.Description = vDescription; model.Html = vHtml; bll.Update(model); bll_upload.UpdateUploadFile_Reset(3, vID, 0); //重置上传表中的数据 bll_upload.UpdateUploadFile(vHtml, 3, vID, 0); string vMsgInfo = CreateFile(vID); //创建文件 MessageBox.ShowAndRedirect(this, vMsgInfo, ViewState["reJumpUrl"].ToString()); } } #endregion #region Delete 数据删除 protected void Fun_Delete(int byID) { model = bll.GetModel(byID); string vPath = Server.MapPath(model.Dir); if (System.IO.File.Exists(vPath)) { System.IO.File.Delete(vPath); } if (bll.Delete(byID) == true) { bll_upload.UpdateUploadFile_Reset(3, byID, 0); //重置上传表中的数据 Response.Redirect(Request.UrlReferrer.AbsoluteUri); } } #endregion /// /// 自定义页面文件创建 /// public string CreateFile(int byID) { string vBackInfo = ""; model = bll.GetModel(byID); string isTemplatePath = model.TemplatePath; string isHtml = model.Html; string isDir = HttpContext.Current.Server.MapPath(model.Dir); string isPath = System.IO.Path.GetDirectoryName(isDir); string vHtmlInfo = ""; //模板文件模式 if (isTemplatePath != null && isTemplatePath.Length > 0) { isTemplatePath = HttpContext.Current.Server.MapPath(siteConfig.TemplateDir + isTemplatePath); if (System.IO.File.Exists(isTemplatePath)) { //读取模板文件 try { System.IO.StreamReader sr = new System.IO.StreamReader(isTemplatePath, System.Text.Encoding.UTF8); vHtmlInfo = sr.ReadToEnd(); sr.Close(); vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{sys:title}"), siteConfig.WebName, RegexOptions.IgnoreCase); vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{sys:seotitle}"), seoConfig.SeoTitle, RegexOptions.IgnoreCase); vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{sys:keywords}"), seoConfig.IndexKeywords, RegexOptions.IgnoreCase); vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{sys:description}"), seoConfig.IndexDescription, RegexOptions.IgnoreCase); vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{sys:sitepath}"), "首页 > " + model.Title, RegexOptions.IgnoreCase); vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{field:pagetitle}"), model.Title, RegexOptions.IgnoreCase); vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{field:pagekeywords}"), model.Keywords, RegexOptions.IgnoreCase); vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{field:pagedescription}"), model.Description, RegexOptions.IgnoreCase); vHtmlInfo = Regex.Replace(vHtmlInfo, Regex.Escape("{field:pagehtml}"), isHtml, RegexOptions.IgnoreCase); WebUI_Template wt = new WebUI_Template(); wt.vContent = vHtmlInfo; wt.Parser_MyTag(); wt.Parser_List(); wt.Parser_Page(); wt.Parser_IF(); vHtmlInfo = wt.vContent; } catch (Exception ex) { string exMessage = ex.Message.Replace(System.Environment.NewLine, ""); exMessage = Command_Validate.SqlTextClear(exMessage); vBackInfo = @"自定义页面生成发生错误!\n" + exMessage; } } else { vBackInfo = @"自定义页面生成发生错误!\n模板文件不存在!"; } } else { vHtmlInfo = isHtml; } //无错误时,生成静态文件 if (string.IsNullOrEmpty(vBackInfo)) { System.IO.Directory.CreateDirectory(isPath); System.IO.StreamWriter sw = null; try { sw = new System.IO.StreamWriter(isDir, false, System.Text.Encoding.UTF8); sw.Write(vHtmlInfo); sw.Flush(); } catch (Exception ex) { vBackInfo = @"自定义页面生成发生错误!\n" + ex.ToString(); } finally { sw.Close(); } } return vBackInfo; } } }