/// <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); }