/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Everest.Library.ExtensionMethod;
using System.IO;
using System.Data.Common;
using System.Data.SqlClient;
using System.Security.Permissions;
using System.Security.AccessControl;
namespace Everest.CmsServices.Installation{
public class DatabaseInfo
{
const string ConnectionString = @"Data Source={0};Initial Catalog={1};User ID={2};Password={3};MultipleActiveResultSets=True";
const string MSSQLAdminConnectionString = "Data Source={0};Initial Catalog=master;User ID={1};Password={2}";
public KoobooDatabaseType DatabaseType { get; set; }
public string CreateOrUseExisting { get; set; }
public string Server { get; set; }
public string DatabaseName { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string ConfirmPassword { get; set; }
public string Administrator { get; set; }
public string AdminPassword { get; set; }
public bool CreateDatabase
{
get
{
return CreateOrUseExisting == "Create";
}
}
public string DatabaseConnectionString
{
get
{
return string.Format(ConnectionString, Server, DatabaseName, UserName, Password);
}
}
public string AdministratorConnectionString
{
get
{
return string.Format(MSSQLAdminConnectionString, Server, Administrator, AdminPassword);
}
}
public DbProviderFactory DbProviderFactory
{
get
{
return SqlClientFactory.Instance;
}
}
}
public abstract class InstallerBase
{
public const string Web_File = "web_{0}.config";
public const string Unity_File = "unity_{0}.config";
public const string RuntimeDefault_FileBody = @"
<%@ Page Language=""C#"" AutoEventWireup=""True"" Inherits=""System.Web.UI.Page"" %>
<%--<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"">--%>
<script language=""C#"" runat=""server"">
protected override void OnLoad(EventArgs e)
{
string originalPath = Request.Path;
HttpContext.Current.RewritePath(Request.ApplicationPath, false);
IHttpHandler httpHandler = GetHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
HttpContext.Current.RewritePath(originalPath, false);
}
private static IHttpHandler GetHttpHandler()
{
//for installation
//IHttpHandler httpHandler = new MvcHttpHandler();
IHttpHandler httpHandler = new Everest.CmsServices.Web.KoobooMvcHttpHandler();
return httpHandler;
}
</script>
";
public string KoobooPath { get; private set; }
public InstallerBase(string koobooPath)
{
KoobooPath = koobooPath;
}
public virtual void Install(DatabaseInfo databaseInfo)
{
CheckFilePermission();
InstallDb(databaseInfo);
SetConfiguration(databaseInfo);
}
protected virtual void CheckFilePermission()
{
string[] writableFiles = new string[]{
"App_Data",
"Assemblies",
"Bin",
"ContentFiles",
"Module",
"SitePackage",
"Template",
"TEMP",
"Default.aspx",
"unity.config",
"Web.config"
};
List<string> unauthorizedAccessFiles = new List<string>();
foreach (var file in writableFiles)
{
if (!FileExtensions.HasWritePermissionOnDir(Path.Combine(KoobooPath, file)))
{
unauthorizedAccessFiles.Add(file);
}
}
if (unauthorizedAccessFiles.Count > 0)
{
throw new UnauthorizedAccessException(string.Format(Resources.Install_UnauthorizedAccess, string.Join(",", unauthorizedAccessFiles.ToArray())));
}
}
protected abstract void InstallDb(DatabaseInfo databaseInfo);
protected abstract void SetConfiguration(DatabaseInfo databaseInfo);
#region File
protected void WriteToFile(string fileName, string fileBody)
{
FileExtensions.SaveFileBody(Path.Combine(KoobooPath, fileName), fileBody);
}
protected string ReadFile(string fileName)
{
return FileExtensions.GetFileBody(Path.Combine(KoobooPath, fileName));
}
#endregion
}
}
|