PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/admin/Settings/Advanced.aspx.cs

#
C# | 193 lines | 143 code | 18 blank | 32 comment | 6 complexity | 7fe966a510ec6284bb32fad94f34a2ae MD5 | raw file
  1namespace admin.Settings
  2{
  3    using System;
  4    using System.Web.Services;
  5    using Resources;
  6    using BlogEngine.Core;
  7    using BlogEngine.Core.Json;
  8    using App_Code;
  9    using Page = System.Web.UI.Page;
 10    using BlogEngine.Core.Providers;
 11    using System.Configuration;
 12    using System.Web.Configuration;
 13    using System.IO;
 14
 15    public partial class Advanced : Page
 16    {
 17        /// <summary>
 18        /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event to initialize the page.
 19        /// </summary>
 20        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
 21        protected override void OnInit(EventArgs e)
 22        {
 23            WebUtils.CheckRightsForAdminSettingsPage(false);
 24
 25            BindSettings();
 26
 27            Page.MaintainScrollPositionOnPostBack = true;
 28            Page.Title = labels.settings;
 29            base.OnInit(e);
 30        }
 31
 32        /// <summary>
 33        /// The bind settings.
 34        /// </summary>
 35        private void BindSettings()
 36        {
 37            // -----------------------------------------------------------------------
 38            // Bind Advanced settings
 39            // -----------------------------------------------------------------------
 40
 41            var settings = BlogSettings.Instance;
 42
 43            cbEnableCompression.Checked = settings.EnableHttpCompression;
 44            cbRemoveWhitespaceInStyleSheets.Checked = settings.RemoveWhitespaceInStyleSheets;
 45            cbCompressWebResource.Checked = settings.CompressWebResource;
 46            cbEnableOpenSearch.Checked = settings.EnableOpenSearch;
 47            cbRequireSslForMetaWeblogApi.Checked = settings.RequireSslMetaWeblogApi;
 48            rblWwwSubdomain.SelectedValue = settings.HandleWwwSubdomain;
 49            cbEnablePingBackSend.Checked = settings.EnablePingBackSend;
 50            cbEnablePingBackReceive.Checked = settings.EnablePingBackReceive;
 51            cbEnableTrackBackSend.Checked = settings.EnableTrackBackSend;
 52            cbEnableTrackBackReceive.Checked = settings.EnableTrackBackReceive;
 53            cbEnableErrorLogging.Checked = settings.EnableErrorLogging;
 54            txtGalleryFeed.Text = settings.GalleryFeedUrl;
 55            cbAllowRemoteFileDownloads.Checked = settings.AllowServerToDownloadRemoteFiles;
 56            txtRemoteTimeout.Text = settings.RemoteFileDownloadTimeout.ToString();
 57            txtRemoteMaxFileSize.Text = settings.RemoteMaxFileSize.ToString();
 58            if (!Page.IsPostBack)
 59            {
 60                ddlProvider.DataSource = BlogService.FileSystemProviders;
 61                ddlProvider.DataTextField = "Description";
 62                ddlProvider.DataValueField = "Name";
 63                ddlProvider.DataBind();
 64                ddlProvider.SelectedValue = BlogService.FileSystemProvider.Name;
 65                hdnProvider.Value = BlogService.FileSystemProvider.Name;
 66            }
 67        }
 68
 69        protected void btnChangeProvider_Click(object sender, EventArgs e)
 70        {
 71            providerError.Visible = false;
 72            var zipArchive = Server.MapPath(string.Format("{0}FileSystemBackup-{1}.zip", Blog.CurrentInstance.StorageLocation, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")));
 73            var msg = new BlogEngine.Core.FileSystem.FileSystemUtilities().DumpProvider(ddlProvider.SelectedValue.ToString(), zipArchive);
 74            if (!string.IsNullOrWhiteSpace(msg))
 75            {
 76                providerError.Visible = true;
 77                providerError.Text = msg;
 78            }
 79            else
 80                hdnProvider.Value = ddlProvider.SelectedValue.ToString();
 81        }
 82
 83        protected void btnDownloadArchive_Click(object sender, EventArgs e)
 84        {
 85            var zipArchive = Server.MapPath(string.Format("{0}FileSystemBackup-{1}.zip", Blog.CurrentInstance.StorageLocation, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")));
 86            new BlogEngine.Core.FileSystem.FileSystemUtilities().CompressDirectory(zipArchive, Blog.CurrentInstance.RootFileStore);
 87            var file = new FileInfo(zipArchive);
 88            byte[] Buffer = null;
 89            System.IO.FileStream FileStream = new System.IO.FileStream(file.FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
 90            System.IO.BinaryReader BinaryReader = new System.IO.BinaryReader(FileStream);
 91            long TotalBytes = file.Length;
 92            Buffer = BinaryReader.ReadBytes((int)TotalBytes);
 93            FileStream.Close();
 94            FileStream.Dispose();
 95            BinaryReader.Close();
 96            Response.AppendHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", file.Name));
 97            Response.BinaryWrite(Buffer);
 98        }
 99
100
101        /// <summary>
102        /// Save settings
103        /// </summary>
104        /// <param name="enableCompression"></param>
105        /// <param name="removeWhitespaceInStyleSheets"></param>
106        /// <param name="compressWebResource"></param>
107        /// <param name="enableOpenSearch"></param>
108        /// <param name="requireSslForMetaWeblogApi"></param>
109        /// <param name="wwwSubdomain"></param>
110        /// <param name="enableTrackBackSend"></param>
111        /// <param name="enableTrackBackReceive"></param>
112        /// <param name="enablePingBackSend"></param>
113        /// <param name="enablePingBackReceive"></param>
114        /// <param name="enableErrorLogging"></param>
115        /// <param name="allowRemoteFileDownloads"></param>
116        /// <param name="remoteTimeout"></param>
117        /// <param name="remoteMaxFileSize"></param>
118        /// <param name="galleryFeedUrl">Online gallery feed URL</param>
119        /// <returns></returns>
120        [WebMethod]
121        public static JsonResponse Save(bool enableCompression, 
122			bool removeWhitespaceInStyleSheets,
123            bool compressWebResource,
124            bool enableOpenSearch,
125            bool requireSslForMetaWeblogApi,
126			string wwwSubdomain,
127            bool enableTrackBackSend,
128            bool enableTrackBackReceive,
129            bool enablePingBackSend,
130            bool enablePingBackReceive,
131            bool enableErrorLogging,
132            bool allowRemoteFileDownloads,
133            int remoteTimeout,
134            int remoteMaxFileSize,
135            string galleryFeedUrl)
136        {
137            var response = new JsonResponse { Success = false };
138            var settings = BlogSettings.Instance;
139
140            if (!WebUtils.CheckRightsForAdminSettingsPage(true))
141            {
142                response.Message = "Not authorized";
143                return response;
144            }
145
146            try
147            {
148
149                // Validate values before setting any of them to the BlogSettings instance.
150                // Because it's a singleton, we don't want partial data being stored to
151                // it if there's any exceptions thrown prior to saving. 
152
153                if (remoteTimeout < 0)
154                {
155                    throw new ArgumentOutOfRangeException("RemoteFileDownloadTimeout must be greater than or equal to 0 milliseconds.");
156                }
157                else if (remoteMaxFileSize < 0)
158                {
159                    throw new ArgumentOutOfRangeException("RemoteMaxFileSize must be greater than or equal to 0 bytes.");
160                }  
161
162                settings.EnableHttpCompression = enableCompression;
163                settings.RemoveWhitespaceInStyleSheets = removeWhitespaceInStyleSheets;
164                settings.CompressWebResource = compressWebResource;
165                settings.EnableOpenSearch = enableOpenSearch;
166                settings.RequireSslMetaWeblogApi = requireSslForMetaWeblogApi;
167                settings.HandleWwwSubdomain = wwwSubdomain;
168                settings.EnableTrackBackSend = enableTrackBackSend;
169                settings.EnableTrackBackReceive = enableTrackBackReceive;
170                settings.EnablePingBackSend = enablePingBackSend;
171                settings.EnablePingBackReceive = enablePingBackReceive;
172                settings.EnableErrorLogging = enableErrorLogging;
173                settings.GalleryFeedUrl = galleryFeedUrl;
174
175                settings.AllowServerToDownloadRemoteFiles = allowRemoteFileDownloads;
176                settings.RemoteFileDownloadTimeout = remoteTimeout;
177                settings.RemoteMaxFileSize = remoteMaxFileSize;
178
179                settings.Save();
180            }
181            catch (Exception ex)
182            {
183                Utils.Log(string.Format("admin.Settings.Advanced.Save(): {0}", ex.Message));
184                response.Message = string.Format("Could not save settings: {0}", ex.Message);
185                return response;
186            }
187
188            response.Success = true;
189            response.Message = "Settings saved";
190            return response;
191        }
192    }
193}