⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 settings.cs

📁 YetAnotherForum.Net+ScrewTurnWiki中文完美汉化增强版
💻 CS
📖 第 1 页 / 共 3 页
字号:

using System;
using System.Configuration;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.Configuration;
using System.Text;

namespace ScrewTurn.Wiki {

	/// <summary>
	/// Allows access to all the ScrewTurn Wiki settings and configuration options.
	/// </summary>
	public static class Settings {

		// Initialization is needed to make locks work in any circumstance
		private static Dictionary<string, string> config;

		/// <summary>
		/// Initializes the internal data.
		/// </summary>
		static Settings() {
			config = new Dictionary<string, string>();
		}

		/// <summary>
		/// Gets the version of the Wiki.
		/// </summary>
		public static string WikiVersion {
			get { return "2.0.11"; }
		}

		/// <summary>
		/// Gets the name of the Login Cookie.
		/// </summary>
		public static string LoginCookieName {
			get { return "ScrewTurnWikiLogin"; }
		}

		/// <summary>
		/// Gets the name of the Culture Cookie.
		/// </summary>
		public static string CultureCookieName {
			get { return "ScrewTurnWikiCulture"; }
		}

		#region Init/Dump

		/// <summary>
		/// Initializes the Configuration data.
		/// </summary>
		public static void Init() {
			lock(config) {
				config = new Dictionary<string, string>();
			}
			FileStream fs = new FileStream(ConfigFile, FileMode.Open, FileAccess.Read, FileShare.None);
			StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
			string data = sr.ReadToEnd();
			sr.Close();

			data = data.Replace("\r", "");

			string[] lines = data.Split('\n');

			string[] fields;
			for(int i = 0; i < lines.Length; i++) {
				lines[i] = lines[i].Trim();
				
				// Skip comments
				if(lines[i].StartsWith("#")) continue;

				fields = new string[2];
				int idx = lines[i].IndexOf("=");
				if(idx < 0) continue;

				try {
					// Extract key
					fields[0] = lines[i].Substring(0, idx).Trim();
				}
				catch {
					// Unexpected string format
					Log.LogEntry("Config. Option '" + lines[i] + "' could not be parsed (key)", EntryType.Error, "SYSTEM");
					continue;
				}

				try {
					// Extract value
					fields[1] = lines[i].Substring(idx + 1).Trim();
				}
				catch {
					// Blank/invalid value?
					Log.LogEntry("Config. Option '" + lines[i] + "' could not be parsed (value)", EntryType.Error, "SYSTEM");
					fields[1] = "";
				}

				lock(config) {
					config.Add(fields[0], fields[1]);
				}
			}
			Log.LogEntry("Configuration correctly initialized", EntryType.General, "SYSTEM");
		}

		/// <summary>
		/// Saves the Configuration to disk.
		/// </summary>
		public static void DumpConfig() {
			// Create a backup
			try {
				Tools.WriteFile(Settings.PublicDirectory + Path.GetFileNameWithoutExtension(Settings.ConfigFile) + ".bak" + Path.GetExtension(Settings.ConfigFile), Tools.LoadFile(Settings.ConfigFile));
			}
			catch {
				Log.LogEntry("Unable to create backup file for Config", EntryType.Warning, "SYSTEM");
			}
			FileStream fs = null;
			for(int i = 0; i < Settings.FileAccessTries; i++) {
				try {
					fs = new FileStream(ConfigFile, FileMode.Create, FileAccess.Write, FileShare.None);
					break;
				}
				catch {
					fs = null;
					Thread.Sleep(Settings.FileAccessTryDelay);
				}
			}
			if(fs == null) throw new IOException("Unable to load file " + Settings.ConfigFile);

			StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
			
			lock(config) {
				string[] keys = new string[config.Keys.Count];
				config.Keys.CopyTo(keys, 0);
				for(int i = 0; i < keys.Length; i++) {
					sw.Write(keys[i] + " = " + config[keys[i]] + "\r\n");
				}
			}

			sw.Close();
			Log.LogEntry("Config dumped", EntryType.General, "SYSTEM");
		}

		#endregion

		#region HardCoded settings

		/// <summary>
		/// Gets the Master Password, used to encrypt the Users data.
		/// </summary>
		public static string MasterPassword {
			get {
				string pass = WebConfigurationManager.AppSettings["MasterPassword"];
				if(pass == null || pass.Length == 0) throw new Exception("Configuration: MasterPassword cannot be null.");
				return pass;
			}
		}

		/// <summary>
		/// Gets the bytes of the MasterPassword.
		/// </summary>
		public static byte[] MasterPasswordBytes {
			get {
				MD5 md5 = MD5CryptoServiceProvider.Create();
				return md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(MasterPassword));
			}
		}

		/// <summary>
		/// Gets the extension used for Pages.
		/// </summary>
		public static string PageExtension {
			get { return ".ashx"; }
		}

		/// <summary>
		/// Gets the Email validation Regex.
		/// </summary>
		public static string EmailRegex {
			get { return @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$"; }
		}

		/// <summary>
		/// Gets the WikiTitle validation Regex.
		/// </summary>
		public static string WikiTitleRegex {
			get { return ".+"; }
		}

		/// <summary>
		/// Gets the MainUrl validation Regex.
		/// </summary>
		public static string MainUrlRegex {
			get { return @"^https?\://{1}\S+/$"; }
		}

		/// <summary>
		/// Gets the SMTP Server validation Regex.
		/// </summary>
		public static string SmtpServerRegex {
			get { return @"^(?:[a-zA-Z0-9_\-]{1,63}\.?)+(?:[a-zA-Z]{2,})|(?=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[0-2]*[0-9]*[0-9]+\.[0-2]*[0-9]*[0-9]+\.[0-2]*[0-9]*[0-9]+\.[0-2]*[0-9]*[0-9]+$"; }
		}

		#endregion

		#region Directories and Files

		/// <summary>
		/// Gets a list of Files that can be edited in the Editor.
		/// </summary>
		public static string[] EditableFiles {
			get {
				return new string[] {
					Path.GetFileName(Settings.HeaderFile),
					Path.GetFileName(Settings.SidebarFile),
					Path.GetFileName(Settings.FooterFile),
					Path.GetFileName(Settings.AccountActivationMessageFile),
					Path.GetFileName(Settings.PasswordResetMessageFile),
					Path.GetFileName(Settings.EditNoticeFile),
					Path.GetFileName(Settings.HtmlHeadFile),
					Path.GetFileName(Settings.PageHeaderFile),
					Path.GetFileName(Settings.PageFooterFile)
					};
			}
		}

		/// <summary>
		/// Gets the Root Directory of the Wiki.
		/// </summary>
		public static string RootDirectory {
			get { return System.Web.HttpRuntime.AppDomainAppPath; }
		}

		/// <summary>
		/// Gets the Public Directory of the Wiki.
		/// </summary>
		public static string PublicDirectory {
			get { return RootDirectory + PublicDirectoryName + "\\"; }
		}

		/// <summary>
		/// Gets the Public Directory Name (without the full Path) of the Wiki.
		/// </summary>
		public static string PublicDirectoryName {
			get {
				string dir = WebConfigurationManager.AppSettings["PublicDirectory"];
				dir = dir.Trim('\\', '/'); // Remove '/' and '\' from head and tail
				dir = dir.Replace("/", "\\"); // Replace slashes with backslashes
				if(dir == null || dir.Length == 0) throw new Exception("PublicDirectory cannot be null.");
				else return dir;
			}
		}

		/// <summary>
		/// Gets the Upload Directory.
		/// </summary>
		public static string UploadDirectory {
			get { return PublicDirectory + UploadDirectoryName + "\\"; }
		}

		/// <summary>
		/// Gets the Name of the Upload Directory.
		/// </summary>
		public static string UploadDirectoryName {
			get { return "Upload"; }
		}

		/// <summary>
		/// Gets the Name of the Themes directory.
		/// </summary>
		public static string ThemesDirectoryName {
			get { return "Themes"; }
		}

		/// <summary>
		/// Gets the Themes directory.
		/// </summary>
		public static string ThemesDirectory {
			get { return RootDirectory + ThemesDirectoryName + "\\"; }
		}

		/// <summary>
		/// Gets the Name of the Plugins directory.
		/// </summary>
		public static string PluginsDirectoryName {
			get { return "Plugins"; }
		}

		/// <summary>
		/// Gets the Plugins directory.
		/// </summary>
		public static string PluginsDirectory {
			get { return PublicDirectory + PluginsDirectoryName + "\\"; }
		}

		/// <summary>
		/// Gets the Name of the Plugins' Configuration directory.
		/// </summary>
		public static string PluginsConfigurationDirectoryName {
			get { return "Config"; }
		}

		/// <summary>
		/// Gets the Plugins' Configuration directory.
		/// </summary>
		public static string PluginsConfigurationDirectory {
			get { return PluginsDirectory + PluginsConfigurationDirectoryName + "\\"; }
		}

		/// <summary>
		/// Gets the Plugins' Status file.
		/// </summary>
		public static string PluginsStatusFile {
			get { return PluginsDirectory + "Status.cs"; }
		}

		/// <summary>
		/// Gets the Name of the JavaScript Directory.
		/// </summary>
		public static string JsDirectoryName {
			get { return "JS"; }
		}

		/// <summary>
		/// Gets the JavaScript Directory.
		/// </summary>
		public static string JsDirectory {
			get { return RootDirectory + JsDirectoryName + "\\"; }
		}

		/// <summary>
		/// Gets the Temp Directory.
		/// </summary>
		public static string TempDirectory {
			get { return PublicDirectory + TempDirectoryName + "\\"; }
		}

		/// <summary>
		/// Gets the Name of the Temp Directory.
		/// </summary>
		public static string TempDirectoryName {
			get { return "Temp"; }
		}

		/// <summary>
		/// Gets the Name of the Config File.
		/// </summary>
		public static string ConfigFile {
			get { return PublicDirectory + "Config.cs"; }
		}

		/// <summary>
		/// Gets the Recent Changes file.
		/// </summary>
		public static string RecentChangesFile {
			get { return Settings.PublicDirectory + "RecentChanges.cs"; }
		}

		/// <summary>
		/// Gets the HTML Head file.
		/// </summary>
		public static string HtmlHeadFile {
			get { return PublicDirectory + "HtmlHead.cs"; }
		}

		/// <summary>
		/// Gets the Users file.
		/// </summary>
		public static string UsersFile {
			get { return PublicDirectory + "Users.cs"; }
		}

		/// <summary>
		/// Gets the Pages file.
		/// </summary>
		public static string PagesFile {
			get { return PublicDirectory + "Pages.cs"; }
		}

		/// <summary>
		/// Gets the Categories file.
		/// </summary>
		public static string CategoriesFile {
			get { return PublicDirectory + "Categories.cs"; }
		}

		/// <summary>
		/// Gets the NavigationPaths file.
		/// </summary>
		public static string NavigationPathsFile {
			get { return PublicDirectory + "NavigationPaths.cs"; }
		}

		/// <summary>
		/// Gets the Pages directory.
		/// </summary>
		public static string PagesDirectory {
			get { return PublicDirectory + "Pages\\"; }
		}

		/// <summary>
		/// Gets the Messages Directory.
		/// </summary>
		public static string MessagesDirectory {
			get { return PublicDirectory + "Messages\\"; }
		}

		/// <summary>
		/// Gets the Snippets directory.
		/// </summary>
		public static string SnippetsDirectory {
			get { return PublicDirectory + "Snippets\\"; }
		}

		/// <summary>
		/// Gets the Log file.
		/// </summary>
		public static string LogFile {
			get { return PublicDirectory + "Log.cs"; }
		}

		/// <summary>
		/// Gets the Header file.
		/// </summary>
		public static string HeaderFile {
			get { return PublicDirectory + "Header.cs"; }
		}

		/// <summary>
		/// Gets the Sidebar file.
		/// </summary>
		public static string SidebarFile {
			get { return PublicDirectory + "Sidebar.cs"; }
		}

		/// <summary>
		/// Gets the Footer file.
		/// </summary>
		public static string FooterFile {
			get { return PublicDirectory + "Footer.cs"; }
		}

		/// <summary>
		/// Gets the Page Header File.
		/// </summary>
		public static string PageHeaderFile {
			get { return PublicDirectory + "PageHeader.cs"; }
		}

		/// <summary>
		/// Gets the Page Footer File.
		/// </summary>
		public static string PageFooterFile {
			get { return PublicDirectory + "PageFooter.cs"; }
		}

		/// <summary>
		/// Gets the Edit Notice file.
		/// </summary>
		public static string EditNoticeFile {
			get { return PublicDirectory + "EditNotice.cs"; }
		}

		/// <summary>
		/// Gets the Account Activation Message File.
		/// </summary>
		public static string AccountActivationMessageFile {
			get { return PublicDirectory + "AccountActivationMessage.cs"; }
		}

		/// <summary>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -