Beispiel #1
0
        /// <summary>
        /// Indicates whether the current request can be identified as an installation script
        /// </summary>
        /// <returns>True if the request is being made to the install directory, false otherwise.</returns>
        public static bool IsInstallRequest()
        {
            HttpRequest request = HttpContextHelper.SafeGetRequest();

            if (request != null)
            {
                // OBTAIN THE SCRIPT PATH FROM THE URL
                string absolutePath = request.Url.AbsolutePath.ToLowerInvariant();
                string relativePath;
                if (request.ApplicationPath.Length > 1)
                {
                    relativePath = absolutePath.Substring(request.ApplicationPath.Length);
                }
                else
                {
                    relativePath = absolutePath;
                }

                // SEE IF THIS IS AN INSTALL SCRIPT
                if (relativePath.StartsWith("/install/"))
                {
                    return(true);
                }
            }

            // AN INSTALL REQUEST WAS NOT DETECTED
            return(false);
        }
        /// <summary>
        /// Update the database connection string
        /// </summary>
        /// <param name="connectionString">new connection string</param>
        /// <param name="encrypt"></param>
        /// <returns></returns>
        public static void UpdateConnectionString(string connectionString, bool encrypt)
        {
            HttpRequest request = HttpContextHelper.SafeGetRequest();

            if (request != null)
            {
                try
                {
                    System.Configuration.Configuration config  = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(request.ApplicationPath);
                    ConnectionStringsSection           section = config.GetSection("connectionStrings") as ConnectionStringsSection;
                    if (section.SectionInformation.IsProtected)
                    {
                        section.SectionInformation.UnprotectSection();
                    }
                    section.ConnectionStrings.Remove("LocalSqlServer");
                    section.ConnectionStrings["AbleCommerce"].ConnectionString = connectionString;
                    if (encrypt)
                    {
                        if (!section.SectionInformation.IsProtected)
                        {
                            section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                        }
                    }
                    else
                    {
                        if (section.SectionInformation.IsProtected)
                        {
                            section.SectionInformation.UnprotectSection();
                        }
                    }
                    config.Save();
                }
                catch
                {
                    //IF MS API FAILS, WRITE THE FILE CLEARTEXT
                    StringBuilder textBuilder = new StringBuilder();
                    textBuilder.AppendLine("<connectionStrings>");
                    textBuilder.AppendFormat("\t<add name=\"AbleCommerce\" connectionString=\"{0}\" providerName=\"System.Data.SqlClient\" />\r\n", connectionString);
                    textBuilder.AppendLine("</connectionStrings>");

                    string configPath = request.MapPath("~/App_Data/database.config");
                    File.WriteAllText(configPath, textBuilder.ToString());
                }
            }
        }