/// <summary> /// /// </summary> /// <returns></returns> public ManagerConfig Clone() { ManagerConfig newConf = new ManagerConfig(); lock (this) { foreach (string val in this.plugins) newConf.plugins.Add(val); foreach (string key in this.AllKeys) newConf[key] = this[key]; } return newConf; }
object IConfigurationSectionHandler.Create(object parent, object config_context, XmlNode section) { ManagerConfig config = new ManagerConfig(); AssemblyLoader loader = new AssemblyLoader(); HttpContext context = HttpContext.Current; bool found = false; // Add all dlls in plugin dir foreach (string file in this.FindDLLs(context.Request.MapPath(@"plugins"), new ArrayList())) { //System.Web.HttpContext.Current.Trace.Write(file); loader.AddFile(file); found = true; } // Add all dlls in plugin dir if (!found) { foreach (string file in this.FindDLLs(context.Request.MapPath(@"..\plugins"), new ArrayList())) { //System.Web.HttpContext.Current.Trace.Write(file); loader.AddFile(file); } } foreach (XmlElement pluginElm in section.SelectNodes("plugins/plugin")) { // Load plugin dll if (pluginElm.GetAttribute("file") != "") loader.AddFile(PathUtils.ToUnixPath(pluginElm.GetAttribute("file"))); // Add class config.Plugins.Add(pluginElm.GetAttribute("class")); } foreach (XmlElement addElm in section.SelectNodes("config/add")) config.Add(addElm.GetAttribute("key"), addElm.GetAttribute("value")); return config; }
/// <summary> /// /// </summary> /// <param name="config"></param> /// <param name="config_prefixes"></param> /// <returns></returns> public ManagerConfig GetJSConfig(ManagerConfig config, string config_prefixes) { string[] encrypted = new string[] { "filesystem.path", "filesystem.rootpath", "filesystem.directory_templates" }; ManagerConfig outConfig = new ManagerConfig(); string[] names, prefixes; string prefix; bool validPrefix; int pos; prefixes = config_prefixes.Split(new char[] { ',' }); foreach (string key in config.AllKeys) { if ((pos = key.IndexOf(".allow_export")) != -1) { if (config[key] == null) { continue; } names = config[key].Split(new char[] { ',' }); prefix = key.Substring(0, pos); validPrefix = false; if (config_prefixes != "*") { foreach (string targetPrefix in prefixes) { if (targetPrefix == prefix) { validPrefix = true; break; } } } else { validPrefix = true; } // All exported or the requested visible ones if (validPrefix) { foreach (string name in names) { string outKey = prefix + "." + name; // Encrypt if needed foreach (string encryptKey in encrypted) { if (encryptKey == outKey) { outConfig[outKey] = this.EncryptPath(config[outKey]); break; } } // Output normal if (outConfig[outKey] == null) { outConfig[outKey] = config[outKey]; } } } } } return(outConfig); }
/// <summary> /// /// </summary> /// <param name="prefix"></param> /// <param name="manager_path"></param> public ManagerEngine(string prefix, string manager_path) { this.rootPaths = new StringList(); this.rootNames = new NameValueCollection(); this.plugins = new PluginList(); this.fileSystems = new FileFactoryDictionary(); this.langPack = null; this.logger = new Logger(); this.pluginLookup = new Hashtable(); if (manager_path != null) { this.ManagerPath = manager_path; } else { this.ManagerPath = System.Web.HttpContext.Current.Server.MapPath(".."); } this.Plugins.Add(new CorePlugin()); ManagerConfig coreConfig = (ManagerConfig)System.Web.HttpContext.Current.GetConfig("CorePlugin"); this.prefix = prefix; // Add instance of each plugin foreach (string className in coreConfig.Plugins) { IPlugin plugin = (IPlugin)InstanceFactory.CreateInstance(className); if (plugin == null) { throw new ManagerException("Could not create instance of plugin class: " + className); } this.Plugins.Add(plugin); } this.DispatchEvent(EventType.PreInit, prefix); this.SetupConfigItems(); // Setup logger if (!this.Config.GetBool("log.enabled", false)) { this.logger.Level = LoggerLevel.Fatal; } else { this.logger.LevelName = this.Config.Get("log.level", "fatal"); } this.logger.Path = this.ToAbsPath(this.Config.Get("log.path", "logs")); this.logger.FileFormat = this.Config.Get("log.filename", "{level}.log"); this.logger.Format = this.Config.Get("log.format", "[{time}] [{level}] {message}"); this.logger.MaxSize = this.Config.Get("log.max_size", "100k"); this.logger.MaxFiles = this.Config.GetInt("log.max_files", 10); // Add instance of each plugin AssemblyLoader loader = new AssemblyLoader(); foreach (string className in this.Config.Plugins) { IPlugin plugin = (IPlugin)loader.CreateInstance(className); if (plugin == null) { throw new Exception("Could not create instance of plugin class: " + className); } this.Plugins.Add(plugin); // Add by class name and by short name this.pluginLookup[className] = plugin; if (plugin.ShortName != null) { this.pluginLookup[plugin.ShortName] = plugin; } } }
/// <summary> /// /// </summary> /// <param name="file"></param> /// <param name="action"></param> /// <param name="config"></param> /// <returns></returns> public bool VerifyFile(IFile file, string action, ManagerConfig config) { BasicFileFilter filter; // Verify filesystem config filter = new BasicFileFilter(); filter.IncludeDirectoryPattern = config["filesystem.include_directory_pattern"]; filter.ExcludeDirectoryPattern = config["filesystem.exclude_directory_pattern"]; filter.IncludeFilePattern = config["filesystem.include_file_pattern"]; filter.ExcludeFilePattern = config["filesystem.exclude_file_pattern"]; filter.IncludeExtensions = config["filesystem.extensions"]; this.invalidFileMsg = "{#error.invalid_filename}"; if (!filter.Accept(file)) { if (filter.Reason == FilterReason.InvalidFileName) { if (file.IsDirectory) { this.invalidFileMsg = config["filesystem.invalid_directory_name_msg"]; } else { this.invalidFileMsg = config["filesystem.invalid_file_name_msg"]; } if (this.invalidFileMsg.Length == 0) { this.invalidFileMsg = "{#error.invalid_filename}"; } } return(false); } // Verify action specific config filter = new BasicFileFilter(); if (action != null) { if (config[action + ".include_directory_pattern"] != null) { filter.IncludeDirectoryPattern = config[action + ".include_directory_pattern"]; } if (config[action + ".exclude_directory_pattern"] != null) { filter.ExcludeDirectoryPattern = config[action + ".exclude_directory_pattern"]; } if (config[action + ".include_file_pattern"] != null) { filter.IncludeFilePattern = config[action + ".include_file_pattern"]; } if (config[action + ".exclude_file_pattern"] != null) { filter.ExcludeFilePattern = config[action + ".exclude_file_pattern"]; } if (config[action + ".extensions"] != null) { filter.IncludeExtensions = config[action + ".extensions"]; } } if (!filter.Accept(file)) { if (filter.Reason == FilterReason.InvalidFileName) { if (file.IsDirectory) { this.invalidFileMsg = config[action + ".invalid_directory_name_msg"]; } else { this.invalidFileMsg = config[action + ".invalid_file_name_msg"]; } if (this.invalidFileMsg == null || this.invalidFileMsg.Length == 0) { this.invalidFileMsg = "{#error.invalid_filename}"; } } return(false); } return(true); }
private ResultSet ListFiles(ManagerEngine man, string cmd, Hashtable input) { ResultSet rs = new ResultSet(new string[] { "name", "path", "size", "type", "created", "modified", "attribs", "custom" }); Hashtable customInfo; ArrayList files = new ArrayList(); BasicFileFilter filter; IFile listDir; ManagerConfig config = man.Config; bool onlyDirs, onlyFiles, remember, hasParent = false; string type, attribs, path, rootPath, configPrefixes, name, tmpPath; int pages, pageSize; HttpRequest req = HttpContext.Current.Request; HttpResponse resp = HttpContext.Current.Response; // Handle remember_last_path state if (input["remember_last_path"] != null) { // URL specified if (((string)input["path"]) == "{default}") { if (input["url"] != null && (string)input["url"] != "") { input["path"] = man.ConvertURIToPath((string)input["url"]); } } path = (string)input["path"]; if (input["remember_last_path"] is bool) { remember = (bool)input["remember_last_path"]; } else if (((string)input["remember_last_path"]) != "auto") { remember = StringUtils.CheckBool((string)input["remember_last_path"]); } else { remember = config.GetBool("general.remember_last_path", false); } // Get/set cookie if (remember) { if (path == "{default}" && req.Cookies["MCManager_" + man.Prefix + "_lastPath"] != null) { tmpPath = req.Cookies["MCManager_" + man.Prefix + "_lastPath"].Value; if (tmpPath != null && man.GetFSFromPath(tmpPath) == "file") { input["path"] = tmpPath; } } else { HttpCookie cookie = new HttpCookie("MCManager_" + man.Prefix + "_lastPath"); cookie.Expires = DateTime.Now.AddDays(30); cookie.Value = path; resp.Cookies.Remove("MCManager_" + man.Prefix + "_lastPath"); if (man.GetFSFromPath(path) == "file") { resp.Cookies.Add(cookie); } } } } path = man.DecryptPath((string)input["path"]); rootPath = man.DecryptPath((string)input["root_path"]); onlyDirs = input["only_dirs"] != null && (bool)input["only_dirs"]; onlyFiles = input["only_files"] != null && (bool)input["only_files"]; configPrefixes = (string)input["config"]; if (man.GetFSFromPath(path) == "file" && !man.VerifyPath(path)) { path = man.Config.Get("filesystem.path"); } // Move path into rootpath if (rootPath != null && !PathUtils.IsChildPath(rootPath, path) && man.GetFSFromPath(path) == "file") { path = rootPath; } listDir = man.GetFile(path); // Use default path instead if (!listDir.Exists) { path = config["filesystem.path"]; listDir = man.GetFile(path); } rs.Header["path"] = man.EncryptPath(path); rs.Header["visual_path"] = man.ToVisualPath(path, rootPath); rs.Header["attribs"] = (listDir.CanRead ? "R" : "-") + (listDir.CanWrite ? "W" : "-"); // List files if (listDir.IsDirectory) { config = listDir.Config; // Return part of the config if (configPrefixes != null) { rs.Config = man.GetJSConfig(config, configPrefixes); } // Verify filesystem config filter = new BasicFileFilter(); filter.IncludeDirectoryPattern = config["filesystem.include_directory_pattern"]; filter.ExcludeDirectoryPattern = config["filesystem.exclude_directory_pattern"]; filter.IncludeFilePattern = config["filesystem.include_file_pattern"]; filter.ExcludeFilePattern = config["filesystem.exclude_file_pattern"]; filter.IncludeExtensions = config["filesystem.extensions"]; filter.OnlyDirs = onlyDirs; // Directory is hidden use parent dir if (!filter.Accept(listDir)) { listDir = listDir.ParentFile; rs.Header["path"] = man.EncryptPath(listDir.AbsolutePath); rs.Header["visual_path"] = man.ToVisualPath(listDir.AbsolutePath, rootPath); } if (input["filter"] != null) { filter.IncludeWildcardPattern = (string)input["filter"]; } if (input["only_files"] != null) { filter.OnlyFiles = onlyFiles; } else if (!onlyDirs) { filter.OnlyFiles = !config.GetBool("filesystem.list_directories", false); } // Add parent if (path != rootPath && input["only_files"] == null && (input["only_dirs"] != null || man.Config.GetBool("filesystem.list_directories", true))) { if (man.VerifyPath(listDir.Parent)) { hasParent = true; rs.Add("..", man.EncryptPath(listDir.Parent), -1, "parent", "", "", "", new NameValueCollection()); } } // Setup input filter BasicFileFilter inputFilter = new BasicFileFilter(); if (input["include_directory_pattern"] != null) { filter.IncludeDirectoryPattern = (string)input["include_directory_pattern"]; } if (input["exclude_directory_pattern"] != null) { filter.ExcludeDirectoryPattern = (string)input["exclude_directory_pattern"]; } if (input["include_file_pattern"] != null) { filter.IncludeFilePattern = (string)input["include_file_pattern"]; } if (input["exclude_file_pattern"] != null) { filter.ExcludeFilePattern = (string)input["exclude_file_pattern"]; } if (input["extensions"] != null) { filter.IncludeExtensions = (string)input["extensions"]; } // Combine the filters CombinedFileFilter combinedFilter = new CombinedFileFilter(); combinedFilter.AddFilter(inputFilter); combinedFilter.AddFilter(filter); files.AddRange(listDir.ListFilesFiltered(combinedFilter)); if (input["page_size"] != null) { if (hasParent) { pageSize = Convert.ToInt32(input["page_size"]) - 1; } else { pageSize = Convert.ToInt32(input["page_size"]); } pages = (int)Math.Ceiling(files.Count / (double)pageSize); // Setup response rs.Header["pages"] = (pages > 1 ? pages : 1); rs.Header["count"] = files.Count; // Remove non visible files int start = Convert.ToInt32(input["page"]) * pageSize; int len = pageSize; len = start + len > files.Count ? len - ((start + len) - files.Count) : len; files = files.GetRange(start, len); } // Sort Files files.Sort(new FileComparer()); // Output folders foreach (IFile file in files) { if (file.IsDirectory) { // Setup attribs attribs = "RW"; type = "folder"; // Fill custom info customInfo = new Hashtable(); man.DispatchEvent(EventType.CustomInfo, file, "list", customInfo); // Special treatment of roots name = file.Name; if (path == "root:///") { if (man.RootNames[file.AbsolutePath] != null) { name = man.RootNames[file.AbsolutePath]; } } // Add to resultset rs.Add( name, man.EncryptPath(file.AbsolutePath), file.IsDirectory ? -1 : file.Length, type, StringUtils.GetDate(file.CreationDate, listDir.Config.Get("filesystem.datefmt", "yyyy-MM-dd HH:mm")), StringUtils.GetDate(file.LastModified, listDir.Config.Get("filesystem.datefmt", "yyyy-MM-dd HH:mm")), attribs, customInfo ); } } // Output files foreach (IFile file in files) { if (file.IsFile) { // Setup attribs attribs = "RW"; type = PathUtils.GetExtension(file.AbsolutePath).ToLower(); // Fill custom info customInfo = new Hashtable(); man.DispatchEvent(EventType.CustomInfo, file, "list", customInfo); // Add to resultset rs.Add( file.Name, man.EncryptPath(file.AbsolutePath), file.Length, type, StringUtils.GetDate(file.CreationDate, listDir.Config.Get("filesystem.datefmt", "yyyy-MM-dd HH:mm")), StringUtils.GetDate(file.LastModified, listDir.Config.Get("filesystem.datefmt", "yyyy-MM-dd HH:mm")), attribs, customInfo ); } } } else { throw new ManagerException("{#error.file_not_exists}"); } return(rs); }
/// <summary> /// /// </summary> /// <param name="long_name"></param> /// <param name="short_name"></param> /// <returns></returns> public ManagerConfig ResolveConfig(string long_name, string short_name) { ManagerConfig config; if ((config = (ManagerConfig) HttpContext.Current.GetConfig(long_name)) != null) return config; if ((config = (ManagerConfig) HttpContext.Current.GetConfig(short_name)) != null) return config; return null; }
/// <summary> /// /// </summary> /// <param name="tool"></param> /// <param name="config"></param> /// <returns></returns> public bool IsToolEnabled(string tool, ManagerConfig config) { string[] ar; // Is disabled ar = config["general.disabled_tools"].Split(new char[] {','}); foreach (string chkTool in ar) { if (chkTool == tool) return false; } // Is it there at all? ar = config["general.tools"].Split(new char[] {','}); foreach (string chkTool in ar) { if (chkTool == tool) return true; } return false; }
/// <summary> /// /// </summary> /// <param name="config"></param> /// <param name="config_prefixes"></param> /// <returns></returns> public ManagerConfig GetJSConfig(ManagerConfig config, string config_prefixes) { string[] encrypted = new string[]{"filesystem.path", "filesystem.rootpath", "filesystem.directory_templates"}; ManagerConfig outConfig = new ManagerConfig(); string[] names, prefixes; string prefix; bool validPrefix; int pos; prefixes = config_prefixes.Split(new char[]{','}); foreach (string key in config.AllKeys) { if ((pos = key.IndexOf(".allow_export")) != -1) { if (config[key] == null) continue; names = config[key].Split(new char[]{','}); prefix = key.Substring(0, pos); validPrefix = false; if (config_prefixes != "*") { foreach (string targetPrefix in prefixes) { if (targetPrefix == prefix) { validPrefix = true; break; } } } else validPrefix = true; // All exported or the requested visible ones if (validPrefix) { foreach (string name in names) { string outKey = prefix + "." + name; // Encrypt if needed foreach (string encryptKey in encrypted) { if (encryptKey == outKey) { outConfig[outKey] = this.EncryptPath(config[outKey]); break; } } // Output normal if (outConfig[outKey] == null) outConfig[outKey] = config[outKey]; } } } } return outConfig; }
/// <summary> /// /// </summary> /// <param name="file"></param> /// <param name="action"></param> /// <param name="config"></param> /// <returns></returns> public bool VerifyFile(IFile file, string action, ManagerConfig config) { BasicFileFilter filter; // Verify filesystem config filter = new BasicFileFilter(); filter.IncludeDirectoryPattern = config["filesystem.include_directory_pattern"]; filter.ExcludeDirectoryPattern = config["filesystem.exclude_directory_pattern"]; filter.IncludeFilePattern = config["filesystem.include_file_pattern"]; filter.ExcludeFilePattern = config["filesystem.exclude_file_pattern"]; filter.IncludeExtensions = config["filesystem.extensions"]; this.invalidFileMsg = "{#error.invalid_filename}"; if (!filter.Accept(file)) { if (filter.Reason == FilterReason.InvalidFileName) { if (file.IsDirectory) this.invalidFileMsg = config["filesystem.invalid_directory_name_msg"]; else this.invalidFileMsg = config["filesystem.invalid_file_name_msg"]; if (this.invalidFileMsg.Length == 0) this.invalidFileMsg = "{#error.invalid_filename}"; } return false; } // Verify action specific config filter = new BasicFileFilter(); if (action != null) { if (config[action + ".include_directory_pattern"] != null) filter.IncludeDirectoryPattern = config[action + ".include_directory_pattern"]; if (config[action + ".exclude_directory_pattern"] != null) filter.ExcludeDirectoryPattern = config[action + ".exclude_directory_pattern"]; if (config[action + ".include_file_pattern"] != null) filter.IncludeFilePattern = config[action + ".include_file_pattern"]; if (config[action + ".exclude_file_pattern"] != null) filter.ExcludeFilePattern = config[action + ".exclude_file_pattern"]; if (config[action + ".extensions"] != null) filter.IncludeExtensions = config[action + ".extensions"]; } if (!filter.Accept(file)) { if (filter.Reason == FilterReason.InvalidFileName) { if (file.IsDirectory) this.invalidFileMsg = config[action + ".invalid_directory_name_msg"]; else this.invalidFileMsg = config[action + ".invalid_file_name_msg"]; if (this.invalidFileMsg == null || this.invalidFileMsg.Length == 0) this.invalidFileMsg = "{#error.invalid_filename}"; } return false; } return true; }
/// <summary> /// Initializes a file instance by a absolute path, child name and type. /// This can be userful when filtering non existing files. /// </summary> /// <param name="man">Reference to manager that requested the file.</param> /// <param name="absolute_path">Absolute file/directory path.</param> /// <param name="child_name">Name of child file for the directory.</param> /// <param name="type">Type of file to create.</param> public LocalFile(ManagerEngine man, string absolute_path, string child_name, FileType type) { this.manager = man; if (child_name != "") this.absPath = PathUtils.ToUnixPath(absolute_path + "/" + child_name); else this.absPath = PathUtils.ToUnixPath(absolute_path); if (type == FileType.Directory) this.dirInfo = new DirectoryInfo(PathUtils.ToOSPath(this.absPath)); else if (type == FileType.File) this.fileInfo = new FileInfo(PathUtils.ToOSPath(this.absPath)); else { // Create file info or dir info this.fileInfo = new FileInfo(PathUtils.ToOSPath(this.absPath)); if (!this.fileInfo.Exists) { this.dirInfo = new DirectoryInfo(PathUtils.ToOSPath(this.absPath)); this.fileInfo = null; } if (this.fileInfo != null) this.absPath = PathUtils.ToUnixPath(this.fileInfo.FullName); if (this.dirInfo != null) this.absPath = PathUtils.RemoveTrailingSlash(PathUtils.ToUnixPath(this.dirInfo.FullName)); } this.config = this.manager.Config; this.configResolved = false; this.triggerEvents = true; }