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