newtelligence.DasBlog.Web.SiteConfig.Save C# (CSharp) Method

Save() public static method

public static Save ( SiteConfig siteConfig ) : void
siteConfig SiteConfig
return void
        public static void Save(SiteConfig siteConfig)
        {
            System.Security.Principal.WindowsImpersonationContext wi = Impersonation.Impersonate();

            XmlSerializer ser = new XmlSerializer(typeof (SiteConfig));

            using (StreamWriter writer = new StreamWriter(SiteConfig.GetConfigFilePathFromCurrentContext()))
            {
                ser.Serialize(writer, siteConfig);
            }

            wi.Undo();
        }

Usage Example

Ejemplo n.º 1
0
        protected void buttonSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
            {
                // There were validation errors, save client states.
                CleanGravatar();
                CleanAutoExpire();
                CleanHtmlComments();
                CleanGeoRss();
                return;
            }

            SharedBasePage requestPage = Page as SharedBasePage;
            SiteConfig     siteConfig  = requestPage.SiteConfig;

            siteConfig.Contact             = textContact.Text;
            siteConfig.Copyright           = textCopyright.Text;
            siteConfig.FrontPageCategory   = textFrontPageCategory.Text;
            siteConfig.FrontPageDayCount   = int.Parse(textFrontPageDayCount.Text);
            siteConfig.FrontPageEntryCount = int.Parse(textFrontPageEntryCount.Text);
            siteConfig.EntriesPerPage      = int.Parse(textEntriesPerPage.Text);
            try
            {
                siteConfig.ContentLookaheadDays = int.Parse(textContentLookaheadDays.Text);
            }
            catch (FormatException)
            {
                siteConfig.ContentLookaheadDays = 0;
            }
            siteConfig.RssDayCount               = int.Parse(textMainMaxDaysInRss.Text);
            siteConfig.RssMainEntryCount         = int.Parse(textMainMaxEntriesInRss.Text);
            siteConfig.RssEntryCount             = int.Parse(textOtherMaxEntriesInRss.Text);
            siteConfig.AlwaysIncludeContentInRSS = checkAlwaysIncludeContentInRSS.Checked;
            siteConfig.EnableRssItemFooters      = checkEnableRSSItemFooter.Checked;
            siteConfig.RssItemFooter             = textRSSItemFooter.Text;
            siteConfig.RSSEndPointRewrite        = txtRSSEndPointRewrite.Text;
            siteConfig.EnablePop3        = checkPop3Enabled.Checked;
            siteConfig.Pop3Interval      = int.Parse(textPop3Interval.Text);
            siteConfig.Pop3Server        = textPop3Server.Text;
            siteConfig.Pop3SubjectPrefix = textPop3SubjectPrefix.Text;
            siteConfig.Pop3Username      = textPop3Username.Text;
            if (textPop3Password.Text.Length > 0 &&
                textPop3Password.Text != passwordPlaceHolder)
            {
                siteConfig.Pop3Password = textPop3Password.Text;
            }
            siteConfig.Pop3DeleteAllMessages = mailDeletionAll.Checked;
            siteConfig.Pop3LogIgnoredEmails  = logIgnoredEmails.Checked;
            siteConfig.EnableXSSUpstream     = checkXssEnabled.Checked;
            siteConfig.XSSUpstreamEndpoint   = textXssEndpoint.Text;
            siteConfig.XSSUpstreamInterval   = int.Parse(textXssInterval.Text);
            if (textXssPassword.Text.Length > 0 &&
                textXssPassword.Text != passwordPlaceHolder)
            {
                siteConfig.XSSUpstreamPassword = textXssPassword.Text;
            }
            siteConfig.XSSUpstreamUsername = textXssUsername.Text;
            siteConfig.XSSRSSFilename      = textXssRssFilename.Text;

            siteConfig.Root                                   = textRoot.Text;
            siteConfig.SmtpServer                             = textSmtpServer.Text;
            siteConfig.SmtpPort                               = int.Parse(textSmtpPort.Text);
            siteConfig.UseSSLForSMTP                          = checkUseSSLForSMTP.Checked;
            siteConfig.NotificationEMailAddress               = textNotificationEmailAddress.Text;
            siteConfig.SendCommentsByEmail                    = checkComments.Checked;
            siteConfig.EnableCoComment                        = checkEnableCoComment.Checked;
            siteConfig.SendPingbacksByEmail                   = checkPingbacks.Checked;
            siteConfig.SendReferralsByEmail                   = checkReferrals.Checked;
            siteConfig.SendTrackbacksByEmail                  = checkTrackbacks.Checked;
            siteConfig.SendPostsByEmail                       = checkPosts.Checked;
            siteConfig.EnableAutoPingback                     = checkEnableAutoPingback.Checked;
            siteConfig.EnableBloggerApi                       = checkEnableBloggerApi.Checked;
            siteConfig.EnableComments                         = checkEnableComments.Checked;
            siteConfig.EnableCommentApi                       = checkEnableCommentApi.Checked;
            siteConfig.ShowCommentsWhenViewingEntry           = checkShowCommentsWhenViewingEntry.Checked;
            siteConfig.EnableConfigEditService                = checkEnableConfigEditService.Checked;
            siteConfig.EnableEditService                      = checkEnableEditService.Checked;
            siteConfig.EnableAutoSave                         = checkEnableAutoSave.Checked;
            siteConfig.EnableTrackbackService                 = checkEnableTrackbackService.Checked;
            siteConfig.EnablePingbackService                  = checkEnablePingbackService.Checked;
            siteConfig.EnableClickThrough                     = checkEnableClickThrough.Checked;
            siteConfig.EnableAggregatorBugging                = checkEnableAggregatorBugging.Checked;
            siteConfig.Subtitle                               = textSubtitle.Text;
            siteConfig.Title                                  = textTitle.Text;
            siteConfig.ShowCommentCount                       = checkShowCommentCounters.Checked;
            siteConfig.Pop3InlineAttachedPictures             = checkPop3InlineAttachedPictures.Checked;
            siteConfig.Pop3InlinedAttachedPicturesThumbHeight = int.Parse(textPop3AttachedPicturesPictureThumbnailHeight.Text);
            siteConfig.ShowItemDescriptionInAggregatedViews   = checkShowItemDescriptionInAggregatedViews.Checked;
            siteConfig.EnableStartPageCaching                 = checkEnableStartPageCaching.Checked;
            siteConfig.EnableBlogrollDescription              = checkEnableBlogrollDescription.Checked;
            siteConfig.EnableUrlRewriting                     = checkEnableUrlRewriting.Checked;
            siteConfig.DisplayTimeZoneIndex                   = Convert.ToInt32(listTimeZones.SelectedValue);
            siteConfig.AdjustDisplayTimeZone                  = !checkUseUTC.Checked;
            siteConfig.EntryTitleAsLink                       = checkEntryTitleAsLink.Checked;
            siteConfig.EnableCrossposts                       = checkEnableCrosspost.Checked;
            if (textPassword.Text.Length > 0 &&
                textPassword.Text != passwordPlaceHolder)
            {
                SiteSecurity.SetPassword(requestPage.User.Identity.Name, textPassword.Text);
            }
            siteConfig.CategoryAllEntries = checkCategoryAllEntries.Checked;
            requestPage.UserTheme         = siteConfig.Theme = listThemes.SelectedValue;

            siteConfig.ReferralUrlBlackList       = textReferralBlacklist.Text.TrimEnd(';');
            siteConfig.EnableReferralUrlBlackList = checkReferralUrlBlacklist.Checked;
            siteConfig.EnableCaptcha = checkCaptchaEnabled.Checked;
            siteConfig.EnableReferralUrlBlackList404s = checkReferralBlacklist404s.Checked;
            siteConfig.ChannelImageUrl            = textRSSChannelImage.Text;
            siteConfig.EnableTitlePermaLink       = checkEnableTitlePermaLink.Checked;
            siteConfig.EnableTitlePermaLinkUnique = checkEnableTitlePermaLinkUnique.Checked;
            siteConfig.EnableTitlePermaLinkSpaces = checkEnableTitlePermaLinkSpaces.Checked;
            siteConfig.EncryptLoginPassword       = checkEnableEncryptLoginPassword.Checked;
            siteConfig.EnableSmtpAuthentication   = checkEnableSmtpAuthentication.Checked;
            siteConfig.SmtpUserName = textSmtpUsername.Text;
            if (textSmtpPassword.Text.Length > 0 &&
                textSmtpPassword.Text != passwordPlaceHolder)
            {
                siteConfig.SmtpPassword = textSmtpPassword.Text;
            }
            siteConfig.RssLanguage           = textRssLanguage.Text;
            siteConfig.EnableSearchHighlight = checkEnableSearchHighlight.Checked;
            siteConfig.EnableEntryReferrals  = checkEnableEntryReferral.Checked;

            siteConfig.FeedBurnerName = textFeedBurnerName.Text.Trim();
            siteConfig.UseFeedSchemeForSyndication = checkUseFeedScheme.Checked;
            siteConfig.LogBlockedReferrals         = checkLogBlockedReferrals.Checked;
            siteConfig.EnableSpamBlockingService   = checkSpamBlockingEnabled.Checked;
            siteConfig.SpamBlockingServiceApiKey   = textSpamBlockingApiKey.Text;
            siteConfig.EnableSpamModeration        = (optionSpamHandling.SelectedValue != SPAM_OPTION_DELETE);
            siteConfig.EnableCommentDays           = checkEnableCommentDays.Checked;
            siteConfig.HtmlTidyContent             = checkAttemptToHtmlTidyContent.Checked;
            siteConfig.ResolveCommenterIP          = checkResolveCommenterIP.Checked;

            siteConfig.TitlePermalinkSpaceReplacement = dropDownTitlePermalinkReplacementCharacter.SelectedValue;

            if (checkEnableCommentDays.Checked)
            {
                try
                {
                    int days = Convert.ToInt32(textDaysCommentsAllowed.Text);
                    if (days > 0)
                    {
                        siteConfig.DaysCommentsAllowed = days;
                    }
                }
                catch (FormatException)
                {
                    siteConfig.DaysCommentsAllowed = 60;
                }
            }
            else
            {
                siteConfig.DaysCommentsAllowed = 60;
            }

            // comments approval
            siteConfig.CommentsRequireApproval = checkCommentsRequireApproval.Checked;

            // removed the ability to edit the html tags, which are allowed for now
            foreach (ListItem li in checkBoxListAllowedTags.Items)
            {
                ValidTag tag = siteConfig.AllowedTags[li.Value];
                if (tag != null)
                {
                    tag.IsAllowed = li.Selected;
                }
            }

            // comments allow html
            siteConfig.CommentsAllowHtml = checkAllowHtml.Checked;

            // comments allow Gravatar and alt path
            GravatarPopulateConfig();

            // supress email address display
            siteConfig.SupressEmailAddressDisplay = checkDisableEmailDisplay.Checked;

            // enable daily report email
            siteConfig.EnableDailyReportEmail = checkDailyReport.Checked;

            PingServiceCollection savePingServices = new PingServiceCollection();

            foreach (PingService pingService in PingServiceCollection)
            {
                if (checkBoxListPingServices.Items.FindByValue(pingService.Endpoint).Selected)
                {
                    savePingServices.Add(pingService);
                }
            }

            siteConfig.PingServices     = savePingServices;
            siteConfig.EntryEditControl = drpEntryEditControl.SelectedValue;

            // GeoRSS stuff.
            siteConfig.EnableGeoRss = checkEnableGeoRss.Checked;
            if (checkEnableGeoRss.Checked)
            {
                siteConfig.EnableDefaultLatLongForNonGeoCodedPosts = checkEnableDefaultLatLongForNonGeoCodedPosts.Checked;
                if (checkEnableDefaultLatLongForNonGeoCodedPosts.Checked)
                {
                    double latitude;
                    if (double.TryParse(textDefaultLatitude.Text, NumberStyles.Float, CultureInfo.InvariantCulture, out latitude))
                    {
                        siteConfig.DefaultLatitude = latitude;
                    }

                    double longitude;
                    if (double.TryParse(textDefaultLongitude.Text, NumberStyles.Float, CultureInfo.InvariantCulture, out longitude))
                    {
                        siteConfig.DefaultLongitude = longitude;
                    }
                }

                siteConfig.EnableGoogleMaps = checkEnableGoogleMaps.Checked;
                if (checkEnableGoogleMaps.Checked)
                {
                    siteConfig.GoogleMapsApiKey = textGoogleMapsApi.Text;
                }
            }

            // open id stuff
            siteConfig.AllowOpenIdAdmin        = chkAllowOpenIdAdmin.Checked;
            siteConfig.AllowOpenIdComments     = chkAllowOpenIdCommenter.Checked;
            siteConfig.BypassSpamOpenIdComment = chkBypassSpamOpenIdCommenter.Checked;

            siteConfig.AMPPagesEnabled = checkAmpEnabled.Checked;

            SiteConfig.Save(siteConfig);

            SeoMetaTags smt = new SeoMetaTags().GetMetaTags();

            smt.MetaDescription = txtMetaDescription.Text;
            smt.MetaKeywords    = txtMetaKeywords.Text;
            smt.TwitterCard     = txtTwitterCard.Text;
            smt.TwitterSite     = txtTwitterSite.Text;
            smt.TwitterCreator  = txtTwitterCreator.Text;
            smt.TwitterImage    = txtTwitterImage.Text;
            smt.FaceBookAdmins  = txtFaceBookAdmins.Text;
            smt.FaceBookAppID   = txtFaceBookAppID.Text;

            SeoMetaTags.Save(smt);

            if (siteConfig.EnableReferralUrlBlackList && siteConfig.ReferralUrlBlackList.Length != 0)
            {
                ReferralBlackListFactory.AddBlacklist(new ReferralUrlBlacklist(), siteConfig.ReferralUrlBlackList);
            }
            else
            {
                ReferralBlackListFactory.RemoveBlacklist(typeof(ReferralUrlBlacklist));
            }

            requestPage.Redirect(Page.Request.Url.AbsoluteUri);
        }