<%@ Page Debug="True" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Dvbbs.Utils" %>
<%@ Import Namespace="Dvbbs.Web.Configuration" %>
<%@ Import Namespace="System.Xml" %>
<script language="C#" runat="server">
protected override void OnInit(EventArgs e)
{
    // 检测论坛地址
    if ("forum_install_directory_test" == Request.QueryString["install_action"])
    {
		/*
		if (null != Config.Settings)
		{
			Response.Write("OK");
		}
		//*/

		// 初始化论坛所需的配置文件……
		if (InitForum())
		{
			Response.Write("OK");
		}
		else
		{
			Response.Write("未知的错误，请联系我们！");
		}
    }
}
private bool InitForum()
{
	// 初始化 web.config
	if (!InitWebConfig())
	{
		return false;
	}

	// 初始化 Dvbbs.Web.config
	if (!InitDvbbsWebConfig())
	{
		return false;
	}

	return true;
}

// 初始化 web.config
private bool InitWebConfig()
{
	string web_config_path = Server.MapPath("~/web.config");
	if (!File.Exists(web_config_path))
	{
		string src_web_config_path = Server.MapPath("../web.config");
		if (!File.Exists(src_web_config_path))
		{
			throw new HttpException("您的安装程序不完整，缺少 web.config");
		}
		File.Copy(src_web_config_path, web_config_path);
		return true;
	}

	// 向 web.config 中添加 httpModules <add type="Dvbbs.Web.UrlRewriter.HttpModule, Dvbbs.bbs" name="DvModuleRewriter" />
	HttpApplication app = Context.ApplicationInstance;
	if (null == app.Modules.Get("DvModuleRewriter"))
	{
		XmlDocument xml_doc = new XmlDocument();
		xml_doc.Load(web_config_path);
		XmlNode xml_node = xml_doc.SelectSingleNode("configuration/system.web/httpModules/add[@name='DvModuleRewriter']");
		if (null == xml_node)	//	很郁闷，测试中发现要再多检查一次才行……
		{
			xml_node = xml_doc.SelectSingleNode("configuration/system.web");
			if (null == xml_node)
			{
				xml_node = xml_doc.CreateElement("system.web");
				xml_doc.DocumentElement.AppendChild(xml_node);
			}
			XmlNode http_modules = xml_node.SelectSingleNode("httpModules");
			if (null == http_modules)
			{
				http_modules = xml_doc.CreateElement("httpModules");
				xml_node.AppendChild(http_modules);
			}
			XmlElement ele = xml_doc.CreateElement("add");
			ele.SetAttribute("name", "DvModuleRewriter");
			ele.SetAttribute("type", "Dvbbs.Web.UrlRewriter.HttpModule, Dvbbs.bbs");
			http_modules.AppendChild(ele);
			try
			{
				xml_doc.Save(web_config_path);
			}
			catch (UnauthorizedAccessException)
			{
				Response.Write("无法向站点根目录的 web.config 添加论坛模块，请手动修改 web.config ");
				return false;
			}
		}
	}

	return true;
}

// 初始化 Dvbbs.Web.config
private bool InitDvbbsWebConfig()
{
	string src_cfg_file_path = Server.MapPath("../Dvbbs.Web.config");
	if (!File.Exists(src_cfg_file_path))
	{
		throw new HttpException("您的安装程序不完整，缺少 Dvbbs.Web.config");
	}

	string dest_cfg_file_path = Server.MapPath("~/Dvbbs.Web.config");
	if (!File.Exists(dest_cfg_file_path))
	{
		File.Copy(src_cfg_file_path, dest_cfg_file_path, false);
	}

	ForumSystemConfigInfoCollection cfgCollection = ForumSystemConfigurationProvider.GetInstance();
	if (null == cfgCollection)
	{
		throw new HttpException("站点根目录下的 Dvbbs.Web.config 不存在或其格式不合法！");
	}

	//string forum_virtual_base_path = "/" + Server.MapPath(Path.GetDirectoryName(Request.Path) + "\\..\\").Replace(Server.MapPath("/"), "").Replace("\\", "/");
	string forum_virtual_base_path = Path.GetDirectoryName(Request.Path).Replace("\\install", "\\").Replace("\\", "/");
	bool exists = false;
	foreach (ForumSystemConfigInfo info in cfgCollection)
	{
		if (info.ForumRootVirtualPath.ToLower() == forum_virtual_base_path.ToLower())
		{
			exists = true;
			break;
		}
	}
	if (!exists)
	{
		ForumSystemConfigInfo cfg_info = new ForumSystemConfigInfo();
		cfg_info.ForumRootVirtualPath = forum_virtual_base_path;
		cfgCollection.Add(cfg_info);
		ForumSystemConfigurationProvider.SaveInstance(cfgCollection);
	}

	return true;
}
</script>